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

TokenPermission Granted
allow-scriptsRun JavaScript inside the iframe
allow-same-originTreat content as same-origin (access cookies, localStorage, DOM APIs)
allow-formsSubmit forms
allow-popupsOpen new windows/tabs via window.open() or target="_blank"
allow-popups-to-escape-sandboxOpened popups are not sandboxed themselves
allow-top-navigationNavigate the top-level browsing context
allow-top-navigation-by-user-activationNavigate the top-level context only after a user gesture
allow-top-navigation-to-custom-protocolsNavigate to non-HTTP protocols (e.g. mailto:)
allow-modalsUse alert(), confirm(), prompt()
allow-orientation-lockLock screen orientation
allow-pointer-lockUse the Pointer Lock API
allow-presentationUse the Presentation API
allow-downloadsTrigger 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

<!-- Maximum restriction: no scripts, no forms, no popups, no navigation --> <iframe src="https://untrusted-widget.com/embed" sandbox title="Third-party widget"></iframe> <!-- Allow scripts and form submission only --> <iframe src="https://forms.example.com/survey" sandbox="allow-scripts allow-forms" title="Survey form"></iframe>

Common Configurations

<!-- Embedded video player: needs scripts and same-origin access --> <iframe src="https://player.example.com/embed/abc123" sandbox="allow-scripts allow-same-origin" title="Video player"></iframe> <!-- Payment form: needs scripts, forms, and top-level navigation for redirect --> <iframe src="https://pay.example.com/checkout" sandbox="allow-scripts allow-forms allow-top-navigation" title="Payment checkout"></iframe> <!-- Ad embed: scripts only, no escape from the iframe --> <iframe src="https://ads.example.com/banner" sandbox="allow-scripts allow-popups" title="Advertisement"></iframe>

Inline Content with srcdoc

Combine sandbox with srcdoc to sandbox inline HTML without needing an external URL.

<!-- Inline sandboxed content using srcdoc --> <iframe sandbox="allow-scripts" srcdoc="<p>Hello from a sandboxed inline document.</p>" title="Sandboxed inline content"></iframe>

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.

<!-- DANGEROUS: allow-scripts + allow-same-origin together --> <!-- The iframe can run JavaScript AND access same-origin APIs, --> <!-- which means it can call frameElement.removeAttribute('sandbox') --> <!-- to remove ALL restrictions on itself. --> <iframe src="https://example.com/untrusted" sandbox="allow-scripts allow-same-origin" title="This can escape its sandbox"></iframe>

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-scripts is 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 content
  • loading — lazy-load iframes for performance
  • nonce — CSP nonces for inline scripts and styles