Skip to content

Commit

Permalink
Fix absolute paths when transforming resources
Browse files Browse the repository at this point in the history
Prior to this commit, `ResourceTransformerSupport.toAbsolutePath`
would call `StringUtils.applyRelativePath` in all cases. But this
implementation is prepending the given path even if the relative path
starts with `"/"`.

This commit skips the entire operation if the given path is absolute,
i.e. it starts with `"/"`.

Issue: SPR-17432
  • Loading branch information
bclozel committed Oct 25, 2018
1 parent a61d107 commit 2146e13
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
Expand Up @@ -96,7 +96,7 @@ protected Mono<String> resolveUrlPath(String resourcePath, ServerWebExchange exc
*/
protected String toAbsolutePath(String path, ServerWebExchange exchange) {
String requestPath = exchange.getRequest().getURI().getPath();
String absolutePath = StringUtils.applyRelativePath(requestPath, path);
String absolutePath = path.startsWith("/") ? path : StringUtils.applyRelativePath(requestPath, path);
return StringUtils.cleanPath(absolutePath);
}

Expand Down
Expand Up @@ -106,6 +106,16 @@ public void resolveUrlPathWithRelativePathInParentDirectory() {
assertEquals("../bar-11e16cf79faee7ac698c805cf28248d2.css", actual);
}

@Test
public void toAbsolutePath() {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/resources/main.css"));
String absolute = this.transformer.toAbsolutePath("img/image.png", exchange);
assertEquals("/resources/img/image.png", absolute);

absolute = this.transformer.toAbsolutePath("/img/image.png", exchange);
assertEquals("/img/image.png", absolute);
}

private Resource getResource(String filePath) {
return new ClassPathResource("test/" + filePath, getClass());
}
Expand Down
Expand Up @@ -94,10 +94,13 @@ protected String resolveUrlPath(String resourcePath, HttpServletRequest request,
* @return the absolute request path for the given resource path
*/
protected String toAbsolutePath(String path, HttpServletRequest request) {
ResourceUrlProvider urlProvider = findResourceUrlProvider(request);
Assert.state(urlProvider != null, "No ResourceUrlProvider");
String requestPath = urlProvider.getUrlPathHelper().getRequestUri(request);
String absolutePath = StringUtils.applyRelativePath(requestPath, path);
String absolutePath = path;
if(!path.startsWith("/")) {
ResourceUrlProvider urlProvider = findResourceUrlProvider(request);
Assert.state(urlProvider != null, "No ResourceUrlProvider");
String requestPath = urlProvider.getUrlPathHelper().getRequestUri(request);
absolutePath = StringUtils.applyRelativePath(requestPath, path);
}
return StringUtils.cleanPath(absolutePath);
}

Expand Down
Expand Up @@ -41,7 +41,7 @@ public class ResourceTransformerSupportTests {

private TestResourceTransformerSupport transformer;

private final MockHttpServletRequest request = new MockHttpServletRequest("GET", "");
private final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");


@Before
Expand Down Expand Up @@ -98,6 +98,17 @@ public void resolveUrlPathWithRelativePathInParentDirectory() {
assertEquals("../bar-11e16cf79faee7ac698c805cf28248d2.css", actual);
}

@Test
public void toAbsolutePath() {
String absolute = this.transformer.toAbsolutePath("img/image.png",
new MockHttpServletRequest("GET", "/resources/style.css"));
assertEquals("/resources/img/image.png", absolute);

absolute = this.transformer.toAbsolutePath("/img/image.png",
new MockHttpServletRequest("GET", "/resources/style.css"));
assertEquals("/img/image.png", absolute);
}

private Resource getResource(String filePath) {
return new ClassPathResource("test/" + filePath, getClass());
}
Expand Down

0 comments on commit 2146e13

Please sign in to comment.