Skip to content

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.basePath without ever declaring them is reading whatever the DOM provides. <a id="config" href="javascript:alert(document.cookie)"> makes config.href an executable URL. Two elements with the same id produce an HTMLCollection, which is why payloads like <img id="x"><img id="x"> appear in real exploits — x.length is now 2, a number the attacker set.
  • Form properties. Named controls are exposed as properties of the form element. form.action normally returns the submission URL; an injected <input name="action"> makes form.action return the input element instead — the form's real action is shadowed, and form.action = "https://evil.example" assignments break in exploitable ways. A control named submit disables form.submit() entirely.
  • Attribute reads. Code doing el.getAttribute('data-url') is fine; code doing el.querySelector('a').href where the a was 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?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.