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 .BeforeEach ;
2526
2627import static org .assertj .core .api .Assertions .assertThat ;
2728import static org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
29+ import static org .assertj .core .api .Assertions .assertThatNoException ;
2830
2931/**
3032 * @author Dave Syer
@@ -236,4 +238,23 @@ public void matchesWhenPasswordOverMaxLengthThenAllowToMatch() {
236238 assertThat (getEncoder ().matches (password73chars , encodedPassword73chars )).isTrue ();
237239 }
238240
241+ /**
242+ * Fixes gh-18133
243+ * @author StringManolo
244+ */
245+ @ Test
246+ void passwordLargerThan72BytesShouldThrowIllegalArgumentException () {
247+ BCryptPasswordEncoder encoder = new BCryptPasswordEncoder ();
248+ String singleByteChars = "a" .repeat (68 );
249+ String password72Bytes = singleByteChars + "😀" ;
250+ assertThat (password72Bytes .length ()).isEqualTo (70 );
251+ assertThat (password72Bytes .getBytes (StandardCharsets .UTF_8 ).length ).isEqualTo (72 );
252+ assertThatNoException ().isThrownBy (() -> encoder .encode (password72Bytes ));
253+ String singleByteCharsTooLong = "a" .repeat (69 );
254+ String password73Bytes = singleByteCharsTooLong + "😀" ;
255+ assertThat (password73Bytes .getBytes (StandardCharsets .UTF_8 ).length ).isEqualTo (73 );
256+ assertThatIllegalArgumentException ().isThrownBy (() -> encoder .encode (password73Bytes ))
257+ .withMessageContaining ("password cannot be more than 72 bytes" );
258+ }
259+
239260}
You can’t perform that action at this time.
0 commit comments