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
27 changes: 24 additions & 3 deletions java/org/apache/tomcat/util/http/parser/Authorization.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,21 @@ public static Map<String,String> parseAuthorizationDigest(StringReader input)

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)
Comment thread
arturobernalg marked this conversation as resolved.
Resolved
while (HttpParser.skipConstant(input, ",") == SkipResult.FOUND) {
// Skip
}
// All field names are valid tokens
String field = HttpParser.readToken(input);
if (field == null) {
Expand All @@ -101,11 +113,20 @@ public static Map<String,String> parseAuthorizationDigest(StringReader input)
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,4 +512,68 @@ public void testNotDigest() throws Exception {
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"));
}
}
}
Loading