Repository navigation
Recognize previously configured external-login markers #364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
2dc0ef0
e2aca52
5f2b281
0009ac8
71e2ac7
9aa936f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,9 +19,12 @@ | |
|
|
||
|
|
||
| import java.util.HashMap; | ||
| import java.util.LinkedHashSet; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| import org.apache.commons.lang3.RandomStringUtils; | ||
| import org.apache.commons.lang3.StringUtils; | ||
| import org.apache.commons.logging.Log; | ||
| import org.apache.commons.logging.LogFactory; | ||
| import org.apache.roller.weblogger.config.WebloggerConfig; | ||
|
|
@@ -57,14 +60,33 @@ public static String externalLoginMarker() { | |
| } | ||
|
|
||
| /** | ||
| * Whether a password is the configured external-login marker or the | ||
| * default one, which accounts may still have stored from before the | ||
| * setting changed. Neither matches at login, so neither can be chosen as a | ||
| * Roller password. | ||
| * Whether a password is the configured external-login marker, the default | ||
| * one, or one listed in users.passwords.externalAuthValue.previous, which | ||
| * accounts may still have stored from before the setting changed. None of | ||
| * them matches at login, so none can be chosen as a Roller password. | ||
| */ | ||
| public static boolean isExternalLoginMarker(CharSequence password) { | ||
| return password != null && (externalLoginMarker().contentEquals(password) | ||
| || DEFAULT_EXTERNAL_LOGIN_MARKER.contentEquals(password)); | ||
| return password != null && externalLoginMarkers().contains(password.toString()); | ||
| } | ||
|
|
||
| /** | ||
| * The configured marker, the default one and the previous ones. The | ||
| * previous ones are a comma-separated list, so a marker that contains a | ||
| * comma can't be listed there. | ||
| */ | ||
| private static Set<String> externalLoginMarkers() { | ||
| Set<String> markers = new LinkedHashSet<>(); | ||
| markers.add(externalLoginMarker()); | ||
| markers.add(DEFAULT_EXTERNAL_LOGIN_MARKER); | ||
| String previous = WebloggerConfig.getProperty("users.passwords.externalAuthValue.previous"); | ||
| for (String marker : StringUtils.split(StringUtils.defaultString(previous), ',')) { | ||
| // an empty entry is never a marker, so an empty password isn't one | ||
| String trimmed = marker.trim(); | ||
| if (!trimmed.isEmpty()) { | ||
| markers.add(trimmed); | ||
| } | ||
| } | ||
| return markers; | ||
| } | ||
|
|
||
| @SuppressWarnings("deprecation") | ||
|
|
@@ -119,14 +141,20 @@ public static PasswordEncoder create() { | |
|
|
||
| log.info("Password Encryption Algorithm set to '" + algorithm + "'"); | ||
|
|
||
| if (externalLoginMarker().contains(",")) { | ||
| log.warn("users.passwords.externalAuthValue contains a comma, so it can't be listed in" | ||
| + " users.passwords.externalAuthValue.previous if the setting changes later."); | ||
| } | ||
|
|
||
| return new ExternalLoginAware(new DelegatingPasswordEncoder(algorithm, encoders)); | ||
| } | ||
|
|
||
| /** | ||
| * Users who log in through LDAP or the servlet container have no Roller | ||
| * password; Roller stores users.passwords.externalAuthValue in its place. | ||
| * That placeholder is not a password, so it never matches, and neither do | ||
| * the default placeholder and an empty password. | ||
| * the default or a previous placeholder, a password the delegate treats as | ||
| * equal to one of them, and an empty password. | ||
| */ | ||
| private record ExternalLoginAware(PasswordEncoder delegate) implements PasswordEncoder { | ||
|
|
||
|
|
@@ -137,13 +165,39 @@ public String encode(CharSequence rawPassword) { | |
|
|
||
| @Override | ||
| public boolean matches(CharSequence rawPassword, String encodedPassword) { | ||
| if (rawPassword == null || rawPassword.length() == 0) { | ||
| return false; | ||
| } | ||
| Set<String> markers = externalLoginMarkers(); | ||
| // a stored marker is checked before the delegate sees it, since a | ||
| // marker such as {noop}secret would otherwise match its own suffix | ||
| if (rawPassword == null || rawPassword.length() == 0 || isExternalLoginMarker(rawPassword) | ||
| || isExternalLoginMarker(encodedPassword)) { | ||
| if (markers.contains(encodedPassword)) { | ||
| return false; | ||
| } | ||
| if (markers.contains(rawPassword.toString())) { | ||
| logRefusal(); | ||
| return false; | ||
| } | ||
| if (!delegate.matches(rawPassword, encodedPassword)) { | ||
| return false; | ||
| } | ||
| return delegate.matches(rawPassword, encodedPassword); | ||
| // 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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🐞Claude Issue: Important: Each successful login now runs one more delegate match for every marker. With BCrypt, that is one more full hash for each marker: 2 in total with the default setting, 3 or more with a custom marker or a String raw = rawPassword.toString();
if (raw.indexOf('\0') >= 0 || raw.getBytes(StandardCharsets.UTF_8).length >= 72) {
for (String marker : markers) { ... }
}The tests still cover this, because |
||
| if (delegate.matches(marker, encodedPassword)) { | ||
| logRefusal(); | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| private static void logRefusal() { | ||
| // the password itself is never logged | ||
| log.info("Refused a password that Roller treats as an external-login marker" | ||
| + " (users.passwords.externalAuthValue or its .previous list). An" | ||
| + " administrator can set a new password for an account that uses one."); | ||
| } | ||
|
|
||
| @Override | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🐞Claude Issue: Important: The 6.1.x twin (#306) also returns
falsefor a null or emptyencodedPasswordbefore the delegate sees it ("the delegate can throw for a missing or empty stored value"). Here, withoutlazyUpgradeFrom,DelegatingPasswordEncoderthrowsIllegalArgumentExceptionfor a stored value with no{id}. That makes the login fail with an error instead of a bad-credentials result. Add the same guard so both branches behave the same: