snoopdave commented on code in PR #304:
URL: https://github.com/apache/roller/pull/304#discussion_r4608463884
##########
app/src/main/java/org/apache/roller/weblogger/ui/core/security/RollerPasswordEncoders.java:
##########
@@ -93,6 +93,33 @@ public static DelegatingPasswordEncoder create() {
log.info("Password Encryption Algorithm set to '" + algorithm + "'");
- return new DelegatingPasswordEncoder(algorithm, encoders);
+ 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.
+ */
+ private record ExternalLoginAware(PasswordEncoder delegate) implements
PasswordEncoder {
+
+ @Override
+ public String encode(CharSequence rawPassword) {
+ return delegate.encode(rawPassword);
+ }
+
+ @Override
+ public boolean matches(CharSequence rawPassword, String
encodedPassword) {
+ String marker =
WebloggerConfig.getProperty("users.passwords.externalAuthValue",
"<externalAuth>");
+ if (rawPassword != null && marker != null && !marker.isEmpty() &&
marker.contentEquals(rawPassword)) {
Review Comment:
**Important:** Database registration and password resets accept the
configured marker as an ordinary password. This check then rejects those users
even when their stored password is a normal hash. Reject the marker when
setting a database password and provide a reset path for existing users, or
distinguish stored external-login placeholders from ordinary hashes.
##########
app/src/test/java/org/apache/roller/weblogger/ui/core/security/ExternalLoginPlaceholderTest.java:
##########
@@ -0,0 +1,129 @@
+/*
+ * 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.lang.reflect.Field;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import org.apache.roller.weblogger.TestUtils;
+import org.apache.roller.weblogger.business.WebloggerFactory;
+import org.apache.roller.weblogger.config.WebloggerConfig;
+import org.apache.roller.weblogger.pojos.User;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import
org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
+import
org.springframework.security.authentication.dao.DaoAuthenticationProvider;
+import org.springframework.security.core.AuthenticationException;
+import org.springframework.security.crypto.password.PasswordEncoder;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Users who log in through LDAP or the servlet container have a placeholder
+ * stored instead of a Roller password. The placeholder is not a password.
+ */
+class ExternalLoginPlaceholderTest {
+
+ private final Map<String, String> savedProperties = new HashMap<>();
+ private String userName;
+
+ @BeforeEach
+ void setUp() throws Exception {
+ TestUtils.setupWeblogger();
+ setProperty("passwds.encryption.enabled", "true");
+ setProperty("passwds.encryption.algorithm", "bcrypt");
+ }
+
+ @AfterEach
+ void tearDown() throws Exception {
+ if (userName != null) {
+ TestUtils.teardownUser(userName);
+ TestUtils.endSession(true);
+ }
+ Properties config = config();
+ savedProperties.forEach((k, v) -> {
+ if (v == null) {
+ config.remove(k);
+ } else {
+ config.setProperty(k, v);
+ }
+ });
+ }
+
+ @Test
+ void anEncodedPlaceholderIsNotAPassword() throws Exception {
+ setProperty("authentication.method", "cma");
+ // as Register stores it for container-managed sign-ups
+ User user = TestUtils.setupUser("cmauser");
+ userName = user.getUserName();
+ // (User.resetPassword encodes with the login's password encoder)
+ user.setPassword(RollerPasswordEncoders.create().encode(
+
WebloggerConfig.getProperty("users.passwords.externalAuthValue",
"<externalAuth>")));
+ WebloggerFactory.getWeblogger().getUserManager().saveUser(user);
+ TestUtils.endSession(true);
+
+ assertThrows(AuthenticationException.class, () ->
provider().authenticate(
+ UsernamePasswordAuthenticationToken.unauthenticated(userName,
"<externalAuth>")));
+ }
+
+ @Test
+ void anUnencodedPlaceholderIsNotAPassword() throws Exception {
+ setProperty("authentication.method", "db");
+ setProperty("passwds.encryption.lazyUpgradeFrom", "plaintext");
+ // as CustomUserRegistry stores it for directory users
+ User user = TestUtils.setupUser("ldapuser");
+ userName = user.getUserName();
+ user.setPassword("<externalAuth>");
+ WebloggerFactory.getWeblogger().getUserManager().saveUser(user);
+ TestUtils.endSession(true);
+
+ assertThrows(AuthenticationException.class, () ->
provider().authenticate(
+ UsernamePasswordAuthenticationToken.unauthenticated(userName,
"<externalAuth>")));
+ }
+
+ @Test
+ void theEncoderDoesNotMatchThePlaceholder() {
+ // the encoder also checks AtomPub and XML-RPC passwords
+ PasswordEncoder encoder = RollerPasswordEncoders.create();
+ assertFalse(encoder.matches("<externalAuth>",
encoder.encode("<externalAuth>")));
+ assertTrue(encoder.matches("secret", encoder.encode("secret")));
Review Comment:
**Important:** The twin PR checks that an ordinary legacy plaintext password
still matches when `passwds.encryption.lazyUpgradeFrom=plaintext`. This test
checks only a bcrypt round trip for ordinary passwords. Add the corresponding
positive plaintext assertion here.
##########
app/src/main/java/org/apache/roller/weblogger/ui/core/security/RollerPasswordEncoders.java:
##########
@@ -93,6 +93,33 @@ public static DelegatingPasswordEncoder create() {
log.info("Password Encryption Algorithm set to '" + algorithm + "'");
- return new DelegatingPasswordEncoder(algorithm, encoders);
+ 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.
+ */
+ private record ExternalLoginAware(PasswordEncoder delegate) implements
PasswordEncoder {
+
+ @Override
+ public String encode(CharSequence rawPassword) {
+ return delegate.encode(rawPassword);
+ }
+
+ @Override
+ public boolean matches(CharSequence rawPassword, String
encodedPassword) {
+ String marker =
WebloggerConfig.getProperty("users.passwords.externalAuthValue",
"<externalAuth>");
+ if (rawPassword != null && marker != null && !marker.isEmpty() &&
marker.contentEquals(rawPassword)) {
Review Comment:
**Important:** `CustomUserRegistry` can store an empty configured marker,
but this condition skips it; legacy plaintext matching then treats it as a
password. Reject an empty marker before it can be stored, and cover that
configuration in a test.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]