Skip to content

Commit 56fdda1

Browse files
committed
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 6d26e61)
1 parent 017f2a8 commit 56fdda1

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.LinkedHashMap;
2323
import java.util.List;
2424
import java.util.Map;
25+
2526
import javax.servlet.http.HttpServletRequest;
2627

2728
import org.apache.commons.logging.Log;
@@ -182,6 +183,9 @@ public final String getForRequestUrl(HttpServletRequest request, String requestU
182183
}
183184
int prefixIndex = getLookupPathIndex(request);
184185
int suffixIndex = getEndPathIndex(requestUrl);
186+
if (prefixIndex >= suffixIndex) {
187+
return null;
188+
}
185189
String prefix = requestUrl.substring(0, prefixIndex);
186190
String suffix = requestUrl.substring(suffixIndex);
187191
String lookupPath = requestUrl.substring(prefixIndex, suffixIndex);
@@ -199,11 +203,11 @@ private int getLookupPathIndex(HttpServletRequest request) {
199203
private int getEndPathIndex(String lookupPath) {
200204
int suffixIndex = lookupPath.length();
201205
int queryIndex = lookupPath.indexOf('?');
202-
if(queryIndex > 0) {
206+
if (queryIndex > 0) {
203207
suffixIndex = queryIndex;
204208
}
205209
int hashIndex = lookupPath.indexOf('#');
206-
if(hashIndex > 0) {
210+
if (hashIndex > 0) {
207211
suffixIndex = Math.min(suffixIndex, hashIndex);
208212
}
209213
return suffixIndex;

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -87,6 +87,16 @@ public void getStaticResourceUrlRequestWithQueryOrHash() {
8787
assertEquals("/resources/foo.css#hash", resolvedUrl);
8888
}
8989

90+
@Test // SPR-16526
91+
public void getStaticResourceWithMissingContextPath() {
92+
MockHttpServletRequest request = new MockHttpServletRequest();
93+
request.setContextPath("/contextpath-longer-than-request-path");
94+
request.setRequestURI("/contextpath-longer-than-request-path/style.css");
95+
String url = "/resources/foo.css";
96+
String resolvedUrl = this.urlProvider.getForRequestUrl(request, url);
97+
assertNull(resolvedUrl);
98+
}
99+
90100
@Test
91101
public void getFingerprintedResourceUrl() {
92102
Map<String, VersionStrategy> versionStrategyMap = new HashMap<>();

0 commit comments

Comments
 (0)