Allow any printable ASCII character in usernames and sanitize invalid OIDC characters on user creation#3336
Conversation
Agent-Logs-Url: https://github.com/rommapp/romm/sessions/f380a1fb-f70f-4943-a90e-bf122fad5abd Co-authored-by: gantoine <3247106+gantoine@users.noreply.github.com>
…import comment Agent-Logs-Url: https://github.com/rommapp/romm/sessions/f380a1fb-f70f-4943-a90e-bf122fad5abd Co-authored-by: gantoine <3247106+gantoine@users.noreply.github.com>
|
@copilot why can't we just allow those special characters for usernames? |
Agent-Logs-Url: https://github.com/rommapp/romm/sessions/c1289afe-e976-40f7-bca3-618e86c12405 Co-authored-by: gantoine <3247106+gantoine@users.noreply.github.com>
Agent-Logs-Url: https://github.com/rommapp/romm/sessions/c1289afe-e976-40f7-bca3-618e86c12405 Co-authored-by: gantoine <3247106+gantoine@users.noreply.github.com>
Good point — updated in ae6d9d9. Dots are now explicitly allowed in usernames across the backend validation ( |
|
@copilot but why is the USERNAME_PATTERN so restrictive? |
Agent-Logs-Url: https://github.com/rommapp/romm/sessions/480ffea8-57cd-404f-9b82-020488f53dd1 Co-authored-by: gantoine <3247106+gantoine@users.noreply.github.com>
…ests Agent-Logs-Url: https://github.com/rommapp/romm/sessions/480ffea8-57cd-404f-9b82-020488f53dd1 Co-authored-by: gantoine <3247106+gantoine@users.noreply.github.com>
It was overly restrictive — removed the character whitelist in 636c3f8 and 9966d87. |
|
@zurdi15 this makes sense to you? |
Test Results (mariadb) 1 files 1 suites 4m 3s ⏱️ Results for commit 9966d87. |
☂️ Python Coverage
Overall Coverage
New FilesNo new covered files... Modified Files
|
Test Results (postgresql) 1 files 1 suites 4m 14s ⏱️ Results for commit 9966d87. |
Hmm im not sure if I'm understanding tbh, but from what I see yeah it seems like the pattern will allow what we want to. |
OIDC-provided usernames containing characters disallowed by RomM's username policy (e.g.
.,@) were stored as-is, making those accounts impossible to edit without first manually renaming them.The fix broadens the username policy to allow any printable ASCII character, and a sanitization fallback handles the remaining cases (spaces, control characters, non-ASCII) from OIDC providers.
Changes
utils/validation.py: UpdatedUSERNAME_PATTERNtor"^[\x21-\x7E]+$"— any printable non-space ASCII character is now valid. Updated error message to "Username must not contain spaces or control characters". Addedsanitize_username()— strips non-ASCII chars, replaces spaces and control characters with-, collapses consecutive hyphens, strips leading/trailing hyphens, truncates to 255 chars, and raisesValidationErrorif the result is too short (< 3 chars). Also added pre-compiledUSERNAME_INVALID_CHARS_PATTERNandUSERNAME_CONSECUTIVE_HYPHENS_PATTERN.handler/auth/base_handler.py: Callssanitize_username()on the OIDCpreferred_usernamebefore creating a new user, to handle spaces and control characters that some OIDC providers may include. Uses a lazy import to avoid a circular dependency. Logs a warning when sanitization changes the username.frontend/src/stores/users.ts: UpdatedusernameCharsregex to/^[\x21-\x7E]*$/to match the backend.frontend/src/locales/en_US/common.jsonanden_GB/common.json: Updatedusername-charsmessage to "Username must not contain spaces or control characters".Tests: Added
TestSanitizeUsernameintests/utils/test_validation.py; addedtest_oidc_valid_add_user_with_dotted_username(printable special chars like.and@preserved as-is) andtest_oidc_valid_add_user_with_sanitized_username(spaces are sanitized to hyphens) intests/handler/auth/test_oidc.py.Example