Skip to content

Commit 7d21863

Browse files
committed
Prevent instantiation of DelegatingPasswordEncoder if idPrefix contains idSuffix
Closes gh-10933
1 parent 1edfa07 commit 7d21863

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

crypto/src/main/java/org/springframework/security/crypto/password/DelegatingPasswordEncoder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
* @author Rob Winch
120120
* @author Michael Simons
121121
* @author heowc
122+
* @author Jihoon Cha
122123
* @since 5.0
123124
* @see org.springframework.security.crypto.factory.PasswordEncoderFactories
124125
*/
@@ -173,6 +174,9 @@ public DelegatingPasswordEncoder(String idForEncode, Map<String, PasswordEncoder
173174
if (idSuffix == null || idSuffix.isEmpty()) {
174175
throw new IllegalArgumentException("suffix cannot be empty");
175176
}
177+
if (idPrefix.contains(idSuffix)) {
178+
throw new IllegalArgumentException("idPrefix " + idPrefix + " cannot contain idSuffix " + idSuffix);
179+
}
176180

177181
if (!idToPasswordEncoder.containsKey(idForEncode)) {
178182
throw new IllegalArgumentException(

crypto/src/test/java/org/springframework/security/crypto/password/DelegatingPasswordEncoderTests.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
* @author Rob Winch
3838
* @author Michael Simons
3939
* @author heowc
40+
* @author Jihoon Cha
4041
* @since 5.0
4142
*/
4243
@ExtendWith(MockitoExtension.class)
@@ -119,9 +120,9 @@ public void constructorWhenPrefixAndSuffixAreEmpty() {
119120

120121
@Test
121122
public void constructorWhenIdContainsPrefixThenIllegalArgumentException() {
122-
this.delegates.put('$' + this.bcryptId, this.bcrypt);
123+
this.delegates.put('{' + this.bcryptId, this.bcrypt);
123124
assertThatIllegalArgumentException()
124-
.isThrownBy(() -> new DelegatingPasswordEncoder(this.bcryptId, this.delegates, "$", "$"));
125+
.isThrownBy(() -> new DelegatingPasswordEncoder(this.bcryptId, this.delegates));
125126
}
126127

127128
@Test
@@ -131,6 +132,12 @@ public void constructorWhenIdContainsSuffixThenIllegalArgumentException() {
131132
.isThrownBy(() -> new DelegatingPasswordEncoder(this.bcryptId, this.delegates, "", "$"));
132133
}
133134

135+
@Test
136+
public void constructorWhenPrefixContainsSuffixThenIllegalArgumentException() {
137+
assertThatIllegalArgumentException()
138+
.isThrownBy(() -> new DelegatingPasswordEncoder(this.bcryptId, this.delegates, "$", "$"));
139+
}
140+
134141
@Test
135142
public void setDefaultPasswordEncoderForMatchesWhenNullThenIllegalArgumentException() {
136143
assertThatIllegalArgumentException()

0 commit comments

Comments
 (0)