Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions multiaddr/codecs/utf8.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
from typing import Any

from ..exceptions import BinaryParseError
from . import CodecBase
from . import LENGTH_PREFIXED_VAR_SIZE, CodecBase


class Codec(CodecBase):
SIZE = 0 # Variable size
IS_PATH = False # Default to False, will be set to True for ip6zone protocol
SIZE = LENGTH_PREFIXED_VAR_SIZE
IS_PATH = False

def to_bytes(self, proto: Any, string: str) -> bytes:
"""Convert a UTF-8 string to its binary representation."""
Expand All @@ -19,6 +19,9 @@ def to_bytes(self, proto: Any, string: str) -> bytes:
string = string.strip()
if not string:
raise ValueError("Zone identifier cannot be empty after stripping whitespace")
if "/" in string:
# Matches go-multiaddr: a '/' would make the text form unparseable
raise ValueError("Zone identifier cannot contain '/'")
else:
# URL decode the string to handle special characters for other protocols
string = urllib.parse.unquote(string)
Expand All @@ -42,6 +45,8 @@ def to_string(self, proto: Any, buf: bytes) -> str:
value = value.strip()
if not value:
raise ValueError("Zone identifier cannot be empty after stripping whitespace")
if "/" in value:
raise ValueError("Zone identifier cannot contain '/'")
return value
# For other protocols, URL encode special characters
return urllib.parse.quote(value, safe="%")
Expand Down
1 change: 1 addition & 0 deletions newsfragments/147.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix ``ip6zone`` being decoded as a value-less protocol: ``protocols()``, ``items()``, ``split()`` and binary parsing now handle zone identifiers of any length (e.g. ``en0``, ``wlan0``).
39 changes: 36 additions & 3 deletions tests/test_multiaddr.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
protocol_with_name,
protocols_with_string,
)
from multiaddr.transforms import string_to_bytes


@pytest.mark.parametrize(
Expand Down Expand Up @@ -173,9 +174,9 @@ def test_valid(addr_str):
# QUIC with WebTransport
"/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/udp/4001/quic-v1/webtransport",
"/ip4/1.2.3.4/udp/4001/quic-v1/webtransport",
# QUIC with IPv6 zones (removed due to binary encoding issues in Python implementation)
# "/ip6zone/x/ip6/fe80::1/udp/1234/quic",
# "/ip6zone/x%y/ip6/fe80::1/udp/1234/quic-v1",
# QUIC with IPv6 zones
"/ip6zone/x/ip6/fe80::1/udp/1234/quic",
"/ip6zone/x%y/ip6/fe80::1/udp/1234/quic-v1",
# QUIC with different ports
"/ip4/192.168.1.1/udp/4001/quic",
"/ip4/10.0.0.1/udp/4001/quic-v1",
Expand Down Expand Up @@ -1143,3 +1144,35 @@ def test_tag_only_protocol_error_message_format():
error_msg = str(exc_info.value)
assert "does not take an argument" in error_msg
assert "webrtc" in error_msg


@pytest.mark.parametrize("zone", ["en0", "eth0", "wlan0"])
def test_ip6zone_value_is_length_prefixed(zone: str) -> None:
"""Regression for #147: ip6zone was decoded as a value-less protocol.

Zones whose length byte collided with a real protocol code (eth0 -> 4 -> ip4)
produced phantom protocols; other lengths (en0, wlan0) failed binary parsing.
"""
ma = Multiaddr(f"/ip6zone/{zone}/ip6/fe80::1/tcp/4001")
assert Multiaddr(ma.to_bytes()) == ma
assert [p.name for p in ma.protocols()] == ["ip6zone", "ip6", "tcp"]
assert ma.value_for_protocol("ip6zone") == zone
assert [(p.name, v) for p, v in ma.items()] == [
("ip6zone", zone),
("ip6", "fe80::1"),
("tcp", "4001"),
]
assert [str(part) for part in ma.split()] == [
f"/ip6zone/{zone}",
"/ip6/fe80::1",
"/tcp/4001",
]
assert string_to_bytes(str(ma)) == ma.to_bytes()


def test_ip6zone_rejects_slash_in_binary_zone() -> None:
"""A zone containing '/' has no parseable text form (go-multiaddr rejects it too)."""
ip6 = Multiaddr("/ip6/fe80::1").to_bytes()
ma = Multiaddr(b"\x2a\x03a/b" + ip6)
with pytest.raises(BinaryParseError):
str(ma)
Loading