Skip to content

Commit b59455b

Browse files
committed
Do not include URL hash in resource paths
When getting the lookup path of a resource, both query params and hashes should be removed from the request path. This commit fixes the public path resolution for paths like `/resources/main.svg#icon-hamburgermenu`. Issue: SPR-14928
1 parent 1a6dc86 commit b59455b

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceUrlProvider.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public final String getForRequestUrl(HttpServletRequest request, String requestU
181181
logger.trace("Getting resource URL for request URL \"" + requestUrl + "\"");
182182
}
183183
int prefixIndex = getLookupPathIndex(request);
184-
int suffixIndex = getQueryParamsIndex(requestUrl);
184+
int suffixIndex = getEndPathIndex(requestUrl);
185185
String prefix = requestUrl.substring(0, prefixIndex);
186186
String suffix = requestUrl.substring(suffixIndex);
187187
String lookupPath = requestUrl.substring(prefixIndex, suffixIndex);
@@ -196,9 +196,17 @@ private int getLookupPathIndex(HttpServletRequest request) {
196196
return requestUri.indexOf(lookupPath);
197197
}
198198

199-
private int getQueryParamsIndex(String lookupPath) {
200-
int index = lookupPath.indexOf("?");
201-
return index > 0 ? index : lookupPath.length();
199+
private int getEndPathIndex(String lookupPath) {
200+
int suffixIndex = lookupPath.length();
201+
int queryIndex = lookupPath.indexOf("?");
202+
if(queryIndex > 0) {
203+
suffixIndex = queryIndex;
204+
}
205+
int hashIndex = lookupPath.indexOf("#");
206+
if(hashIndex > 0) {
207+
suffixIndex = Math.min(suffixIndex, hashIndex);
208+
}
209+
return suffixIndex;
202210
}
203211

204212
/**

spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceUrlProviderTests.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,18 @@ public void getStaticResourceUrl() {
7373
}
7474

7575
@Test // SPR-13374
76-
public void getStaticResourceUrlRequestWithRequestParams() {
76+
public void getStaticResourceUrlRequestWithQueryOrHash() {
7777
MockHttpServletRequest request = new MockHttpServletRequest();
7878
request.setContextPath("/");
7979
request.setRequestURI("/");
8080

81-
String url = this.urlProvider.getForRequestUrl(request, "/resources/foo.css?foo=bar&url=http://example.org");
82-
assertEquals("/resources/foo.css?foo=bar&url=http://example.org", url);
81+
String url = "/resources/foo.css?foo=bar&url=http://example.org";
82+
String resolvedUrl = this.urlProvider.getForRequestUrl(request, url);
83+
assertEquals("/resources/foo.css?foo=bar&url=http://example.org", resolvedUrl);
84+
85+
url = "/resources/foo.css#hash";
86+
resolvedUrl = this.urlProvider.getForRequestUrl(request, url);
87+
assertEquals("/resources/foo.css#hash", resolvedUrl);
8388
}
8489

8590
@Test

0 commit comments

Comments
 (0)