An empty sandbox attribute makes an iframe's content as powerless as possible: no scripts, no forms, no popups, no top-level navigation, and an opaque origin so it can't touch cookies, storage or same-origin APIs. You add capabilities back one token at a time (allow-scripts, allow-forms, …). For truly untrusted content, such as user HTML, ads, plugins or code playgrounds, combine sandboxing with serving the content from a separate origin, a restrictive allow (Permissions Policy) attribute and a narrow postMessage protocol.
| Token | Grants | Risk |
|---|---|---|
| (none) | Static rendering only | — |
allow-scripts | JavaScript runs | Needed for most widgets |
allow-same-origin | Keeps the content's real origin (cookies, storage) | Dangerous with allow-scripts if the frame is same-origin with the parent |
allow-forms | Form submission | Phishing forms |
allow-popups / allow-popups-to-escape-sandbox | window.open, target=_blank; the second lets popups run unsandboxed | Popups can lead to malicious pages |
allow-top-navigation-by-user-activation | Navigate the parent page, but only after a click | Prefer it over allow-top-navigation |
allow-modals, allow-downloads, allow-storage-access-by-user-activation | Specific features | Grant only when needed |
<!-- Same-origin content with both tokens can remove its own sandbox -->
<iframe src="/user-content/123.html" sandbox="allow-scripts allow-same-origin"></iframe>
The framed script can reach parent.document, find the iframe element, delete the sandbox attribute and reload itself unsandboxed. It can also read your cookies and storage directly. Never combine these two tokens for untrusted content on your own origin.
<iframe
src="https://usercontent-sandbox.example-cdn.net/render/123"
sandbox="allow-scripts allow-popups"
allow="camera 'none'; microphone 'none'; geolocation 'none'"
referrerpolicy="no-referrer"
loading="lazy"
title="User submitted widget"></iframe>
SameSite protects your session. This is why Google uses googleusercontent.com and GitHub uses githubusercontent.com.sandbox with the minimum tokens.allow turns off powerful features (camera, payment, clipboard). Cross-origin iframes don't get most of them by default, but being explicit documents intent.postMessage contract: validate event.origin and the message schema on both sides. With an opaque origin, event.origin is the string "null", so validate event.source === iframe.contentWindow instead.connect-src limits) and frame-ancestors restricted to your app.srcdoc is handy for small snippets, but srcdoc content inherits the parent's origin unless sandboxed, so always sandbox it (without allow-same-origin) when the content is untrusted.
sandbox directive: the framed server can force sandboxing on itself, even when loaded directly.SharedArrayBuffer).sandbox="allow-scripts allow-same-origin" on same-origin content.postMessage from * or sending sensitive data with target origin '*'.title on iframes (accessibility).postMessage as the full pattern.*usercontent.com domains) and that Shadow DOM isn't isolation.Spotted a mistake or a rights concern? Report an issue