Repository navigation
wire: ignore retired Tor v2 addresses in addr messages - #2663
Harshil-1603 wants to merge 1 commit into
Conversation
allocz
left a comment
There was a problem hiding this comment.
Code makes sense, but needs refactoring. Commit message should explain the objective of the patch, and what was done to fulfill the objective.
| func TestAddrMessagesIgnoreTorV2(t *testing.T) { | ||
| ip := net.ParseIP("8.8.8.8") | ||
| onion := net.ParseIP("fd87:d87e:eb43:2020:2020:2020:2020:2020") | ||
| legacy := []*NetAddress{ | ||
| NewNetAddressIPPort(ip, 8333, SFNodeNetwork), | ||
| NewNetAddressIPPort(onion, 8333, SFNodeNetwork), | ||
| NewNetAddressIPPort(net.ParseIP("1.1.1.1"), 8333, SFNodeNetwork), | ||
| } | ||
| now := time.Unix(1700000000, 0) | ||
| v2 := []*NetAddressV2{ | ||
| NetAddressV2FromBytes(now, SFNodeNetwork, ip, 8333), | ||
| NetAddressV2FromBytes(now, SFNodeNetwork, onion, 8333), | ||
| NetAddressV2FromBytes(now, SFNodeNetwork, bytes.Repeat([]byte{1}, TorV3Size), 8333), | ||
| NetAddressV2FromBytes(now, SFNodeNetwork, net.ParseIP("1.1.1.1"), 8333), | ||
| } | ||
| t.Run("legacy", func(t *testing.T) { | ||
| var raw bytes.Buffer | ||
| if err := WriteVarInt(&raw, ProtocolVersion, uint64(len(legacy))); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| for _, addr := range legacy { | ||
| if err := writeNetAddress(&raw, ProtocolVersion, addr, true); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| } | ||
| var decoded MsgAddr | ||
| if err := decoded.BtcDecode(&raw, ProtocolVersion, BaseEncoding); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if len(decoded.AddrList) != 2 || raw.Len() != 0 { | ||
| t.Fatalf("decode retained Tor v2: %d addresses, %d trailing bytes", len(decoded.AddrList), raw.Len()) | ||
| } | ||
| var encoded bytes.Buffer | ||
| if err := (&MsgAddr{AddrList: legacy}).BtcEncode(&encoded, ProtocolVersion, BaseEncoding); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if encoded.Bytes()[0] != 2 { | ||
| t.Fatalf("encoded address count %d, want 2", encoded.Bytes()[0]) | ||
| } | ||
| var roundTrip MsgAddr | ||
| if err := roundTrip.BtcDecode(&encoded, ProtocolVersion, BaseEncoding); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if len(roundTrip.AddrList) != 2 || encoded.Len() != 0 { | ||
| t.Fatal("incorrect filtered payload") | ||
| } | ||
| }) | ||
| t.Run("addrv2", func(t *testing.T) { | ||
| var raw bytes.Buffer | ||
| if err := WriteVarInt(&raw, ProtocolVersion, uint64(len(v2))); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| for _, addr := range v2 { | ||
| if err := writeNetAddressV2(&raw, ProtocolVersion, addr); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| } | ||
| var decoded MsgAddrV2 | ||
| if err := decoded.BtcDecode(&raw, ProtocolVersion, BaseEncoding); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if len(decoded.AddrList) != 3 || raw.Len() != 0 { | ||
| t.Fatalf("decode retained Tor v2: %d addresses, %d trailing bytes", len(decoded.AddrList), raw.Len()) | ||
| } | ||
| if !decoded.AddrList[1].IsTorV3() { | ||
| t.Fatal("Tor v3 was not retained") | ||
| } | ||
| var encoded bytes.Buffer | ||
| if err := (&MsgAddrV2{AddrList: v2}).BtcEncode(&encoded, ProtocolVersion, BaseEncoding); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if encoded.Bytes()[0] != 3 { | ||
| t.Fatalf("encoded address count %d, want 3", encoded.Bytes()[0]) | ||
| } | ||
| var roundTrip MsgAddrV2 | ||
| if err := roundTrip.BtcDecode(&encoded, ProtocolVersion, BaseEncoding); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if len(roundTrip.AddrList) != 3 || encoded.Len() != 0 { | ||
| t.Fatal("incorrect filtered payload") | ||
| } | ||
| }) | ||
| } |
There was a problem hiding this comment.
Test code does not follow the formatting rules.
| count := len(m.AddrList) | ||
| count := 0 | ||
| for _, na := range m.AddrList { | ||
| if _, retired := na.Addr.(*torv2Addr); !retired { |
There was a problem hiding this comment.
This is not a common syntax for _, ok type assertion, also, I would like to see comments explaining better the reasoning about what you are doing in the code, as this makes the code more readable. Remember to use empty lines between logical stanzas.
allocz
left a comment
There was a problem hiding this comment.
Commits can be squashed.
Note that this is breaking change, as several of the modified procedures are exported, which now have a new behavior, and therefore a major version bump is required if we don't want to break dependents.
|
|
||
| // Tor v2 is retired; keep its entries out of outgoing addr messages. | ||
| for _, na := range m.AddrList { | ||
| _, ok := na.Addr.(*torv2Addr) |
There was a problem hiding this comment.
I think that the comment // Tor v2 is retired; keep its entries out of outgoing addr messages. should be above this line, right? Also you can use just if _, ok := na.Addr.(*torv2Addr).
| // Consume the complete entry before skipping it so the next | ||
| // address remains aligned in the enclosing addrv2 message. | ||
| return ErrSkippedNetworkID |
There was a problem hiding this comment.
Note that this way, bad tor v2 addresses may fail with ErrInvalidAddressSize, and the BIP-0155 specifies that we should handle the torv2 net ID as if it is unknown, so you can instead edit this commit with the following patch:
diff --git a/wire/netaddressv2.go b/wire/netaddressv2.go
index f269bb72..abbad930 100644
--- a/wire/netaddressv2.go
+++ b/wire/netaddressv2.go
@@ -301,13 +301,7 @@ func readNetAddressV2(r io.Reader, pver uint32, na *NetAddressV2) error {
return err
}
- if !isKnownNetworkID(netID) {
- // In the case of an unknown networkID, we'll read the address
- // size and error with ErrInvalidAddressSize if it's greater
- // than maxAddrV2Size. If the address size is within the valid
- // range, we'll just read and discard the address. In this
- // case, ErrSkippedNetworkID will be returned to signal to the
- // caller to continue reading.
+ consumeAddrBytes := func(r io.Reader) error {
if decodedSize > maxAddrV2Size {
return ErrInvalidAddressSize
}
@@ -318,6 +312,21 @@ func readNetAddressV2(r io.Reader, pver uint32, na *NetAddressV2) error {
return err
}
+ return nil
+ }
+
+ if !isKnownNetworkID(netID) {
+ // In the case of an unknown networkID, we'll read the address
+ // size and error with ErrInvalidAddressSize if it's greater
+ // than maxAddrV2Size. If the address size is within the valid
+ // range, we'll just read and discard the address. In this
+ // case, ErrSkippedNetworkID will be returned to signal to the
+ // caller to continue reading.
+ err := consumeAddrBytes(r)
+ if err != nil {
+ return err
+ }
+
return ErrSkippedNetworkID
}
@@ -373,24 +382,16 @@ func readNetAddressV2(r io.Reader, pver uint32, na *NetAddressV2) error {
return ErrSkippedNetworkID
}
case torv2:
- addr := &torv2Addr{}
- addr.netID = torv2
- if decodedSize != uint64(torv2Size) {
- return ErrInvalidAddressSize
- }
-
- if err := readElement(r, &addr.addr); err != nil {
- return err
- }
-
- na.Port, err = binarySerializer.Uint16(r, bigEndian)
+ // BIP-0155 requires torv2 addresses to be processed as if the
+ // network is unknown. So we just consume the address bytes and
+ // then return ErrSkippedNetworkID.
+ err := consumeAddrBytes(r)
if err != nil {
return err
}
- // Consume the complete entry before skipping it so the next
- // address remains aligned in the enclosing addrv2 message.
return ErrSkippedNetworkID
+
case torv3:
addr := &torv3Addr{}
addr.netID = torv3
diff --git a/wire/netaddressv2_test.go b/wire/netaddressv2_test.go
index d3b28b22..f631ddc6 100644
--- a/wire/netaddressv2_test.go
+++ b/wire/netaddressv2_test.go
@@ -265,11 +265,14 @@ func TestReadNetAddressV2(t *testing.T) {
nil,
},
- // Invalid torv2 size.
+ // Torv2 with an unexpected size is skipped.
{
- []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x02},
+ []byte{
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x03, 0x01,
+ 0x02, 0x03, 0x00, 0x00,
+ },
"",
- ErrInvalidAddressSize,
+ ErrSkippedNetworkID,
},
// Retired torv2 encoding is consumed and ignored.
Do not relay retired Tor v2 addresses in legacy addr or addrv2 messages. Consume bounded Tor v2 entries like unknown networks, including unusual lengths, without misaligning following entries. Preserve oversize and truncation errors, omit retired entries on encode and count only retained entries. Cover legacy and addrv2 filtering, framing and size boundaries. Suggested-by: allocz Assisted-by: OpenAI Codex
ccb51d9 to
8a40d08
Compare
Change Description
BIP-155 requires retired Tor v2 addresses to be ignored on receipt and never relayed. Both legacy
addrmessages using OnionCat encoding andaddrv2messages currently retain and serialize them.Skip Tor v2 entries when decoding both message types and filter them when encoding, writing the count of entries actually emitted. The addrv2 decoder consumes the complete address and port before signaling a skipped network ID, preserving subsequent entries. IPv4 and Tor v3 addresses remain intact; the historical address conversion helpers remain available.
Fixes #2660 for network address message reception and relay.
Steps to Test
make unitandmake unit-raceacross all ten modules using an in-tree workspace so downstream tests use the changed wire module.go vet ./...,go build ./...,git diff --check, and golangci-lint v2.15.0 against the actual upstream base (dd169fe): no new findings. The configured older lint baseline reports 16 pre-existing root findings. The wire check also has no new findings againstdd169fe; the configured older baseline reports 12 pre-existing errcheck findings in unrelated wire files.Pull Request Checklist
Specification: https://github.com/bitcoin/bips/blob/master/bip-0155.mediawiki
AI assistance: prepared with Codex and verified using the checks above.