Skip to content

Commit

Permalink
Allow protocol relative URLs in CssLink Transformer
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bclozel committed Jan 20, 2015
1 parent bf7a975 commit 028c0e8
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ public Resource transform(HttpServletRequest request, Resource resource, Resourc

private boolean hasScheme(String link) {
int schemeIndex = link.indexOf(":");
return schemeIndex > 0 && !link.substring(0, schemeIndex).contains("/");
return (schemeIndex > 0 && !link.substring(0, schemeIndex).contains("/"))
|| link.indexOf("//") == 0;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ public void transformExtLinksNotAllowed() throws Exception {
TransformedResource transformedResource = (TransformedResource) resource;

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

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
@import url("http://example.org/fonts/css");
body { background: url("file:///home/spring/image.png") }
body { background: url("file:///home/spring/image.png") }
figure { background: url("//example.org/style.css")}

0 comments on commit 028c0e8

Please sign in to comment.