Skip to content

Commit

Permalink
fix: prevent nre in user controllers (#1628)
Browse files Browse the repository at this point in the history
* fix: prevent nre in user controllers

* fix: prevent nre in user controllers
  • Loading branch information
lodicolo committed Oct 16, 2022
1 parent f5c316c commit b67f554
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions Intersect.Server/Web/RestApi/Routes/V1/UserController.cs
Expand Up @@ -159,7 +159,7 @@ public object RegisterUser([FromBody] UserInfo user)
}
else
{
DbInterface.CreateAccount(null, user.Username, user.Password.ToUpper().Trim(), user.Email);
DbInterface.CreateAccount(null, user.Username, user.Password?.ToUpperInvariant()?.Trim(), user.Email);

return new
{
Expand Down Expand Up @@ -575,7 +575,7 @@ public object UserChangeEmailByName(string userName, [FromBody] AuthorizedChange
return Request.CreateErrorResponse(HttpStatusCode.NotFound, $@"No user with name '{userName}'.");
}

if (!user.IsPasswordValid(authorizedChange.Authorization.ToUpper().Trim()))
if (!user.IsPasswordValid(authorizedChange.Authorization?.ToUpperInvariant()?.Trim()))
{
return Request.CreateErrorResponse(HttpStatusCode.Forbidden, @"Invalid credentials.");
}
Expand Down Expand Up @@ -619,7 +619,7 @@ public object UserChangeEmailById(Guid userId, [FromBody] AuthorizedChange autho
return Request.CreateErrorResponse(HttpStatusCode.NotFound, $@"No user with id '{userId}'.");
}

if (!user.IsPasswordValid(authorizedChange.Authorization.ToUpper().Trim()))
if (!user.IsPasswordValid(authorizedChange.Authorization?.ToUpperInvariant()?.Trim()))
{
return Request.CreateErrorResponse(HttpStatusCode.Forbidden, @"Invalid credentials.");
}
Expand Down Expand Up @@ -653,7 +653,7 @@ public object UserValidatePasswordByName(string userName, [FromBody] PasswordVal
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, @"No password provided.");
}

if (!Regex.IsMatch(data.Password.ToUpper().Trim(), "^[0-9A-Fa-f]{64}$", RegexOptions.Compiled))
if (!Regex.IsMatch(data.Password?.ToUpperInvariant()?.Trim(), "^[0-9A-Fa-f]{64}$", RegexOptions.Compiled))
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, @"Did not receive a valid password.");
}
Expand All @@ -665,7 +665,7 @@ public object UserValidatePasswordByName(string userName, [FromBody] PasswordVal
return Request.CreateErrorResponse(HttpStatusCode.NotFound, $@"No user with name '{userName}'.");
}

if (user.IsPasswordValid(data.Password.ToUpper().Trim()))
if (user.IsPasswordValid(data.Password?.ToUpperInvariant()?.Trim()))
{
return Request.CreateMessageResponse(HttpStatusCode.OK, "Password Correct");
}
Expand All @@ -687,7 +687,7 @@ public object UserValidatePasswordById(Guid userId, [FromBody] PasswordValidatio
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, @"No password provided.");
}

if (!Regex.IsMatch(data.Password.ToUpper().Trim(), "^[0-9A-Fa-f]{64}$", RegexOptions.Compiled))
if (!Regex.IsMatch(data.Password?.ToUpperInvariant()?.Trim(), "^[0-9A-Fa-f]{64}$", RegexOptions.Compiled))
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, @"Did not receive a valid password.");
}
Expand All @@ -699,7 +699,7 @@ public object UserValidatePasswordById(Guid userId, [FromBody] PasswordValidatio
return Request.CreateErrorResponse(HttpStatusCode.NotFound, $@"No user with name '{userId}'.");
}

if (user.IsPasswordValid(data.Password.ToUpper().Trim()))
if (user.IsPasswordValid(data.Password?.ToUpperInvariant()?.Trim()))
{
return Request.CreateMessageResponse(HttpStatusCode.OK, "Password Correct");
}
Expand Down Expand Up @@ -733,7 +733,7 @@ public object UserChangePassword(string userName, [FromBody] AdminChange authori
return Request.CreateErrorResponse(HttpStatusCode.NotFound, $@"No user with name '{userName}'.");
}

if (!user.TrySetPassword(authorizedChange.New.ToUpper().Trim()))
if (!user.TrySetPassword(authorizedChange.New?.ToUpperInvariant()?.Trim()))
{
return Request.CreateErrorResponse(HttpStatusCode.Forbidden, @"Failed to update password.");
}
Expand Down Expand Up @@ -765,7 +765,7 @@ public object UserChangePassword(Guid userId, [FromBody] AdminChange authorizedC
return Request.CreateErrorResponse(HttpStatusCode.NotFound, $@"No user with name '{userId}'.");
}

if (!user.TrySetPassword(authorizedChange.New.ToUpper().Trim()))
if (!user.TrySetPassword(authorizedChange.New?.ToUpperInvariant()?.Trim()))
{
return Request.CreateErrorResponse(HttpStatusCode.Forbidden, @"Failed to update password.");
}
Expand Down Expand Up @@ -797,7 +797,7 @@ public object UserChangePassword(string userName, [FromBody] AuthorizedChange au
}

if (!user.TryChangePassword(
authorizedChange.Authorization.ToUpper().Trim(), authorizedChange.New.ToUpper().Trim()
authorizedChange.Authorization?.ToUpperInvariant()?.Trim(), authorizedChange.New?.ToUpperInvariant()?.Trim()
))
{
return Request.CreateErrorResponse(HttpStatusCode.Forbidden, @"Invalid credentials.");
Expand Down Expand Up @@ -830,7 +830,7 @@ public object UserChangePassword(Guid userId, [FromBody] AuthorizedChange author
}

if (!user.TryChangePassword(
authorizedChange.Authorization.ToUpper().Trim(), authorizedChange.New.ToUpper().Trim()
authorizedChange.Authorization?.ToUpperInvariant()?.Trim(), authorizedChange.New?.ToUpperInvariant()?.Trim()
))
{
return Request.CreateErrorResponse(HttpStatusCode.Forbidden, @"Invalid credentials.");
Expand Down

0 comments on commit b67f554

Please sign in to comment.