Skip to content

Commit

Permalink
Merge bitcoin#23679: Sanitize port in addpeeraddress()
Browse files Browse the repository at this point in the history
ada8358 Sanitize port in `addpeeraddress()` (amadeuszpawlik)

Pull request description:

  In connection to bitcoin#22087, it has been [pointed out](bitcoin#22087 (review)) that `addpeeraddress` needs to get its port-value sanitized.

ACKs for top commit:
  fanquake:
    ACK ada8358

Tree-SHA512: 48771cd4f6940aa7840fa23488565c09dea86bd5ec5a5a1fc0374afb4857aebcd2a1f51e2d4cb7348460e0ad9793dc5d2962df457084ed2b8d8142cae650003f
  • Loading branch information
fanquake committed May 17, 2022
2 parents dd8a2df + ada8358 commit d5d40d5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/rpc/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ static RPCHelpMan addpeeraddress()
}

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

UniValue obj(UniValue::VOBJ);
Expand Down
4 changes: 4 additions & 0 deletions test/functional/rpc_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,10 @@ def test_addpeeraddress(self):
assert_equal(node.addpeeraddress(address="", port=8333), {"success": False})
assert_equal(node.getnodeaddresses(count=0), [])

self.log.debug("Test that adding an address with invalid port fails")
assert_raises_rpc_error(-1, "JSON integer out of range", self.nodes[0].addpeeraddress, address="1.2.3.4", port=-1)
assert_raises_rpc_error(-1, "JSON integer out of range", self.nodes[0].addpeeraddress,address="1.2.3.4", port=65536)

self.log.debug("Test that adding a valid address to the tried table succeeds")
assert_equal(node.addpeeraddress(address="1.2.3.4", tried=True, port=8333), {"success": True})
with node.assert_debug_log(expected_msgs=["CheckAddrman: new 0, tried 1, total 1 started"]):
Expand Down

0 comments on commit d5d40d5

Please sign in to comment.