From 0e33db68c53154780545d53fe4880f4e673ca340 Mon Sep 17 00:00:00 2001 From: Tigran Mkrtchyan Date: Thu, 2 Nov 2023 14:42:42 +0100 Subject: [PATCH] webdav: fix parsing of urls with two slashes in the path Motivation: The surrent implementations will strip the urls like: https://door.domain.foo:1234//pnfs/domain.foo/path/to/file into /domain.foo/path/to/file Modification: Stop using doggy ServletRequest.stripToPath in a favor of own implementation. Result: Correct behavior parsing of files. Acked-by: Paul Millar Target: master, 9.2, 9.1, 9.0, 8.2 Require-book: no Require-notes: yes (cherry picked from commit 830a8a1469c875ee199d423f38f6775cec6cdcf0) Signed-off-by: Tigran Mkrtchyan --- .../dcache/webdav/DcacheResourceFactory.java | 2 +- .../main/java/org/dcache/webdav/Requests.java | 23 +++++++++++++++++ .../java/org/dcache/webdav/RequestsTest.java | 25 +++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/modules/dcache-webdav/src/main/java/org/dcache/webdav/DcacheResourceFactory.java b/modules/dcache-webdav/src/main/java/org/dcache/webdav/DcacheResourceFactory.java index 379b104935f..c9f966f38ad 100644 --- a/modules/dcache-webdav/src/main/java/org/dcache/webdav/DcacheResourceFactory.java +++ b/modules/dcache-webdav/src/main/java/org/dcache/webdav/DcacheResourceFactory.java @@ -1692,7 +1692,7 @@ public HttpTransfer(PnfsHandler pnfs, Subject subject, var request = ServletRequest.getRequest(); request.setAttribute(TRANSACTION_ATTRIBUTE, getTransaction()); - _requestPath = ServletRequest.stripToPath(request.getRequestURL().toString()); + _requestPath = Requests.stripToPath(request.getRequestURL().toString()); } protected ProtocolInfo createProtocolInfo(InetSocketAddress address) { diff --git a/modules/dcache-webdav/src/main/java/org/dcache/webdav/Requests.java b/modules/dcache-webdav/src/main/java/org/dcache/webdav/Requests.java index c6ff1300e02..b78ad9da39a 100644 --- a/modules/dcache-webdav/src/main/java/org/dcache/webdav/Requests.java +++ b/modules/dcache-webdav/src/main/java/org/dcache/webdav/Requests.java @@ -28,6 +28,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.annotation.Nullable; +import java.net.URI; +import java.net.URL; +import java.nio.file.Path; import java.util.Collection; /** @@ -158,4 +161,24 @@ private static float qValueOf(MediaType m) { return 1.0f; } } + + /** + * Extract the normalized path element of the given URL String excluding query information. + * + * @param url The string representation of the URL. + * @return The path component of the URL. + */ + public static String stripToPath(String uri) { + return stripToPath(URI.create(uri).getPath()); + } + + /** + * Extract the normalized path element of the given URL excluding query information. + * + * @param url The URL to extract path from. + * @return The path component of the URL. + */ + public static String stripToPath(URL url) { + return Path.of(url.getPath()).normalize().toString(); + } } diff --git a/modules/dcache-webdav/src/test/java/org/dcache/webdav/RequestsTest.java b/modules/dcache-webdav/src/test/java/org/dcache/webdav/RequestsTest.java index eec8528af42..2d2ccdad41b 100644 --- a/modules/dcache-webdav/src/test/java/org/dcache/webdav/RequestsTest.java +++ b/modules/dcache-webdav/src/test/java/org/dcache/webdav/RequestsTest.java @@ -20,6 +20,8 @@ import com.google.common.net.MediaType; +import java.net.MalformedURLException; +import java.net.URL; import java.util.Set; import org.junit.Test; @@ -107,4 +109,27 @@ public void shouldAcceptWithMatchingParameter() { assertThat(responseType, equalTo(PLAIN_TEXT_UTF_8)); } + + @Test + public void shouldReturnFilePathOfUrl() throws MalformedURLException { + var u = new URL("https://door.domain.foo/pnfs/domain.foo/path/to/file"); + assertThat(Requests.stripToPath(u), equalTo("/pnfs/domain.foo/path/to/file")); + } + + @Test + public void shouldReturnFilePathOfUrlWithPort() throws MalformedURLException { + var u = new URL("https://door.domain.foo:1234/pnfs/domain.foo/path/to/file?foo=bar"); + assertThat(Requests.stripToPath(u), equalTo("/pnfs/domain.foo/path/to/file")); + } + @Test + public void shouldReturnFilePathOfUrlWithPortAndExtraSlash() throws MalformedURLException { + var u = new URL("https://door.domain.foo:1234//pnfs/domain.foo//path/to/file"); + assertThat(Requests.stripToPath(u), equalTo("/pnfs/domain.foo/path/to/file")); + } + + @Test + public void shouldReturnFilePathOfUrlWithQuery() throws MalformedURLException { + var u = new URL("https://door.domain.foo:1234/pnfs/domain.foo/path/to/file?foo=bar"); + assertThat(Requests.stripToPath(u), equalTo("/pnfs/domain.foo/path/to/file")); + } } \ No newline at end of file