Skip to content

#62732 Fix password validation in PasswordHasher`1: add bound check for salt size before array allocation #62734

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

Merged
merged 3 commits into from
Jul 18, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion src/Identity/Extensions.Core/src/PasswordHasher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ private static bool VerifyHashedPasswordV3(byte[] hashedPassword, string passwor
int saltLength = (int)ReadNetworkByteOrder(hashedPassword, 9);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As stated in the issue, this isn't a real problem. However, I do appreciate being cautious as we've been bit by overallocating arrays before.

Having an arbitrary upper limit though isn't super clean. If someone wants to use a massive salt then we shouldn't disallow them unless it's part of the spec.

If we incorporated something like the below, that would be much better as the bounds checking would be more implicit and actually based on the data present.

byte[] salt = hashedPassword.Slice(13, saltLength).ToArray();
byte[] expectedSubkey = hashedPassword.Slice(13 + saltLength).ToArray();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, the ArraySegment and Slice are not fully supported for target frameworks other than .net10, so I just added the hashedPassword.Length - 13 as an upper-bound check for the salt size, which would have same logic as suggested Slice approach.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use hashedPassword.AsSpan(13, saltLength).ToArray() instead.


// Read the salt: must be >= 128 bits
if (saltLength < 128 / 8)
if (saltLength < 128 / 8 || saltLength + 13 > hashedPassword.Length)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI this second clause should read (long)saltLength + 13 > hashedPassword.Length or saltLength > hashedPassword.Length - 13. Otherwise you're potentially subject to integer overflow.

{
return false;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Identity/test/Identity.Test/PasswordHasherTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ public void HashPassword_Version3()
[InlineData("AQAAAAAAAAD6AAAAEAhftMyfTJyAAAAAAAAAAAAAAAAAAAih5WsjXaR3PA9M")] // incorrect password
[InlineData("AQAAAAIAAAAyAAAAEOMwvh3+FZxqkdMBz2ekgGhwQ4A=")] // too short
[InlineData("AQAAAAIAAAAyAAAAEOMwvh3+FZxqkdMBz2ekgGhwQ4B6pZWND6zgESBuWiHwAAAAAAAAAAAA")] // extra data at end
[InlineData("AQAAAAIAAYagAP///wABAgMEBQYHCAkKCwwNDg/Q8A0WMKbtHQJQ2DHCdoEeeFBrgNlldq6vH4qX/CGqGQ==")] // salt length greater than data length
[InlineData("AQAAAAIAAYagAAAACAABAgMEBQYH4qLSh7iNSI12qySxAkyR0XgpXpvNiwqhBJFNLbJKKFw=")] // salt length (8 bytes) less than minimum allowed
public void VerifyHashedPassword_FailureCases(string hashedPassword)
{
// Arrange
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy