Course contentsShow
← Web Development
Lesson 2879 of 2,98251. Content Security PolicyFree lesson

X-Frame-Options vs frame-ancestors

Compare the legacy X-Frame-Options header with CSP's frame-ancestors directive.

X-Frame-Options vs frame-ancestors

What you'll learn: How the older X-Frame-Options header compares to CSP's modern frame-ancestors directive for controlling iframe embedding.

The Two Approaches

Both X-Frame-Options and CSP's frame-ancestors directive solve the same problem: preventing clickjacking attacks by controlling whether your site can be embedded in an iframe on another domain. Think of them as different generations of the same security tool—one legacy, one modern.

X-Frame-Options (The Legacy Approach)

X-Frame-Options is the older HTTP header with three possible values:

X-Frame-Options: DENY
X-Frame-Options: SAMEORIGIN
X-Frame-Options: ALLOW-FROM https://example.com

Limitations:

  • Only allows one value (DENY, SAMEORIGIN, or ALLOW-FROM)
  • ALLOW-FROM only supports a single origin (not multiple)
  • ALLOW-FROM has poor browser support and is deprecated
  • Cannot be combined with other security features in a unified policy

frame-ancestors (The Modern CSP Approach)

The frame-ancestors directive in Content Security Policy is more flexible:

Content-Security-Policy: frame-ancestors 'self' https://trusted.com https://another.com

Advantages:

  • Supports multiple allowed origins
  • Part of your unified CSP policy
  • Better browser support for advanced use cases
  • More expressive (can use wildcards and schemes)
  • Can be combined with report-only mode for testing

Compatibility Note

For maximum browser coverage, you can set both headers. Modern browsers will respect frame-ancestors and ignore X-Frame-Options when a CSP policy is present. Older browsers fall back to X-Frame-Options.

Key Takeaway: X-Frame-Options is the legacy method for iframe protection with limited options; frame-ancestors is the modern, flexible CSP directive that supersedes it—use both for backward compatibility, but design around frame-ancestors for new implementations.