markup-after-sanitize

Markup written into text after it was sanitized or escaped.

Group
Code
Runs
Runs under ur scan: ur scan --rule markup-after-sanitize
Level
warning
Reads
Every file
Fix
None: the markup must be built another way, which a rewrite cannot choose

Why

Sanitizing or escaping text makes it safe to render as it is. Markup written into it afterwards, as a replace that wraps each match in a <span> does, is not sanitized: a pattern can match inside an entity or a tag the sanitizer kept and split it, and a match or a pattern a user chose is written into the markup as it is. Build the highlighted text from text nodes, as a component that renders each part with {{ }} does, or sanitize the markup after it is written.

The fix, as Ur suggests it: render the parts as text, or sanitize after writing the markup.

For agents

Do not write markup into text after sanitizing or escaping it; render the parts as text nodes, or sanitize the result last.

Example

app/highlight.js
import { sanitize } from '~/lib/dompurify';

export const highlight = (path, term) =>
  sanitize(path, { ALLOWED_TAGS: [] }).replace(new RegExp(`(${term})`, 'g'), '<b>$1</b>');
What Ur prints
$ ur scan --rule markup-after-sanitize
warning[markup-after-sanitize]: `replace` writes markup into text that was sanitized
  --> ./app/highlight.js:4:3 `sanitize(path, { ALLOWED_TAGS: [] }).replace(new RegExp(`(${...`
  = hint: render the parts as text, or sanitize after writing the markup
  = see: CWE-79: Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting'), MITRE (2024-11-19), https://cwe.mitre.org/data/definitions/79.html
scanned 1 file (no framework recognized)
  markup-after-sanitize: 1 high, 0 medium
1 finding and 0 of medium confidence

Try it in the playground →

Source

CWE-79: Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting'), MITRE (2024-11-19).