Course contentsShow
← Security
Lesson 1136 of 3,05223. Web Security - Modern Web AttacksFree lesson

Content-Security-Policy frame-ancestors Directive

Implementing modern frame-ancestors CSP directive for granular control over which origins can embed your content.

Content-Security-Policy frame-ancestors Directive

What you'll learn: How to use the modern frame-ancestors CSP directive to precisely control which sites can embed your content in frames, preventing clickjacking attacks.


Why frame-ancestors Exists

You've already learned about X-Frame-Options, which provides basic framing control. The frame-ancestors directive is its modern, more powerful successor within the Content-Security-Policy framework. It gives you granular control over exactly which origins can embed your page.

How It Works

While X-Frame-Options offers only three simple settings (DENY, SAMEORIGIN, ALLOW-FROM), frame-ancestors lets you specify multiple allowed origins with precise matching:

Content-Security-Policy: frame-ancestors 'none';

(Blocks all framing - equivalent to X-Frame-Options: DENY)

Content-Security-Policy: frame-ancestors 'self';

(Allows only same-origin framing)

Content-Security-Policy: frame-ancestors https://trusted.example.com https://partner.example.org;

(Allows embedding only from these two specific origins)

Key Advantages

Precision: Unlike X-Frame-Options's single-origin limitation, you can whitelist multiple specific domains.

Standard compliance: frame-ancestors is part of CSP Level 2 and supported by modern browsers. When both headers are present, browsers prioritize frame-ancestors.

Scheme and port awareness: You can specify https:// vs http:// and different ports, giving you exact control over the embedding context.

Think of It Like...

If X-Frame-Options is a simple door lock with three positions, frame-ancestors is a digital access system where you can program exactly which keycards (origins) are authorized to open the door.

Key Takeaway:
The frame-ancestors CSP directive provides granular, multi-origin control over framing, replacing X-Frame-Options with more flexible and precise protection against clickjacking attacks.