Skip to content

Commit bd42b85

Browse files
author
MarcoFalke
committed
Merge #12905: [rpcwallet] Clamp walletpassphrase value at 100M seconds
2b2b96c Use std::bind instead of boost::bind to re-lock the wallet (Suhas Daftuar) 662d19f [rpcwallet] Clamp walletpassphrase value at 100M seconds (Suhas Daftuar) Pull request description: Larger values seem to trigger a bug on macos+libevent (resulting in the rpc server stopping). Tree-SHA512: 890f3b641f6c586e2f8f629a9d23bca6ceb8b237b285561aad488cb7adf941a21177d3129d0c2b8293c0a673cd8e401957dbe2b6b3b7c8c4e991bb411d260102
2 parents 9778586 + 2b2b96c commit bd42b85

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

src/wallet/rpcwallet.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737

3838
#include <univalue.h>
3939

40+
#include <functional>
41+
4042
static const std::string WALLET_ENDPOINT_BASE = "/wallet/";
4143

4244
CWallet *GetWalletForJSONRPCRequest(const JSONRPCRequest& request)
@@ -2349,8 +2351,7 @@ UniValue walletpassphrase(const JSONRPCRequest& request)
23492351
"This is needed prior to performing transactions related to private keys such as sending bitcoins\n"
23502352
"\nArguments:\n"
23512353
"1. \"passphrase\" (string, required) The wallet passphrase\n"
2352-
"2. timeout (numeric, required) The time to keep the decryption key in seconds. Limited to at most 1073741824 (2^30) seconds.\n"
2353-
" Any value greater than 1073741824 seconds will be set to 1073741824 seconds.\n"
2354+
"2. timeout (numeric, required) The time to keep the decryption key in seconds; capped at 100000000 (~3 years).\n"
23542355
"\nNote:\n"
23552356
"Issuing the walletpassphrase command while the wallet is already unlocked will set a new unlock\n"
23562357
"time that overrides the old one.\n"
@@ -2383,9 +2384,10 @@ UniValue walletpassphrase(const JSONRPCRequest& request)
23832384
if (nSleepTime < 0) {
23842385
throw JSONRPCError(RPC_INVALID_PARAMETER, "Timeout cannot be negative.");
23852386
}
2386-
// Clamp timeout to 2^30 seconds
2387-
if (nSleepTime > (int64_t)1 << 30) {
2388-
nSleepTime = (int64_t)1 << 30;
2387+
// Clamp timeout
2388+
constexpr int64_t MAX_SLEEP_TIME = 100000000; // larger values trigger a macos/libevent bug?
2389+
if (nSleepTime > MAX_SLEEP_TIME) {
2390+
nSleepTime = MAX_SLEEP_TIME;
23892391
}
23902392

23912393
if (strWalletPass.length() > 0)
@@ -2402,7 +2404,7 @@ UniValue walletpassphrase(const JSONRPCRequest& request)
24022404
pwallet->TopUpKeyPool();
24032405

24042406
pwallet->nRelockTime = GetTime() + nSleepTime;
2405-
RPCRunLater(strprintf("lockwallet(%s)", pwallet->GetName()), boost::bind(LockWallet, pwallet), nSleepTime);
2407+
RPCRunLater(strprintf("lockwallet(%s)", pwallet->GetName()), std::bind(LockWallet, pwallet), nSleepTime);
24062408

24072409
return NullUniValue;
24082410
}

test/functional/wallet_encryption.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,15 @@ def run_test(self):
6464
assert_raises_rpc_error(-8, "Timeout cannot be negative.", self.nodes[0].walletpassphrase, passphrase2, -10)
6565
# Check the timeout
6666
# Check a time less than the limit
67-
expected_time = int(time.time()) + (1 << 30) - 600
68-
self.nodes[0].walletpassphrase(passphrase2, (1 << 30) - 600)
67+
MAX_VALUE = 100000000
68+
expected_time = int(time.time()) + MAX_VALUE - 600
69+
self.nodes[0].walletpassphrase(passphrase2, MAX_VALUE - 600)
6970
actual_time = self.nodes[0].getwalletinfo()['unlocked_until']
7071
assert_greater_than_or_equal(actual_time, expected_time)
7172
assert_greater_than(expected_time + 5, actual_time) # 5 second buffer
7273
# Check a time greater than the limit
73-
expected_time = int(time.time()) + (1 << 30) - 1
74-
self.nodes[0].walletpassphrase(passphrase2, (1 << 33))
74+
expected_time = int(time.time()) + MAX_VALUE - 1
75+
self.nodes[0].walletpassphrase(passphrase2, MAX_VALUE + 1000)
7576
actual_time = self.nodes[0].getwalletinfo()['unlocked_until']
7677
assert_greater_than_or_equal(actual_time, expected_time)
7778
assert_greater_than(expected_time + 5, actual_time) # 5 second buffer

0 commit comments

Comments
 (0)