Prevent instantiation of DelegatingPasswordEncoder if idPrefix contains idSuffix

Closes gh-10933
This commit is contained in:
Jihoon Cha 2022-03-29 19:28:41 +09:00 committed by Steve Riesenberg
parent c719a8e4d4
commit da606627b6
2 changed files with 13 additions and 2 deletions

View File

@ -119,6 +119,7 @@ import java.util.Map;
* @author Rob Winch
* @author Michael Simons
* @author heowc
* @author Jihoon Cha
* @since 5.0
* @see org.springframework.security.crypto.factory.PasswordEncoderFactories
*/
@ -173,6 +174,9 @@ public class DelegatingPasswordEncoder implements PasswordEncoder {
if (idSuffix == null || idSuffix.isEmpty()) {
throw new IllegalArgumentException("suffix cannot be empty");
}
if (idPrefix.contains(idSuffix)) {
throw new IllegalArgumentException("idPrefix " + idPrefix + " cannot contain idSuffix " + idSuffix);
}
if (!idToPasswordEncoder.containsKey(idForEncode)) {
throw new IllegalArgumentException(

View File

@ -37,6 +37,7 @@ import static org.mockito.Mockito.verifyZeroInteractions;
* @author Rob Winch
* @author Michael Simons
* @author heowc
* @author Jihoon Cha
* @since 5.0
*/
@ExtendWith(MockitoExtension.class)
@ -119,9 +120,9 @@ public class DelegatingPasswordEncoderTests {
@Test
public void constructorWhenIdContainsPrefixThenIllegalArgumentException() {
this.delegates.put('$' + this.bcryptId, this.bcrypt);
this.delegates.put('{' + this.bcryptId, this.bcrypt);
assertThatIllegalArgumentException()
.isThrownBy(() -> new DelegatingPasswordEncoder(this.bcryptId, this.delegates, "$", "$"));
.isThrownBy(() -> new DelegatingPasswordEncoder(this.bcryptId, this.delegates));
}
@Test
@ -131,6 +132,12 @@ public class DelegatingPasswordEncoderTests {
.isThrownBy(() -> new DelegatingPasswordEncoder(this.bcryptId, this.delegates, "", "$"));
}
@Test
public void constructorWhenPrefixContainsSuffixThenIllegalArgumentException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> new DelegatingPasswordEncoder(this.bcryptId, this.delegates, "$", "$"));
}
@Test
public void setDefaultPasswordEncoderForMatchesWhenNullThenIllegalArgumentException() {
assertThatIllegalArgumentException()