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

rmaucher pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit a0e957a3552d938ea65b48073f825a929a5614da
Author: opencode <[email protected]>
AuthorDate: Tue Dec 1 22:18:18 2026 +0100

    Mask URL passwords carrying path, fragment or whitespace characters
    
    URL authority scanning stopped at whitespace, and the masker skipped a 
candidate credential containing /, # or white space. Those passwords were 
therefore printed in clear text in configuration diagnostics.
    
    Scan to a line break or nested URL and treat delimiters before the last @ 
as part of the password. Keep a numeric host:port followed by a path or 
fragment readable, preserving the existing non-userinfo @ diagnostics.
    
    Add tests for passwords containing /, # and space. mvn test passes: 1185 
tests, 0 failures.
---
 .../apache/tomcat/jdbc/pool/PoolProperties.java    | 93 +++++++++++++---------
 .../tomcat/jdbc/pool/PoolPropertiesBase.java       | 19 +++++
 2 files changed, 74 insertions(+), 38 deletions(-)

diff --git 
a/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/PoolProperties.java
 
b/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/PoolProperties.java
index 20de29f529..767a97bece 100644
--- 
a/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/PoolProperties.java
+++ 
b/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/PoolProperties.java
@@ -1874,11 +1874,12 @@ public class PoolProperties implements 
PoolConfiguration, Cloneable, Serializabl
      * parse of a driver's dialect, covering the {@code user:password@host}
      * userinfo form of a {@code scheme://} URL and the {@code 
user/password@host}
      * form of the legacy Oracle driver URLs. The scan of an authority is
-     * bounded so that a {@code :} of a port or an {@code @} or {@code :}
-     * appearing later in the path or query is not mistaken for a credential
-     * separator; a name that is not followed by a credential separator inside
-     * the authority is treated as a bare host and left alone rather than
-     * guessed at.
+     * bounded at a line break and at a nested URL, and by the first
+     * path/query/fragment delimiter after a candidate {@code @}. Separators
+     * before that {@code @} are treated as part of the credential, because a
+     * driver can accept them unencoded in a password. The masking is
+     * intentionally conservative: if the text is ambiguous, masking it is
+     * preferred to leaving a possible password tail in cleartext.
      *
      * @param value the raw configuration string, not {@code null}
      *
@@ -1892,10 +1893,10 @@ public class PoolProperties implements 
PoolConfiguration, Cloneable, Serializabl
         List<int[]> ranges = new ArrayList<>(2);
         // Every scheme:// authority in the string: a failover or logging URL
         // carried in a value is as credential-bearing as the URL in front of
-        // it. The scan of one URL runs until the next URL, a space, or the
-        // first path/query/host delimiter that follows a candidate userinfo;
-        // a delimiter before the '@' is part of the password, not the end of
-        // the authority, because a driver can accept those characters
+        // it. The scan of one URL runs until the next URL or a line break, and
+        // then to the first path/query/host delimiter that follows a candidate
+        // userinfo. A delimiter before the '@' is part of the password, not 
the
+        // end of the authority, because a driver can accept those characters
         // unencoded in a credential.
         int from = 0;
         int schemeEnd;
