From 7a5844f1ce3ca53a598ea574d0abc8c05f38ea3d Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Thu, 23 Apr 2026 11:29:47 +0200 Subject: [PATCH] Reject unsafe resource handling locations As of gh-36692, Spring logs a WARN message when an unsafe resource handling location is configured. This change now rejects entirely such setups by failing before the application starts up. Closes gh-36695 --- .../resource/ResourceHandlerUtils.java | 24 +-- .../resource/ResourceHandlerUtilsTests.java | 156 ++++++++++++++++++ .../resource/ResourceHandlerUtils.java | 24 +-- .../DelegatingWebMvcConfigurationTests.java | 4 +- .../resource/ResourceHandlerUtilsTests.java | 87 ++++++++++ .../mvc-config-custom-pattern-parser.xml | 2 +- .../mvc-config-deprecated-path-matcher.xml | 2 +- .../mvc-config-path-matching-mappings.xml | 2 +- .../mvc-config-resources-chain-no-auto.xml | 2 +- .../config/mvc-config-resources-chain.xml | 2 +- .../mvc-config-resources-optional-attrs.xml | 2 +- .../servlet/config/mvc-config-resources.xml | 2 +- 12 files changed, 264 insertions(+), 45 deletions(-) create mode 100644 spring-webflux/src/test/java/org/springframework/web/reactive/resource/ResourceHandlerUtilsTests.java create mode 100644 spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHandlerUtilsTests.java diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceHandlerUtils.java b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceHandlerUtils.java index 2e95c58fd5b..e2687b2909f 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceHandlerUtils.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceHandlerUtils.java @@ -19,7 +19,6 @@ package org.springframework.web.reactive.resource; import java.io.IOException; import java.net.URLDecoder; import java.nio.charset.StandardCharsets; -import java.util.Locale; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -69,17 +68,15 @@ public abstract class ResourceHandlerUtils { } else if (location instanceof ClassPathResource classPathResource) { path = classPathResource.getPath(); - if (path.isEmpty() || "/".equals(path)) { - logger.warn("Resource location '" + location + "' is considered unsafe " + - "and should not be used as it provides access to the entire classpath."); - } + Assert.isTrue(!path.isEmpty() && !"/".equals(path), + () -> "Resource location '" + location + "' is considered unsafe " + + "and cannot be used as it provides access to the entire classpath."); } else if (location instanceof ContextResource contextResource) { path = contextResource.getPathWithinContext(); - if ("/".equals(path)) { - logger.warn("Resource location '" + location + "' is considered unsafe " + - "and should not be used as it provides access to the root servlet context."); - } + Assert.isTrue(!"/".equals(path), + () -> "Resource location '" + location + "' is considered unsafe " + + "and cannot be used as it provides access to the root servlet context."); } else if (location instanceof UrlResource) { path = location.getURL().toExternalForm(); @@ -176,7 +173,6 @@ public abstract class ResourceHandlerUtils { /** * Checks for invalid resource input paths rejecting the following: *