Skip to content

Commit 6ca0162

Browse files
closes gh-14880
1 parent 8dd28b7 commit 6ca0162

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,17 @@ public String encode(CharSequence rawPassword) {
286286
@Override
287287
public boolean matches(CharSequence rawPassword, String prefixEncodedPassword) {
288288
String id = extractId(prefixEncodedPassword);
289+
checkIfStringIsEmptyOrNull(id);
289290
throw new IllegalArgumentException("There is no PasswordEncoder mapped for the id \"" + id + "\"");
290291
}
291292

293+
private void checkIfStringIsEmptyOrNull(String string) {
294+
if (string == null || string.isEmpty()) {
295+
throw new IllegalArgumentException(
296+
"You have entered a password with no PasswordEncoder. If that is your intent, it should be prefixed with `{noop}`.");
297+
}
298+
}
299+
292300
}
293301

294302
}

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
@ExtendWith(MockitoExtension.class)
4444
public class DelegatingPasswordEncoderTests {
4545

46+
public static final String NO_PASSWORD_ENCODER = "You have entered a password with no PasswordEncoder. If that is your intent, it should be prefixed with `{noop}`.";
47+
4648
@Mock
4749
private PasswordEncoder bcrypt;
4850

@@ -201,23 +203,23 @@ public void matchesWhenUnMappedThenIllegalArgumentException() {
201203
public void matchesWhenNoClosingPrefixStringThenIllegalArgumentException() {
202204
assertThatIllegalArgumentException()
203205
.isThrownBy(() -> this.passwordEncoder.matches(this.rawPassword, "{bcrypt" + this.rawPassword))
204-
.withMessage("There is no PasswordEncoder mapped for the id \"null\"");
206+
.withMessage(NO_PASSWORD_ENCODER);
205207
verifyNoMoreInteractions(this.bcrypt, this.noop);
206208
}
207209

208210
@Test
209211
public void matchesWhenNoStartingPrefixStringThenFalse() {
210212
assertThatIllegalArgumentException()
211213
.isThrownBy(() -> this.passwordEncoder.matches(this.rawPassword, "bcrypt}" + this.rawPassword))
212-
.withMessage("There is no PasswordEncoder mapped for the id \"null\"");
214+
.withMessage(NO_PASSWORD_ENCODER);
213215
verifyNoMoreInteractions(this.bcrypt, this.noop);
214216
}
215217

216218
@Test
217219
public void matchesWhenNoIdStringThenFalse() {
218220
assertThatIllegalArgumentException()
219221
.isThrownBy(() -> this.passwordEncoder.matches(this.rawPassword, "{}" + this.rawPassword))
220-
.withMessage("There is no PasswordEncoder mapped for the id \"\"");
222+
.withMessage(NO_PASSWORD_ENCODER);
221223
verifyNoMoreInteractions(this.bcrypt, this.noop);
222224
}
223225

@@ -226,7 +228,7 @@ public void matchesWhenPrefixInMiddleThenFalse() {
226228
assertThatIllegalArgumentException()
227229
.isThrownBy(() -> this.passwordEncoder.matches(this.rawPassword, "invalid" + this.bcryptEncodedPassword))
228230
.isInstanceOf(IllegalArgumentException.class)
229-
.withMessage("There is no PasswordEncoder mapped for the id \"null\"");
231+
.withMessage(NO_PASSWORD_ENCODER);
230232
verifyNoMoreInteractions(this.bcrypt, this.noop);
231233
}
232234

@@ -236,7 +238,7 @@ public void matchesWhenIdIsNullThenFalse() {
236238
DelegatingPasswordEncoder passwordEncoder = new DelegatingPasswordEncoder(this.bcryptId, this.delegates);
237239
assertThatIllegalArgumentException()
238240
.isThrownBy(() -> passwordEncoder.matches(this.rawPassword, this.rawPassword))
239-
.withMessage("There is no PasswordEncoder mapped for the id \"null\"");
241+
.withMessage(NO_PASSWORD_ENCODER);
240242
verifyNoMoreInteractions(this.bcrypt, this.noop);
241243
}
242244

@@ -289,4 +291,14 @@ public void upgradeEncodingWhenDifferentIdThenTrue() {
289291
verifyNoMoreInteractions(this.bcrypt);
290292
}
291293

294+
@Test
295+
void matchesShouldThrowIllegalArgumentExceptionWhenNoPasswordEncoderIsMappedForTheId() {
296+
assertThatIllegalArgumentException()
297+
.isThrownBy(() -> this.passwordEncoder.matches("rawPassword", "prefixEncodedPassword"))
298+
.isInstanceOf(IllegalArgumentException.class)
299+
.withMessage(NO_PASSWORD_ENCODER);
300+
verifyNoMoreInteractions(this.bcrypt, this.noop);
301+
302+
}
303+
292304
}

0 commit comments

Comments
 (0)