Skip to content

Commit 7ead1e1

Browse files
authored
Switch to Spring Security implementation of BCrypt (#241)
Co-authored-by: Daniel Beck <[email protected]>
1 parent 4b61987 commit 7ead1e1

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/main/java/hudson/plugins/active_directory/CacheUtil.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package hudson.plugins.active_directory;
22

33
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
4+
import java.nio.charset.StandardCharsets;
5+
import java.util.Arrays;
46
import org.kohsuke.accmod.Restricted;
57
import org.kohsuke.accmod.restrictions.NoExternalUse;
6-
import org.mindrot.jbcrypt.BCrypt;
8+
import org.springframework.security.crypto.bcrypt.BCrypt;
79

810
import edu.umd.cs.findbugs.annotations.CheckForNull;
911
import edu.umd.cs.findbugs.annotations.NonNull;
@@ -13,6 +15,7 @@
1315

1416
@Restricted(NoExternalUse.class)
1517
public final class CacheUtil {
18+
private static final int BCRYPT_MAX_LENGTH = 72;
1619
@SuppressFBWarnings("MS_SHOULD_BE_FINAL")
1720
public static /* non-final for Groovy */ boolean NO_CACHE_AUTH = Boolean.getBoolean(CacheUtil.class.getName() + ".noCacheAuth"); // Groovy console: hudson.plugins.active_directory.CacheUtil.NO_CACHE_AUTH = true
1821
@SuppressFBWarnings("MS_SHOULD_BE_FINAL")
@@ -62,8 +65,14 @@ private static CacheKey findExistingKeyForUserAndPasswordInSet(String username,
6265
if (!Objects.equals(key.getUsername(), username)) {
6366
continue;
6467
}
65-
// username matches
66-
if (BCrypt.checkpw(password, key.getPasswordHash())) {
68+
// At this point, username matches.
69+
// Next, truncate the password to 72 bytes due to the length limit of BCrypt, otherwise Spring's impl would throw.
70+
// TODO We should use an unlimited length password hash here, but realistically this is unlikely to be a problem
71+
byte[] passwordBytes = password.getBytes(StandardCharsets.UTF_8);
72+
if (passwordBytes.length > BCRYPT_MAX_LENGTH) {
73+
passwordBytes = Arrays.copyOfRange(passwordBytes, 0, BCRYPT_MAX_LENGTH);
74+
}
75+
if (BCrypt.checkpw(passwordBytes, key.getPasswordHash())) {
6776
return key;
6877
}
6978
}

0 commit comments

Comments
 (0)