File tree Expand file tree Collapse file tree 1 file changed +21
-0
lines changed
crypto/src/test/java/org/springframework/security/crypto/bcrypt Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change 1616
1717package org .springframework .security .crypto .bcrypt ;
1818
19+ import java .nio .charset .StandardCharsets ;
1920import java .security .SecureRandom ;
2021
2122import org .junit .jupiter .api .Test ;
2223
2324import static org .assertj .core .api .Assertions .assertThat ;
2425import 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}
You can’t perform that action at this time.
0 commit comments