Skip to content

Lesson 3 of 14 · 13m

Framework Escape Hatches

React's dangerouslySetInnerHTML and unfiltered href schemes, Vue v-html, Angular's bypassSecurityTrust* calls, and jQuery's script-executing parseHTML.

The framework only protects what it interpolates

Every modern frontend developer has absorbed the same belief: "React escapes for me, so XSS is handled." The belief is true inside a boundary and false outside it. JSX stringifies and HTML-escapes {expr} interpolations. Everything that is not an interpolation — props bound to URLs, raw-HTML props, the deliberate escape hatches — is your problem.

React

dangerouslySetInnerHTML={{__html: raw}} inserts raw through innerHTML, unescaped. The name is a deliberate warning, and teams still populate it from CMS fields and markdown renders. Whatever goes into __html must come out of DOMPurify.sanitize() first — React will not catch it.

The subtler hole is URLs. React escapes text; it does not validate schemes. <a href={user.website}> renders javascript:alert(document.cookie) verbatim, and the click executes it. React has printed a console warning for javascript: URLs since 16.9 (August 2019) — a warning, not a block. It ships anyway. The same applies to <form action> and anything else that navigates. The fix is a scheme allowlist applied before binding:

function safeHref(url: string): string {
  try {
    const u = new URL(url, location.origin);
    return ['https:', 'http:', 'mailto:'].includes(u.protocol) ? u.href : '#';
  } catch { return '#'; }
}

Vue

v-html="raw" is innerHTML with a directive. Vue's own documentation says never use it on user-provided content — and says nothing about sanitizing for you. v-bind:href has the same scheme problem as React's href.

Angular

Angular is the exception that proves the rule. Property bindings route through DomSanitizer, which strips javascript: URLs and sanitizes HTML before it reaches the DOM. It works — until someone calls bypassSecurityTrustHtml(), bypassSecurityTrustUrl(), bypassSecurityTrustScript(), or bypassSecurityTrustResourceUrl(). Those functions exist for the rare case where you have already made the value safe; in practice they get called "to make the sanitizer error go away." Each call is the framework's sanitizer being explicitly unplugged. Treat any of them in a diff as a finding until the sanitization argument is in writing.

jQuery

$(htmlString) and $.parseHTML() are worse than innerHTML in one specific way: jQuery finds <script> elements inside the fragment and executes them via globalEval. innerHTML leaves parser-inserted scripts inert; $(userInput) runs them. Codebases that survived an innerHTML audit die on $.parseHTML. jQuery.htmlPrefilter historically rewrote tags like <xmp> and <option> — a sanitizer-bypass surface that produced its own advisories (CVE-2020-23064 among them).

Markdown and templates

A markdown renderer's output is HTML — by design, since markdown allows inline HTML blocks. marked.parse(userText) handed to innerHTML passes <img src=x onerror=...> straight through unless the renderer was configured to strip raw HTML; sanitize the rendered output, not the source. In Handlebars and Mustache, {{{x}}} and {{{ }}} are the unescaped forms of {{x}}. Server-side template engines have the same split: EJS <%- is raw, <%= is escaped.

The same scheme hole applies anywhere a URL prop reaches an anchor — router components like <Link to={userUrl}> render real <a href> elements, and the sanitizing layer is upstream of them, not inside.

Serialization into scripts

One more hatch people forget: putting state inside <script> as JSON. JSON.stringify() does not make a value safe for embedding — a string containing </script> terminates the block early and the rest parses as HTML. serialize-javascript exists precisely for this; it escapes < as \u003c. If you hand-roll JSON.stringify into a script tag or an onclick attribute, you are building the XSS yourself.

What to actually do

Grep for the hatches: dangerouslySetInnerHTML, v-html, bypassSecurityTrust, parseHTML, {{{, <%-. Every hit must pair with a sanitizer call or a written justification. Allowlist URL schemes before binding href/action — escaping never reaches the scheme. The framework protects interpolations. Audits live everywhere else.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.