Skip to content

Commit 3c81e12

Browse files
alan-czajkowskirwinch
authored andcommitted
BCryptPasswordEncoder rawPassword cannot be null
Closes gh-8317
1 parent 929a5de commit 3c81e12

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

crypto/src/main/java/org/springframework/security/crypto/bcrypt/BCryptPasswordEncoder.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ public BCryptPasswordEncoder(int strength, SecureRandom random) {
6565
}
6666

6767
public String encode(CharSequence rawPassword) {
68+
if (rawPassword == null) {
69+
throw new IllegalArgumentException("rawPassword cannot be null");
70+
}
71+
6872
String salt;
6973
if (strength > 0) {
7074
if (random != null) {
@@ -81,6 +85,10 @@ public String encode(CharSequence rawPassword) {
8185
}
8286

8387
public boolean matches(CharSequence rawPassword, String encodedPassword) {
88+
if (rawPassword == null) {
89+
throw new IllegalArgumentException("rawPassword cannot be null");
90+
}
91+
8492
if (encodedPassword == null || encodedPassword.length() == 0) {
8593
logger.warn("Empty encoded password");
8694
return false;

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,15 @@ public void doesntMatchBogusEncodedValue() {
9292
assertThat(encoder.matches("password", "012345678901234567890123456789")).isFalse();
9393
}
9494

95+
@Test(expected = IllegalArgumentException.class)
96+
public void encodeNullRawPassword() {
97+
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
98+
encoder.encode(null);
99+
}
100+
101+
@Test(expected = IllegalArgumentException.class)
102+
public void matchNullRawPassword() {
103+
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
104+
encoder.matches(null, "does-not-matter");
105+
}
95106
}

0 commit comments

Comments
 (0)