Template engines, such as Mustache or Handlebars, provide automatic escaping of template variables to prevent cross-site scripting (XSS) attacks, but this protection can be explicitly disabled.
Template engines provide auto-escaping as a safety mechanism that transforms HTML special characters in variable output before rendering,
preventing user-controlled input from being interpreted as HTML or JavaScript by the browser. Disabling this protection — through settings like
autoescape: false, escape-bypass filters like |safe, or equivalent configuration — allows untrusted input to pass through
unmodified and be executed by the browser as markup or script.
When auto-escaping is disabled, an attacker who can control the content of template variables can inject malicious HTML or JavaScript into pages served to other users. An attacker could steal session tokens, redirect users to phishing pages, or perform unauthorized actions on behalf of the victim.
The following examples configure the template engine to disable its auto-escaping feature, allowing template variables to be rendered without HTML encoding.
let Mustache = require("mustache");
Mustache.escape = function(text) {return text;}; // Noncompliant
let rendered = Mustache.render(template, { name: inputName });
let Mustache = require("mustache");
let rendered = Mustache.render(template, { name: inputName }); // Compliant autoescaping is on by default
The following examples configure the template engine to disable its auto-escaping feature, allowing template variables to be rendered without HTML encoding.
const Handlebars = require('handlebars');
let source = "<p>attack {{name}}</p>";
let template = Handlebars.compile(source, { noEscape: true }); // Noncompliant
const Handlebars = require('handlebars');
let source = "<p>attack {{name}}</p>";
let data = { "name": "<b>Alan</b>" };
let template = Handlebars.compile(source); // Compliant by default noEscape is set to false
The following examples configure the template engine to disable its auto-escaping feature, allowing template variables to be rendered without HTML encoding.
const markdownIt = require('markdown-it');
let md = markdownIt({
html: true // Noncompliant
});
let result = md.render('# <b>attack</b>');
let md = require('markdown-it')(); // Compliant by default html is set to false
let result = md.render('# <b>attack</b>');
The following examples configure the template engine to disable its auto-escaping feature, allowing template variables to be rendered without HTML encoding.
const marked = require('marked');
marked.setOptions({
renderer: new marked.Renderer(),
sanitize: false // Noncompliant
});
const marked = require('marked');
marked.setOptions({
renderer: new marked.Renderer()
}); // Compliant by default sanitize is set to true
The following examples configure the template engine to disable its auto-escaping feature, allowing template variables to be rendered without HTML encoding.
let kramed = require('kramed');
let options = {
renderer: new kramed.Renderer({
sanitize: false // Noncompliant
})
};
let kramed = require('kramed');
let options = {
renderer: new kramed.Renderer({
sanitize: true // Compliant
})
};