Skip to content

Commit ef4479a

Browse files
committed
Merge branch '6.4.x'
2 parents 19090e7 + cb60d8b commit ef4479a

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

Diff for: crypto/src/main/java/org/springframework/security/crypto/bcrypt/BCrypt.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,8 @@ private static String hashpw(byte passwordb[], String salt, boolean for_check) {
611611
int rounds, off;
612612
StringBuilder rs = new StringBuilder();
613613

614-
if (passwordb.length > 72) {
614+
// Enforce max length for new passwords only
615+
if (!for_check && passwordb.length > 72) {
615616
throw new IllegalArgumentException("password cannot be more than 72 bytes");
616617
}
617618
if (salt == null) {

Diff for: crypto/src/test/java/org/springframework/security/crypto/bcrypt/BCryptPasswordEncoderTests.java

+26-5
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,34 @@ public void checkWhenNoRoundsThenTrue() {
223223
}
224224

225225
@Test
226-
public void enforcePasswordLength() {
226+
public void encodeWhenPasswordOverMaxLengthThenThrowIllegalArgumentException() {
227227
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
228+
228229
String password72chars = "123456789012345678901234567890123456789012345678901234567890123456789012";
229-
assertThat(encoder.matches(password72chars, encoder.encode(password72chars))).isTrue();
230-
String password73chars = password72chars.concat("a");
231-
assertThatIllegalArgumentException()
232-
.isThrownBy(() -> encoder.matches(password73chars, encoder.encode(password73chars)));
230+
encoder.encode(password72chars);
231+
232+
String password73chars = password72chars + "3";
233+
assertThatIllegalArgumentException().isThrownBy(() -> encoder.encode(password73chars));
234+
}
235+
236+
@Test
237+
public void matchesWhenPasswordOverMaxLengthThenAllowToMatch() {
238+
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
239+
240+
String password71chars = "12345678901234567890123456789012345678901234567890123456789012345678901";
241+
String encodedPassword71chars = "$2a$10$jx3x2FaF.iX5QZ9i3O424Os2Ou5P5JrnedmWYHuDyX8JKA4Unp4xq";
242+
assertThat(encoder.matches(password71chars, encodedPassword71chars)).isTrue();
243+
244+
String password72chars = password71chars + "2";
245+
String encodedPassword72chars = "$2a$10$oXYO6/UvbsH5rQEraBkl6uheccBqdB3n.RaWbrimog9hS2GX4lo/O";
246+
assertThat(encoder.matches(password72chars, encodedPassword72chars)).isTrue();
247+
248+
// Max length is 72 bytes, however, we need to ensure backwards compatibility
249+
// for previously encoded passwords that are greater than 72 bytes and allow the
250+
// match to be performed.
251+
String password73chars = password72chars + "3";
252+
String encodedPassword73chars = "$2a$10$1l9.kvQTsqNLiCYFqmKtQOHkp.BrgIrwsnTzWo9jdbQRbuBYQ/AVK";
253+
assertThat(encoder.matches(password73chars, encodedPassword73chars)).isTrue();
233254
}
234255

235256
}

0 commit comments

Comments
 (0)