Skip to content

Commit 11590d3

Browse files
committed
Properly bound check conf_target in wallet RPC calls
1 parent fd29d3d commit 11590d3

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

src/rpc/mining.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@
3030

3131
#include <univalue.h>
3232

33+
unsigned int ParseConfirmTarget(const UniValue& value)
34+
{
35+
int target = value.get_int();
36+
unsigned int max_target = ::feeEstimator.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
37+
if (target < 1 || (unsigned int)target > max_target) {
38+
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid conf_target, must be between %u - %u", 1, max_target));
39+
}
40+
return (unsigned int)target;
41+
}
42+
3343
/**
3444
* Return average network hashes per second based on the last 'lookup' blocks,
3545
* or from the last difficulty change if 'lookup' is nonpositive.

src/rpc/mining.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,7 @@
1212
/** Generate blocks (mine) */
1313
UniValue generateBlocks(std::shared_ptr<CReserveScript> coinbaseScript, int nGenerate, uint64_t nMaxTries, bool keepScript);
1414

15+
/** Check bounds on a command line confirm target */
16+
unsigned int ParseConfirmTarget(const UniValue& value);
17+
1518
#endif

src/wallet/rpcwallet.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ UniValue sendtoaddress(const JSONRPCRequest& request)
460460
}
461461

462462
if (request.params.size() > 6 && !request.params[6].isNull()) {
463-
coin_control.m_confirm_target = request.params[6].get_int();
463+
coin_control.m_confirm_target = ParseConfirmTarget(request.params[6]);
464464
}
465465

466466
if (request.params.size() > 7 && !request.params[7].isNull()) {
@@ -981,7 +981,7 @@ UniValue sendmany(const JSONRPCRequest& request)
981981
}
982982

983983
if (request.params.size() > 6 && !request.params[6].isNull()) {
984-
coin_control.m_confirm_target = request.params[6].get_int();
984+
coin_control.m_confirm_target = ParseConfirmTarget(request.params[6]);
985985
}
986986

987987
if (request.params.size() > 7 && !request.params[7].isNull()) {
@@ -2795,7 +2795,7 @@ UniValue fundrawtransaction(const JSONRPCRequest& request)
27952795
coinControl.signalRbf = options["replaceable"].get_bool();
27962796
}
27972797
if (options.exists("conf_target")) {
2798-
coinControl.m_confirm_target = options["conf_target"].get_int();
2798+
coinControl.m_confirm_target = ParseConfirmTarget(options["conf_target"]);
27992799
}
28002800
if (options.exists("estimate_mode")) {
28012801
if (!FeeModeFromString(options["estimate_mode"].get_str(), coinControl.m_fee_mode)) {
@@ -2917,12 +2917,8 @@ UniValue bumpfee(const JSONRPCRequest& request)
29172917

29182918
if (options.exists("confTarget") && options.exists("totalFee")) {
29192919
throw JSONRPCError(RPC_INVALID_PARAMETER, "confTarget and totalFee options should not both be set. Please provide either a confirmation target for fee estimation or an explicit total fee for the transaction.");
2920-
} else if (options.exists("confTarget")) {
2921-
int target = options["confTarget"].get_int();
2922-
if (target <= 0) { // FIXME: Check upper bound too
2923-
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid confTarget (cannot be <= 0)");
2924-
}
2925-
coin_control.m_confirm_target = target;
2920+
} else if (options.exists("confTarget")) { // TODO: alias this to conf_target
2921+
coin_control.m_confirm_target = ParseConfirmTarget(options["confTarget"]);
29262922
} else if (options.exists("totalFee")) {
29272923
totalFee = options["totalFee"].get_int64();
29282924
if (totalFee <= 0) {

0 commit comments

Comments
 (0)