Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -2241,8 +2241,8 @@ public static OpaqueHost parse(String input, WhatWgUrlParser p) {
// If input contains a U+0025 (%) and the two code points following it
// are not ASCII hex digits, invalid-URL-unit validation error.
if (p.validate() && ch == '%' &&
(input.length() - i < 2 || !isAsciiDigit(input.codePointAt(i + 1)) ||
!isAsciiDigit(input.codePointAt(i + 2)))) {
(input.length() - i < 3 || !isAsciiHexDigit(input.codePointAt(i + 1)) ||
!isAsciiHexDigit(input.codePointAt(i + 2)))) {
p.validationError("Code point \"" + ch + "\" is not a URL unit.");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package org.springframework.web.util;

import java.util.ArrayList;
import java.util.List;

import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -63,6 +66,21 @@ void parseAsciiHost() {
testParse("file://xn--/p", "file", "xn--", null, "/p", null, null);
}

@Test
void parseOpaqueHostTruncatedPercentEscape() {
// A truncated or non-hex percent-escape in an opaque host is a validation error,
// not a failure, and must not read past the end of the input
// (see https://url.spec.whatwg.org/#concept-opaque-host-parser).
List<String> errors = new ArrayList<>();
WhatWgUrlParser.UrlRecord record = WhatWgUrlParser.parse("foo://%4", EMPTY_URL_RECORD, null, errors::add);
assertThat(record.host().toString()).isEqualTo("%4");

record = WhatWgUrlParser.parse("foo://%4x", EMPTY_URL_RECORD, null, errors::add);
assertThat(record.host().toString()).isEqualTo("%4x");

assertThat(errors).isNotEmpty();
}

private void testParse(String input, String scheme, @Nullable String host, @Nullable String port, String path, @Nullable String query, @Nullable String fragment) {
WhatWgUrlParser.UrlRecord result = WhatWgUrlParser.parse(input, EMPTY_URL_RECORD, null, null);
assertThat(result.scheme()).as("Invalid scheme").isEqualTo(scheme);
Expand Down