Fix OutOfBoundsException in ResourceUrlEncodingFilter
Prior to this change, the `ResourceUrlEncodingFilter` would try to
lookup resources URLs as soon as the given URL would be longer than the
expected context+servlet prefix path. This can lead to
OutOfBoundsExceptions when the provided URL does not start with that
prefix and still has the required length.
This commit makes sure that all candidate URLs for resources lookup are
prefixed with the cached servlet and context path. This underlines the
fact that the `ResourceUrlEncodingFilter` does not support relative URLs
for now and delegates to the native servlet implementation in that case.
Issue: SPR-13861
cherry-picked from 2f6d86b7
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -150,6 +150,25 @@ public class ResourceUrlEncodingFilterTests {
|
||||
});
|
||||
}
|
||||
|
||||
// SPR-13847
|
||||
@Test
|
||||
public void encodeUrlPreventStringOutOfBounds() throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/context-path/index");
|
||||
request.setContextPath("/context-path");
|
||||
request.setServletPath("");
|
||||
request.setAttribute(ResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR, this.resourceUrlProvider);
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
this.filter.doFilterInternal(request, response, new FilterChain() {
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
|
||||
String result = ((HttpServletResponse)response).encodeURL("index?key=value");
|
||||
assertEquals("index?key=value", result);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
protected ResourceUrlProvider createResourceUrlProvider(List<ResourceResolver> resolvers) {
|
||||
ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
|
||||
handler.setLocations(Arrays.asList(new ClassPathResource("test/", getClass())));
|
||||
|
||||
Reference in New Issue
Block a user