Skip to content

Commit d1909ec

Browse files
alan-czajkowskirwinch
authored andcommitted
BCryptPasswordEncoder rawPassword cannot be null
Closes gh-8317
1 parent 2d71297 commit d1909ec

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

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

+8
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ public BCryptPasswordEncoder(BCryptVersion version, int strength, SecureRandom r
9999
}
100100

101101
public String encode(CharSequence rawPassword) {
102+
if (rawPassword == null) {
103+
throw new IllegalArgumentException("rawPassword cannot be null");
104+
}
105+
102106
String salt;
103107
if (random != null) {
104108
salt = BCrypt.gensalt(version.getVersion(), strength, random);
@@ -109,6 +113,10 @@ public String encode(CharSequence rawPassword) {
109113
}
110114

111115
public boolean matches(CharSequence rawPassword, String encodedPassword) {
116+
if (rawPassword == null) {
117+
throw new IllegalArgumentException("rawPassword cannot be null");
118+
}
119+
112120
if (encodedPassword == null || encodedPassword.length() == 0) {
113121
logger.warn("Empty encoded password");
114122
return false;

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

+12
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,16 @@ public void upgradeFromNonBCrypt() {
200200
encoder.upgradeEncoding("not-a-bcrypt-password");
201201
}
202202

203+
@Test(expected = IllegalArgumentException.class)
204+
public void encodeNullRawPassword() {
205+
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
206+
encoder.encode(null);
207+
}
208+
209+
@Test(expected = IllegalArgumentException.class)
210+
public void matchNullRawPassword() {
211+
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
212+
encoder.matches(null, "does-not-matter");
213+
}
214+
203215
}

0 commit comments

Comments
 (0)