Skip to content

Commit b376c09

Browse files
committed
merge bitcoin#23679: Sanitize port in addpeeraddress()
1 parent cd6c2b5 commit b376c09

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
@@ -999,7 +999,7 @@ static RPCHelpMan addpeeraddress()
999999
}
10001000

10011001
const std::string& addr_string{request.params[0].get_str()};
1002-
const uint16_t port = request.params[1].getInt<int>();
1002+
const auto port{request.params[1].getInt<uint16_t>()};
10031003
const bool tried{request.params[2].isTrue()};
10041004

10051005
UniValue obj(UniValue::VOBJ);

test/functional/rpc_net.py

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

344+
self.log.debug("Test that adding an address with invalid port fails")
345+
assert_raises_rpc_error(-1, "JSON integer out of range", self.nodes[0].addpeeraddress, address="1.2.3.4", port=-1)
346+
assert_raises_rpc_error(-1, "JSON integer out of range", self.nodes[0].addpeeraddress,address="1.2.3.4", port=65536)
347+
344348
self.log.debug("Test that adding a valid address to the tried table succeeds")
345349
assert_equal(node.addpeeraddress(address="1.2.3.4", tried=True, port=8333), {"success": True})
346350
with node.assert_debug_log(expected_msgs=["CheckAddrman: new 0, tried 1, total 1 started"]):

0 commit comments

Comments
 (0)