-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Fixed to not throw an exception when password is null. #58168
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The alternative would be to make the password parameter a non-nullable string. We've been willing to make this kind of "breaking" change in the past since it's low impact. You'd get a runtime exception if you ever passed in null previously after all.
Changing the nullable annotation would be preferrable if we're worried about developers being confused by our use of IdentityErrorDescriber.PasswordTooShort for this case. An ArgumentNullException is clearer about exactly what's wrong with the password.
However, I'm fine with allowing null considering IPasswordValidator<TUser> and PasswordValidator<TUser> has declared the password parameter as a nullable string? since .NET 7. Using PasswordTooShort for null doesn't seem that much more confusing than doing the same for a string with just whitespace which we already do today, and it does seem nice to be able to leave the null checking to the password validator given null is a possible input. And I don't think it's worth adding anything to IdentityErrorDescriber just for this case.
@JamesNK @BrennanConroy Do either of you have opinions on this?
|
I think this fix is fine as is. I would be in favor of changing the nullable annotation if |
|
|
Thanks @shethaadit! |
Do not throw an exception when password is null.
Summary of the changes (Less than 80 chars)
Description
PR Summary
This PR refactors and optimizes our password validation tests while also fixing a critical bug that was causing unintended exceptions for
nullpasswords. The password validation method is designed to acceptnullvalues (handled as empty strings), but it was incorrectly throwing exceptions. This fix resolves the issue and ensures correct behavior.Key Improvements:
Bug Fix for
nullPassword Handling: TheValidateAsyncmethod is expected to acceptnullas input, treating it as an empty string. However, it was throwing an exception, resulting in failed tests fornullpasswords. This PR corrects that behavior, ensuring thatnullpasswords are properly validated without errors, in line with the expected behavior.Combined Test Cases: Separate tests for
null, empty, and too-short passwords have been consolidated into one comprehensive test using the[Theory]attribute withInlineData. This eliminates redundancy and ensures all edge cases (null, empty, and short passwords) are covered in one unified test.Improved Readability and Maintainability: The refactored test cases provide clearer intent and minimize repetitive setup, making the code easier to maintain in the long run.
Performance Gains: By reducing the number of test methods and eliminating duplicate code, we optimize the test execution process, leading to improved performance without sacrificing test coverage.
This change not only resolves the bug but also enhances the maintainability and efficiency of our validation framework, driving significant long-term value.
Fixes #58133