Skip to content

Commit e53f352

Browse files
committed
[RPC] require valid URL scheme on budget commands
1 parent 58e9863 commit e53f352

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

src/rpc/budget.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,32 +55,33 @@ void checkBudgetInputs(const UniValue& params, std::string &strProposalName, std
5555
CBlockIndex* pindexPrev = chainActive.Tip();
5656

5757
if (strProposalName.size() > 20)
58-
throw std::runtime_error("Invalid proposal name, limit of 20 characters.");
58+
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid proposal name, limit of 20 characters.");
5959

6060
strURL = SanitizeString(params[1].get_str());
61-
if (strURL.size() > 64)
62-
throw std::runtime_error("Invalid url, limit of 64 characters.");
61+
std::string strErr;
62+
if (!validateURL(strURL, strErr))
63+
throw JSONRPCError(RPC_INVALID_PARAMETER, result);
6364

6465
nPaymentCount = params[2].get_int();
6566
if (nPaymentCount < 1)
66-
throw std::runtime_error("Invalid payment count, must be more than zero.");
67+
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid payment count, must be more than zero.");
6768

6869
// Start must be in the next budget cycle
6970
if (pindexPrev != NULL) nBlockMin = pindexPrev->nHeight - pindexPrev->nHeight % Params().GetBudgetCycleBlocks() + Params().GetBudgetCycleBlocks();
7071

7172
nBlockStart = params[3].get_int();
7273
if (nBlockStart % Params().GetBudgetCycleBlocks() != 0) {
7374
int nNext = pindexPrev->nHeight - pindexPrev->nHeight % Params().GetBudgetCycleBlocks() + Params().GetBudgetCycleBlocks();
74-
throw std::runtime_error(strprintf("Invalid block start - must be a budget cycle block. Next valid block: %d", nNext));
75+
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid block start - must be a budget cycle block. Next valid block: %d", nNext));
7576
}
7677

7778
int nBlockEnd = nBlockStart + (Params().GetBudgetCycleBlocks() * nPaymentCount); // End must be AFTER current cycle
7879

7980
if (nBlockStart < nBlockMin)
80-
throw std::runtime_error("Invalid block start, must be more than current height.");
81+
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid block start, must be more than current height.");
8182

8283
if (nBlockEnd < pindexPrev->nHeight)
83-
throw std::runtime_error("Invalid ending block, starting block + (payment_cycle*payments) must be more than current height.");
84+
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid ending block, starting block + (payment_cycle*payments) must be more than current height.");
8485

8586
address = params[4].get_str();
8687
if (!address.IsValid())

src/utilstrencodings.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,46 @@ std::string SanitizeString(const std::string& str, int rule)
3939
return strResult;
4040
}
4141

42+
/*
43+
** bool validateURL(std::string, int [optional, defaulted to 64])
44+
**
45+
** Input:
46+
** strURL: A std::string URL to be be processed for validity.
47+
** strErr: A std::string to be filled with any error messages.
48+
** maxSize: An int to define the maximum size the URL can be;
49+
** Optional, defaulting to 64.
50+
**
51+
** Return:
52+
** boolean true|false result if the validation passes.
53+
** strRrror: Filled with any error messages.
54+
*/
55+
bool validateURL(std::string strURL, std::string& strErr, unsigned int maxSize) {
56+
57+
// Check URL size
58+
if (strURL.size() > maxSize) {
59+
strErr = strprintf("Invalid URL: %d exceeds limit of %d characters.", strURL.size(), maxSize);
60+
return false;
61+
}
62+
63+
std::vector<std::string> reqPre;
64+
65+
// Required initial strings; URL must contain one
66+
reqPre.push_back("http://");
67+
reqPre.push_back("https://");
68+
69+
// check fronts
70+
bool found = false;
71+
for (int i=0; i < reqPre.size() && !found; i++) {
72+
if (strURL.find(reqPre[i]) == 0) found = true;
73+
}
74+
if ((!found) && (reqPre.size() > 0)) {
75+
strErr = "Invalid URL, check scheme (e.g. https://)";
76+
return false;
77+
}
78+
79+
return true;
80+
}
81+
4282
const signed char p_util_hexdigit[256] =
4383
{
4484
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,

src/utilstrencodings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ enum SafeChars
4040
* @return A new string without unsafe chars
4141
*/
4242
std::string SanitizeString(const std::string& str, int rule = SAFE_CHARS_DEFAULT);
43+
bool validateURL(std::string strURL, std::string& strErr, unsigned int maxSize = 64);
4344
std::vector<unsigned char> ParseHex(const char* psz);
4445
std::vector<unsigned char> ParseHex(const std::string& str);
4546
signed char HexDigit(char c);

0 commit comments

Comments
 (0)