From 56fdda167e094acf73763128b5b40ab1a35d5b2d Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Mon, 5 Mar 2018 15:17:42 +0100 Subject: [PATCH] Guard against invalid paths in ResourceUrlProvider This commit makes sure that no `StringIndexOutOfBoundsException` is thrown when `getForRequestUrl` is called with a URL that's shorter than the expected context path. Issue: SPR-16526 (cherry picked from commit 6d26e61ac7) --- .../web/servlet/resource/ResourceUrlProvider.java | 8 ++++++-- .../servlet/resource/ResourceUrlProviderTests.java | 12 +++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceUrlProvider.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceUrlProvider.java index 15f635fdd27..36cf72c4990 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceUrlProvider.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceUrlProvider.java @@ -22,6 +22,7 @@ import java.util.Comparator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; + import javax.servlet.http.HttpServletRequest; import org.apache.commons.logging.Log; @@ -182,6 +183,9 @@ public class ResourceUrlProvider implements ApplicationListener= suffixIndex) { + return null; + } String prefix = requestUrl.substring(0, prefixIndex); String suffix = requestUrl.substring(suffixIndex); String lookupPath = requestUrl.substring(prefixIndex, suffixIndex); @@ -199,11 +203,11 @@ public class ResourceUrlProvider implements ApplicationListener 0) { + if (queryIndex > 0) { suffixIndex = queryIndex; } int hashIndex = lookupPath.indexOf('#'); - if(hashIndex > 0) { + if (hashIndex > 0) { suffixIndex = Math.min(suffixIndex, hashIndex); } return suffixIndex; diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceUrlProviderTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceUrlProviderTests.java index c0bd8c54a7c..39ce0d46de8 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceUrlProviderTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceUrlProviderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2018 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. @@ -87,6 +87,16 @@ public class ResourceUrlProviderTests { assertEquals("/resources/foo.css#hash", resolvedUrl); } + @Test // SPR-16526 + public void getStaticResourceWithMissingContextPath() { + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setContextPath("/contextpath-longer-than-request-path"); + request.setRequestURI("/contextpath-longer-than-request-path/style.css"); + String url = "/resources/foo.css"; + String resolvedUrl = this.urlProvider.getForRequestUrl(request, url); + assertNull(resolvedUrl); + } + @Test public void getFingerprintedResourceUrl() { Map versionStrategyMap = new HashMap<>();