Skip to content

Commit

Permalink
Merge pull request #12591 from jetty/fix/12.0.x/npe-httpuri-decodedpath
Browse files Browse the repository at this point in the history
Issue #12577 - Fixing NPE from HttpURI.getDecodedPath() if path doesn't exist
  • Loading branch information
joakime authored Dec 2, 2024
2 parents a8aec30 + 89adf5b commit 901e86d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -497,18 +497,21 @@ public static Stream<Arguments> decodePathTests()
{"http://localhost:9000/x\uD83C\uDF32\uD83C\uDF32\uD83C\uDF32\uD83C\uDF32\uD83C\uDF32", "/x\uD83C\uDF32\uD83C\uDF32\uD83C\uDF32\uD83C\uDF32\uD83C\uDF32", "/x\uD83C\uDF32\uD83C\uDF32\uD83C\uDF32\uD83C\uDF32\uD83C\uDF32", EnumSet.noneOf(Violation.class)},
{"http://localhost:9000/\uD83C\uDF32\uD83C\uDF32\uD83C\uDF32\uD83C\uDF32\uD83C\uDF32", "/\uD83C\uDF32\uD83C\uDF32\uD83C\uDF32\uD83C\uDF32\uD83C\uDF32", "/\uD83C\uDF32\uD83C\uDF32\uD83C\uDF32\uD83C\uDF32\uD83C\uDF32", EnumSet.noneOf(Violation.class)},
// @checkstyle-enable-check : AvoidEscapedUnicodeCharactersCheck

// An empty (null) authority
{"http://", null, null, null}
}).map(Arguments::of);
}

@ParameterizedTest
@MethodSource("decodePathTests")
public void testDecodedPath(String input, String canonicalPath, String decodedPath, EnumSet<Violation> expected)
public void testDecodedPath(String input, String expectedCanonicalPath, String expectedDecodedPath, EnumSet<Violation> expected)
{
try
{
HttpURI uri = HttpURI.from(input);
assertThat("Canonical Path", uri.getCanonicalPath(), is(canonicalPath));
assertThat("Decoded Path", uri.getDecodedPath(), is(decodedPath));
assertThat("Canonical Path", uri.getCanonicalPath(), is(expectedCanonicalPath));
assertThat("Decoded Path", uri.getDecodedPath(), is(expectedDecodedPath));

EnumSet<Violation> ambiguous = EnumSet.copyOf(expected);
ambiguous.retainAll(EnumSet.complementOf(EnumSet.of(Violation.UTF16_ENCODINGS, Violation.BAD_UTF8_ENCODING)));
Expand All @@ -523,9 +526,9 @@ public void testDecodedPath(String input, String canonicalPath, String decodedPa
}
catch (Exception e)
{
if (decodedPath != null)
if (expectedDecodedPath != null)
e.printStackTrace();
assertThat(decodedPath, nullValue());
assertThat(expectedDecodedPath, nullValue());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,11 +471,14 @@ public static StringBuilder encodeString(StringBuilder buf,
/**
* Decodes a percent-encoded URI path (assuming UTF-8 characters) and strips path parameters.
* @param path The URI path to decode
* @return the decoded path (or null if input path is null)
* @see #canonicalPath(String)
* @see #normalizePath(String)
*/
public static String decodePath(String path)
{
if (path == null)
return null;
return decodePath(path, 0, path.length());
}

Expand All @@ -484,11 +487,14 @@ public static String decodePath(String path)
* @param path A String holding the URI path to decode
* @param offset The start of the URI within the path string
* @param length The length of the URI within the path string
* @return the decoded path (or null if input path is null)
* @see #canonicalPath(String)
* @see #normalizePath(String)
*/
public static String decodePath(String path, int offset, int length)
{
if (path == null)
return null;
try
{
Utf8StringBuilder builder = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public static Stream<Arguments> decodePathSource()

// Test for null character (real world ugly test case)
Arguments.of("/%00/", "/%00/", "/\u0000/"),
Arguments.of(null, null, null),

// Deprecated Microsoft Percent-U encoding
Arguments.of("abc%u3040", "abc\u3040", "abc\u3040"),
Expand Down Expand Up @@ -179,10 +180,10 @@ public void testCanonicalEncodedPath(String encodedPath, String canonicalPath, S

@ParameterizedTest(name = "[{index}] {0}")
@MethodSource("decodePathSource")
public void testDecodePath(String encodedPath, String canonicalPath, String decodedPath)
public void testDecodePath(String encodedPath, String expectedCanonicalPath, String expectedDecodedPath)
{
String path = URIUtil.decodePath(encodedPath);
assertEquals(decodedPath, path);
assertEquals(expectedDecodedPath, path);
}

public static Stream<Arguments> decodeBadPathSource()
Expand Down

0 comments on commit 901e86d

Please sign in to comment.