From 8b9e092770e8eb47a8db60f246821b9cd088c96e Mon Sep 17 00:00:00 2001 From: Daniel Kec Date: Sun, 24 Sep 2023 17:02:39 +0200 Subject: [PATCH] Double-slash URI issue #7474 --- .../java/io/helidon/common/uri/UriPathNoParam.java | 13 +++++++++---- .../java/io/helidon/common/uri/UriPathTest.java | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/common/uri/src/main/java/io/helidon/common/uri/UriPathNoParam.java b/common/uri/src/main/java/io/helidon/common/uri/UriPathNoParam.java index e284fc134b6..4aca2ddc44a 100644 --- a/common/uri/src/main/java/io/helidon/common/uri/UriPathNoParam.java +++ b/common/uri/src/main/java/io/helidon/common/uri/UriPathNoParam.java @@ -73,7 +73,7 @@ public String rawPathNoParams() { @Override public String path() { if (decodedPath == null) { - decodedPath = decode(rawPath); + decodedPath = decode(rawPath, false); } return decodedPath; } @@ -104,11 +104,11 @@ public String toString() { @Override public void validate() { if (decodedPath == null) { - this.decodedPath = URI.create(rawPath).normalize().getPath(); + this.decodedPath = decode(rawPath, true); } } - private static String decode(String rawPath) { + private static String decode(String rawPath, boolean validate) { /* Raw path may: - be encoded (%20) @@ -121,10 +121,15 @@ private static String decode(String rawPath) { int dot = rawPath.indexOf("."); int doubleSlash = rawPath.indexOf("//"); - if (percent == -1 && doubleSlash == -1 && dot == -1) { + if (!validate && percent == -1 && doubleSlash == -1 && dot == -1) { return rawPath; } + if (doubleSlash == 0) { + // JDK-8316799 URI.create("//foo/bar").normalize().getPath() --> "/bar" + rawPath = rawPath.substring(1); + } + return URI.create(rawPath).normalize().getPath(); } } diff --git a/common/uri/src/test/java/io/helidon/common/uri/UriPathTest.java b/common/uri/src/test/java/io/helidon/common/uri/UriPathTest.java index d230b964f1a..f94f70f1193 100644 --- a/common/uri/src/test/java/io/helidon/common/uri/UriPathTest.java +++ b/common/uri/src/test/java/io/helidon/common/uri/UriPathTest.java @@ -17,6 +17,8 @@ package io.helidon.common.uri; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -37,4 +39,16 @@ void testEncodeSpace() { assertThat(path.path(), is("/my path")); assertThat(path.rawPath(), is("/my%20path")); } + + @ParameterizedTest + @ValueSource(strings = { + "//foo/bar", + "//foo//bar", + "/foo//bar", + "/foo/bar", + }) + void testDoubleSlash(String rawPath) { + UriPath path = UriPath.create(rawPath); + assertThat(path.path(), is("/foo/bar")); + } }