Skip to content

Commit

Permalink
[Util] Use C++11 std::regex for validateURL() function
Browse files Browse the repository at this point in the history
  • Loading branch information
random-zebra committed Sep 8, 2021
1 parent e988d40 commit a62f88a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
26 changes: 12 additions & 14 deletions src/utilstrencodings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <cstring>
#include <errno.h>
#include <limits>
#include <regex>



Expand All @@ -37,27 +38,24 @@ std::string SanitizeString(const std::string& str, int rule)
return strResult;
}

bool validateURL(std::string strURL, std::string& strErr, unsigned int maxSize) {
bool validateURL(std::string strURL)
{
std::string strErr;
return validateURL(strURL, strErr);
}

bool validateURL(std::string strURL, std::string& strErr, unsigned int maxSize)
{
// Check URL size
if (strURL.size() > maxSize) {
strErr = strprintf("Invalid URL: %d exceeds limit of %d characters.", strURL.size(), maxSize);
return false;
}

std::vector<std::string> reqPre;

// Required initial strings; URL must contain one
reqPre.push_back("http://");
reqPre.push_back("https://");

// check fronts
bool found = false;
for (int i=0; i < (int) reqPre.size() && !found; i++) {
if (strURL.find(reqPre[i]) == 0) found = true;
}
if ((!found) && (reqPre.size() > 0)) {
strErr = "Invalid URL, check scheme (e.g. https://)";
// Validate URL
std::regex url_regex("^(https?)://[^\\s/$.?#][^\\s]*[^\\s/.]\\.[^\\s/.][^\\s]*[^\\s.]$");
if (!std::regex_match(strURL, url_regex)) {
strErr = "Invalid URL";
return false;
}

Expand Down
5 changes: 3 additions & 2 deletions src/utilstrencodings.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ std::string SanitizeString(const std::string& str, int rule = SAFE_CHARS_DEFAULT
/**
* Check URL format for conformance for validity to a defined pattern
* @param[in] strURL The string to be processed for validity
* @param[in] stdErr A string that will be loaded with any validation error message
* @param[in] strErr A string that will be loaded with any validation error message
* @param[in] maxSize An unsigned int, defaulted to 64, to restrict the length
* @return A bool, true if valid, false if not (reason in stdErr)
* @return A bool, true if valid, false if not (reason in strErr)
*/
bool validateURL(std::string strURL, std::string& strErr, unsigned int maxSize = 64);
bool validateURL(std::string strURL);

std::vector<unsigned char> ParseHex(const char* psz);
std::vector<unsigned char> ParseHex(const std::string& str);
Expand Down
6 changes: 3 additions & 3 deletions test/functional/rpc_budget.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ def run_test(self):

self.log.info("Test without URL scheme")
scheme = ''
assert_raises_rpc_error(-8, "Invalid URL, check scheme (e.g. https://)", self.nodes[0].preparebudget, name, scheme + url, 1, nextsuperblock, address, 100)
assert_raises_rpc_error(-8, "Invalid URL", self.nodes[0].preparebudget, name, scheme + url, 1, nextsuperblock, address, 100)

self.log.info('Test with invalid URL scheme: ftp://')
scheme = 'ftp://'
assert_raises_rpc_error(-8, "Invalid URL, check scheme (e.g. https://)", self.nodes[0].preparebudget, name, scheme + url, 1, nextsuperblock, address, 100)
assert_raises_rpc_error(-8, "Invalid URL", self.nodes[0].preparebudget, name, scheme + url, 1, nextsuperblock, address, 100)

self.log.info("Test with invalid double character scheme: hhttps://")
scheme = 'hhttps://'
url = 'test.com'
assert_raises_rpc_error(-8, "Invalid URL, check scheme (e.g. https://)", self.nodes[0].preparebudget, name, scheme + url, 1, nextsuperblock, address, 100)
assert_raises_rpc_error(-8, "Invalid URL", self.nodes[0].preparebudget, name, scheme + url, 1, nextsuperblock, address, 100)

self.log.info("Test with valid scheme: http://")
name = 'testvalid1'
Expand Down

0 comments on commit a62f88a

Please sign in to comment.