Lesson 4 of 14 · 12m
DOM Clobbering
Injected id and name attributes shadow globals your JS reads — how AMP4Email in Gmail fell to markup that contained no script at all.
Markup that defines globals
The HTML spec gives elements names that reach the global scope. An element with id="x" becomes window.x. Elements with name — form, iframe, img, object, embed, and historically a — appear as named properties on document. The mechanism is called named access, and it means injected markup can define the globals your JavaScript reads — without running a single line of script.
That breaks the mental model most engineers carry: "HTML injection without script is cosmetic — defacement at worst." DOM clobbering is the counterexample. An attacker who can only inject inert HTML — say, through a comment field run through a strict allowlist sanitizer that keeps id and name attributes — can still steer code that trusts the global scope.
What an attacker can shadow
- Undeclared globals. Code that reads
window.config,window.filters,document.basePathwithout ever declaring them is reading whatever the DOM provides.<a id="config" href="javascript:alert(document.cookie)">makesconfig.hrefan executable URL. Two elements with the same id produce anHTMLCollection, which is why payloads like<img id="x"><img id="x">appear in real exploits —x.lengthis now2, a number the attacker set. - Form properties. Named controls are exposed as properties of the form element.
form.actionnormally returns the submission URL; an injected<input name="action">makesform.actionreturn the input element instead — the form's real action is shadowed, andform.action = "https://evil.example"assignments break in exploitable ways. A control namedsubmitdisablesform.submit()entirely. - Attribute reads. Code doing
el.getAttribute('data-url')is fine; code doingel.querySelector('a').hrefwhere theawas injected is reading attacker input.
One scoping detail determines whether this works: named access only defines a name that isn't already a real property. document.createElement cannot be clobbered — it exists on the prototype — but document.filter, window.config, and any other name your code reads without declaring are open. The exploit surface is exactly the set of globals your JavaScript assumes but never created.
The Gmail proof
In August 2019, researcher Michał Bentkowski disclosed an XSS in Gmail's AMP4Email renderer that chained DOM clobbering with a CSP bypass. AMP's own script trusted globals the renderer never declared; injected markup supplied them, a CSP gap did the rest, and the result was script execution inside Gmail's origin — the highest-value DOM XSS of that year precisely because it required no script in the payload itself.
The technique kept maturing. Gareth Heyes's PortSwigger research on "DOM clobbering" documented exploitable instances across production sites and popular libraries, and sanitizers had to respond: DOMPurify added the SANITIZE_NAMED_PROPS option (prefixes id/name values in its output so attacker-chosen names cannot collide with globals) specifically because allowing id/name through a sanitizer was enough to clobber.
Why the usual defenses don't see it
CSP blocks script execution; clobbering needs none. DOMPurify strips dangerous tags and attributes; id and name are legitimate attributes that pass through unless SANITIZE_NAMED_PROPS is on. Trusted Types constrains sinks; clobbering poisons reads. The bug lives in the assumption that globals are code-defined.
What to actually do
Two rules cover it. First, never read a global your code did not declare — if you need a value, getElementById it and read properties off the returned element, which is a real object rather than a name collision. TypeScript and linters help here: an undeclared window.config read is a tsc error if it was never defined, which is a free audit. Second, strip or neutralize id/name in sanitized user content: DOMPurify with SANITIZE_NAMED_PROPS: true, or drop the attributes outright. And when auditing, treat any window.foo/document.foo read where foo isn't defined in the codebase as a sink-adjacent finding — you just have to find markup that defines 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.