Restrict username minimum length too (3 chars)

This commit is contained in:
Henrik Rydgård
2025-01-07 18:09:19 +01:00
parent 77e575dd65
commit 6ebbb434e4
7 changed files with 19 additions and 9 deletions
+9 -1
View File
@@ -105,7 +105,7 @@ int countChar(std::string_view haystack, char needle) {
return count;
}
std::string SanitizeString(std::string_view input, StringRestriction restriction, int maxLength) {
std::string SanitizeString(std::string_view input, StringRestriction restriction, int minLength, int maxLength) {
if (restriction == StringRestriction::None) {
return std::string(input);
}
@@ -127,6 +127,14 @@ std::string SanitizeString(std::string_view input, StringRestriction restriction
}
}
if (minLength >= 0) {
if (sanitized.size() < minLength) {
// Just reject it by returning an empty string, as we can't really
// conjure up new characters here.
return std::string();
}
}
if (maxLength >= 0) {
// TODO: Cut at whole UTF-8 chars!
if (sanitized.size() > maxLength) {
+1 -1
View File
@@ -81,7 +81,7 @@ enum class StringRestriction {
AlphaNumDashUnderscore, // Used for infrastructure usernames
};
std::string SanitizeString(std::string_view username, StringRestriction restriction, int maxLength = -1);
std::string SanitizeString(std::string_view username, StringRestriction restriction, int minLength, int maxLength);
void DataToHexString(const uint8_t *data, size_t size, std::string *output);
void DataToHexString(int indent, uint32_t startAddr, const uint8_t* data, size_t size, std::string* output);
+2 -2
View File
@@ -529,7 +529,7 @@ EventReturn PopupTextInputChoice::HandleClick(EventParams &e) {
// Choose method depending on platform capabilities.
if (System_GetPropertyBool(SYSPROP_HAS_TEXT_INPUT_DIALOG)) {
System_InputBoxGetString(token_, text_, *value_, passwordMasking_, [=](const std::string &enteredValue, int) {
*value_ = SanitizeString(StripSpaces(enteredValue), restriction_, maxLen_);
*value_ = SanitizeString(StripSpaces(enteredValue), restriction_, minLen_, maxLen_);
EventParams params{};
OnChange.Trigger(params);
});
@@ -553,7 +553,7 @@ std::string PopupTextInputChoice::ValueText() const {
}
EventReturn PopupTextInputChoice::HandleChange(EventParams &e) {
*value_ = StripSpaces(SanitizeString(*value_, restriction_, maxLen_));
*value_ = StripSpaces(SanitizeString(*value_, restriction_, minLen_, maxLen_));
e.v = this;
OnChange.Trigger(e);
+3 -1
View File
@@ -374,8 +374,9 @@ public:
Event OnChange;
void SetRestriction(StringRestriction restriction) {
void SetRestriction(StringRestriction restriction, int minLength) {
restriction_ = restriction;
minLen_ = minLength;
}
protected:
@@ -390,6 +391,7 @@ private:
std::string placeHolder_;
std::string defaultText_;
int maxLen_;
int minLen_ = 0;
bool restoreFocus_ = false;
StringRestriction restriction_;
};
+1 -1
View File
@@ -608,7 +608,7 @@ static std::string DefaultInfrastructureUsername() {
// NOTE: This type of dependency means that network settings must be AFTER system settings in sections[].
if (g_Config.sNickName != "PPSSPP" &&
!g_Config.sNickName.empty() &&
g_Config.sNickName == SanitizeString(g_Config.sNickName, StringRestriction::AlphaNumDashUnderscore, 16)) {
g_Config.sNickName == SanitizeString(g_Config.sNickName, StringRestriction::AlphaNumDashUnderscore, 3, 16)) {
return g_Config.sNickName;
}
+1 -1
View File
@@ -116,7 +116,7 @@ static int sceNpInit()
ERROR_LOG(Log::sceNet, "UNIMPL %s()", __FUNCTION__);
// We'll sanitize an extra time here, just to be safe from ini modifications.
if (g_Config.sInfrastructureUsername == SanitizeString(g_Config.sInfrastructureUsername, StringRestriction::AlphaNumDashUnderscore, 16)) {
if (g_Config.sInfrastructureUsername == SanitizeString(g_Config.sInfrastructureUsername, StringRestriction::AlphaNumDashUnderscore, 3, 16)) {
npOnlineId = g_Config.sInfrastructureUsername;
} else {
npOnlineId.clear();
+2 -2
View File
@@ -945,7 +945,7 @@ void GameSettingsScreen::CreateNetworkingSettings(UI::ViewGroup *networkingSetti
networkingSettings->Add(new NoticeView(NoticeLevel::WARN, n->T("To play in Infrastructure Mode, you must enter a username"), ""));
}
PopupTextInputChoice *usernameChoice = networkingSettings->Add(new PopupTextInputChoice(GetRequesterToken(), &g_Config.sInfrastructureUsername, n->T("Username"), "", 16, screenManager()));
usernameChoice->SetRestriction(StringRestriction::AlphaNumDashUnderscore);
usernameChoice->SetRestriction(StringRestriction::AlphaNumDashUnderscore, 3);
usernameChoice->OnChange.Add([this](UI::EventParams &e) {
RecreateViews();
return UI::EVENT_DONE;
@@ -1316,7 +1316,7 @@ void GameSettingsScreen::CreateSystemSettings(UI::ViewGroup *systemSettings) {
systemSettings->Add(new PopupTextInputChoice(GetRequesterToken(), &g_Config.sNickName, sy->T("Change Nickname"), "", 32, screenManager()))->OnChange.Add([this](UI::EventParams &e) {
// Copy to infrastructure name if valid and not already set.
if (g_Config.sInfrastructureUsername.empty()) {
if (g_Config.sNickName == SanitizeString(g_Config.sNickName, StringRestriction::AlphaNumDashUnderscore, 16)) {
if (g_Config.sNickName == SanitizeString(g_Config.sNickName, StringRestriction::AlphaNumDashUnderscore, 3, 16)) {
g_Config.sInfrastructureUsername = g_Config.sNickName;
}
}