postmessage-xss
Detect postMessage handlers that trust unvalidated origins or write attacker-controlled data to dangerous DOM sinks.
Best use case
postmessage-xss is best used when you need a repeatable AI agent workflow instead of a one-off prompt.
Detect postMessage handlers that trust unvalidated origins or write attacker-controlled data to dangerous DOM sinks.
Teams using postmessage-xss should expect a more consistent output, faster repeated execution, less prompt rewriting.
When to use this skill
- You want a reusable workflow that can be run more than once with consistent structure.
When not to use this skill
- You only need a quick one-off answer and do not need a reusable workflow.
- You cannot install or maintain the underlying files, dependencies, or repository context.
Installation
Claude Code / Cursor / Codex
Manual Installation
- Download SKILL.md from GitHub
- Place it in
.claude/skills/postmessage-xss/SKILL.mdinside your project - Restart your AI agent — it will auto-discover the skill
How postmessage-xss Compares
| Feature / Agent | postmessage-xss | Standard Approach |
|---|---|---|
| Platform Support | Not specified | Limited / Varies |
| Context Awareness | High | Baseline |
| Installation Complexity | Unknown | N/A |
Frequently Asked Questions
What does this skill do?
Detect postMessage handlers that trust unvalidated origins or write attacker-controlled data to dangerous DOM sinks.
Where can I find the source code?
You can find the source code on GitHub using the link provided at the top of the page.
SKILL.md Source
# DOM XSS via postMessage
This skill covers detecting and exploiting Cross-Origin Messaging (postMessage) vulnerabilities that lead to DOM-based XSS.
---
## Overview
`postMessage` is a browser API that allows cross-origin communication between windows/iframes. When message handlers don't validate `event.origin`, any website can send malicious data that may execute as XSS.
**Why This Is Critical:**
- Often overlooked by security scanners (server-focused)
- Enables XSS from any origin without user interaction
- Commonly chained with SSRF/bot scenarios to bypass localhost restrictions
---
## Vulnerability Pattern
### Vulnerable Code
A handler that accepts messages from ANY origin and writes to DOM:
```
window.addEventListener("message", (event) => {
const data = event.data;
document.getElementById("output")[".inner" + "HTML"] = data.html; // XSS!
});
```
### Secure Code
```
window.addEventListener("message", (event) => {
if (event.origin !== "https://trusted-domain.com") {
return; // Reject untrusted origins
}
document.getElementById("output").textContent = data.text; // Safe
});
```
---
## Detection Patterns
### Step 1: Find Message Handlers
```bash
# Find all postMessage event listeners
grep -rniE "addEventListener\s*\(\s*['\"]message['\"]" --include="*.js" --include="*.ts" --include="*.html"
# Find direct onmessage assignments
grep -rniE "\.onmessage\s*=" --include="*.js" --include="*.ts"
```
### Step 2: Check for Missing Origin Validation
```bash
# Find handlers and check next 10 lines for origin check
grep -rniE "addEventListener\s*\(\s*['\"]message['\"]" --include="*.js" --include="*.ts" -A 10 | grep -v "origin"
```
### Step 3: Trace to Dangerous Sinks
```bash
# event.data to DOM write sinks
grep -rniE "event\.data.*(inner|outer)HTML" --include="*.js" --include="*.ts"
# event.data to location (open redirect)
grep -rniE "event\.data.*location|location.*event\.data" --include="*.js" --include="*.ts"
# event.data assigned to variable (trace further)
grep -rniE "=\s*event\.data" --include="*.js" --include="*.ts"
```
---
## Common Vulnerable Patterns
| Pattern | Risk | Detection |
|---------|------|-----------|
| No origin check + DOM sink | Critical | Missing `event.origin` validation |
| Weak origin check (regex) | High | `origin.includes()` or `origin.match()` |
| Variable assignment then sink | High | `const data = event.data` then later to sink |
| jQuery `.html()` method | Critical | `$(elem).html(event.data)` |
---
## Exploitation via SSRF + iframe
When combined with SSRF (bot visits attacker URL):
1. Bot visits attacker page
2. Attacker page creates iframe to `http://localhost:1337/`
3. postMessage sends XSS payload to iframe
4. XSS executes in localhost context (bypasses network restrictions)
This chain bypasses Chrome's Private Network Access restrictions.
---
## Remediation
### 1. Always Validate Origin
```javascript
const ALLOWED_ORIGINS = ["https://trusted.com"];
window.addEventListener("message", (event) => {
if (!ALLOWED_ORIGINS.includes(event.origin)) {
return; // Reject
}
// Process message safely...
});
```
### 2. Use Safe Sinks
- Use `textContent` instead of HTML sinks
- Use DOMPurify to sanitize HTML content
- Validate message structure before processing
### 3. Add Frame Protection
```
X-Frame-Options: SAMEORIGIN
Content-Security-Policy: frame-ancestors 'self'
```
---
## References
- OWASP: DOM-based XSS
- CWE-79: Improper Neutralization of Input
- PortSwigger: DOM-based XSS via postMessageRelated Skills
Workspace Discovery
This skill should be used when the user asks to "detect workspaces", "find packages", "list monorepo packages", "workspace structure", "monorepo analysis", or needs to identify workspace/package boundaries in a codebase for focused security analysis.
vulnerability-chains
This skill should be used when the user asks about "vulnerability chains", "chained exploits", "multi-step attacks", "SSRF to RCE", "pivot attacks", or needs to identify how vulnerabilities in different components can be combined during whitebox security review.
Vulnerability Patterns
This skill should be used when the user asks about "vulnerability patterns", "how to find SQL injection", "XSS patterns", "command injection techniques", "OWASP vulnerabilities", "common web vulnerabilities", "exploitation patterns", or needs to understand how specific vulnerability classes work during whitebox security review.
Threat Modeling
This skill should be used when the user asks about "threat model", "STRIDE", "data flow diagram", "attack surface", "threat analysis", "security architecture", "component threats", "trust boundaries", "technology decomposition", or needs systematic threat identification during whitebox security review.
verify-finding
Drive a single finding through CPG verification and false-positive triage.
start-audit
Guided first-run security audit: doctor, scope, threats, scan, verify, report.
scope-repo
Decide audit boundaries for large or monorepo targets and write audit-plan.md.
review-pr
Diff-aware PR security review with verified findings and PR comment payload.
package-evidence
Bundle findings, reports, audit plan, and ledger into one evidence zip.
Sensitive Data Leakage
Detect ANY credential/secret flowing to ANY output sink. Use when asked about "credential leakage", "secret logging", "sensitive data exposure", "CWE-532", "password in logs", "token exposure", or security logging issues.
Security Misconfiguration
This skill should be used when the user asks about "security misconfiguration", "default credentials", "debug mode", "security headers", "exposed endpoints", "TLS configuration", or needs to find configuration-related vulnerabilities during whitebox security review.
Secret Scanning
This skill should be used when the user asks about "secret scanning", "find secrets", "hardcoded credentials", "leaked API keys", "git history secrets", "credential scanning", "detect passwords in code", or needs to identify secrets and credentials in source code or git history during whitebox security review.