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

Add validation to prevent update of a user or member to an invalid username (13) #18261

Merged
merged 2 commits into from
Feb 10, 2025
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
11 changes: 11 additions & 0 deletions src/Umbraco.Web.BackOffice/Controllers/MemberController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.ComponentModel.DataAnnotations;

Check notice on line 1 in src/Umbraco.Web.BackOffice/Controllers/MemberController.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v13/dev)

ℹ Getting worse: Overall Code Complexity

The mean cyclomatic complexity increases from 6.75 to 6.81, threshold = 4. This file has many conditional statements (e.g. if, for, while) across its implementation, leading to lower code health. Avoid adding more conditionals.
using System.Globalization;
using System.Net.Mime;
using System.Text;
Expand Down Expand Up @@ -723,6 +723,17 @@
return false;
}

// User names can only contain the configured allowed characters. This is validated by ASP.NET Identity on create
// as the setting is applied to the IdentityOptions, but we need to check ourselves for updates.
var allowedUserNameCharacters = _securitySettings.AllowedUserNameCharacters;
if (contentItem.Username.Any(c => allowedUserNameCharacters.Contains(c) == false))
{
ModelState.AddPropertyError(
new ValidationResult("Username contains invalid characters"),
$"{Constants.PropertyEditors.InternalGenericPropertiesPrefix}login");
return false;
}

Check warning on line 736 in src/Umbraco.Web.BackOffice/Controllers/MemberController.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v13/dev)

❌ Getting worse: Complex Method

ValidateMemberDataAsync increases in cyclomatic complexity from 10 to 11, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.
if (contentItem.Password != null && !contentItem.Password.NewPassword.IsNullOrWhiteSpace())
{
IdentityResult validPassword = await _memberManager.ValidatePasswordAsync(contentItem.Password.NewPassword);
Expand Down
9 changes: 9 additions & 0 deletions src/Umbraco.Web.BackOffice/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Globalization;

Check notice on line 1 in src/Umbraco.Web.BackOffice/Controllers/UsersController.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v13/dev)

ℹ Getting worse: Overall Code Complexity

The mean cyclomatic complexity increases from 5.68 to 5.73, threshold = 4. This file has many conditional statements (e.g. if, for, while) across its implementation, leading to lower code health. Avoid adding more conditionals.
using System.Net;
using System.Runtime.Serialization;
using System.Security.Cryptography;
Expand Down Expand Up @@ -714,6 +714,15 @@

var hasErrors = false;

// User names can only contain the configured allowed characters. This is validated by ASP.NET Identity on create
// as the setting is applied to the BackOfficeIdentityOptions, but we need to check ourselves for updates.
var allowedUserNameCharacters = _securitySettings.AllowedUserNameCharacters;
if (userSave.Username.Any(c => allowedUserNameCharacters.Contains(c) == false))
{
ModelState.AddModelError("Username", "Username contains invalid characters");
hasErrors = true;
}

Check warning on line 725 in src/Umbraco.Web.BackOffice/Controllers/UsersController.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v13/dev)

❌ Getting worse: Complex Method

PostSaveUser increases in cyclomatic complexity from 21 to 22, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.
// we need to check if there's any Deny Local login providers present, if so we need to ensure that the user's email address cannot be changed
var hasDenyLocalLogin = _externalLogins.HasDenyLocalLogin();
if (hasDenyLocalLogin)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div ng-controller="Umbraco.Editors.Users.DetailsController as vm" class="umb-user-details-details">
<div ng-controller="Umbraco.Editors.Users.DetailsController as vm" class="umb-user-details-details">

<div class="umb-user-details-details__main-content">

Expand Down Expand Up @@ -45,6 +45,8 @@
ng-model="model.user.username"
umb-auto-focus name="username"
required
autocomplete="off"
no-password-manager
val-server-field="Username" />
<span ng-messages="userProfileForm.username.$error" show-validation-on-submit>
<span class="help-inline" ng-message="required"><localize key="general_required">Required</localize></span>
Expand Down
Loading