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

markt-asf pushed a commit to branch 10.1.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/10.1.x by this push:
     new a1eff82513 Parse the Digest Authorization header as specified by RFC 
9110 (#1109)
a1eff82513 is described below

commit a1eff825135f15b64ebe384842e3ff5b14337753
Author: Arturo Bernal <[email protected]>
AuthorDate: Tue Nov 17 12:19:48 2026 +0100

    Parse the Digest Authorization header as specified by RFC 9110 (#1109)
    
    The authentication scheme and the parameter names are case-insensitive
    (sections 11.1 and 11.2) and a recipient must ignore empty list
    elements (section 5.6.1.2). The parser matched "Digest" case-sensitively,
    kept the parameter names as received, so a name such as "Username" was
    never found, and rejected a list with an empty element. Match the scheme
    as a token, lower case the parameter names and skip the empty elements.
---
 .../tomcat/util/http/parser/Authorization.java     | 27 ++++++++-
 .../util/http/parser/TestAuthorizationDigest.java  | 64 ++++++++++++++++++++++
 2 files changed, 88 insertions(+), 3 deletions(-)

diff --git a/java/org/apache/tomcat/util/http/parser/Authorization.java 
b/java/org/apache/tomcat/util/http/parser/Authorization.java
index 5edd6ffdb6..ae864b461f 100644
--- a/java/org/apache/tomcat/util/http/parser/Authorization.java
+++ b/java/org/apache/tomcat/util/http/parser/Authorization.java
@@ -74,9 +74,21 @@ public class Authorization {
 
         Map<String,String> result = new HashMap<>();
 
-        if (HttpParser.skipConstant(input, "Digest") != SkipResult.FOUND) {
+        // The authentication scheme is case-insensitive (RFC 9110, section 
11.1)
+        if (!"Digest".equalsIgnoreCase(HttpParser.readToken(input))) {
             return null;
         }
+        // The scheme must be followed by at least one SP (RFC 9110, section 
11.4)
+        input.mark(1);
+        int c = input.read();
+        if (c != -1 && c != ' ') {
+            return null;
+        }
+        input.reset();
+        // Empty list elements must be ignored (RFC 9110, section 5.6.1.2)
+        while (HttpParser.skipConstant(input, ",") == SkipResult.FOUND) {
+            // Skip
+        }
         // All field names are valid tokens
         String field = HttpParser.readToken(input);
         if (field == null) {
@@ -110,11 +122,20 @@ public class Authorization {
             if (value == null) {
                 return null;
             }
-            result.put(field, value);
+            // The parameter names are case-insensitive (RFC 9110, section 
11.2)
+            // Duplicate parameters are not allowed
+            if (result.put(field.toLowerCase(Locale.ENGLISH), value) != null) {
+                return null;
+            }
 
-            if (HttpParser.skipConstant(input, ",") == SkipResult.NOT_FOUND) {
+            SkipResult skipResult = HttpParser.skipConstant(input, ",");
+            if (skipResult == SkipResult.NOT_FOUND) {
                 return null;
             }
+            // Empty list elements must be ignored (RFC 9110, section 5.6.1.2)
+            while (skipResult == SkipResult.FOUND) {
+                skipResult = HttpParser.skipConstant(input, ",");
+            }
             field = HttpParser.readToken(input);
             if (field == null) {
                 return null;
diff --git 
a/test/org/apache/tomcat/util/http/parser/TestAuthorizationDigest.java 
b/test/org/apache/tomcat/util/http/parser/TestAuthorizationDigest.java
index 9436f33a9d..57d18466c3 100644
--- a/test/org/apache/tomcat/util/http/parser/TestAuthorizationDigest.java
+++ b/test/org/apache/tomcat/util/http/parser/TestAuthorizationDigest.java
@@ -512,4 +512,68 @@ public class TestAuthorizationDigest {
         Map<String,String> result = 
Authorization.parseAuthorizationDigest(input);
         Assert.assertNull(result);
     }
+
+    @Test
+    public void testSchemeCaseInsensitive() throws Exception {
+        // RFC 9110, section 11.1: the authentication scheme is 
case-insensitive
+        for (String scheme : new String[] { "digest", "DIGEST", "dIgEsT" }) {
+            Map<String,String> result =
+                    Authorization.parseAuthorizationDigest(new 
StringReader(scheme + " username=\"a\""));
+            Assert.assertNotNull(scheme, result);
+            Assert.assertEquals(scheme, "a", result.get("username"));
+        }
+    }
+
+    @Test
+    public void testSchemeMustBeFollowedBySpace() throws Exception {
+        // RFC 9110, section 11.4: the scheme is followed by 1*SP
+        String[] headers = new String[] { "Digestusername=\"a\"", 
"Digest,username=\"a\"",
+                "Digest\tusername=\"a\"" };
+        for (String header : headers) {
+            Assert.assertNull(header, 
Authorization.parseAuthorizationDigest(new StringReader(header)));
+        }
+    }
+
+    @Test
+    public void testSchemeFollowedByMultipleSpaces() throws Exception {
+        Map<String,String> result = Authorization.parseAuthorizationDigest(new 
StringReader("Digest   username=\"a\""));
+        Assert.assertEquals("a", result.get("username"));
+    }
+
+    @Test
+    public void testDuplicateParameter() throws Exception {
+        String[] headers = new String[] { "Digest username=\"a\", 
username=\"b\"",
+                "Digest username=\"a\", USERNAME=\"a\"" };
+        for (String header : headers) {
+            Assert.assertNull(header, 
Authorization.parseAuthorizationDigest(new StringReader(header)));
+        }
+    }
+
+    @Test
+    public void testParameterNamesCaseInsensitive() throws Exception {
+        // RFC 9110, section 11.2: parameter names are matched 
case-insensitively
+        String header = "Digest Username=\"a\", REALM=\"r\", Nc=00000001, 
QoP=auth";
+
+        Map<String,String> result = Authorization.parseAuthorizationDigest(new 
StringReader(header));
+
+        Assert.assertEquals("a", result.get("username"));
+        Assert.assertEquals("r", result.get("realm"));
+        Assert.assertEquals("00000001", result.get("nc"));
+        Assert.assertEquals("auth", result.get("qop"));
+    }
+
+    @Test
+    public void testEmptyListElements() throws Exception {
+        // RFC 9110, section 5.6.1.2: empty list elements must be ignored
+        String[] headers = new String[] { "Digest username=\"a\",, 
realm=\"r\"",
+                "Digest username=\"a\", , realm=\"r\"", "Digest 
,username=\"a\", realm=\"r\"",
+                "Digest username=\"a\", realm=\"r\",,", "Digest , 
username=\"a\" ,, , realm=\"r\" , " };
+        for (String header : headers) {
+            Map<String,String> result = 
Authorization.parseAuthorizationDigest(new StringReader(header));
+            Assert.assertNotNull(header, result);
+            Assert.assertEquals(header, 2, result.size());
+            Assert.assertEquals(header, "a", result.get("username"));
+            Assert.assertEquals(header, "r", result.get("realm"));
+        }
+    }
 }


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

Reply via email to