Can a strange site load your page in an iframe? Stop clickjacking

Clickjacking uses transparent or opaque layers so the user clicks a button on another page while meaning to click the page in front. The fix starts with one header: CSP frame-ancestors, with X-Frame-Options.

Published 2 min read#App security#Web development

You have a page with a "delete my account" or "transfer the amount" button. A strange site can load that page in an invisible layer and put a "win a prize" button over it. The user clicks the prize, and the click goes to your button. In this article you close that door with one header.

What is clickjacking?

Clickjacking is an attack that uses transparent or opaque layers to make the user click a button or link on another page while they meant to click the page on top. Its other name is a UI redress attack.

That is how OWASP's clickjacking attack page describes it: the attacker hijacks clicks meant for their own page and routes them to another page, here yours.

The attack does not break the login. The user is already signed in to your site, and the click comes from them, so your server sees a normal request.

How do you stop your page loading inside an iframe?

Send the CSP frame-ancestors header: it tells the browser whether your page may be shown inside a frame or an iframe, and from which sites.

The OWASP clickjacking cheat sheet says its defenses are independent of each other, and advises using more than one where possible, for defense in depth. It gives three forms of frame-ancestors:

Content-Security-Policy: frame-ancestors 'none';
Content-Security-Policy: frame-ancestors 'self';
Content-Security-Policy: frame-ancestors 'self' https://partner.example.com;
  1. 'none': no site can put your page in a frame. This is the right one for account and payment pages.

  2. 'self': only your own site.

  3. 'self' with a named origin: your site and the partner site you listed.

Why add X-Frame-Options too?

X-Frame-Options is the older header for the same job, and it protects in browsers that do not know frame-ancestors. It has two values that work: DENY (no site) and SAMEORIGIN (only your site).

X-Frame-Options: DENY

The cheat sheet warns about two things:

  • ALLOW-FROM no longer works in modern browsers. If you rely on it, there is no protection at all in those browsers.

  • When both headers are there, the CSP spec says X-Frame-Options must be ignored, so frame-ancestors decides. Make them say the same thing.

Why is a meta tag not enough?

frame-ancestors and X-Frame-Options have to come as a header in the server response. The cheat sheet is clear that a meta tag does not work for them, so if you only put them in the HTML, your page still opens inside an iframe.

The same cheat sheet has another defense: SameSite on the session cookie, so it is not sent with a request from an iframe on another site. It is an extra layer, not a replacement for the header.

Where do you start now?

The lesson for you as a developer: the browser does not know whether your page may be framed until you tell it.

  1. Open the developer tools on your account page and look at the server response in the Network tab.

  2. If there is no frame-ancestors, add it to every page, with 'none' if you have no reason for an iframe.

  3. Add X-Frame-Options: DENY next to it for older browsers.

If you already have a CSP and worry about breaking something, the CSP Report-Only article shows how to test it first. The session cookie has its own check: the session cookie flags article. And if you want someone to test your whole platform, there is Muhassan.

Your account page: what comes back from it in the headers right now?

FAQ

Is X-Frame-Options on its own enough?

It protects in browsers that support it, but it is better to add CSP frame-ancestors too. When both are there, a modern browser follows frame-ancestors.

Can I put frame-ancestors in a meta tag on the page?

No. It has to be a header in the server response. A meta tag does not work for frame-ancestors or for X-Frame-Options.

What if I want a partner site to put my page in an iframe?

You add its origin to frame-ancestors next to self, and every other site is blocked. Do not use ALLOW-FROM, because it no longer works in modern browsers.

Share

More articles