fix(p2p): improve address validation rules#1245
Conversation
📝 WalkthroughWalkthroughAdds explicit hostname validation (new ValidateHostname and IsValidHostname) into p2p and validator address validation paths, adds Endpoint.Equal for precise endpoint comparisons, and updates tests to reject emoji/non-ASCII hostnames and relax resolution comparisons. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
No actionable comments were generated in the recent review. 🎉 🧹 Recent nitpick comments
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Fix all issues with AI agents
In `@internal/p2p/address_test.go`:
- Around line 214-218: The test case in address_test.go uses the external
hostname "one.one.one.one", making the NodeAddress -> resolution path flaky;
update the test entry that constructs p2p.NodeAddress (the case comparing to
&p2p.Endpoint) to use a literal IP-only input (e.g., "1.1.1.1") or otherwise
provide the resolved net.IP directly so the resolver isn't invoked, or modify
the test to mock the resolver used by the address parsing code; target the
specific test case using the p2p.NodeAddress/ p2p.Endpoint values so the
behavior is deterministic without external DNS.
In `@internal/p2p/transport.go`:
- Around line 160-174: The Endpoint.Equal method can panic if the receiver
`other` is nil; add an early nil guard in Endpoint.Equal to return false when
`other == nil` before accessing other.Protocol (and also defensively handle
other.IP being nil before calling other.IP.Equal). Update the function
(Endpoint.Equal) to check `other == nil` first and ensure any subsequent
dereferences like other.IP are nil-checked to avoid nil pointer dereferences.
🧹 Nitpick comments (1)
internal/p2p/address.go (1)
183-193: Code duplication withtypes/validator_address.go.This
validateHostnamefunction is nearly identical to the one intypes/validator_address.go(lines 120-131). Consider consolidating into a single shared implementation to avoid maintenance overhead. Sinceinternal/p2palready importstypes, you could reuse or export the validation function fromtypes.♻️ Suggested approach
Either export a single
ValidateHostnamefunction from thetypespackage and call it from both locations, or haveinternal/p2p/address.godirectly call thetypespackage helper:func validateHostname(hostname string) error { - if _, _, err := net.SplitHostPort(hostname); err == nil { - return errors.New("hostname must not include port") - } - if net.ParseIP(hostname) != nil { - return nil - } - if !types.IsValidHostname(hostname) { - return errors.New("invalid hostname") - } - return nil + return types.ValidateHostname(hostname) // after exporting from types }
Signed-off-by: Lukasz Klimek <842586+lklimek@users.noreply.github.com>
Add hostname value to ValidateHostname error messages for easier debugging, fix uint16 port comparison, document IDN rejection policy, and add dedicated unit tests for Endpoint.Equal covering IPv4/IPv6 representations. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Issue being fixed or feature implemented
We have detected records with invalid IP addresses in the peer DB, hotfixed in #1243
What was done?
Stronger validation rules for peer and validator addresses.
How Has This Been Tested?
GHA
Breaking Changes
None
Checklist:
For repository code-owners and collaborators only
Summary by CodeRabbit
New Features
Bug Fixes
Tests