dom-clobbering-and-postmessage
Skill2 starsChanged 4 months agoMITView on GitHub
SAST detection methodology for DOM clobbering and insecure cross-window messaging (CWE-79, CWE-451). Covers id/name attributes overwriting JS globals and document properties, postMessage receivers with missing/weak origin checks, wildcard targetOrigin data leaks, window.name / location.hash / document.referrer as DOM sources, and document.domain relaxation. Use when reviewing client-side JS that reads DOM-derived globals or handles postMessage. Writes confirmed findings to findings/15-dom-clobbering-and-postmessage.md.
In this repository49 other files it gives its agents.
- authentication-flawsSkill
- broken-access-control-idorSkill
- business-logic-flawsSkill
- cicd-and-pipeline-injectionSkill
- code-injectionSkill
- command-injectionSkill
- cors-misconfigurationSkill
- crlf-and-header-injectionSkill
- cross-site-scriptingSkill
- csrfSkill
- dependency-and-supply-chainSkill
- graphql-injection-and-abuseSkill
- hardcoded-secretsSkill
- http-request-smugglingSkill
- iac-misconfigurationSkill
- information-disclosureSkill
- insecure-deserializationSkill
- insecure-randomnessSkill
- jwt-vulnerabilitiesSkill
- ldap-injectionSkill
- llm-and-ai-application-securitySkill
- mass-assignmentSkill
- memory-safetySkill
- mobile-android-securitySkill
- mobile-ios-securitySkill
- nosql-injectionSkill
- oauth-oidc-saml-flawsSkill
- open-redirectSkill
- path-traversal-and-file-inclusionSkill
- prototype-pollutionSkill
- race-conditions-and-toctouSkill
- redosSkill
- sast-final-reportSkill
- sast-finding-validationSkill
- sast-methodologySkill
- security-logging-and-monitoring-failuresSkill
- security-misconfiguration-and-headersSkill
- server-side-template-injectionSkill
- session-managementSkill
- smart-contract-securitySkill
- sql-injectionSkill
- ssrfSkill
- unrestricted-file-uploadSkill
- unsafe-parsers-and-yamlSkill
- weak-cryptographySkill
- web-cache-poisoning-and-deceptionSkill
- websocket-securitySkill
- xpath-and-xml-injectionSkill
- xxeSkill
The file
We split this skill into the rules it gives an agent
2 rules. About 0 are common, 0 are distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 93
Don't rely on globals/document properties for security-relevant values.
- Very specific: line 96
Use typeof x === 'string'/type guards before use.
≈ “Common” is an estimate: at least 1 in 5 of the 372,429 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
DOM Clobbering & Insecure postMessage — SAST Methodology
Class: DOM Clobbering / Cross-Window Messaging · CWE-79 (and CWE-451) · OWASP: A03 Injection
Findings file: findings/15-dom-clobbering-and-postmessage.md
1. Overview
Two related client-side trust failures.
DOM clobbering: an attacker who can inject non-script HTML (a name/id
attribute is enough, so it survives many sanitizers and CSP) overwrites JS
variables and document/window properties, because named elements are exposed
as globals (window.x, document.x). Logic that reads an "expected" global then
consumes an attacker-shaped object/string.
Insecure postMessage: a receiver that acts on event.data without verifying
event.origin trusts messages from any window; a sender using '*' targetOrigin
leaks data to any framing/opener origin. The core tests: does code read a global/
document property that a named DOM element could clobber? and does a message
handler validate origin before using the data (and is that check correct)?
2. Where it lives
addEventListener('message', …)/window.onmessagehandlers, especially in SDKs, payment/auth iframes, chat widgets, embeds, and cross-origin iframe bridges.- Bootstrap/config code reading globals:
if (window.CONFIG),document.currentScript,document.getElementById(x)fallbacks,var x = window.x || default. - HTML sinks that consume clobbered values (
document.write,innerHTML,srcset from a global,<script src>built from a clobberable variable). window.postMessage(data, '*')senders;document.domain = …relaxation code.
3. Sources (tainted input)
- DOM-clobbering source: attacker-injectable HTML with
id/nameattributes (stored comments, profile HTML, markdown, sanitized-but-tag-allowed content, ad/embed slots). No script needed — clobbering is the injection. - postMessage source:
event.datafrom any window (opener, parent, child iframe, popup) when origin is unchecked. - Other DOM sources feeding sinks:
window.name(survives cross-origin navigation → cross-domain data smuggling),location.hash/location.search,document.referrer,document.cookie. Second-order: a value read from one of these, stored, then rendered.
4. Sinks (dangerous operations)
// DOM clobbering — dangerous reads of clobberable globals
if (window.CONFIG) { load(window.CONFIG.url); } // <a id=CONFIG> clobbers to an element
let s = document.getElementById; // can be clobbered by <img id=getElementById>
var url = window.APP_BASE || '/'; // <a id=APP_BASE href="//evil"> → url is element, .toString()→href
document.write('<script src="' + window.tpl + '">'); // window.tpl clobbered
element.innerHTML = someGlobalFromDom;
// nested clobbering: <form id=x><input id=y> makes window.x.y an attacker value
config = window.settings.token; // form/input pair clobbers .token// postMessage RECEIVER — dangerous: no / weak origin check
window.addEventListener('message', (e) => {
// MISSING origin check entirely
document.getElementById('out').innerHTML = e.data.html; // → DOM XSS
eval(e.data.cmd); // → code exec
location = e.data.redirect; // → open redirect
});
// Weak origin checks — dangerous
if (e.origin.indexOf('trusted.com') !== -1) { … } // matches evil-trusted.com.attacker.com
if (e.origin.match(/trusted\.com/)) { … } // unanchored, unescaped dot
if (e.origin.startsWith('https://trusted.com')) { … } // matches https://trusted.com.evil.com// postMessage SENDER — dangerous: wildcard target leaks data
otherWindow.postMessage({ token: authToken }, '*'); // any framing origin receives it
parent.postMessage(sensitiveData, '*');// document.domain relaxation — dangerous
document.domain = 'example.com'; // widens same-origin to every subdomain → one XSS'd subdomain owns all5. Sanitizers / safe patterns
Safe (clobbering):
- Don't rely on globals/
documentproperties for security-relevant values; read config from aconstin module scope,JSON.parseof a data attribute you fetch explicitly, or aMap. - Use
typeof x === 'string'/type guards before use; clobbered values areHTMLElement/HTMLCollection, not the expected primitive. - Sanitize output HTML with a config that strips or namespaces
id/name(DOMPurifySANITIZE_DOM: true/SANITIZE_NAMED_PROPS: true). - Reference DOM APIs via a saved local (
const gebi = document.getElementById.bind(document)) captured before any untrusted HTML is inserted — or just don't shadow them.
Safe (postMessage):
- Receiver: strict, exact origin allowlist —
if (e.origin === 'https://app.example.com'), ideally plus ae.source === expectedWindowcheck and a message-type/nonce schema. - Sender: specify the exact targetOrigin, never
'*', when data is sensitive. - Validate
e.datashape before use; never route it toeval/innerHTML/location.
Fails / not a real sanitizer:
origin.indexOf(x) !== -1,.includes(x),.match(/x/)(unanchored),.endsWith('trusted.com')(matchesnottrusted.com),.startsWith('https://trusted.com')(matches…trusted.com.evil.com) — all bypassable via substring/suffix/prefix tricks.- Checking
e.originbut with an unescaped.in the regex (trusted.commatchestrustedxcom). - Sanitizing tags but allowing
id/nameattributes (clobbering still works). document.domainset to a "safe" parent — it broadens the origin; not a fix.- Trusting
e.source/referrer without also pinning origin.
6. Detection methodology
- Find postMessage receivers and audit the origin check:
For each handler: is there an origin check at all? Is it an exactrg -n "addEventListener\(\s*['\"]message['\"]|onmessage\s*=" rg -n "e(vent)?\.origin\s*(===|==|!=|!==|\.(indexOf|includes|match|startsWith|endsWith|search))"===match against an allowlist, or a bypassable substring/regex? Where doese.dataflow? - Find wildcard senders:
rg -n "postMessage\([^,]+,\s*['\"]\*['\"]\)" - Find DOM-clobbering-prone global reads:
rg -n "window\.\w+\s*\|\||document\.\w+\s*\|\||if\s*\(\s*window\.\w+" rg -n "getElementById|document\.(all|forms|images|cookie)|document\.currentScript" - Find other DOM sources into sinks:
rg -n "window\.name|location\.(hash|search|href)|document\.referrer" rg -n "document\.domain\s*=" - Confirm data flow: clobbered/
event.data/window.namevalue → does it reach an HTML/src/eval/locationsink or a security decision? - Confirm the injection surface (clobbering): can the attacker actually inject
an element with a controlled
id/name? (stored HTML, allowed tags in sanitizer) - Confirm reachability: is the handler/embed loaded on a real, framed/opened page?
7. Modern & niche variants
- DOM clobbering fundamentals:
<a id=x>exposeswindow.x; two elements with the samenamecreate anHTMLCollection; a<form id=x>containing<input id=y>/<input name=y>yieldswindow.x.y— enabling nested/multi-level clobbering of dotted property paths (config.api.url).document.getElementByIdand otherdocumentprops can themselves be clobbered by matchingid/name. Clobbering survives sanitizers that only strip scripts and survives script-src CSP. - Clobbering → code load / XSS: a clobbered
window.tpl/base-URL used to build a<script src>orinnerHTMLturns HTML injection into script execution without ever injecting a<script>tag. - postMessage receiver, no/weak origin check: the classic bug — handler routes
e.datatoinnerHTML/eval/location/DOM without verifyinge.origin, so any page that frames or opens the target sends the payload. Weak checks (indexOf, unanchored regex,startsWith/endsWith) are effectively no check. - Wildcard
targetOrigindata leak:postMessage(secret, '*')delivers to whatever origin currently occupies the target window — an attacker who controls framing/timing harvests tokens (CWE-451 / information exposure to wrong origin). window.nameas a source: persists across cross-origin navigations, so an attacker page can seedwindow.namethen navigate the victim to the target, smuggling a large payload into a DOM sink.location.hash/document.referrerDOM sinks: hash-based routing/config that feedsinnerHTML/eval; referrer echoed into the page.document.domainrelaxation: settingdocument.domainto a shared parent broadens same-origin to all subdomains, so a single subdomain XSS/takeover compromises the main app — turns a low-value subdomain into a full-origin foothold.
8. Common false positives
- postMessage handlers with an exact
e.origin === 'https://…'allowlist (and no bypassable operator) that also validatee.datashape. - Senders using a specific, hard-coded targetOrigin (not
'*') for sensitive data, or'*'used only for non-sensitive ping/handshake messages. - Global reads whose value is type-guarded and never reaches a dangerous sink, or where the page never renders attacker-injectable HTML (no clobbering surface).
location.hash/window.nameread but only routed totextContent/safe APIs.document.domainassignments that were removed/deprecated (dead code) — verify.
9. Severity & exploitability
Base Medium. Raise to High when the flow yields DOM XSS / code execution
(postMessage e.data → innerHTML/eval; clobbered value → <script src>), or
when a wildcard sender leaks auth tokens (→ account takeover, chain-Critical).
document.domain relaxation is Medium/High as an amplifier (one subdomain XSS →
whole origin). Lower if the DOM sink is safe or the clobbering surface can't be
reached. See references/severity-model.md.
10. Remediation
postMessage receivers: validate event.origin against an exact allowlist
(===), optionally pin event.source, and schema-validate event.data before use;
never route it to eval/innerHTML/location. Senders: always pass the specific
target origin, never '*', for sensitive data. DOM clobbering: don't derive
security-relevant values from named globals/document properties — use lexical
constants/Map, add type guards, and sanitize output HTML to strip/namespace
id/name (DOMPurify SANITIZE_NAMED_PROPS). Avoid document.domain; use
postMessage/CORS with explicit origins for cross-subdomain communication.
11. Output
Append each confirmed finding to findings/15-dom-clobbering-and-postmessage.md
using references/finding-template.md. Set Class: DOM Clobbering / Cross-Window
Messaging, CWE: CWE-79 (or CWE-451 for wrong-origin data leaks), and in
Chain potential name primitives such as cross-origin message injection
(provides a route to cross-site-scripting DOM sinks or open-redirect),
global variable override (arms a DOM XSS gadget, related to prototype-pollution),
sensitive-data leak to arbitrary origin (consumes framing control; provides
stolen tokens), or origin-scope broadening (document.domain). These findings are
usually chain links — always name the downstream sink the clobbered/message value
reaches.
Primitives (controlled): provides JS_EXEC,GADGET; consumes HTML_OUTPUT
References
- CWE-79, CWE-451; OWASP A03:2021; HTML Living Standard named-access rules; PortSwigger DOM clobbering & postMessage research; OWASP DOM XSS Prevention Cheat Sheet.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

