Skip to content

Commit 76b6fb9

Browse files
committed
Parse the Digest Authorization header as specified by RFC 9110
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.
1 parent 17aa176 commit 76b6fb9

2 files changed

Lines changed: 88 additions & 3 deletions

File tree

‎java/org/apache/tomcat/util/http/parser/Authorization.java‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,21 @@ public static Map<String,String> parseAuthorizationDigest(StringReader input)
7474

7575
Map<String,String> result = new HashMap<>();
7676

77-
if (HttpParser.skipConstant(input, "Digest") != SkipResult.FOUND) {
77+
// The authentication scheme is case-insensitive (RFC 9110, section 11.1)
78+
if (!"Digest".equalsIgnoreCase(HttpParser.readToken(input))) {
7879
return null;
7980
}
81+
// The scheme must be followed by at least one SP (RFC 9110, section 11.4)
82+
input.mark(1);
83+
int c = input.read();
84+
if (c != -1 && c != ' ') {
85+
return null;
86+
}
87+
input.reset();
88+
// Empty list elements must be ignored (RFC 9110, section 5.6.1.2)
89+
while (HttpParser.skipConstant(input, ",") == SkipResult.FOUND) {
90+
// Skip
91+
}
8092
// All field names are valid tokens
8193
String field = HttpParser.readToken(input);
8294
if (field == null) {
@@ -101,11 +113,20 @@ public static Map<String,String> parseAuthorizationDigest(StringReader input)
101113
if (value == null) {
102114
return null;
103115
}
104-
result.put(field, value);
116+
// The parameter names are case-insensitive (RFC 9110, section 11.2)
117+
// Duplicate parameters are not allowed
118+
if (result.put(field.toLowerCase(Locale.ENGLISH), value) != null) {
119+
return null;
120+
}
105121

106-
if (HttpParser.skipConstant(input, ",") == SkipResult.NOT_FOUND) {
122+
SkipResult skipResult = HttpParser.skipConstant(input, ",");
123+
if (skipResult == SkipResult.NOT_FOUND) {
107124
return null;
108125
}
126+
// Empty list elements must be ignored (RFC 9110, section 5.6.1.2)
127+
while (skipResult == SkipResult.FOUND) {
128+
skipResult = HttpParser.skipConstant(input, ",");
129+
}
109130
field = HttpParser.readToken(input);
110131
if (field == null) {
111132
return null;

‎test/org/apache/tomcat/util/http/parser/TestAuthorizationDigest.java‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,4 +512,68 @@ public void testNotDigest() throws Exception {
512512
Map<String,String> result = Authorization.parseAuthorizationDigest(input);
513513
Assert.assertNull(result);
514514
}
515+
516+
@Test
517+
public void testSchemeCaseInsensitive() throws Exception {
518+
// RFC 9110, section 11.1: the authentication scheme is case-insensitive
519+
for (String scheme : new String[] { "digest", "DIGEST", "dIgEsT" }) {
520+
Map<String,String> result =
521+
Authorization.parseAuthorizationDigest(new StringReader(scheme + " username=\"a\""));
522+
Assert.assertNotNull(scheme, result);
523+
Assert.assertEquals(scheme, "a", result.get("username"));
524+
}
525+
}
526+
527+
@Test
528+
public void testSchemeMustBeFollowedBySpace() throws Exception {
529+
// RFC 9110, section 11.4: the scheme is followed by 1*SP
530+
String[] headers = new String[] { "Digestusername=\"a\"", "Digest,username=\"a\"",
531+
"Digest\tusername=\"a\"" };
532+
for (String header : headers) {
533+
Assert.assertNull(header, Authorization.parseAuthorizationDigest(new StringReader(header)));
534+
}
535+
}
536+
537+
@Test
538+
public void testSchemeFollowedByMultipleSpaces() throws Exception {
539+
Map<String,String> result = Authorization.parseAuthorizationDigest(new StringReader("Digest username=\"a\""));
540+
Assert.assertEquals("a", result.get("username"));
541+
}
542+
543+
@Test
544+
public void testDuplicateParameter() throws Exception {
545+
String[] headers = new String[] { "Digest username=\"a\", username=\"b\"",
546+
"Digest username=\"a\", USERNAME=\"a\"" };
547+
for (String header : headers) {
548+
Assert.assertNull(header, Authorization.parseAuthorizationDigest(new StringReader(header)));
549+
}
550+
}
551+
552+
@Test
553+
public void testParameterNamesCaseInsensitive() throws Exception {
554+
// RFC 9110, section 11.2: parameter names are matched case-insensitively
555+
String header = "Digest Username=\"a\", REALM=\"r\", Nc=00000001, QoP=auth";
556+
557+
Map<String,String> result = Authorization.parseAuthorizationDigest(new StringReader(header));
558+
559+
Assert.assertEquals("a", result.get("username"));
560+
Assert.assertEquals("r", result.get("realm"));
561+
Assert.assertEquals("00000001", result.get("nc"));
562+
Assert.assertEquals("auth", result.get("qop"));
563+
}
564+
565+
@Test
566+
public void testEmptyListElements() throws Exception {
567+
// RFC 9110, section 5.6.1.2: empty list elements must be ignored
568+
String[] headers = new String[] { "Digest username=\"a\",, realm=\"r\"",
569+
"Digest username=\"a\", , realm=\"r\"", "Digest ,username=\"a\", realm=\"r\"",
570+
"Digest username=\"a\", realm=\"r\",,", "Digest , username=\"a\" ,, , realm=\"r\" , " };
571+
for (String header : headers) {
572+
Map<String,String> result = Authorization.parseAuthorizationDigest(new StringReader(header));
573+
Assert.assertNotNull(header, result);
574+
Assert.assertEquals(header, 2, result.size());
575+
Assert.assertEquals(header, "a", result.get("username"));
576+
Assert.assertEquals(header, "r", result.get("realm"));
577+
}
578+
}
515579
}

0 commit comments

Comments
 (0)