HTML Iframe Sandbox and Permissions Policy
Begin with a restrictive frame
An iframe embeds another browsing context. The sandbox attribute with an empty value applies its restrictions, including an opaque origin and disabled scripts. srcdoc supplies a local document; use only trusted static markup here. A title names the frame for users.
<iframe title="Reading preview" sandbox="" srcdoc="<p>Read only</p>"></iframe>The sandboxed frame displays a paragraph.
Grant only a required capability
Sandbox tokens remove specific restrictions. allow-scripts permits scripting but does not restore the original origin. Combining allow-scripts and allow-same-origin on same-origin content can undermine isolation; avoid that combination for untrusted documents. The static fixture merely demonstrates token configuration.
<iframe title="Restricted demo" sandbox="allow-scripts" srcdoc="<p>Demo</p>"></iframe>Only the script restriction is relaxed.
Restrict powerful APIs
The allow attribute configures frame Permissions Policy. It can further restrict features delegated by the parent policy; it does not override a server denial or user permission. This example denies camera and microphone without calling those APIs.
<iframe title="Article preview" sandbox="" allow="camera 'none'; microphone 'none'" srcdoc="<p>Article</p>"></iframe>The frame declares both features denied.
Common mistakes and practice
Sandbox is not a universal sanitizer or network blocker. Serve untrusted content from a separate origin, review network and navigation policies, and give frames meaningful titles. Do not remove restrictions merely to fix a broken demo.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
HTML Iframe Sandbox and Permissions Policy — Knowledge Check
Apply the security, privacy and performance concepts taught in this lesson.
What does an empty sandbox apply?
What does allow-scripts restore?
Which pair is risky for same-origin untrusted content?
Does allow override a restrictive parent policy?
What does an iframe title provide?