Skip to content

Commit 51c941c

Browse files
committed
Fix empty URLs handling in ResourceUrlEncodingFilter
Prior to this commit, the ResourceUrlEncodingFilter would fail with a StringIndexOutOfBoundsException when: * the current request has a servlet context * the URL to encode is relative and is shorter than the context value This change defensively checks for those lengths and delegates to the parent implementation if necessary. Issue: SPR-13018
1 parent 9e925aa commit 51c941c

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,15 @@ public String encodeURL(String url) {
7373
return super.encodeURL(url);
7474
}
7575
initIndexLookupPath(resourceUrlProvider);
76-
String prefix = url.substring(0, this.indexLookupPath);
77-
String lookupPath = url.substring(this.indexLookupPath);
78-
lookupPath = resourceUrlProvider.getForLookupPath(lookupPath);
79-
return (lookupPath != null ? super.encodeURL(prefix + lookupPath) : super.encodeURL(url));
76+
if(url.length() >= this.indexLookupPath) {
77+
String prefix = url.substring(0, this.indexLookupPath);
78+
String lookupPath = url.substring(this.indexLookupPath);
79+
lookupPath = resourceUrlProvider.getForLookupPath(lookupPath);
80+
if (lookupPath != null) {
81+
return super.encodeURL(prefix + lookupPath);
82+
}
83+
}
84+
return super.encodeURL(url);
8085
}
8186

8287
private ResourceUrlProvider getResourceUrlProvider() {

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,23 @@ public void doFilter(ServletRequest request, ServletResponse response) throws IO
8989
});
9090
}
9191

92+
// SPR-13018
93+
@Test
94+
public void encodeEmptyURLWithContext() throws Exception {
95+
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/context/foo");
96+
request.setContextPath("/context");
97+
request.setAttribute(ResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR, this.resourceUrlProvider);
98+
MockHttpServletResponse response = new MockHttpServletResponse();
99+
100+
this.filter.doFilterInternal(request, response, new FilterChain() {
101+
@Override
102+
public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
103+
String result = ((HttpServletResponse)response).encodeURL("?foo=1");
104+
assertEquals("?foo=1", result);
105+
}
106+
});
107+
}
108+
92109
protected ResourceUrlProvider createResourceUrlProvider(List<ResourceResolver> resolvers) {
93110
ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
94111
handler.setLocations(Arrays.asList(new ClassPathResource("test/", getClass())));

0 commit comments

Comments
 (0)