This is an automated email from the ASF dual-hosted git repository.

mraible pushed a commit to branch marker-hashes-when-needed-6.1.x
in repository https://gitbox.apache.org/repos/asf/roller.git

commit 4ac60951db68bb1211a8b21f3c5002a4f99c3bc1
Author: Matt Raible <[email protected]>
AuthorDate: Tue Nov 24 08:49:43 2026 -0700

    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.
---
 .../security/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 class ExternalLoginAwarePasswordEncoder implements 
PasswordEncoder {
             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 @@ class ExternalLoginPlaceholderTest {
         assertFalse(ExternalLoginAwarePasswordEncoder.isExternalLoginMarker(" 
"));
     }
 
+    @Test
+    void aPasswordThatBcryptTreatsAsEqualToAPlaceholderIsNotAPassword() throws 
Exception {
+        String longMarker = "<external-login-" + "x".repeat(60) + ">";
+        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 = "<externalAuth>" + '\0' + "<externalAuth>";
+        // BCrypt reads only the first 72 bytes, so this has the long one's
+        String sameStart = longMarker + "suffix";
+        for (String[] pair : new String[][] {{"<externalAuth>", 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");

Reply via email to