Skip to content

Commit 7772e87

Browse files
committed
fix: preventing IP 0.0.0.0 from being published and allowing peer exchange connections with localhost IPs
1 parent ea31b53 commit 7772e87

File tree

5 files changed

+41
-7
lines changed

5 files changed

+41
-7
lines changed

tests/all_tests_waku.nim

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import
55
./waku_core/test_namespaced_topics,
66
./waku_core/test_time,
77
./waku_core/test_message_digest,
8-
./waku_core/test_peers
8+
./waku_core/test_peers,
9+
./waku_core/test_published_address
910

1011

1112
# Waku archive test suite
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{.used.}
2+
3+
import
4+
stew/shims/net as stewNet,
5+
std/[strutils],
6+
testutils/unittests
7+
import
8+
../testlib/wakucore,
9+
../testlib/wakunode
10+
11+
suite "Waku Core - Published Address":
12+
13+
test "Test IP 0.0.0.0":
14+
let
15+
node = newTestWakuNode(generateSecp256k1Key(), ValidIpAddress.init(
16+
"0.0.0.0"),Port(0))
17+
18+
check:
19+
($node.announcedAddresses).contains("127.0.0.1")
20+
21+
test "Test custom IP":
22+
let
23+
node = newTestWakuNode(generateSecp256k1Key(), ValidIpAddress.init(
24+
"8.8.8.8"),Port(0))
25+
26+
check:
27+
($node.announcedAddresses).contains("8.8.8.8")

tests/wakunode_jsonrpc/test_jsonrpc_admin.nim

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ procSuite "Waku v2 JSON-RPC API - Admin":
3131
asyncTest "connect to ad-hoc peers":
3232
# Create a couple of nodes
3333
let
34-
node1 = newTestWakuNode(generateSecp256k1Key(), ValidIpAddress.init("0.0.0.0"), Port(60600))
35-
node2 = newTestWakuNode(generateSecp256k1Key(), ValidIpAddress.init("0.0.0.0"), Port(60602))
34+
node1 = newTestWakuNode(generateSecp256k1Key(), ValidIpAddress.init("127.0.0.1"), Port(60600))
35+
node2 = newTestWakuNode(generateSecp256k1Key(), ValidIpAddress.init("127.0.0.1"), Port(60602))
3636
peerInfo2 = node2.switch.peerInfo
37-
node3 = newTestWakuNode(generateSecp256k1Key(), ValidIpAddress.init("0.0.0.0"), Port(60604))
37+
node3 = newTestWakuNode(generateSecp256k1Key(), ValidIpAddress.init("127.0.0.1"), Port(60604))
3838
peerInfo3 = node3.switch.peerInfo
3939

4040
await allFutures([node1.start(), node2.start(), node3.start()])
@@ -90,7 +90,7 @@ procSuite "Waku v2 JSON-RPC API - Admin":
9090

9191
asyncTest "get managed peer information":
9292
# Create 3 nodes and start them with relay
93-
let nodes = toSeq(0..<3).mapIt(newTestWakuNode(generateSecp256k1Key(), ValidIpAddress.init("0.0.0.0"), Port(60220+it*2)))
93+
let nodes = toSeq(0..<3).mapIt(newTestWakuNode(generateSecp256k1Key(), ValidIpAddress.init("127.0.0.1"), Port(60220+it*2)))
9494
await allFutures(nodes.mapIt(it.start()))
9595
await allFutures(nodes.mapIt(it.mountRelay()))
9696

waku/node/config.nim

+7-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ else:
44
{.push raises: [].}
55

66
import
7-
std/[options, sequtils],
7+
std/[options, sequtils, strutils],
88
stew/results,
99
stew/shims/net,
1010
libp2p/multiaddress
@@ -53,6 +53,11 @@ template wsFlag(wssEnabled: bool): MultiAddress =
5353
else: MultiAddress.init("/ws").tryGet()
5454

5555

56+
proc formatListenAddress(inputMultiAdd: MultiAddress): MultiAddress =
57+
let inputStr = $inputMultiAdd
58+
# If MultiAddress contains "0.0.0.0", replace it for "127.0.0.1"
59+
return MultiAddress.init(inputStr.replace("0.0.0.0", "127.0.0.1")).get()
60+
5661
proc init*(T: type NetConfig,
5762
bindIp: ValidIpAddress,
5863
bindPort: Port,
@@ -111,7 +116,7 @@ proc init*(T: type NetConfig,
111116
if hostExtAddress.isSome():
112117
announcedAddresses.add(hostExtAddress.get())
113118
else:
114-
announcedAddresses.add(hostAddress) # We always have at least a bind address for the host
119+
announcedAddresses.add(formatListenAddress(hostAddress)) # We always have at least a bind address for the host
115120

116121
# External multiaddrs that the operator may have configured
117122
if extMultiAddrs.len > 0:

waku/node/peer_manager/peer_manager.nim

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ proc addPeer*(pm: PeerManager, remotePeerInfo: RemotePeerInfo, origin = UnknownO
121121
discard remotePeerInfo.peerId.extractPublicKey(publicKey)
122122

123123
if pm.peerStore[AddressBook][remotePeerInfo.peerId] == remotePeerInfo.addrs and
124+
not ($remotePeerInfo.addrs).contains("127.0.0.1") and
124125
pm.peerStore[KeyBook][remotePeerInfo.peerId] == publicKey:
125126
# Peer already managed
126127
return

0 commit comments

Comments
 (0)