Skip to content

Commit 35d521c

Browse files
committed
[RPC] require valid URL scheme on budget commands
1 parent 4b14327 commit 35d521c

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

src/rpc/budget.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ void checkBudgetInputs(const UniValue& params, std::string &strProposalName, std
5656
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid proposal name, limit of 20 characters.");
5757

5858
strURL = SanitizeString(params[1].get_str());
59-
if (strURL.size() > 64)
60-
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid url, limit of 64 characters.");
59+
std::string strErr;
60+
if (!validateURL(strURL, strErr))
61+
throw JSONRPCError(RPC_INVALID_PARAMETER, strErr);
6162

6263
nPaymentCount = params[2].get_int();
6364
if (nPaymentCount < 1)

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)