frame-ancestors and Clickjacking Prevention
You'll learn: How to use the CSP frame-ancestors directive to stop attackers from embedding your pages in malicious iframes and tricking users into unintended actions.
What is Clickjacking?
Clickjacking is an attack where a malicious site embeds your webpage inside an invisible or disguised iframe. Users think they're clicking on the attacker's content, but they're actually clicking buttons or links on your site—potentially changing settings, authorizing payments, or deleting data without realizing it.
Analogy: Imagine someone places a transparent touchscreen over your phone while you're playing a game. You think you're tapping game buttons, but you're actually approving bank transfers underneath.
How frame-ancestors Protects You
The frame-ancestors directive in your Content Security Policy controls which origins are allowed to embed your page in a <frame>, <iframe>, <object>, or <embed>.
Basic Syntax
Content-Security-Policy: frame-ancestors 'none';
This completely prevents your page from being framed by any site—even your own.
Content-Security-Policy: frame-ancestors 'self';
Allows your own origin to frame the page (useful for internal dashboards).
Content-Security-Policy: frame-ancestors https://trusted-partner.com;
Permits only trusted-partner.com to embed your content.
Why This Matters
Unlike the older X-Frame-Options header, frame-ancestors is part of CSP and offers more granular control. It works with CSP's reporting and can specify multiple trusted origins.
Key Takeaway:
Use frame-ancestors in your CSP to explicitly control who can embed your pages, effectively preventing clickjacking attacks by blocking unauthorized framing attempts.