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
7 changes: 5 additions & 2 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,11 @@
- **New passwords can't be the external-login placeholder.** Registration,
the profile page and user administration refuse
`users.passwords.externalAuthValue` (by default `<externalAuth>`) as a
password, and it never works as one. An administrator can set a new password
for an account that used it (#306).
password, and it never works as one. When you change that setting, list its
earlier values in the new `users.passwords.externalAuthValue.previous`
(comma-separated) so they're treated the same. A login refused for this
reason is logged, and an administrator can set a new password for an
account that used one (#306).
- **The LDAP comment authenticator escapes the commenter's user name** when
it builds the bind name (#334).
- **Roller UI stylesheets, scripts and images don't start a session** (#332).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.apache.roller.weblogger.business.startup.WebloggerStartup;
import org.apache.roller.weblogger.ui.core.plugins.UIPluginManager;
import org.apache.roller.weblogger.ui.core.plugins.UIPluginManagerImpl;
import org.apache.roller.weblogger.ui.core.security.ExternalLoginAwarePasswordEncoder;
import org.apache.roller.weblogger.ui.core.security.AutoProvision;
import org.apache.roller.weblogger.ui.core.security.BootstrapSecurity;
import org.apache.roller.weblogger.util.Reflection;
Expand All @@ -70,7 +71,7 @@ public class RollerContext extends ContextLoaderListener
private static final Log log = LogFactory.getLog(RollerContext.class);

private static ServletContext servletContext = null;
private static DelegatingPasswordEncoder encoder;
private static PasswordEncoder encoder;


public RollerContext() {
Expand Down Expand Up @@ -278,7 +279,7 @@ protected void initializeSecurityFeatures(ServletContext context) {
}
}

encoder = createPasswordEncoder();
encoder = new ExternalLoginAwarePasswordEncoder(createPasswordEncoder());
Comment thread
mraible marked this conversation as resolved.
Resolved

String daoBeanName = "org.springframework.security.authentication.dao.DaoAuthenticationProvider#0";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ else if(userDetails instanceof LdapUserDetails) {

// for LDAP we don't store its password in the roller_users table,
// just an string indicating external auth method being used.
unusedPassword = WebloggerConfig.getProperty("users.passwords.externalAuthValue","<externalAuth>");
unusedPassword = ExternalLoginAwarePasswordEncoder.externalLoginMarker();
ud.setPassword(unusedPassword);
ud.setEnabled(enabled ? Boolean.TRUE : Boolean.FALSE);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.roller.weblogger.ui.core.security;

import java.util.LinkedHashSet;
import java.util.Set;

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;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
* 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 this encoder never matches it, nor
* the default or a previous placeholder, nor a password the delegate treats
* as equal to one of them, nor an empty password.
*/
public class ExternalLoginAwarePasswordEncoder implements PasswordEncoder {

private static final Log log = LogFactory.getLog(ExternalLoginAwarePasswordEncoder.class);

/** Stored in place of a password when no other value is configured. */
public static final String DEFAULT_EXTERNAL_LOGIN_MARKER = "<externalAuth>";

private final PasswordEncoder delegate;

public ExternalLoginAwarePasswordEncoder(PasswordEncoder delegate) {
this.delegate = delegate;
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.");
}
}

/**
* The value stored in place of a Roller password for users who log in
* through LDAP or the servlet container: users.passwords.externalAuthValue,
* or the default when that setting is empty.
*/
public static String externalLoginMarker() {
String marker = WebloggerConfig.getProperty("users.passwords.externalAuthValue");
return marker == null || marker.isEmpty() ? DEFAULT_EXTERNAL_LOGIN_MARKER : marker;
}

/**
* 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 && 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;
}

@Override
public String encode(CharSequence rawPassword) {
return delegate.encode(rawPassword);
}

@Override
public boolean matches(CharSequence rawPassword, String encodedPassword) {
// the delegate can throw for a missing or empty stored value
if (rawPassword == null || rawPassword.length() == 0
|| encodedPassword == null || encodedPassword.isEmpty()) {
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 (markers.contains(encodedPassword)) {
return false;
}
if (markers.contains(rawPassword.toString())) {
logRefusal();
return false;
}
if (!delegate.matches(rawPassword, 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;
}
}
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
public boolean upgradeEncoding(String encodedPassword) {
return delegate.upgradeEncoding(encodedPassword);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected String makeTokenSignature(long tokenExpiryTime, String username, Strin

// for LDAP we don't store its password in the roller_users table,
// just an string indicating external auth method being used.
password = WebloggerConfig.getProperty("users.passwords.externalAuthValue","<externalAuth>");
password = ExternalLoginAwarePasswordEncoder.externalLoginMarker();
}

String data = username + ":" + tokenExpiryTime + ":" + password + ":" + getKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.apache.roller.weblogger.ui.core.RollerLoginSessionManager;
import org.apache.roller.weblogger.ui.struts2.core.Register;
import org.apache.roller.weblogger.ui.struts2.util.UIAction;
import org.apache.roller.weblogger.ui.core.security.ExternalLoginAwarePasswordEncoder;
import org.apache.struts2.interceptor.validation.SkipValidation;


Expand Down Expand Up @@ -230,6 +231,10 @@ private boolean isAdd() {
}

private void myValidate() {
// the external-login marker never matches at login
if (ExternalLoginAwarePasswordEncoder.isExternalLoginMarker(getBean().getPassword())) {
addError("error.add.user.reservedPassword");
}
if (isAdd()) {
String allowed = WebloggerConfig.getProperty("username.allowedChars");
if(allowed == null || allowed.isBlank()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.roller.weblogger.config.WebloggerConfig;
import org.apache.roller.weblogger.pojos.User;
import org.apache.roller.weblogger.ui.struts2.util.UIAction;
import org.apache.roller.weblogger.ui.core.security.ExternalLoginAwarePasswordEncoder;
import org.apache.struts2.interceptor.validation.SkipValidation;

import java.util.Arrays;
Expand Down Expand Up @@ -133,6 +134,10 @@ public String save() {
}

public void myValidate() {
// the external-login marker never matches at login
if (ExternalLoginAwarePasswordEncoder.isExternalLoginMarker(getBean().getPasswordText())) {
addError("error.add.user.reservedPassword");
}
if (StringUtils.isEmpty(getBean().getOpenIdUrl())) {
// check that passwords match if they were specified (w/StringUtils.equals, null == null)
if (!StringUtils.equals(getBean().getPasswordText(), getBean().getPasswordConfirm())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.apache.roller.weblogger.ui.core.security.BootstrapSecurity;
import org.apache.roller.weblogger.ui.struts2.util.UIAction;
import org.apache.roller.weblogger.util.MailUtil;
import org.apache.roller.weblogger.ui.core.security.ExternalLoginAwarePasswordEncoder;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.validation.SkipValidation;
Expand Down Expand Up @@ -395,7 +396,7 @@ public void myValidate() {
if (usingSSO) {
// store an unused marker in the Roller DB for the passphrase in
// the LDAP or CMA cases, as actual passwords are stored externally
String unusedPassword = WebloggerConfig.getProperty("users.passwords.externalAuthValue", "<externalAuth>");
String unusedPassword = ExternalLoginAwarePasswordEncoder.externalLoginMarker();

// Preserve username and password, Spring Security case
preserveUsernameAndPassword(unusedPassword);
Expand Down Expand Up @@ -427,8 +428,14 @@ public void myValidate() {
getBean().setPasswordConfirm(randomString);
}

// the external-login marker is stored for users who log in elsewhere
// and never matches at login
if (!usingSSO && ExternalLoginAwarePasswordEncoder.isExternalLoginMarker(getBean().getPasswordText())) {
addError("error.add.user.reservedPassword");
}

// check that passwords match
if (!getBean().getPasswordText().equals(getBean().getPasswordConfirm())) {
if (!StringUtils.equals(getBean().getPasswordText(), getBean().getPasswordConfirm())) {
addError("userRegister.error.mismatchedPasswords");
}

Expand Down
1 change: 1 addition & 0 deletions app/src/main/resources/ApplicationResources.properties
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ error.add.user.openIdInUse=Open ID already in use with another account.
error.add.user.missingUserName=You must specify a username.
error.add.user.badUserName=Invalid user name (must be alpha-numerics only).
error.add.user.missingPassword=You must specify a password.
error.add.user.reservedPassword=That password is reserved for accounts that log in elsewhere. Please choose a different password.
error.add.user.invalid.timezone=Invalid timezone.
error.add.user.invalid.locale=Invalid locale.
error.upload.dirmax=You cannot exceed the maximum directory size of {0} MB.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,13 @@ weblog.inlineImages.maxFieldBytes=60000
# openid presently generates a random (long) password string instead.
users.passwords.externalAuthValue=<externalAuth>

# Comma-separated values that users.passwords.externalAuthValue had before.
# Accounts created then still have them stored, so list them here when changing
# that setting; none of them is accepted as a password. A value that contains
# a comma can't be listed, and an account whose own password is one of them
# needs an administrator to set a new password.
users.passwords.externalAuthValue.previous=

# Password security settings
passwds.encryption.enabled=true
passwds.encryption.algorithm=bcrypt
Expand Down
Loading
Loading