Skip to content

StringIndexOutOfBoundsException when rewriting links in CSS resources [SPR-16526] #21069

@spring-projects-issues

Description

@spring-projects-issues

Ravish Bhagdev opened SPR-16526 and commented

Scenario,

  • CSS with relative paths to PNG files within.
  • Happens regardless of versioning strategy used (Fixed and Content)
  • Fails for the web service with relatively longer context path when trying to parse URL for PNG with the CSS :
2018-02-22T11:27:31.721017610Z java.lang.StringIndexOutOfBoundsException: String index out of range: -2
2018-02-22T11:27:31.721022567Z  at java.lang.String.substring(String.java:1967) ~[na:1.8.0_161]
2018-02-22T11:27:31.721027608Z  at org.springframework.web.servlet.resource.ResourceUrlProvider.getForRequestUrl(ResourceUrlProvider.java:187) ~[spring-webmvc-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]
2018-02-22T11:27:31.721032196Z  at org.springframework.web.servlet.resource.ResourceTransformerSupport.resolveUrlPath(ResourceTransformerSupport.java:80) ~[spring-webmvc-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]
2018-02-22T11:27:31.721037033Z  at org.springframework.web.servlet.resource.CssLinkResourceTransformer.transform(CssLinkResourceTransformer.java:105) ~[spring-webmvc-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]
2018-02-22T11:27:31.721041105Z  at org.springframework.web.servlet.resource.DefaultResourceTransformerChain.transform(DefaultResourceTransformerChain.java:67) ~[spring-webmvc-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]
2018-02-22T11:27:31.721045190Z  at org.springframework.web.servlet.resource.CachingResourceTransformer.transform(CachingResourceTransformer.java:76) ~[spring-webmvc-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]
2018-02-22T11:27:31.721049282Z  at org.springframework.web.servlet.resource.DefaultResourceTransformerChain.transform(DefaultResourceTransformerChain.java:67) ~[spring-webmvc-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]
2018-02-22T11:27:31.721053655Z  at org.springframework.web.servlet.resource.ResourceHttpRequestHandler.getResource(ResourceHttpRequestHandler.java:538) ~[spring-webmvc-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]
2018-02-22T11:27:31.721058140Z  at org.springframework.web.servlet.resource.ResourceHttpRequestHandler.handleRequest(ResourceHttpRequestHandler.java:433) ~[spring-webmvc-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]
2018-02-22T11:27:31.721063672Z  at org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.handle(HttpRequestHandlerAdapter.java:51) ~[spring-webmvc-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]
2018-02-22T11:27:31.721068193Z  at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) ~[spring-webmvc-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]

It seems like the relative paths to PNG files within my CSS are processed incorrectly. Prefix and suffix generated in ResourceUrlProvider.java on line number 183 and 184 and then incorrect:
int prefixIndex = getLookupPathIndex(request);
int suffixIndex = getEndPathIndex(requestUrl);

Suffix index is often > prefix index. Causing above exception.

Here is my configuration:

VersionResourceResolver versionResourceResolver = new VersionResourceResolver()
.addVersionStrategy(new ContentVersionStrategy(), "/**");

registry.addResourceHandler("/js/**", "/css/**", "/images/**", "/favicon.ico")
        .addResourceLocations(
                "classpath:static/js/", "static/js/",
                "classpath:static/css/", "static/css/",
                "classpath:static/images/", "static/images/"
        )
        .setCachePeriod(CACHE_PERIOD)
        .resourceChain(true)
        .addResolver(versionResourceResolver);

Works fine if I remove versionResourceResolver from above resource handler registry.

There was a similar issue fixed earlier with ResourceUrlEncodingFilter :
#18420

I wonder if this is similar to that but in ResourceUrlProvider? 

 I've now created a sample project and reproduced the exception reported. There is a Readme on this project with steps for testing:
https://github.com/RavBhagdev/sample

Will appreciate any help or advice with this. 

Affects: 4.3.13

Reference URL: #18420

Issue Links:

Referenced from: commits 56fdda1, 6d26e61

Backported to: 4.3.15

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)in: webIssues in web modules (web, webmvc, webflux, websocket)status: backportedAn issue that has been backported to maintenance branchestype: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions