Developer Tools
Review a Template or Rendering Change for Values Escaped for the Wrong Output Context
You are reviewing a code change that renders untrusted data into HTML, email templates, or other markup. Your job is to find places where a value reaches the output without escaping that matches the context it lands in. Do not review style or naming.
Inputs I will give you:
- The diff, plus the template files and helpers it touches.
- The templating engine and whether auto-escaping is on by default.
- Which values come from users, third parties, or the database (if I know).
For every place a dynamic value is written into output, work out:
1. The source: is the value fully controlled by our code, or could a user or third party influence it, even indirectly (a stored profile field, a filename, a webhook payload)?
2. The output context: HTML text, a quoted HTML attribute, an unquoted attribute, a URL in href/src, inline JavaScript or a JSON blob inside a script tag, CSS, or an email header. Each needs different escaping, and HTML-escaping alone is not safe for URLs, script contexts, or unquoted attributes.
3. The escaping actually applied: auto-escape, an explicit filter, or a bypass (raw/safe/unescaped markers, building markup with string concatenation, or marking a string as pre-escaped).
4. Whether a URL value can carry a dangerous scheme such as javascript: and whether it is checked against an allow-list.
Report findings as a table: location, value, source, context, escaping applied, why it is unsafe, and a concrete fix. Rank by exploitability: user-controlled values in script or URL contexts first. List separately any bypass that is safe today but only because of an assumption (for example, "this field is always numeric") and name the assumption. If you can't see where a value comes from, say "source unknown" instead of guessing.
When to use it: before merging any change that adds a template, a rich-text or Markdown renderer, an email notification, or a place where code marks HTML as safe.
How to adapt it: name your engine (Jinja2, React JSX, Handlebars, ERB) so the review knows its default escaping and its bypass syntax. For plain-text emails, swap HTML contexts for header injection (newlines in subject or recipient fields).
Example input: a Jinja2 template diff adding <a href="{{ user.website }}">{{ user.name }}</a> and a script tag with var cfg = {{ settings | tojson }}.
Good output flags user.website as a user-controlled URL that auto-escaping does not protect from a javascript: scheme and recommends a scheme allow-list. It notes that user.name in text context is covered by auto-escape, and it checks whether tojson is the engine's script-safe JSON filter rather than flagging it by reflex. It does not pad the report with generic advice.
Sign in to upvote this prompt.