How to stop other sites framing yours (X-Frame-Options and frame-ancestors)

Send a frame-ancestors policy, and X-Frame-Options SAMEORIGIN for older browsers, so only your own site may show its pages in a frame.

Last reviewed

Why it matters

A page that can be framed can be laid under an attacker's page, so a visitor clicking the attacker's button actually clicks yours, such as Buy or Delete in an account screen. Frame protection tells the browser which sites may embed the page.

How to fix it

Send both Content-Security-Policy: frame-ancestors 'self' and X-Frame-Options: SAMEORIGIN; browsers that understand frame-ancestors ignore the older header, so the two never conflict.

  • No enforcing CSP yet: a CSP containing only frame-ancestors 'self' restricts nothing but framing. When you add a full policy later, move frame-ancestors 'self' into it and drop this one.
  • Already sending an enforcing CSP: append ; frame-ancestors 'self' to it instead of adding a second policy.
  • Framed on purpose by another of the client's domains: use frame-ancestors 'self' https://booking.example.org with that domain. X-Frame-Options cannot express this (its ALLOW-FROM is obsolete).

frame-ancestors does nothing in a report-only policy or in a <meta> tag.

Replace the highlighted values: www.example.com with the site's hostname.

Cloudflare

  1. Rules > Overview > Create rule > Response Header Transform Rule, name it, and apply it to all incoming requests.
  2. Set static: Header name X-Frame-Options, Value SAMEORIGIN.
  3. Set new header, Set static: Header name Content-Security-Policy, Value frame-ancestors 'self'. Only if the origin sends no CSP: Set static would overwrite it.
  4. Deploy.

Sources: create a response header rule, response header modification.

nginx

add_header X-Frame-Options "SAMEORIGIN" always;
add_header Content-Security-Policy "frame-ancestors 'self'" always;

Sources: ngx_http_headers_module, MDN: X-Frame-Options.

Apache

<IfModule mod_headers.c>
    Header always set X-Frame-Options "SAMEORIGIN"
    Header always set Content-Security-Policy "frame-ancestors 'self'"
</IfModule>

Source: mod_headers.

Caddy

header {
	X-Frame-Options "SAMEORIGIN"
	Content-Security-Policy "frame-ancestors 'self'"
}

Behind reverse_proxy, if the app sends its own CSP, merge the values or use the > prefix.

Source: the header directive.

Vercel

{
  "headers": [
    {
      "source": "/(.*)",
      "headers": [
        { "key": "X-Frame-Options", "value": "SAMEORIGIN" },
        { "key": "Content-Security-Policy", "value": "frame-ancestors 'self'" }
      ]
    }
  ]
}

Source: vercel.json.

Netlify

/*
  X-Frame-Options: SAMEORIGIN
  Content-Security-Policy: frame-ancestors 'self'

Source: Netlify custom headers.

WordPress

Plugin route (HTTP Headers): Settings > HTTP Headers > Security, Edit next to X-Frame-Options, select On, choose SAMEORIGIN (not ALLOW-FROM), and Save Changes. That alone clears this finding.

Code route: inside the send_headers function in wp-content/mu-plugins/security-headers.php (see the HSTS guide for the file). Leave out the second line if a plugin or the server already sends a CSP, and add frame-ancestors 'self' to that policy instead:

header( 'X-Frame-Options: SAMEORIGIN' );
header( "Content-Security-Policy: frame-ancestors 'self'" );

Sources: HTTP Headers plugin, send_headers.

Shopify

Shopify documents no way for merchants to set response headers, and sends its own frame protection, whose value varies between stores. If this finding appears on a Shopify store, there is no documented setting to change it.

Check that it worked

curl -sI https://www.example.com/ | grep -i -E "x-frame-options|content-security-policy"

There should be one content-security-policy line with frame-ancestors in it (not in a report-only line). From a page on another origin, an <iframe> of the site should refuse to display, and the console names the header that blocked it. Then run the Owl Keeper Security headers check again.

Test it now with the free HTTP status code checker. No account needed.

What can go wrong

  • It breaks framing you wanted: page-builder previews loaded from another origin, a client's other domain embedding a booking form, partner portals. Ask the client first and list those origins in frame-ancestors.
  • DENY or 'none' also stops the site framing itself, which breaks some admin previews; that is why SAMEORIGIN and 'self' are the starting point.