This is an automated email from the ASF dual-hosted git repository. coheigea pushed a commit to branch coheigea/swagger in repository https://gitbox.apache.org/repos/asf/cxf.git
commit 3187fe3125ad6a34e0ff17114adc4d8e25aacae7 Author: Colm O hEigeartaigh <[email protected]> AuthorDate: Fri Nov 20 06:52:59 2026 +0000 Reject path traversal in SwaggerUiResourceLocator --- .../jaxrs/swagger/ui/SwaggerUiResourceLocator.java | 35 +++++++++++++ .../swagger/ui/SwaggerUIResourceLocatorTest.java | 59 +++++++++++++++++++++- 2 files changed, 93 insertions(+), 1 deletion(-) diff --git a/rt/rs/description-swagger-ui/src/main/java/org/apache/cxf/jaxrs/swagger/ui/SwaggerUiResourceLocator.java b/rt/rs/description-swagger-ui/src/main/java/org/apache/cxf/jaxrs/swagger/ui/SwaggerUiResourceLocator.java index 6c396ddf6a5..e2b298acb62 100644 --- a/rt/rs/description-swagger-ui/src/main/java/org/apache/cxf/jaxrs/swagger/ui/SwaggerUiResourceLocator.java +++ b/rt/rs/description-swagger-ui/src/main/java/org/apache/cxf/jaxrs/swagger/ui/SwaggerUiResourceLocator.java @@ -25,11 +25,14 @@ import java.net.URI; import java.net.URL; import org.apache.cxf.common.util.StringUtils; +import org.apache.cxf.common.util.UrlUtils; /** * Swagger UI resource locator */ public class SwaggerUiResourceLocator { + private static final int MAX_DECODE_PASSES = 3; + private final String swaggerUiRoot; public SwaggerUiResourceLocator(String swaggerUiRoot) { @@ -50,6 +53,9 @@ public class SwaggerUiResourceLocator { if (resourcePath.startsWith("/")) { resourcePath = resourcePath.substring(1); } + if (isTraversal(resourcePath)) { + throw new MalformedURLException("Invalid Swagger UI resource path"); + } URL ret; try { @@ -60,6 +66,35 @@ public class SwaggerUiResourceLocator { return ret; } + /** + * Checks whether the resource path could escape the Swagger UI root: whether it contains a ".." + * segment or a backslash once percent-decoded. The path is decoded until it no longer changes, + * as the URL handler (e.g. for file: URLs) may decode it again. + */ + private static boolean isTraversal(String resourcePath) { + String decoded = resourcePath; + boolean stable = false; + try { + for (int i = 0; i < MAX_DECODE_PASSES && !stable; i++) { + String next = UrlUtils.pathDecode(decoded); + stable = next.equals(decoded); + decoded = next; + } + } catch (IllegalArgumentException ex) { + // invalid percent-encoding + return true; + } + if (!stable || decoded.indexOf('\\') != -1) { + return true; + } + for (String segment : decoded.split("/")) { + if ("..".equals(segment)) { + return true; + } + } + return false; + } + /** * Checks the existence of the Swagger UI resource corresponding to resource path * @param resourcePath resource path diff --git a/rt/rs/description-swagger-ui/src/test/java/org/apache/cxf/jaxrs/swagger/ui/SwaggerUIResourceLocatorTest.java b/rt/rs/description-swagger-ui/src/test/java/org/apache/cxf/jaxrs/swagger/ui/SwaggerUIResourceLocatorTest.java index 198127e81c6..14337cd7cb1 100644 --- a/rt/rs/description-swagger-ui/src/test/java/org/apache/cxf/jaxrs/swagger/ui/SwaggerUIResourceLocatorTest.java +++ b/rt/rs/description-swagger-ui/src/test/java/org/apache/cxf/jaxrs/swagger/ui/SwaggerUIResourceLocatorTest.java @@ -19,14 +19,71 @@ package org.apache.cxf.jaxrs.swagger.ui; +import java.io.File; import java.net.MalformedURLException; +import java.nio.file.Files; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; - +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; public class SwaggerUIResourceLocatorTest { + @Rule + public TemporaryFolder tempFolder = new TemporaryFolder(); + + @Test + public void testPathTraversalIsRejected() throws Exception { + SwaggerUiResourceLocator locator = createFileLocator(); + String[] paths = { + "../secret.txt", + "/../secret.txt", + "css/../../secret.txt", + "..", + "%2e%2e/secret.txt", + "%2E%2E%2Fsecret.txt", + "%252e%252e/secret.txt", + "..%5csecret.txt", + "..\\secret.txt", + "%zz/secret.txt" + }; + for (String path : paths) { + assertFalse("Must not resolve: " + path, locator.exists(path)); + try { + locator.locate(path); + fail("Expected MalformedURLException for: " + path); + } catch (MalformedURLException ex) { + // expected + } + } + } + + @Test + public void testResourcesInsideRootAreLocated() throws Exception { + SwaggerUiResourceLocator locator = createFileLocator(); + assertTrue(locator.exists("index.html")); + assertTrue(locator.exists("/index.html")); + assertTrue(locator.exists("")); + assertTrue(locator.exists("swagger-ui..min.js")); + assertTrue(locator.exists("a+b.js")); + assertEquals("index.html", new File(locator.locate("index.html").toURI()).getName()); + } + + private SwaggerUiResourceLocator createFileLocator() throws Exception { + File base = tempFolder.getRoot(); + Files.write(new File(base, "secret.txt").toPath(), "secret".getBytes()); + File root = tempFolder.newFolder("swagger-ui"); + for (String name : new String[] {"index.html", "swagger-ui..min.js", "a+b.js"}) { + Files.write(new File(root, name).toPath(), "ui".getBytes()); + } + return new SwaggerUiResourceLocator(root.toURI().toString()); + } + @Test(expected = MalformedURLException.class) public void testLocateWithBadCharactersInUrl() throws MalformedURLException { String url =
