Skip to content

Commit 532d0be

Browse files
committed
Add Test to Confirm 72-byte BCrypt Password Limit
Closes gh-18133
1 parent fed6df5 commit 532d0be

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

crypto/src/test/java/org/springframework/security/crypto/bcrypt/BCryptPasswordEncoderTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616

1717
package org.springframework.security.crypto.bcrypt;
1818

19+
import java.nio.charset.StandardCharsets;
1920
import java.security.SecureRandom;
2021

2122
import org.junit.jupiter.api.Test;
2223

2324
import static org.assertj.core.api.Assertions.assertThat;
2425
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
26+
import static org.assertj.core.api.Assertions.assertThatNoException;
2527

2628
/**
2729
* @author Dave Syer
@@ -253,4 +255,23 @@ public void matchesWhenPasswordOverMaxLengthThenAllowToMatch() {
253255
assertThat(encoder.matches(password73chars, encodedPassword73chars)).isTrue();
254256
}
255257

258+
/**
259+
* Fixes gh-18133
260+
* @author StringManolo
261+
*/
262+
@Test
263+
void passwordLargerThan72BytesShouldThrowIllegalArgumentException() {
264+
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
265+
String singleByteChars = "a".repeat(68);
266+
String password72Bytes = singleByteChars + "😀";
267+
assertThat(password72Bytes.length()).isEqualTo(70);
268+
assertThat(password72Bytes.getBytes(StandardCharsets.UTF_8).length).isEqualTo(72);
269+
assertThatNoException().isThrownBy(() -> encoder.encode(password72Bytes));
270+
String singleByteCharsTooLong = "a".repeat(69);
271+
String password73Bytes = singleByteCharsTooLong + "😀";
272+
assertThat(password73Bytes.getBytes(StandardCharsets.UTF_8).length).isEqualTo(73);
273+
assertThatIllegalArgumentException().isThrownBy(() -> encoder.encode(password73Bytes))
274+
.withMessageContaining("password cannot be more than 72 bytes");
275+
}
276+
256277
}

0 commit comments

Comments
 (0)