Skip to content

Commit bb5da15

Browse files
committed
Allow protocol relative URLs in CssLink Transformer
This commit allows the use of "protcol relative URLs" (i.e. URLs without scheme, starting with `//`), often used to serve resources automatically from https or http with third party domains. This syntax is allowed by RFC 3986. Issue: SPR-12632
1 parent 0c8d07f commit bb5da15

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ public Resource transform(HttpServletRequest request, Resource resource, Resourc
123123

124124
private boolean hasScheme(String link) {
125125
int schemeIndex = link.indexOf(":");
126-
return schemeIndex > 0 && !link.substring(0, schemeIndex).contains("/");
126+
return (schemeIndex > 0 && !link.substring(0, schemeIndex).contains("/"))
127+
|| link.indexOf("//") == 0;
127128
}
128129

129130

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ public void transformExtLinksNotAllowed() throws Exception {
9999
TransformedResource transformedResource = (TransformedResource) resource;
100100

101101
String expected = "@import url(\"http://example.org/fonts/css\");\n" +
102-
"body { background: url(\"file:///home/spring/image.png\") }";
102+
"body { background: url(\"file:///home/spring/image.png\") }\n" +
103+
"figure { background: url(\"//example.org/style.css\")}";
103104
String result = new String(transformedResource.getByteArray(), "UTF-8");
104105
result = StringUtils.deleteAny(result, "\r");
105106
assertEquals(expected, result);
@@ -108,6 +109,8 @@ public void transformExtLinksNotAllowed() throws Exception {
108109
.resolveUrlPath("http://example.org/fonts/css", Arrays.asList(externalCss));
109110
Mockito.verify(resolverChain, Mockito.never())
110111
.resolveUrlPath("file:///home/spring/image.png", Arrays.asList(externalCss));
112+
Mockito.verify(resolverChain, Mockito.never())
113+
.resolveUrlPath("//example.org/style.css", Arrays.asList(externalCss));
111114
}
112115

113116
@Test
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
@import url("http://example.org/fonts/css");
2-
body { background: url("file:///home/spring/image.png") }
2+
body { background: url("file:///home/spring/image.png") }
3+
figure { background: url("//example.org/style.css")}

0 commit comments

Comments
 (0)