Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GUI][BUG] Properly validate proposal creation Name/URL #2760

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/budget/budgetproposal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "budget/budgetproposal.h"
#include "chainparams.h"
#include "script/standard.h"
#include "utilstrencodings.h"

CBudgetProposal::CBudgetProposal():
nAllotted(0),
Expand Down Expand Up @@ -145,6 +146,20 @@ bool CBudgetProposal::CheckAddress()
return true;
}

/* TODO: Add this to IsWellFormed() for the next hard-fork
* This will networkly reject malformed proposal names and URLs
*/
bool CBudgetProposal::CheckStrings()
{
if (strProposalName != SanitizeString(strProposalName)) {
strInvalid = "Proposal name contains illegal characters.";
return false;
}
if (strURL != SanitizeString(strURL)) {
strInvalid = "Proposal URL contains illegal characters.";
}
}

bool CBudgetProposal::IsWellFormed(const CAmount& nTotalBudget)
{
return CheckStartEnd() && CheckAmount(nTotalBudget) && CheckAddress();
Expand Down
1 change: 1 addition & 0 deletions src/budget/budgetproposal.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class CBudgetProposal
bool CheckStartEnd();
bool CheckAmount(const CAmount& nTotalBudget);
bool CheckAddress();
bool CheckStrings();

protected:
std::map<COutPoint, CBudgetVote> mapVotes;
Expand Down
12 changes: 2 additions & 10 deletions src/qt/pivx/forms/createproposaldialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -774,11 +774,7 @@
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEditPropName">
<property name="maxLength">
<number>20</number>
</property>
</widget>
<widget class="QLineEdit" name="lineEditPropName"/>
</item>
</layout>
</item>
Expand All @@ -795,11 +791,7 @@
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEditURL">
<property name="maxLength">
<number>64</number>
</property>
</widget>
<widget class="QLineEdit" name="lineEditURL"/>
</item>
</layout>
</item>
Expand Down
14 changes: 11 additions & 3 deletions src/qt/pivx/governancemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,16 +182,24 @@ std::vector<VoteInfo> GovernanceModel::getLocalMNsVotesForProposal(const Proposa

OperationResult GovernanceModel::validatePropName(const QString& name) const
{
if (name.toUtf8().size() > (int)PROP_NAME_MAX_SIZE) { // limit
return {false, _("Invalid name, maximum size exceeded")};
std::string strName = SanitizeString(name.toStdString());
if (strName != name.toStdString()) { // invalid characters
return {false, _("Invalid name, invalid characters")};
}
if (strName.size() > (int)PROP_NAME_MAX_SIZE) { // limit
return {false, strprintf(_("Invalid name, maximum size of %d exceeded"), PROP_NAME_MAX_SIZE)};
}
return {true};
}

OperationResult GovernanceModel::validatePropURL(const QString& url) const
{
std::string strURL = SanitizeString(url.toStdString());
if (strURL != url.toStdString()) {
return {false, _("Invalid URL, invalid characters")};
}
std::string strError;
return {validateURL(url.toStdString(), strError, PROP_URL_MAX_SIZE), strError};
return {validateURL(strURL, strError, PROP_URL_MAX_SIZE), strError};
}

OperationResult GovernanceModel::validatePropAmount(CAmount amount) const
Expand Down
6 changes: 3 additions & 3 deletions src/utilstrencodings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ std::string SanitizeString(const std::string& str, int rule)
return strResult;
}

bool validateURL(std::string strURL)
bool validateURL(const std::string& strURL)
{
std::string strErr;
return validateURL(strURL, strErr);
}

bool validateURL(std::string strURL, std::string& strErr, unsigned int maxSize)
bool validateURL(const std::string& strURL, std::string& strErr, unsigned int maxSize)
{
// Check URL size
if (strURL.size() > maxSize) {
Expand All @@ -53,7 +53,7 @@ bool validateURL(std::string strURL, std::string& strErr, unsigned int maxSize)
}

// Validate URL
std::regex url_regex("^(https?)://[^\\s/$.?#][^\\s]*[^\\s/.]\\.[^\\s/.][^\\s]*[^\\s.]$");
std::regex url_regex(R"(^(https?)://[^\s/$.?#][^\s]*[^\s/.]\.[^\s/.][^\s]*[^\s.]$)");
if (!std::regex_match(strURL, url_regex)) {
strErr = "Invalid URL";
return false;
Expand Down
4 changes: 2 additions & 2 deletions src/utilstrencodings.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ std::string SanitizeString(const std::string& str, int rule = SAFE_CHARS_DEFAULT
* @param[in] maxSize An unsigned int, defaulted to 64, to restrict the length
* @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);
bool validateURL(const std::string& strURL, std::string& strErr, unsigned int maxSize = 64);
bool validateURL(const std::string& strURL);

std::vector<unsigned char> ParseHex(const char* psz);
std::vector<unsigned char> ParseHex(const std::string& str);
Expand Down