sandbox
Restricts iframe capabilities by default, then selectively re-enables permissions. Essential for embedding untrusted third-party content safely.
Overview
The sandbox attribute on <iframe> applies a strict security policy to embedded content. When present with no value, it enables all restrictions: no scripts, no form submission, no popups, no navigation, no plugins, and the content is treated as a unique origin (blocking same-origin API access).
You then selectively re-enable capabilities by adding space-separated permission tokens. This "deny by default, allow by exception" model makes it far harder for embedded content to do something unexpected.
Values
| Token | Permission Granted |
|---|---|
allow-scripts | Run JavaScript inside the iframe |
allow-same-origin | Treat content as same-origin (access cookies, localStorage, DOM APIs) |
allow-forms | Submit forms |
allow-popups | Open new windows/tabs via window.open() or target="_blank" |
allow-popups-to-escape-sandbox | Opened popups are not sandboxed themselves |
allow-top-navigation | Navigate the top-level browsing context |
allow-top-navigation-by-user-activation | Navigate the top-level context only after a user gesture |
allow-top-navigation-to-custom-protocols | Navigate to non-HTTP protocols (e.g. mailto:) |
allow-modals | Use alert(), confirm(), prompt() |
allow-orientation-lock | Lock screen orientation |
allow-pointer-lock | Use the Pointer Lock API |
allow-presentation | Use the Presentation API |
allow-downloads | Trigger file downloads |
When sandbox is present with no value, all of the above are denied. The attribute value is a space-separated list of tokens to allow.
Examples
Basic Sandboxing
Common Configurations
Inline Content with srcdoc
Combine sandbox with srcdoc to sandbox inline HTML without needing an external URL.
Security Considerations
The allow-scripts + allow-same-origin Escape
Warning: Combining allow-scripts and allow-same-origin on content from your own origin allows the iframe to remove its own sandbox attribute via JavaScript. The embedded page can access frameElement and call removeAttribute('sandbox'), effectively escaping all restrictions.
This combination is safe only when the iframe content is from a different origin (the browser's same-origin policy blocks frameElement access). For same-origin content, avoid granting both tokens.
Silent Failure
Sandboxed actions fail silently. A blocked form submission, popup, or navigation simply does not happen — no error is thrown, no console warning appears. This makes debugging difficult. If embedded content seems broken, check whether a missing sandbox token is the cause.
Limitations
- Silent failures: Blocked actions produce no errors, making it hard to diagnose issues during development.
- No granular script control:
allow-scriptsis all-or-nothing. You cannot allow specific scripts or APIs while blocking others. - Unique origin side effects: Without
allow-same-origin, the iframe gets a unique opaque origin. This breaks localStorage, cookies, service workers, and any API that depends on origin. - Not a substitute for CSP: Sandbox restricts what the iframe can do; Content Security Policy restricts what resources it can load. Use both for defense in depth.
See Also
referrerpolicy— control referrer information sent to iframed contentloading— lazy-load iframes for performancenonce— CSP nonces for inline scripts and styles