Skip to main content
All Terms Glossary

Sandbox Attribute for Iframes

Definition

The sandbox attribute for iframes provides fine-grained control over what content within an iframe can do. It can restrict actions like form submission, JavaScript execution, and popup creation. This helps protect against malicious content in embedded frames while still allowing legitimate functionality. The attribute can be configured with specific permissions as needed.

What the iframe sandbox attribute does

The sandbox attribute is a token list you add to an iframe to strip away the embedded document's default capabilities and then grant back only the ones you choose. An empty sandbox applies the maximum set of restrictions: the framed content runs in a unique opaque origin, cannot execute scripts, cannot submit forms, cannot open popups, cannot navigate the top-level page, and cannot use plugins. You loosen it selectively with flags such as allow-scripts, allow-forms, allow-same-origin, and allow-popups. Each keyword re-enables one capability, so the frame receives the least privilege the feature actually needs. It is defined in the HTML standard and enforced by the browser itself.

Why the sandbox attribute matters for security

Embedding third-party or user-supplied content without isolation lets that content script the host page, read cookies, submit forms, or hijack navigation. The sandbox attribute reduces the blast radius when embedded markup is untrusted, for example an advertisement, a rich-text preview, or a widget. A key subtlety is that combining allow-scripts with allow-same-origin defeats much of the protection: the frame can then reach into its own served origin, and if that origin matches the embedder, it can remove its own sandbox by rewriting the frame. Sandboxing is a containment control, not a content-inspection control, so it limits what compromised markup can do but does not tell you whether the markup was tampered with.

Applying the sandbox attribute safely

Grant the narrowest set of flags the embedded feature requires, and never pair allow-scripts with allow-same-origin for content you do not fully trust. Serve untrusted embeds from a separate, cookieless origin so an opaque-origin frame cannot reach real session data. Pair the attribute with a Content Security Policy and framing controls so defenses do not depend on a single flag. Sandboxing governs iframes you author; it does nothing about the behavior of the many first- and third-party scripts loading directly on your own pages. cside addresses that gap by routing those scripts through a Script method, analysing the JavaScript payload, blocking malicious behavior in real time, and keeping a forensic record.

Definition

Does the sandbox attribute replace a Content Security Policy?

No. The sandbox attribute restricts what one embedded iframe can do, while a Content Security Policy governs what the whole page may load and execute. They cover different scopes and work best together: sandbox contains an untrusted embed, and CSP restricts scripts, connections, and framing across the document.

Definition

Why is allow-scripts plus allow-same-origin considered dangerous?

Together they let the framed document run scripts with access to its real origin. If that origin is the embedder's, the frame can read the same data and even rewrite its own markup to drop the sandbox attribute, effectively escaping the restrictions you intended to impose.

Got more questions

Talk to a security expert

We answer client-side security questions every day. Bring yours.

Book a demo

Want to walk through this with an engineer?

Thirty minutes, on your own site. Not a slide deck.

We'll show you:

Which third-party scripts are running on your site right now
Where you stand on PCI DSS 6.4.3 and 11.6.1
How much of your traffic is bots and AI agents

Rather just send a question?

Finding open slots…

Real humans only. We'd know.

Having trouble booking? Open scheduler in a new tab

What are you trying to solve?

Tell us in a line and we'll come back with something useful, not a generic pitch.

We usually help with:

Seeing which third-party scripts run on your site
PCI DSS 6.4.3 and 11.6.1 evidence
Bots, AI agents and account takeover

Prefer to just book a time? Pick a slot instead