Skip to content

Commit ada8358

Browse files
Sanitize port in addpeeraddress()
- Ensures port sanitization in `addpeeraddress()` - Adds test to check for invalid port values
1 parent 225e5b5 commit ada8358

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

src/rpc/net.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,7 @@ static RPCHelpMan addpeeraddress()
932932
}
933933

934934
const std::string& addr_string{request.params[0].get_str()};
935-
const uint16_t port{static_cast<uint16_t>(request.params[1].get_int())};
935+
const auto port{request.params[1].getInt<uint16_t>()};
936936
const bool tried{request.params[2].isTrue()};
937937

938938
UniValue obj(UniValue::VOBJ);

test/functional/rpc_net.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ def test_addpeeraddress(self):
257257
assert_equal(node.addpeeraddress(address="", port=8333), {"success": False})
258258
assert_equal(node.getnodeaddresses(count=0), [])
259259

260+
self.log.debug("Test that adding an address with invalid port fails")
261+
assert_raises_rpc_error(-1, "JSON integer out of range", self.nodes[0].addpeeraddress, address="1.2.3.4", port=-1)
262+
assert_raises_rpc_error(-1, "JSON integer out of range", self.nodes[0].addpeeraddress,address="1.2.3.4", port=65536)
263+
260264
self.log.debug("Test that adding a valid address to the tried table succeeds")
261265
assert_equal(node.addpeeraddress(address="1.2.3.4", tried=True, port=8333), {"success": True})
262266
with node.assert_debug_log(expected_msgs=["CheckAddrman: new 0, tried 1, total 1 started"]):

0 commit comments

Comments
 (0)