Skip to content

Commit b331ce7

Browse files
committed
multi: remove tor v2 onion address encoder paths
Drop the v2 onion encoder branches from lnwire.WriteOnionAddr, graph/db.encodeOnionAddr, graph/db.collectAddressRecords, and the tor.OnionHostToFakeIP helper. lnd no longer produces v2 onion addresses on any code path. Decoders for v2 addresses are intentionally retained in lnwire, graph/db, and tor so that existing graph databases and peer-announced v2 addresses continue to deserialize. Update unit tests to use v3 onion fixtures.
1 parent 86f3908 commit b331ce7

16 files changed

Lines changed: 102 additions & 139 deletions

chanbackup/single_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ var (
3838
addr1, _ = net.ResolveTCPAddr("tcp", "10.0.0.2:9000")
3939
addr2, _ = net.ResolveTCPAddr("tcp", "10.0.0.3:9000")
4040
addr3 = &tor.OnionAddr{
41-
OnionService: "3g2upl4pq6kufc4m.onion",
42-
Port: 9735,
41+
OnionService: "vww6ybal4bd7szmgncyruucpgfkqahzd" +
42+
"di37ktceo3ah7ngmcopnpyyd.onion",
43+
Port: 9735,
4344
}
4445
addr4 = &lnwire.DNSAddress{
4546
Hostname: "example.com",

graph/db/addr.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,6 @@ func encodeOnionAddr(w io.Writer, addr *tor.OnionAddr) error {
104104
var suffixIndex int
105105
hostLen := len(addr.OnionService)
106106
switch hostLen {
107-
case tor.V2Len:
108-
if _, err := w.Write([]byte{byte(v2OnionAddr)}); err != nil {
109-
return err
110-
}
111-
suffixIndex = tor.V2Len - tor.OnionSuffixLen
112107
case tor.V3Len:
113108
if _, err := w.Write([]byte{byte(v3OnionAddr)}); err != nil {
114109
return err
@@ -131,12 +126,7 @@ func encodeOnionAddr(w io.Writer, addr *tor.OnionAddr) error {
131126
}
132127

133128
// Sanity check the decoded length.
134-
switch {
135-
case hostLen == tor.V2Len && len(host) != tor.V2DecodedLen:
136-
return fmt.Errorf("onion service %v decoded to invalid host %x",
137-
addr.OnionService, host)
138-
139-
case hostLen == tor.V3Len && len(host) != tor.V3DecodedLen:
129+
if hostLen == tor.V3Len && len(host) != tor.V3DecodedLen {
140130
return fmt.Errorf("onion service %v decoded to invalid host %x",
141131
addr.OnionService, host)
142132
}

graph/db/addr_test.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ var (
2929
Port: 65535,
3030
}
3131

32-
testOnionV2Addr = &tor.OnionAddr{
33-
OnionService: "3g2upl4pq6kufc4m.onion",
34-
Port: 9735,
35-
}
36-
3732
testOnionV3Addr = &tor.OnionAddr{
3833
OnionService: "vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd.onion", //nolint:ll
3934
Port: 80,
@@ -63,9 +58,6 @@ var addrTests = []struct {
6358
{
6459
expAddr: testIPV6Addr,
6560
},
66-
{
67-
expAddr: testOnionV2Addr,
68-
},
6961
{
7062
expAddr: testOnionV3Addr,
7163
},

graph/db/graph_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,7 @@ func testNodeInsertionAndDeletion(t *testing.T, v lnwire.GossipVersion) {
400400
// Add 2 IPV6 addresses.
401401
testIPV6Addr,
402402
anotherAddr,
403-
// Add one v2 and one v3 onion address.
404-
testOnionV2Addr,
403+
// Add a v3 onion address.
405404
testOnionV3Addr,
406405
// Add a DNS host address.
407406
testDNSAddr,
@@ -439,7 +438,6 @@ func testNodeInsertionAndDeletion(t *testing.T, v lnwire.GossipVersion) {
439438

440439
// Finally, update the set to only contain the Tor addresses.
441440
expAddrs = []net.Addr{
442-
testOnionV2Addr,
443441
testOnionV3Addr,
444442
}
445443
node = nodeWithAddrs(expAddrs)

graph/db/sql_store.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4821,7 +4821,6 @@ func collectAddressRecords(addresses []net.Addr) (map[dbAddressType][]string,
48214821
newAddresses := map[dbAddressType][]string{
48224822
addressTypeIPv4: {},
48234823
addressTypeIPv6: {},
4824-
addressTypeTorV2: {},
48254824
addressTypeTorV3: {},
48264825
addressTypeDNS: {},
48274826
addressTypeOpaque: {},
@@ -4844,8 +4843,6 @@ func collectAddressRecords(addresses []net.Addr) (map[dbAddressType][]string,
48444843

48454844
case *tor.OnionAddr:
48464845
switch len(addr.OnionService) {
4847-
case tor.V2Len:
4848-
addAddr(addressTypeTorV2, addr)
48494846
case tor.V3Len:
48504847
addAddr(addressTypeTorV3, addr)
48514848
default:

itest/lnd_channel_graph_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,6 @@ func testNodeAnnouncement(ht *lntest.HarnessTest) {
378378
advertisedAddrs := []string{
379379
"192.168.1.1:8333",
380380
"[2001:db8:85a3:8d3:1319:8a2e:370:7348]:8337",
381-
"bkb6azqggsaiskzi.onion:9735",
382381
"fomvuglh6h6vcag73xo5t5gv56ombih3zr2xvplkpbfd7wrog4swj" +
383382
"wid.onion:1234",
384383
}
@@ -435,7 +434,6 @@ func testUpdateNodeAnnouncement(ht *lntest.HarnessTest) {
435434
extraAddrs := []string{
436435
"192.168.1.1:8333",
437436
"[2001:db8:85a3:8d3:1319:8a2e:370:7348]:8337",
438-
"bkb6azqggsaiskzi.onion:9735",
439437
"fomvuglh6h6vcag73xo5t5gv56ombih3zr2xvplkpbfd7wrog4swj" +
440438
"wid.onion:1234",
441439
}

lnwire/lnwire_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,11 @@ func TestDecodeUnknownAddressType(t *testing.T) {
137137

138138
// Add an onion address.
139139
onionAddr := &tor.OnionAddr{
140-
OnionService: "abcdefghijklmnop.onion",
141-
Port: 9065,
140+
OnionService: "abcdefghij" +
141+
"abcdefghijabcdefghij" +
142+
"abcdefghijabcdefghij" +
143+
"234567.onion",
144+
Port: 9065,
142145
}
143146

144147
// Now add an address with an unknown type.

lnwire/message_test.go

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -964,24 +964,6 @@ func randTCP6Addr(t testing.TB, r *rand.Rand) *net.TCPAddr {
964964
return &net.TCPAddr{IP: addrIP, Port: addrPort}
965965
}
966966

967-
func randV2OnionAddr(t testing.TB, r *rand.Rand) *tor.OnionAddr {
968-
t.Helper()
969-
970-
var serviceID [tor.V2DecodedLen]byte
971-
_, err := r.Read(serviceID[:])
972-
require.NoError(t, err, "unable to read serviceID")
973-
974-
var port [2]byte
975-
_, err = r.Read(port[:])
976-
require.NoError(t, err, "unable to read port")
977-
978-
onionService := tor.Base32Encoding.EncodeToString(serviceID[:])
979-
onionService += tor.OnionSuffix
980-
addrPort := int(binary.BigEndian.Uint16(port[:]))
981-
982-
return &tor.OnionAddr{OnionService: onionService, Port: addrPort}
983-
}
984-
985967
func randV3OnionAddr(t testing.TB, r *rand.Rand) *tor.OnionAddr {
986968
t.Helper()
987969

@@ -1021,11 +1003,10 @@ func randDNSAddr(t testing.TB, r *rand.Rand) *lnwire.DNSAddress {
10211003
func randAddrs(t testing.TB, r *rand.Rand) []net.Addr {
10221004
tcp4Addr := randTCP4Addr(t, r)
10231005
tcp6Addr := randTCP6Addr(t, r)
1024-
v2OnionAddr := randV2OnionAddr(t, r)
10251006
v3OnionAddr := randV3OnionAddr(t, r)
10261007
dnsAddr := randDNSAddr(t, r)
10271008

1028-
return []net.Addr{tcp4Addr, tcp6Addr, v2OnionAddr, v3OnionAddr, dnsAddr}
1009+
return []net.Addr{tcp4Addr, tcp6Addr, v3OnionAddr, dnsAddr}
10291010
}
10301011

10311012
func randAlias(r *rand.Rand) lnwire.NodeAlias {

lnwire/node_announcement.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"io"
88
"net"
99
"unicode/utf8"
10+
11+
"github.com/lightningnetwork/lnd/tor"
1012
)
1113

1214
// ErrUnknownAddrType is an error returned if we encounter an unknown address type
@@ -131,6 +133,20 @@ func (a *NodeAnnouncement1) Decode(r io.Reader, _ uint32) error {
131133
return err
132134
}
133135

136+
// Reject any node announcement that advertises a v2 onion address so
137+
// it is never relayed onward or persisted to the graph DB. Tor v2
138+
// onion services have been obsolete since October 2021.
139+
for _, addr := range a.Addresses {
140+
onion, ok := addr.(*tor.OnionAddr)
141+
if !ok {
142+
continue
143+
}
144+
145+
if len(onion.OnionService) == tor.V2Len {
146+
return ErrV2OnionAddrNotSupported
147+
}
148+
}
149+
134150
return a.ExtraOpaqueData.ValidateTLV()
135151
}
136152

lnwire/writer.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ var (
4444
// ErrUnknownServiceLength is returned when the onion service length is
4545
// unknown.
4646
ErrUnknownServiceLength = errors.New("unknown onion service length")
47+
48+
// ErrV2OnionAddrNotSupported is returned by the wire-format decoder
49+
// when an incoming message advertises a v2 onion address. lnd no
50+
// longer relays or persists v2 onion addresses since Tor dropped
51+
// support for v2 services in October 2021.
52+
ErrV2OnionAddrNotSupported = errors.New(
53+
"v2 onion addresses are no longer supported",
54+
)
4755
)
4856

4957
// ErrOutpointIndexTooBig is used when the outpoint index exceeds the max value
@@ -336,10 +344,6 @@ func WriteOnionAddr(buf *bytes.Buffer, addr *tor.OnionAddr) error {
336344

337345
// Decide the suffixIndex and descriptor.
338346
switch len(addr.OnionService) {
339-
case tor.V2Len:
340-
descriptor = []byte{byte(v2OnionAddr)}
341-
suffixIndex = tor.V2Len - tor.OnionSuffixLen
342-
343347
case tor.V3Len:
344348
descriptor = []byte{byte(v3OnionAddr)}
345349
suffixIndex = tor.V3Len - tor.OnionSuffixLen

0 commit comments

Comments
 (0)