Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[util] Percent-encode all non-ASCII characters in URL fragment #4917

Merged
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
16 changes: 9 additions & 7 deletions vividus-util/src/main/java/org/vividus/util/UriUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 the original author or authors.
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -311,23 +311,25 @@
: relativeUrl;
try
{
URI parsedRelativeUrl = URI.create(normalizedRelativeUrl);
String decodedFragment = extractDecodedFragment(normalizedRelativeUrl);
if (url.isOpaque())
{
return new URI(url.getScheme(), url.getSchemeSpecificPart(), parsedRelativeUrl.getFragment());
return new URI(url.getScheme(), url.getSchemeSpecificPart(), decodedFragment);
}

URI parsedRelativeUrl = new URI(removeFragment(decodeUrl(normalizedRelativeUrl), decodedFragment));
String path = StringUtils.repeat(SLASH, indexOfFirstNonSlashChar - 1) + parsedRelativeUrl.getRawPath();
if (!path.isEmpty() && path.charAt(0) != '/')
{
throw new IllegalArgumentException(String
.format("Relative path '%s' for '%s' should start with forward slash ('/')", path, url));
}
String uriAsString = createUriAsString(url.getScheme(), url.getRawAuthority(), encodeNonAsciiChars(path),
parsedRelativeUrl.getQuery(), parsedRelativeUrl.getFragment());
return new URI(uriAsString);
String uriAsString = createUriAsString(url.getScheme(), url.getRawAuthority(), path,
parsedRelativeUrl.getQuery(), decodedFragment);

return buildUrl(uriAsString, decodedFragment);
}
catch (URISyntaxException e)
catch (URISyntaxException | MalformedURLException e)

Check warning on line 332 in vividus-util/src/main/java/org/vividus/util/UriUtils.java

View check run for this annotation

Codecov / codecov/patch

vividus-util/src/main/java/org/vividus/util/UriUtils.java#L332

Added line #L332 was not covered by tests
{
throw new IllegalArgumentException(e.getMessage(), e);
}
Expand Down
20 changes: 11 additions & 9 deletions vividus-util/src/test/java/org/vividus/util/UriUtilsTests.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 the original author or authors.
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -197,14 +197,16 @@ void testCreateUriWithException()
@ParameterizedTest
@CsvSource({
// CHECKSTYLE:OFF
"http://somehost:8080/path, /newPath, http://somehost:8080/newPath",
"http://somehost:8080/path, /newPath?name1=value1&name2=value2#fragement, http://somehost:8080/newPath?name1=value1&name2=value2#fragement",
"https://www.somehost.by//cookies, //path/extra-path/extra-extra-path, https://www.somehost.by//path/extra-path/extra-extra-path",
"https://www.somehost.by/, /////crazy-url-path, https://www.somehost.by/////crazy-url-path",
"https://www.somehost.by/, //путь-джедая, https://www.somehost.by//%D0%BF%D1%83%D1%82%D1%8C-%D0%B4%D0%B6%D0%B5%D0%B4%D0%B0%D1%8F",
"https://www.somehost.by, '', https://www.somehost.by",
"tel:1234567, '', tel:1234567",
"https://test:pas%40dsad@host.com, '', https://test:pas%40dsad@host.com"
"http://somehost:8080/path, /newPath, http://somehost:8080/newPath",
"http://somehost:8080/path, /newPath?name1=value1&name2=value2#fragement, http://somehost:8080/newPath?name1=value1&name2=value2#fragement",
"https://www.somehost.by//cookies, //path/extra-path/extra-extra-path, https://www.somehost.by//path/extra-path/extra-extra-path",
"https://www.somehost.by/, /////crazy-url-path, https://www.somehost.by/////crazy-url-path",
"https://www.somehost.by/, //путь-джедая, https://www.somehost.by//%D0%BF%D1%83%D1%82%D1%8C-%D0%B4%D0%B6%D0%B5%D0%B4%D0%B0%D1%8F",
"https://www.somehost.by, '', https://www.somehost.by",
"tel:1234567, '', tel:1234567",
"https://test:pas%40dsad@host.com, '', https://test:pas%40dsad@host.com",
"http://localhost:4200, /m/cool#%E7%94%A2%E5%93%81%E6%A6%82%E8%A6%BD%20overview, http://localhost:4200/m/cool#%E7%94%A2%E5%93%81%E6%A6%82%E8%A6%BD%20overview",
"http://localhost:4200, /m/cool#產品概覽 overview, http://localhost:4200/m/cool#%E7%94%A2%E5%93%81%E6%A6%82%E8%A6%BD%20overview",
// CHECKSTYLE:ON
})
void testBuildNewUri(String baseUrl, String relativeUrl, String expectedUrl)
Expand Down
Loading