From b99f1f20f773eb55c870a033b3f2e8f13d55d0c8 Mon Sep 17 00:00:00 2001 From: brunoerg Date: Thu, 5 Jan 2023 15:11:32 -0300 Subject: [PATCH] p2p, rpc: don't allow past absolute timestamp in `setban` --- src/rpc/net.cpp | 4 ++++ test/functional/p2p_disconnect_ban.py | 3 +++ 2 files changed, 7 insertions(+) diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp index 4611e032706..d430087358b 100644 --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -741,6 +741,10 @@ static RPCHelpMan setban() const bool absolute{request.params[3].isNull() ? false : request.params[3].get_bool()}; + if (absolute && banTime < GetTime()) { + throw JSONRPCError(RPC_INVALID_PARAMETER, "Error: Absolute timestamp is in the past"); + } + if (isSubnet) { node.banman->Ban(subNet, banTime, absolute); if (node.connman) { diff --git a/test/functional/p2p_disconnect_ban.py b/test/functional/p2p_disconnect_ban.py index 91c2a439323..8d4f50e6817 100755 --- a/test/functional/p2p_disconnect_ban.py +++ b/test/functional/p2p_disconnect_ban.py @@ -46,6 +46,9 @@ class DisconnectBanTest(BitcoinTestFramework): assert_raises_rpc_error(-30, "Error: Invalid IP/Subnet", self.nodes[1].setban, "127.0.0.1/42", "add") 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 + self.log.info("setban: fail to ban with past absolute timestamp") + assert_raises_rpc_error(-8, "Error: Absolute timestamp is in the past", self.nodes[1].setban, "127.27.0.1", "add", 123, True) + self.log.info("setban remove: fail to unban a non-banned subnet") assert_raises_rpc_error(-30, "Error: Unban failed", self.nodes[1].setban, "127.0.0.1", "remove") assert_equal(len(self.nodes[1].listbanned()), 1)