Skip to content

Commit f53d01b

Browse files
bclozeljhoeller
authored andcommitted
Do not rewrite relative links with FixedVersionStrategy
Prior to this change, the resource handling FixedVersionStrategy would be applied on all links that match the configured pattern. This is problematic for relative links and can lead to rewritten links such as "/fixedversion/../css/main.css" which breaks. This commit prevents that Strategy from being applied to such links. Of course, one should avoid to use that VersionStrategy with relative links, but this change aims at not breaking existing links even if it means not prefixing the version as expected. Issue: SPR-13727 (cherry picked from commit c226753)
1 parent b3eefa2 commit f53d01b

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 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.
@@ -100,7 +100,13 @@ public String removeVersion(String requestPath, String version) {
100100

101101
@Override
102102
public String addVersion(String path, String version) {
103-
return (this.prefix.endsWith("/") || path.startsWith("/") ? this.prefix + path : this.prefix + "/" + path);
103+
if (path.startsWith(".")) {
104+
return path;
105+
}
106+
else {
107+
return (this.prefix.endsWith("/") || path.startsWith("/") ?
108+
this.prefix + path : this.prefix + "/" + path);
109+
}
104110
}
105111
}
106112

@@ -118,7 +124,7 @@ public String extractVersion(String requestPath) {
118124
Matcher matcher = pattern.matcher(requestPath);
119125
if (matcher.find()) {
120126
String match = matcher.group(1);
121-
return match.contains("-") ? match.substring(match.lastIndexOf("-") + 1) : match;
127+
return (match.contains("-") ? match.substring(match.lastIndexOf("-") + 1) : match);
122128
}
123129
else {
124130
return null;
@@ -134,7 +140,7 @@ public String removeVersion(String requestPath, String version) {
134140
public String addVersion(String requestPath, String version) {
135141
String baseFilename = StringUtils.stripFilenameExtension(requestPath);
136142
String extension = StringUtils.getFilenameExtension(requestPath);
137-
return baseFilename + "-" + version + "." + extension;
143+
return (baseFilename + "-" + version + "." + extension);
138144
}
139145
}
140146

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 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.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.web.servlet.resource;
1718

1819
import org.junit.Before;
@@ -21,7 +22,8 @@
2122
import static org.junit.Assert.*;
2223

2324
/**
24-
* Unit tests for {@link org.springframework.web.servlet.resource.FixedVersionStrategy}
25+
* Unit tests for {@link org.springframework.web.servlet.resource.FixedVersionStrategy}.
26+
*
2527
* @author Brian Clozel
2628
*/
2729
public class FixedVersionStrategyTests {
@@ -57,7 +59,13 @@ public void removeVersion() throws Exception {
5759

5860
@Test
5961
public void addVersion() throws Exception {
60-
assertEquals(this.version + "/" + this.path, this.strategy.addVersion(this.path, this.version));
62+
assertEquals(this.version + "/" + this.path, this.strategy.addVersion("/" + this.path, this.version));
63+
}
64+
65+
@Test // SPR-13727
66+
public void addVersionRelativePath() throws Exception {
67+
String relativePath = "../" + this.path;
68+
assertEquals(relativePath, this.strategy.addVersion(relativePath, this.version));
6169
}
6270

6371
}

0 commit comments

Comments
 (0)