Security rules
NGB079: Server-side template injection
Traces untrusted data into the *template source* compiled by server-side template engines: ejs.render/compile, pug.render/compile, Handlebars.compile, nunjucks.renderString, lodash / underscore _.template, Mustache.render, doT, Eta, LiquidJS and Twig.
| Property | Value |
|---|---|
| Rule ID | NGB079 |
| Category | Angular SSR security |
| Default severity | critical |
| CWE | CWE-1336, CWE-94 |
| Built-in presets | recommended: error, strict: error |
Confidence is reported per finding (high, medium, low) and is independent of severity. See Reading findings.
What it checks
Traces untrusted data into the template source compiled by server-side template engines: ejs.render/compile, pug.render/compile, Handlebars.compile, nunjucks.renderString, lodash / underscore _.template, Mustache.render, doT, Eta, LiquidJS and Twig. Data passed as template variables (the second argument) is not reported.
Why it matters
Template languages are programming languages: EJS, Pug, doT and lodash templates execute embedded JavaScript, and Handlebars / Nunjucks have known sandbox escapes. A request value that becomes part of the template text is code execution on the server (or at least full control of the rendered page), unlike the same value passed as data, which the engine escapes.
Detection behavior and supported patterns
ejs.render(<h1>${req.query.title}</h1>),pug.compile(req.body.layout),Handlebars.compile(tpl),_.template(req.body.text)… where the template source contains traced untrusted data (import, namespace / default import orrequire())- Not reported: constant templates with request values as data (
ejs.render(TEMPLATE, { title: req.query.title })),res.render(view, data)(view files)
Example
res.send(ejs.render(`<h1>Hello ${req.query.name}</h1>`));Safer alternative
res.send(ejs.render('<h1>Hello <%= name %></h1>', { name: req.query.name }));Remediation
Keep templates constant (files or literals in the code base) and pass request values only as template data. If users must author templates, use a logic-less engine in a sandboxed process and treat the output as untrusted HTML.
False-positive considerations
- Engines configured as logic-less with escaping (e.g. Mustache) are less dangerous; the finding still means users can rewrite the page markup.
Known limitations
- Template engines reached through framework adapters (
app.engine,consolidate) are not modeled;res.render()with a request-chosen view name is not reported. - Templates loaded from the database or CMS (stored template injection) are only reported when the HTTP response source is traced.
References
Run it
ngbastion explain NGB079