Skip to content

Commit 6d26e61

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
1 parent 4f8c454 commit 6d26e61

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;
@@ -177,6 +178,9 @@ public final String getForRequestUrl(HttpServletRequest request, String requestU
177178
}
178179
int prefixIndex = getLookupPathIndex(request);
179180
int suffixIndex = getEndPathIndex(requestUrl);
181+
if (prefixIndex >= suffixIndex) {
182+
return null;
183+
}
180184
String prefix = requestUrl.substring(0, prefixIndex);
181185
String suffix = requestUrl.substring(suffixIndex);
182186
String lookupPath = requestUrl.substring(prefixIndex, suffixIndex);
@@ -194,11 +198,11 @@ private int getLookupPathIndex(HttpServletRequest request) {
194198
private int getEndPathIndex(String lookupPath) {
195199
int suffixIndex = lookupPath.length();
196200
int queryIndex = lookupPath.indexOf('?');
197-
if(queryIndex > 0) {
201+
if (queryIndex > 0) {
198202
suffixIndex = queryIndex;
199203
}
200204
int hashIndex = lookupPath.indexOf('#');
201-
if(hashIndex > 0) {
205+
if (hashIndex > 0) {
202206
suffixIndex = Math.min(suffixIndex, hashIndex);
203207
}
204208
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.
@@ -90,6 +90,16 @@ public void getStaticResourceUrlRequestWithQueryOrHash() {
9090
assertEquals("/resources/foo.css#hash", resolvedUrl);
9191
}
9292

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

0 commit comments

Comments
 (0)