Skip to content

Commit

Permalink
fix(webmvc): fixes the trailing slash handling during resource locati…
Browse files Browse the repository at this point in the history
…on resolution.

closes gh-33815
  • Loading branch information
cagliostro92 committed Oct 30, 2024
1 parent 1255bd1 commit dc3ea16
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
* {@link ResourceHttpRequestHandler} and {@link org.springframework.web.servlet.function}.
*
* @author Rossen Stoyanchev
* @author Edoardo Patti
* @since 6.2
*/
public abstract class ResourceHandlerUtils {
Expand Down Expand Up @@ -65,20 +66,27 @@ else if (location instanceof ClassPathResource classPathResource) {
else {
path = location.getURL().getPath();
}
assertLocationPath(path);
addTrailingSlashIfAbsent(path);
}
catch (IOException ex) {
// ignore
}
}

/**
* Assert the given location path is a directory and ends on slash.
* Assert the given location path is not null and look for the trailing slash adding it when absent.
* @return the input string if the trailing slash was already present
* the input string plus the trailing slash otherwise.
*/
public static void assertLocationPath(@Nullable String path) {
public static String addTrailingSlashIfAbsent(@Nullable String path) {
Assert.notNull(path, "Resource location path must not be null");
Assert.isTrue(path.endsWith(FOLDER_SEPARATOR) || path.endsWith(WINDOWS_FOLDER_SEPARATOR),
"Resource location does not end with slash: " + path);
if(!path.endsWith(FOLDER_SEPARATOR) && !path.endsWith(WINDOWS_FOLDER_SEPARATOR)) {
logger.warn(("Observed an attempting to register a resource with a path without trailing slash: '%s' " +
"the framework will add it automatically. Consider to add it on your own.").formatted(path));
var trailingSlash = path.contains(WINDOWS_FOLDER_SEPARATOR) ? WINDOWS_FOLDER_SEPARATOR : FOLDER_SEPARATOR;
return path += trailingSlash;
}
return path;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ private void resolveResourceLocations() {
charset = Charset.forName(value);
location = location.substring(endIndex + 1);
}
ResourceHandlerUtils.assertLocationPath(location);
location = ResourceHandlerUtils.addTrailingSlashIfAbsent(location);
Resource resource = applicationContext.getResource(location);
if (location.equals("/") && !(resource instanceof ServletContextResource)) {
throw new IllegalStateException(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.springframework.web.servlet.resource;

import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;

/**
* Tests for {@link ResourceHandlerUtils}.
*
* @author Edoardo Patti
*/
public class ResourceHandlerUtilsTests {

@Test
void testTrailingSlash() {
var windowsDirectory = "META-INF\\resources\\webjars";
var directory = "META-INF/resources/webjars";
var directoryWithoutSlash = "META-INF";
var correctWindowsDirectory = "META-INF\\resources\\webjars\\";
var correctDirectory = "META-INF/resources/webjars/";
assertThat(ResourceHandlerUtils.addTrailingSlashIfAbsent(windowsDirectory).endsWith("\\")).isTrue();
assertThat(ResourceHandlerUtils.addTrailingSlashIfAbsent(directory).endsWith("/")).isTrue();
assertThat(ResourceHandlerUtils.addTrailingSlashIfAbsent(directoryWithoutSlash).endsWith("/")).isTrue();
assertThat(ResourceHandlerUtils.addTrailingSlashIfAbsent(correctWindowsDirectory)).isEqualTo(correctWindowsDirectory);
assertThat(ResourceHandlerUtils.addTrailingSlashIfAbsent(correctDirectory)).isEqualTo(correctDirectory);
}
}

0 comments on commit dc3ea16

Please sign in to comment.