Skip to content

fix: decode ip6zone as length-prefixed, not a flag protocol (fixes #147) - #148

Merged
acul71 merged 3 commits into
multiformats:masterfrom
yashksaini-coder:fix/ip6zone-size
Nov 19, 2026
Merged

acul71 merged 3 commits into
multiformats:masterfrom
yashksaini-coder:fix/ip6zone-size

Conversation

@yashksaini-coder

Copy link
Copy Markdown
Contributor

What was wrong

multiaddr/codecs/utf8.py (used only by ip6zone) set SIZE = 0, which the framework treats as a value-less flag protocol. bytes_to_string happened to read the length prefix anyway, so str() looked correct, but bytes_iter (→ protocols(), items(), split(), Multiaddr(bytes)) and string_to_bytes did not:

  • /ip6zone/eth0/... → protocols() contained a phantom ip4 (length byte 0x04 == P_IP4), value_for_protocol("ip6zone") was None, split() raised.
  • /ip6zone/en0/..., /ip6zone/wlan0/... → Multiaddr(ma.to_bytes()) raised BinaryParseError (unknown protocol 3 / 5).

Fix

  • SIZE = LENGTH_PREFIXED_VAR_SIZE, matching go-multiaddr (protoIP6ZONE, Size: LengthPrefixedVarSize).
  • Since zones now actually decode from binary, reject / in a zone value (both directions), as go-multiaddr's ip6zoneVal does — otherwise str() of such an address could not be parsed back.
  • Removed a stale comment claiming IS_PATH is set for ip6zone (it never is).

Tests

  • New test_ip6zone_value_is_length_prefixed[en0|eth0|wlan0]: bytes round-trip, protocols(), items(), value_for_protocol, split(), and transforms.string_to_bytes — every symptom listed in ip6zone is decoded as a flag protocol: phantom /ip4, broken binary round-trip for zones ≠ 4 bytes #147.
  • New test_ip6zone_rejects_slash_in_binary_zone. Without the fix: en0/wlan0 fail parsing, eth0 fails on the phantom ip4.
  • Re-enabled the two ip6zone QUIC cases in test_quic_valid that were commented out as "removed due to binary encoding issues in Python implementation" — they fail without this fix and pass with it.
  • Full suite: 435 passed, 1 skipped.

Notes

  • newsfragments/147.bugfix.rst added; towncrier build --draft renders it.
  • pre-commit: all hooks pass except pyrefly-local, which also fails on unmodified master (451 errors with pyrefly 1.4.1, the unpinned latest). This branch adds no new pyrefly errors (451 → 451). Happy to open a separate issue about pinning pyrefly if useful.

Out of scope (possible follow-ups)

  • Encoders add a length prefix for SIZE <= 0 while bytes_iter reads one only for SIZE < 0; that asymmetry is what let a wrong SIZE silently corrupt parsing. Keying flag protocols on codec is None would harden it.
  • Multiaddr(bytes) validates framing only, not values, so malformed values surface at str() time.
  • ip6zone to_string strips whitespace, so decode is lossy for zones with surrounding spaces.

…ltiformats#147)

The utf8 codec (used only by ip6zone) declared SIZE = 0, which the
framework treats as a value-less flag protocol. bytes_iter therefore
read the zone's length byte as the next protocol code: /ip6zone/eth0
gained a phantom /ip4, and zones of other lengths (en0, wlan0) failed
binary parsing. Use LENGTH_PREFIXED_VAR_SIZE, matching go-multiaddr.

Re-enables the ip6zone QUIC cases that were commented out for 'binary
encoding issues' and adds a regression test over en0/eth0/wlan0.
Now that ip6zone values decode from binary, a zone containing '/' would
produce a text form that cannot be parsed back. Reject it in both
directions, as go-multiaddr's ip6zoneVal does. Drop the stale IS_PATH
comment and extend the multiformats#147 regression test to items() and
transforms.string_to_bytes, which the issue also reported broken.
@yashksaini-coder

Copy link
Copy Markdown
Contributor Author

Note on the red tox (3.10, lint) check: it fails only on the pyrefly-local hook, and that failure is pre-existing. CI installed the unpinned pyrefly==1.4.1, which reports 451 errors on unmodified master too. This branch has the same 451, and none of them are in codecs/utf8.py or the new tests. All other lint hooks and all core/docs/windows jobs pass.

Tracked in #149, fixed by #150 (pins pyrefly==1.2.0, the version from the last green lint run; 0 errors on both master and this branch). I'll rebase this PR once #150 lands.

@acul71 acul71 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review

LGTM — solid, well-tested fix for #147.

What looks good

  • Root cause is correct: SIZE = 0 made ip6zone a flag protocol, so the zone length byte was misread as the next protocol code (phantom ip4 for eth0, binary parse failures for other lengths).
  • Switching to LENGTH_PREFIXED_VAR_SIZE matches go-multiaddr and is scoped safely (utf8 is only used by ip6zone).
  • Slash rejection in both directions is the right go-parity guard for values that cannot round-trip in text form.
  • Regression coverage is excellent: bytes round-trip, protocols(), items(), value_for_protocol, split(), string_to_bytes, plus re-enabled QUIC+ip6zone cases that were previously disabled for this exact bug.
  • Newsfragment 147.bugfix.rst is correct.
  • After resync with #150, CI is fully green (core / lint / docs / windows).

Non-blocking notes

  • Optional follow-up: tiny unit test for encode-path "/" rejection on the codec, and/or a tracking issue for the broader SIZE == 0 vs SIZE < 0 asymmetry called out in the PR.

Approved — ready to squash-merge.

@acul71
acul71 merged commit c3e3d6d into multiformats:master Nov 19, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants