Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,26 @@ void emptyEntriesInThePreviousPlaceholdersAreIgnored() throws Exception {
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");
Expand Down