From 4ac60951db68bb1211a8b21f3c5002a4f99c3bc1 Mon Sep 17 00:00:00 2001 From: Matt Raible Date: Tue, 24 Nov 2026 08:49:43 -0700 Subject: [PATCH] Hash the external-login markers only for passwords that could collide with one (6.1.x) BCrypt treats a different password as equal to a marker only when the password contains a NUL or is at least 72 bytes, so other logins skip the extra hash per marker. Adds tests for both kinds of collision, which 6.1.x didn't have. --- .../ExternalLoginAwarePasswordEncoder.java | 21 ++++++++++++++----- .../ui/core/ExternalLoginPlaceholderTest.java | 20 ++++++++++++++++++ 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/org/apache/roller/weblogger/ui/core/security/ExternalLoginAwarePasswordEncoder.java b/app/src/main/java/org/apache/roller/weblogger/ui/core/security/ExternalLoginAwarePasswordEncoder.java index 6bf9cc9dd..a3d5a71ad 100644 --- a/app/src/main/java/org/apache/roller/weblogger/ui/core/security/ExternalLoginAwarePasswordEncoder.java +++ b/app/src/main/java/org/apache/roller/weblogger/ui/core/security/ExternalLoginAwarePasswordEncoder.java @@ -17,6 +17,7 @@ package org.apache.roller.weblogger.ui.core.security; +import java.nio.charset.StandardCharsets; import java.util.LinkedHashSet; import java.util.Set; @@ -116,16 +117,26 @@ public boolean matches(CharSequence rawPassword, String encodedPassword) { return false; } // BCrypt, for one, treats some other passwords as equal to a marker, - // so refuse any password whose stored value a marker matches too - for (String marker : markers) { - if (delegate.matches(marker, encodedPassword)) { - logRefusal(); - return false; + // so refuse any password whose stored value a marker matches too. + // BCrypt repeats its key with a NUL after each copy and reads at most + // 72 bytes, so only a password with a NUL, or of 72 bytes or more, + // can collide; the rest skip the extra hashes. + if (mayCollideWithAMarker(rawPassword.toString())) { + for (String marker : markers) { + if (delegate.matches(marker, encodedPassword)) { + logRefusal(); + return false; + } } } return true; } + private static boolean mayCollideWithAMarker(String rawPassword) { + return rawPassword.indexOf('\0') >= 0 + || rawPassword.getBytes(StandardCharsets.UTF_8).length >= 72; + } + private static void logRefusal() { // the password itself is never logged log.info("Refused a password that Roller treats as an external-login marker" diff --git a/app/src/test/java/org/apache/roller/weblogger/ui/core/ExternalLoginPlaceholderTest.java b/app/src/test/java/org/apache/roller/weblogger/ui/core/ExternalLoginPlaceholderTest.java index e93592900..e154c5e99 100644 --- a/app/src/test/java/org/apache/roller/weblogger/ui/core/ExternalLoginPlaceholderTest.java +++ b/app/src/test/java/org/apache/roller/weblogger/ui/core/ExternalLoginPlaceholderTest.java @@ -194,6 +194,26 @@ void emptyEntriesInThePreviousPlaceholdersAreIgnored() throws Exception { assertFalse(ExternalLoginAwarePasswordEncoder.isExternalLoginMarker(" ")); } + @Test + void aPasswordThatBcryptTreatsAsEqualToAPlaceholderIsNotAPassword() throws Exception { + String longMarker = ""; + setProperty("users.passwords.externalAuthValue", longMarker); + PasswordEncoder encoder = new ExternalLoginAwarePasswordEncoder(new BCryptPasswordEncoder()); + // BCrypt repeats its key with a NUL after each copy, so this has the + // default placeholder's hash + String repeated = "" + '\0' + ""; + // BCrypt reads only the first 72 bytes, so this has the long one's + String sameStart = longMarker + "suffix"; + for (String[] pair : new String[][] {{"", repeated}, {longMarker, sameStart}}) { + String stored = encoder.encode(pair[0]); + assertTrue(new BCryptPasswordEncoder().matches(pair[1], stored)); + assertFalse(encoder.matches(pair[1], stored)); + } + // a long password that isn't a placeholder still works + String longPassword = "y".repeat(80); + assertTrue(encoder.matches(longPassword, encoder.encode(longPassword))); + } + @Test void anEmptyConfiguredPlaceholderIsNotAPassword() throws Exception { setProperty("authentication.method", "db");