From c22675398502e433dc0ea5382a927cec542840aa Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Tue, 1 Dec 2015 14:47:23 +0100 Subject: [PATCH] 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 --- .../web/servlet/resource/AbstractVersionStrategy.java | 7 ++++++- .../web/servlet/resource/FixedVersionStrategyTests.java | 9 ++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/AbstractVersionStrategy.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/AbstractVersionStrategy.java index ebd4e73c57..5d639444f7 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/AbstractVersionStrategy.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/AbstractVersionStrategy.java @@ -100,7 +100,12 @@ public abstract class AbstractVersionStrategy implements VersionStrategy { @Override public String addVersion(String path, String version) { - return (this.prefix.endsWith("/") || path.startsWith("/") ? this.prefix + path : this.prefix + "/" + path); + if(path.startsWith(".")) { + return path; + } + else { + return (this.prefix.endsWith("/") || path.startsWith("/") ? this.prefix + path : this.prefix + "/" + path); + } } } diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/FixedVersionStrategyTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/FixedVersionStrategyTests.java index 8eeb0ab0ee..b0488ee861 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/FixedVersionStrategyTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/FixedVersionStrategyTests.java @@ -57,7 +57,14 @@ public class FixedVersionStrategyTests { @Test public void addVersion() throws Exception { - assertEquals(this.version + "/" + this.path, this.strategy.addVersion(this.path, this.version)); + assertEquals(this.version + "/" + this.path, this.strategy.addVersion("/" + this.path, this.version)); + } + + // SPR-13727 + @Test + public void addVersionRelativePath() throws Exception { + String relativePath = "../" + this.path; + assertEquals(relativePath, this.strategy.addVersion(relativePath, this.version)); } } -- GitLab