@@ -1907,11 +1908,13 @@ public class PoolProperties implements 
PoolConfiguration, Cloneable, Serializabl
                 int colon = value.indexOf(':', authorityStart);
                 if (colon >= 0 && colon < at) {
                     String secret = value.substring(colon + 1, at);
-                    // A '/', '#' or white space before the '@' means the scan
-                    // crossed into a path, fragment or another value rather
-                    // than reading userinfo.
-                    if (secret.indexOf('/') < 0 && secret.indexOf('#') < 0
-                            && !containsWhitespace(secret)) {
+                    // The text between the first ':' and the last '@' can be
+                    // the userinfo password even when it carries '/', '#' or
+                    // white space, which drivers can accept unencoded in a
+                    // password. A purely numeric prefix followed by one of
+                    // those delimiters is indistinguishable from a host:port
+                    // followed by a path or fragment, so it is left alone.
+                    if (!looksLikePortAndPath(secret)) {
                         ranges.add(new int[] { colon + 1, at });
                     }
                 }
@@ -1963,12 +1966,44 @@ public class PoolProperties implements 
PoolConfiguration, Cloneable, Serializabl
 
 
     /**
-     * Finds the end of one scheme:// URL scan. The scan runs until white 
space,
-     * a line break, a nested {@code ://}, the end of the string, or a
+     * Whether a candidate credential begins with digits followed by a path, 
fragment or value boundary, the shape a
+     * host:port authority followed by a path or fragment also has. The 
numeric prefix is what lets this helper leave
+     * {@code host:5432/[email protected]} and {@code host:80#frag@x} 
readable instead of masking their host and
+     * path. A password whose own prefix is purely numeric and followed by one 
of those characters remains ambiguous
+     * and is not masked; no URL-shaped scan can distinguish the two.
+     *
+     * @param candidate the text between the first colon and the last '@'
+     *
+     * @return {@code true} when the candidate looks like a host:port followed 
by a path or fragment
+     */
+    private static boolean looksLikePortAndPath(String candidate) {
+        int delimiter = -1;
+        for (int i = 0; i < candidate.length(); i++) {
+            char c = candidate.charAt(i);
+            if (c == '/' || c == '#' || isWhitespace(c)) {
+                delimiter = i;
+                break;
+            }
+        }
+        if (delimiter <= 0) {
+            return false;
+        }
+        for (int i = 0; i < delimiter; i++) {
+            if (!Character.isDigit(candidate.charAt(i))) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+
+    /**
+     * Finds the end of one scheme:// URL scan. The scan runs until a line 
break,
+     * a nested {@code ://}, the end of the string, or a
      * path/query/fragment delimiter that follows a candidate '@'. Separators
-     * before that '@' are password characters: a URL password may carry them
-     * unencoded, and ending the authority at one would leave the password
-     * tail unmasked.
+     * before that '@' are password characters, including white space: a URL
+     * password may carry them unencoded, and ending the authority at one would
+     * leave the password tail unmasked.
      *
      * @param value the raw configuration string
      * @param start the first character after the {@code ://}
@@ -1980,7 +2015,7 @@ public class PoolProperties implements PoolConfiguration, 
Cloneable, Serializabl
         int i = start;
         while (i < value.length()) {
             char c = value.charAt(i);
-            if (isWhitespace(c) || (i > start && value.startsWith("://", i))) {
+            if (c == '\n' || c == '\r' || (i > start && 
value.startsWith("://", i))) {
                 return i;
             }
             if (c == '@') {
@@ -2069,24 +2104,6 @@ public class PoolProperties implements 
PoolConfiguration, Cloneable, Serializabl
     }
 
 
-    /**
-     * Whether a candidate credential contains white space, which means the
-     * scan crossed into a later value rather than reading userinfo.
-     *
-     * @param value the candidate credential
-     *
-     * @return {@code true} when it contains white space
-     */
-    private static boolean containsWhitespace(String value) {
-        for (int i = 0; i < value.length(); i++) {
-            if (isWhitespace(value.charAt(i))) {
-                return true;
-            }
-        }
-        return false;
-    }
-
-
     /**
      * Finds the first occurrence of {@code needle} in {@code value} at or
      * after {@code from}, ignoring case.
diff --git 
a/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/pool/PoolPropertiesBase.java
 
b/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/pool/PoolPropertiesBase.java
index 7dd8a5893a..89ad319ee0 100644
--- 
a/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/pool/PoolPropertiesBase.java
+++ 
b/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/pool/PoolPropertiesBase.java
@@ -1000,6 +1000,25 @@ abstract class PoolPropertiesBase {
         }
     }
 
+    @Test
+    void 
toStringMasksUserinfoPasswordsCarryingPathFragmentOrWhitespaceCharacters() {
+        String[][] cases = {
+                { "jdbc:mysql://user:pa/ss@host/db", 
"jdbc:mysql://user:********@host/db", "pa/ss" },
+                { "jdbc:mysql://user:pa#ss@host/db", 
"jdbc:mysql://user:********@host/db", "pa#ss" },
+                { "jdbc:mysql://user:pa ss@host/db", 
"jdbc:mysql://user:********@host/db", "pa ss" } };
+        for (String[] testCase : cases) {
+            PoolProperties properties = new PoolProperties();
+            properties.setUrl(testCase[0]);
+
+            String asString = properties.toString();
+
+            Assertions.assertFalse(asString.contains(testCase[2]),
+                    "the password carrying the delimiter must not leak: " + 
testCase[0] + ", was: " + asString);
+            Assertions.assertTrue(asString.contains("url=" + testCase[1]),
+                    "the user and host must stay readable around the mask: " + 
testCase[0] + ", was: " + asString);
+        }
+    }
+
     @Test
     void toStringDoesNotMistakeAFragmentOrQueryAtSignForAUserinfoPassword() {
         String[] urls = {


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to