-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Make the DelegatingPasswordEncoder
work correctly, even if the prefix and suffix are the same
#10933
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
DelegatingPasswordEncoder
work correctly, even if the prefix and and suffix are the sameDelegatingPasswordEncoder
work correctly, even if the prefix and suffix are the same
Hi @ghoonch4, I apologize for the delay on a review. Note that often it's best to open an issue to discuss potential changes prior to submitting a PR. Can you go into detail about why this change is necessary, meaning what use case exists for using the same prefix/suffix? Is the goal to prevent user confusion? Could it also be solved by rejecting the same prefix/suffix? Should the documentation be enhanced to prevent confusion? |
According to the Lines 53 to 58 in 0a2a327
This seems to have been added in gh-10278. It means users can use any prefix and suffix they want instead of the default prefix public static PasswordEncoder createDelegatingPasswordEncoder() {
Map<String, PasswordEncoder> encoders = new HashMap<>();
encoders.put("bcrypt", new BCryptPasswordEncoder());
encoders.put("some", new SomePasswordEncoder());
return new DelegatingPasswordEncoder("bcrypt", encoders, "$", "$");
} The instance is created correctly. And users can call the PasswordEncoder passwordEncoder = createDelegatingPasswordEncoder();
boolean matches = passwordEncoder.matches("password", "$bcrypt$$2a$10$dXJ3SW6G7P50lGmMkkmwe.20cQQubK3.HZWzG3YB1tlRy.fqvM/BG");
assertTrue(matches); Expected result is true, but it throws an exception:
The problem starts here: Line 247 in 0a2a327
This code is fine before gh-10278, where prefix and suffix were fixed as |
@ghoonch4 thanks for your detailed reply! I do understand that it is now technically possible to set the prefix/suffix to the same value, hence the bug. What I'm asking is more specifically is there a good reason to do so? The intention, as far as I can tell, is to use different values. For example "[" and "]". I'm not clear on why you would want to use "$" and "$", even though it is possible. If there's a good reason to do so (usually discussed in an issue prior to the PR being opened), we can go from there. I was wondering if you could provide the use case you're running into that requires this functionality to work. |
@sjohnr There is no use case for me. 😅 |
@ghoonch4 not a problem, that’s just the ideal flow.
But this isn’t 100% true though is it? Based on your reported issue, it will never actually work, so no one can successfully use this in the wild, right? They can certainly construct an instance successfully but it won’t work for them. The reason I’m going down this path is that I’m worried there’s a hidden case here we could open up by allowing this. I really think the intended config is two different characters. When we allow two $ chars, our prefix can actually look like bcrypt without a prefix, eg. I feel like it would be better to start by preventing this case proactively with a check in the constructor. We can always change our mind and allow this later, but we wouldn’t be able to easily change our mind if we apply your current suggestion. What do you think? |
@sjohnr Okay, I understand what you're worried about. I think instantiation should be prevented if prefix contains suffix. Would this be enough? |
I think so, yes! |
@sjohnr Please review it again. |
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.
Thanks @ghoonch4!
See a few minor items below, then please squash all commits and rebase on main
using your final commit message (you can omit the back-ticks, e.g. "Prevent instantiation of DelegatingPasswordEncoder if idPrefix contains idSuffix").
...c/test/java/org/springframework/security/crypto/password/DelegatingPasswordEncoderTests.java
Show resolved
Hide resolved
...to/src/main/java/org/springframework/security/crypto/password/DelegatingPasswordEncoder.java
Outdated
Show resolved
Hide resolved
Oops.. I missed it. Thank you! 👍 |
You can inject prefix and suffix when calling the constructor of
DelegatingPasswordEncoder
. And you can also inject the "same" prefix and suffix. In this case the instantiation ofDelegatingPasswordEncoder
works fine, but calling thematches(CharSequence, String)
method throws an exception. There is a bug in the methodsextractId(String)
andextractEncodedPassword(String)
.