Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,12 @@ private void renderNode(
if (value == null) out.append(checked).append(";\n");
else dynamic.put(attribute, new MvcExpression.Value(checked, "java.lang.String"));
}
if (tag.equals("link") && dynamic.containsKey("href"))
// A link's href is usually a resource the browser fetches (stylesheet, preload, icon),
// so it must be static. rel="canonical" fetches nothing -- it names the page's own URL,
// which a page served at many URLs has to compute -- so it alone may be dynamic. The
// rel has to be static for that to hold, and Html.attribute still applies safeUrl to
// the rendered href.
if (tag.equals("link") && dynamic.containsKey("href") && !canonicalLink(e, dynamic))
throw new IllegalArgumentException("Dynamic link resource URLs are not supported");
if (tag.equals("script")
&& (dynamic.containsKey("src")
Expand Down Expand Up @@ -946,6 +951,22 @@ private MvcExpression.Value expression(
return parser.parse(value);
}

/// True for a link whose static rel is exactly "canonical". Any other token (or a dynamic
/// rel) could make the browser fetch the href, so it does not qualify.
private static boolean canonicalLink(Element e, Map<String, MvcExpression.Value> dynamic) {
if (dynamic.containsKey("rel") || !e.hasAttr("rel")) return false;
// Tokenize and compare the way browsers do: split on HTML (ASCII) whitespace only and
// fold ASCII case only. equalsIgnoreCase would also accept "canonical" spelled with a
// dotless i.
String rel = e.attr("rel").trim();
if (rel.isEmpty()) return false;
for (String token : rel.split("[ \\t\\n\\f\\r]+")) {
if (!token.toLowerCase(java.util.Locale.ROOT).equals("canonical")
|| !token.matches("[A-Za-z]+")) return false;
}
return true;
}

private MvcExpression.Value url(
String value, Map<String, MvcExpression.Value> env, String form) {
int open = value.indexOf('(');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2372,6 +2372,40 @@ public void linkResourceUrlsMustBeStatic() throws Exception {
assertTrue(render("link", new Model()).contains("href=\"https://example.test/\""));
}

@Test
public void canonicalLinkMayBeDynamic() throws Exception {
setup();
String declaration = "<!-- cn1:model url java.lang.String -->";
template("canonical", declaration + "<link rel=\"canonical\" th:href=\"${url}\">");
compile();
assertTrue(
render("canonical", new Model().addAttribute("url", "https://example.test/pricing"))
.contains("href=\"https://example.test/pricing\""));
try {
render("canonical", new Model().addAttribute("url", "javascript:alert(1)"));
fail("Rendered an unsafe canonical URL");
} catch (IllegalArgumentException expected) {
assertTrue(expected.getMessage(), expected.getMessage().contains("Unsafe URL"));
}
// Only a rel that is canonical alone, and static, qualifies.
for (String link :
Arrays.asList(
"<link rel=\"canonical stylesheet\" th:href=\"${url}\">",
"<link rel=\"canon\u0131cal\" th:href=\"${url}\">",
"<link rel=\"canonical\u000bstylesheet\" th:href=\"${url}\">",
"<link rel=\"canonical\" th:attr=\"rel=${url},href=${url}\">")) {
template("canonical", declaration + link);
try {
new MvcTemplates(context).sources();
fail("Accepted dynamic link resource URL: " + link);
} catch (IllegalArgumentException expected) {
assertTrue(
expected.getMessage(),
expected.getMessage().contains("Dynamic link resource URLs"));
}
}
}

@Test
public void nativeGetSubmitOverridesCannotDiscloseCsrfTokens() throws Exception {
setup();
Expand Down
Loading