Skip to content
Open
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
29 changes: 18 additions & 11 deletions libp2p/identity/identify/identify.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
StreamHandlerFn,
TProtocol,
)
from libp2p.exceptions import (
ParseError,
)
from libp2p.network.stream.exceptions import (
StreamClosed,
)
Expand Down Expand Up @@ -87,17 +90,21 @@ def parse_identify_response(response: bytes) -> Identify:
"""
# Try new format first: length-prefixed protobuf
if len(response) >= 1:
length, varint_size = decode_varint_with_size(response)
if varint_size > 0 and length > 0 and varint_size + length <= len(response):
protobuf_data = response[varint_size : varint_size + length]
try:
identify_response = Identify()
identify_response.ParseFromString(protobuf_data)
# Sanity check: must have agent_version (protocol_version is optional)
if identify_response.agent_version:
return identify_response
except Exception:
pass # Fall through to old format
try:
length, varint_size = decode_varint_with_size(response)
except ParseError:
pass # Fall through to old format
else:
if varint_size > 0 and length > 0 and varint_size + length <= len(response):
protobuf_data = response[varint_size : varint_size + length]
try:
identify_response = Identify()
identify_response.ParseFromString(protobuf_data)
# Sanity check: must have agent_version.
if identify_response.agent_version:
return identify_response
except Exception:
pass # Fall through to old format

# Fall back to old format: raw protobuf
try:
Expand Down
1 change: 1 addition & 0 deletions newsfragments/1328.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allowed identify response parsing to fall back to raw protobuf messages when the length-prefix probe raises ``ParseError``.
25 changes: 25 additions & 0 deletions tests/core/identity/identify/test_identify.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
Multiaddr,
)

from libp2p.exceptions import (
ParseError,
)
from libp2p.identity.identify import identify as identify_module
from libp2p.identity.identify.identify import (
AGENT_VERSION,
ID,
Expand All @@ -22,6 +26,27 @@
logger = logging.getLogger("libp2p.identity.identify-test")


def test_parse_identify_response_falls_back_after_prefix_parse_error(monkeypatch):
raw_response = identify_module.Identify(
agent_version=AGENT_VERSION,
protocol_version=PROTOCOL_VERSION,
).SerializeToString()

def raise_parse_error(data: bytes) -> tuple[int, int]:
raise ParseError("Unexpected end of data while decoding varint")

monkeypatch.setattr(
identify_module,
"decode_varint_with_size",
raise_parse_error,
)

identify_response = parse_identify_response(raw_response)

assert identify_response.agent_version == AGENT_VERSION
assert identify_response.protocol_version == PROTOCOL_VERSION


@pytest.mark.trio
async def test_identify_protocol(security_protocol):
async with host_pair_factory(security_protocol=security_protocol) as (
Expand Down