From 6ebbb434e4a016583c0ebbd5e75918e53b3b1e00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Tue, 7 Jan 2025 18:09:19 +0100 Subject: [PATCH] Restrict username minimum length too (3 chars) --- Common/StringUtils.cpp | 10 +++++++++- Common/StringUtils.h | 2 +- Common/UI/PopupScreens.cpp | 4 ++-- Common/UI/PopupScreens.h | 4 +++- Core/Config.cpp | 2 +- Core/HLE/sceNp.cpp | 2 +- UI/GameSettingsScreen.cpp | 4 ++-- 7 files changed, 19 insertions(+), 9 deletions(-) diff --git a/Common/StringUtils.cpp b/Common/StringUtils.cpp index 3d7ca6f5df..57bd71f399 100644 --- a/Common/StringUtils.cpp +++ b/Common/StringUtils.cpp @@ -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) { diff --git a/Common/StringUtils.h b/Common/StringUtils.h index 1bd4aa0091..878b252f62 100644 --- a/Common/StringUtils.h +++ b/Common/StringUtils.h @@ -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); diff --git a/Common/UI/PopupScreens.cpp b/Common/UI/PopupScreens.cpp index af484ac4a9..037670f096 100644 --- a/Common/UI/PopupScreens.cpp +++ b/Common/UI/PopupScreens.cpp @@ -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); diff --git a/Common/UI/PopupScreens.h b/Common/UI/PopupScreens.h index 54032d275f..ae30a50eb3 100644 --- a/Common/UI/PopupScreens.h +++ b/Common/UI/PopupScreens.h @@ -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_; }; diff --git a/Core/Config.cpp b/Core/Config.cpp index 2a8810e308..8779b16674 100644 --- a/Core/Config.cpp +++ b/Core/Config.cpp @@ -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; } diff --git a/Core/HLE/sceNp.cpp b/Core/HLE/sceNp.cpp index e43ea67d53..dde0bcfb0e 100644 --- a/Core/HLE/sceNp.cpp +++ b/Core/HLE/sceNp.cpp @@ -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(); diff --git a/UI/GameSettingsScreen.cpp b/UI/GameSettingsScreen.cpp index f2d1afab18..ef22b8bfd9 100644 --- a/UI/GameSettingsScreen.cpp +++ b/UI/GameSettingsScreen.cpp @@ -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; } }