Skip to content

Lesson 14 of 14 · 13m

iframes, sandbox, and Clickjacking

The sandbox attribute's capability tokens, the allow-same-origin plus allow-scripts escape, frame-ancestors as the real anti-framing control, and COOP/COEP isolation.

sandbox: downgrade the framed document

An iframe is a nested browsing context running someone else's document inside your page's real estate. The sandbox attribute decides how dangerous that document is: with no tokens, the framed page gets a unique opaque origin — no cookies, no storage, no DOM reach into the parent — and its scripting, forms, popups, downloads, and top navigation are all blocked.

Each token re-enables a capability: allow-scripts, allow-same-origin, allow-forms, allow-popups, allow-top-navigation, allow-top-navigation-by-user-activation, allow-modals, allow-downloads, allow-pointer-lock. The -by-user-activation variant gates top navigation behind a real click — a fit for payment and login widgets. The combination that bites people:

<iframe sandbox="allow-scripts allow-same-origin" src="/untrusted.html"></iframe>

For a same-origin frame this pair is a self-own. allow-same-origin keeps the document in your origin; allow-scripts lets it run — and a same-origin scripted frame can reach parent.document, remove its own sandbox attribute, and reload unrestricted. You built a cage the prisoner can unlock. For untrusted same-origin content, allow-scripts without allow-same-origin: JS runs, but the opaque origin means the same-origin policy walls it off from your DOM and credentials.

srcdoc frames parse a full inline document — <iframe srcdoc="..."> — and inherit the embedding origin, so the same token logic applies. A CSP sandbox directive on a response sandboxes your own document — rare, but it exists as a self-imposed limit. On the introspection side, location.ancestorOrigins tells a framed document exactly which origins embedded it — supported in Chromium for years and shipped in Firefox 148 (February 2026) — so a page that consents to being embedded can enforce its own partner list in script, in addition to relying on frame-ancestors at the header.

Clickjacking: your page as the attacker's UI

UI redress is the inverse embedding: the attacker iframes your page, transparent or cropped, over a decoy. The victim clicks the decoy's "play video"; the click lands on your "transfer" button. Jeremiah Grossman and Robert Hansen made the class public in 2008 — the canonical demo was hijacking Adobe Flash's settings manager to enable the victim's webcam — and it never stopped working, because the web composes by default.

The defense is a response header, not JavaScript. CSP's frame-ancestors directive lists who may embed you: frame-ancestors 'none' for bank-shaped surfaces, 'self' or a partner list for embeddables. It replaced X-Frame-Options (DENY/SAMEORIGIN; ALLOW-FROM was dropped) — XFO still works and sending both is fine, but frame-ancestors is the directive that supports real host lists. JS framebusting — if (top !== self) top.location = self.location — is cosmetic: the attacker iframes you inside sandbox without allow-top-navigation, your bust script throws on the navigation attempt, and your page renders anyway inside their overlay.

COOP, COEP: isolating your own window

Two newer headers isolate your page from attacker-controlled windows rather than attacker content:

  • Cross-Origin-Opener-Policy: same-origin ejects your page from any browsing-context group shared with cross-origin openers — window.opener is severed, and cross-origin windows cannot script your handle. It also closes the class of tab-nabbing and opener-based leaks lesson 13 skirted.
  • Cross-Origin-Embedder-Policy: require-corp requires every subresource to opt in via Cross-Origin-Resource-Policy or CORS. Both together yield crossOriginIsolated, which unlocks SharedArrayBuffer and high-resolution timers — gated behind isolation since the Spectre mitigations (Chrome ~91, 2021). If you do not need SAB or precise timers, COOP alone is cheap and useful; COEP has real adoption cost because every third-party resource must cooperate.

What to actually do

Untrusted embeds get sandbox="allow-scripts" and nothing else; never pair it with allow-same-origin on same-origin content. Every sensitive response ships frame-ancestors (and XFO for old engines). Treat framebusting scripts as decoration. Add Cross-Origin-Opener-Policy: same-origin where the app tolerates it — it severs the last casual cross-window channel the sandbox does not cover.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.