Skip to content

Commit f45de22

Browse files
committed
merge bitcoin#26822: don't allow past absolute timestamp in setban
1 parent 920e85d commit f45de22

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/rpc/net.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,10 @@ static RPCHelpMan setban()
782782
if (request.params[3].isTrue())
783783
absolute = true;
784784

785+
if (absolute && banTime < GetTime()) {
786+
throw JSONRPCError(RPC_INVALID_PARAMETER, "Error: Absolute timestamp is in the past");
787+
}
788+
785789
if (isSubnet) {
786790
banman.Ban(subNet, banTime, absolute);
787791
if (node.connman) {

test/functional/p2p_disconnect_ban.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ def run_test(self):
5757
assert_raises_rpc_error(-30, "Error: Invalid IP/Subnet", self.nodes[1].setban, "127.0.0.1/42", "add")
5858
assert_equal(len(self.nodes[1].listbanned()), 1) # still only one banned ip because 127.0.0.1 is within the range of 127.0.0.0/24
5959

60+
self.log.info("setban: fail to ban with past absolute timestamp")
61+
assert_raises_rpc_error(-8, "Error: Absolute timestamp is in the past", self.nodes[1].setban, "127.27.0.1", "add", 123, True)
62+
6063
self.log.info("setban remove: fail to unban a non-banned subnet")
6164
assert_raises_rpc_error(-30, "Error: Unban failed", self.nodes[1].setban, "127.0.0.1", "remove")
6265
assert_equal(len(self.nodes[1].listbanned()), 1)
@@ -74,9 +77,13 @@ def run_test(self):
7477
self.nodes[1].setban("2001:4d48:ac57:400:cacf:e9ff:fe1d:9c63/19", "add", 1000) # ban for 1000 seconds
7578
listBeforeShutdown = self.nodes[1].listbanned()
7679
assert_equal("192.168.0.1/32", listBeforeShutdown[2]['address'])
80+
81+
self.log.info("setban: test banning with absolute timestamp")
82+
self.nodes[1].setban("192.168.0.2", "add", self.mocktime + 120, True)
83+
7784
# Move time forward by 3 seconds so the third ban has expired
7885
self.bump_mocktime(3)
79-
self.wait_until(lambda: len(self.nodes[1].listbanned()) == 3, timeout=10)
86+
self.wait_until(lambda: len(self.nodes[1].listbanned()) == 4, timeout=10)
8087

8188
self.log.info("Test ban_duration and time_remaining")
8289
for ban in self.nodes[1].listbanned():
@@ -86,13 +93,17 @@ def run_test(self):
8693
elif ban["address"] == "2001:4d48:ac57:400:cacf:e9ff:fe1d:9c63/19":
8794
assert_equal(ban["ban_duration"], 1000)
8895
assert_equal(ban["time_remaining"], 997)
96+
elif ban["address"] == "192.168.0.2/32":
97+
assert_equal(ban["ban_duration"], 120)
98+
assert_equal(ban["time_remaining"], 117)
8999

90100
self.restart_node(1)
91101

92102
listAfterShutdown = self.nodes[1].listbanned()
93103
assert_equal("127.0.0.0/24", listAfterShutdown[0]['address'])
94104
assert_equal("127.0.0.0/32", listAfterShutdown[1]['address'])
95-
assert_equal("/19" in listAfterShutdown[2]['address'], True)
105+
assert_equal("192.168.0.2/32", listAfterShutdown[2]['address'])
106+
assert_equal("/19" in listAfterShutdown[3]['address'], True)
96107

97108
# Clear ban lists
98109
self.nodes[1].clearbanned()

0 commit comments

Comments
 (0)