Clickjacking Defense Cheat Sheet

Revision as of 21:02, 1 December 2012 by Jmanico (talk | contribs) (Clickjacking Defense Introduction)

Jump to: navigation, search

Clickjacking Defense Introduction

This cheat sheet is focused on providing developer guidance on Clickjack/UI Redress attack prevention. For more information on the risk of Clickjacking, please visit this page. For addition references on Clickjacking, please visit this page.

The most popular way to defend against clickjacking is to include some sort of "frame-breaking" functionality which prevents other web pages from framing the site you wish to defend. This cheat sheet will discuss two methods of implementing frame-braking; via X-FRAME-OPTIONS headers (if the browser or browsers in question support it) and via javascript "frame-breaking" code.

Defending with X-FRAME-OPTIONS response headers

The X-Frame-Options HTTP response header can be used to indicate whether or not a browser should be allowed to render a page in a <frame> or <iframe>. Sites can use this to avoid clickjacking attacks, by ensuring that their content is not embedded into other sites.


There are three types of X-FRAME-OPTIONS headers.

  • The first is X-FRAME-OPTIONS DENY, which prevents any domain from framing the content.
  • The second option is X-FRAME-OPTIONS SAMEORIGIN, which only allows the current site to frame the content.
  • The third, is the X-FRAME-OPTIONS ALLOW-FROM 'sitename' header, which permits the specified 'sitename' to frame this page. (e.g., ALLOW-FROM The ALLOW-FROM option is a relatively recent addition (circa 2012) and may not be supported by all browsers yet.

Browser Support

The following browsers support X-FRAME-OPTIONS headers.

Browser Lowest version
Internet Explorer 8.0
Firefox (Gecko) 3.6.9 (
Opera 10.50
Safari 4.0



To implement this protection, you need to add the header to any page that you want to protect from being clickjacked. One way to do this is to add the header manually to every page. A possibly simpler way is to implement a filter that automatically adds the header to every page.

OWASP has an article and some code that provides all the details for implementing this in the Java EE environment.

The SDL blog has posted an article covering how to implement this in a .NET environment.


  • Per-page policy specifcation

The policy needs to be specifed for every page, which can complicate deployment. Providing the ability to enforce it for the entire site, at login time for instance, could simplify adoption.

  • Problems with multi-domain sites

The current implementation does not allow the webmaster to provide a whitelist of domains that are allowed to frame the page. While whitelisting can be dangerous, in some cases a webmaster might have no choice but to use more than one hostname.

  • Proxies

Web proxies are notorious for adding and stripping headers. If a web proxy strips the X-FRAME-OPTIONS header then the site loses its framing protection.

Clickjacking defense for legacy browsers

The following methodology will prevent a webpage from being framed even in legacy browsers.

In the document HEAD element, add the following:

First apply an ID to the style element itself:

<style id="antiClickjack">body{display:none !important;}</style>

And then delete that style by its ID immediately after in the script:

<script type="text/javascript">
   if (self === top) {
       var antiClickjack = document.getElementById("antiClickjack");
   } else {
       top.location = self.location;

This way, everything can be in the document HEAD and you only need one method/taglib in your API.


Other Cheatsheets

OWASP Cheat Sheets Project Homepage