From 8619b1cf590dfbdbffdeacf51381a6341f4f0bd3 Mon Sep 17 00:00:00 2001 From: Ludy Date: Sun, 12 Jan 2025 16:30:17 +0100 Subject: [PATCH] Restriction of username and email (#2676) # Description - https://github.com/Stirling-Tools/Stirling-PDF/security/code-scanning/8 - https://github.com/Stirling-Tools/Stirling-PDF/security/code-scanning/9 - https://github.com/Stirling-Tools/Stirling-PDF/security/code-scanning/21 - https://github.com/Stirling-Tools/Stirling-PDF/security/code-scanning/22 ## Checklist - [x] I have read the [Contribution Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md) - [x] I have performed a self-review of my own code - [ ] I have attached images of the change if it is UI based - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] If my code has heavily changed functionality I have updated relevant docs on [Stirling-PDFs doc repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/) - [x] My changes generate no new warnings - [ ] I have read the section [Add New Translation Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md#add-new-translation-tags) (for new translation tags only) --- .../software/SPDF/config/security/UserService.java | 8 ++++++-- src/main/resources/templates/account.html | 9 ++++++++- src/main/resources/templates/addUsers.html | 9 ++++++++- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/main/java/stirling/software/SPDF/config/security/UserService.java b/src/main/java/stirling/software/SPDF/config/security/UserService.java index 2bc93352..eefa5a2c 100644 --- a/src/main/java/stirling/software/SPDF/config/security/UserService.java +++ b/src/main/java/stirling/software/SPDF/config/security/UserService.java @@ -329,12 +329,16 @@ public class UserService implements UserServiceInterface { public boolean isUsernameValid(String username) { // Checks whether the simple username is formatted correctly + // Regular expression for user name: Min. 3 characters, max. 50 characters boolean isValidSimpleUsername = - username.matches("^[a-zA-Z0-9][a-zA-Z0-9@._+-]*[a-zA-Z0-9]$"); + username.matches("^[a-zA-Z0-9](?!.*[-@._+]{2,})[a-zA-Z0-9@._+-]{1,48}[a-zA-Z0-9]$"); + // Checks whether the email address is formatted correctly + // Regular expression for email addresses: Max. 320 characters, with RFC-like validation boolean isValidEmail = username.matches( - "^(?=.{1,64}@)[A-Za-z0-9]+(\\.[A-Za-z0-9_+.-]+)*@[^-][A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$"); + "^(?=.{1,320}$)(?=.{1,64}@)[A-Za-z0-9](?:[A-Za-z0-9_.+-]*[A-Za-z0-9])?@[^-][A-Za-z0-9-]+(?:\\\\.[A-Za-z0-9-]+)*(?:\\\\.[A-Za-z]{2,})$"); + List notAllowedUserList = new ArrayList<>(); notAllowedUserList.add("ALL_USERS".toLowerCase()); boolean notAllowedUser = notAllowedUserList.contains(username.toLowerCase()); diff --git a/src/main/resources/templates/account.html b/src/main/resources/templates/account.html index 0fa36504..e054bdbd 100644 --- a/src/main/resources/templates/account.html +++ b/src/main/resources/templates/account.html @@ -104,7 +104,14 @@