JStachio fails to escape single quotes '
in HTML, allowing an attacker to inject malicious code.
Use the following template code:
<div attr='{{value}}'></div>
Set the value variable to ' onblur='alert(1)
.
public class Escaping {
public static void main(String[] args) {
Model model = new Model("' onblur='alert(1)");
String result = AttributeTemplate.of().execute(model);
System.out.println(result);
}
@JStache(template = "<div attr='{{value}}'></div>",
name="AttributeTemplate")
@JStacheConfig(contentType= Html.class)
public static class Model {
public final String value;
public Model(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
}
Expected Result: The resulting output should have properly escaped the single quotes and not execute the injected JavaScript code.
Actual Result: The resulting output is vulnerable and renders as follows:
<div attr='' onblur='alert(1)'></div>
This vulnerability can be exploited by an attacker to execute arbitrary JavaScript code in the context of other users visiting pages that use this template engine. This can lead to various consequences, including session hijacking, defacement of web pages, theft of sensitive information, or even the propagation of malware.
To mitigate this vulnerability, the template engine should properly escape special characters, including single quotes. Common practice is to escape '
as '
.
### Patches -
As long as users stick with double quotes "
for HTML attributes, they should not be affected.
For instance, Rocker's general purpose HTML escaping
{ "nvd_published_at": "2023-05-30T22:15:10Z", "cwe_ids": [ "CWE-79" ], "severity": "MODERATE", "github_reviewed": true, "github_reviewed_at": "2023-06-06T00:45:18Z" }