Skip to content

Validate P2P bytes before serializing multiaddrs - #460

Open
Officeyutong wants to merge 1 commit into
nervosnetwork:masterfrom
Officeyutong:fix-multiaddr-p2p-constructor-validation
Open

Validate P2P bytes before serializing multiaddrs#460
Officeyutong wants to merge 1 commit into
nervosnetwork:masterfrom
Officeyutong:fix-multiaddr-p2p-constructor-validation

Conversation

@Officeyutong

Copy link
Copy Markdown
Collaborator

Summary

  • validate Protocol::P2P bytes before writing them into a Multiaddr
  • prevent safe construction APIs from creating internally invalid multiaddrs
  • add regression tests for Multiaddr::from, push, and FromIterator

Rationale

Protocol::P2P is publicly constructible from raw bytes. Parsers already validate P2P bytes, but construction paths serialized them directly, allowing invalid internal Multiaddr values that could later panic during iteration or display.

This keeps the existing API shape while enforcing the same P2P invariant at serialization time.

Tests

  • cargo fmt --check
  • cargo test -p tentacle-multiaddr -- --nocapture

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR enforces Protocol::P2P byte invariants at serialization time so that Multiaddr construction APIs (from, push, FromIterator) can’t produce internally invalid multiaddrs that may later panic during iteration/display.

Changes:

  • Add Protocol::P2P validation (check_p2p) inside Protocol::write_to_bytes before encoding into a multiaddr.
  • Add regression tests asserting invalid Protocol::P2P bytes are rejected (panic) via Multiaddr::from, push, and collect.
  • Add a positive regression test confirming valid P2P construction paths continue to work.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
multiaddr/src/protocol.rs Validates Protocol::P2P bytes before encoding, preventing invalid Multiaddr byte representations from being created via serialization.
multiaddr/tests/lib.rs Adds regression coverage for invalid/valid P2P behavior across key construction APIs.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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