Skip to content

Commit a043ba1

Browse files
committed
better password checking
1 parent a427bc1 commit a043ba1

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

Intersect (Core)/Security/PasswordUtils.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ public static string ComputePasswordHash(string password)
99
{
1010
return BitConverter.ToString(SHA256.HashData(Encoding.UTF8.GetBytes(password ?? string.Empty))).Replace("-", string.Empty);
1111
}
12+
13+
public static bool IsValidClientPasswordHash(string? hashToValidate) =>
14+
hashToValidate is { Length: 64 } && hashToValidate.All(char.IsAsciiHexDigit);
1215
}

Intersect.Server/Web/RestApi/Routes/V1/UserController.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Text.RegularExpressions;
33
using Intersect.Enums;
44
using Intersect.GameObjects;
5+
using Intersect.Security;
56
using Intersect.Server.Collections.Indexing;
67
using Intersect.Server.Collections.Sorting;
78
using Intersect.Server.Database;
@@ -99,7 +100,7 @@ public IActionResult RegisterUser([FromBody] UserInfoRequestBody user)
99100
return BadRequest($@"Invalid username '{user.Username}'.");
100101
}
101102

102-
if (!Regex.IsMatch(user.Password?.ToUpperInvariant()?.Trim(), "^[0-9A-Fa-f]{64}$", RegexOptions.Compiled))
103+
if (PasswordUtils.IsValidClientPasswordHash(user.Password))
103104
{
104105
return BadRequest(@"Did not receive a valid password.");
105106
}
@@ -390,7 +391,7 @@ public IActionResult ValidatePassword(LookupKey lookupKey, [FromBody] PasswordVa
390391
return BadRequest(@"No password provided.");
391392
}
392393

393-
if (!Regex.IsMatch(data.Password?.ToUpperInvariant()?.Trim(), "^[0-9A-Fa-f]{64}$", RegexOptions.Compiled))
394+
if (PasswordUtils.IsValidClientPasswordHash(data.Password))
394395
{
395396
return BadRequest(@"Did not receive a valid password.");
396397
}

0 commit comments

Comments
 (0)