Clickjacking
Introduction
Clickjacking is an attack that tricks a user by clicking a web page element which is invisible or disguised as another element. It is performed by displaying an invisible page or HTML element, inside an iframe, on top of the page the user sees. The user believes they are clicking the visible page but in fact they are clicking an invisible element in the additional page transposed on top of it.
How to test
A basic way to test if your site is vulnerable to clickjacking is to create an HTML page and attempt to include a sensitive page from your website in an iframe. It is important to execute the test code on another web server, because this is the typical behavior in a clickjacking attack.
Use code like the following, provided as part of the OWASP Testing Guide:
If the text “Website is vulnerable to clickjacking” appears [as shown below] and below it you see the content of your sensitive page, the page is vulnerable to clickjacking.

Solution
Prevent the browser from loading the page in frame using the X-Frame-Options or Content Security Policy (frame-ancestors) HTTP headers.
Method 1
X-Frame-Options
Parameter Value
Meaning
SAMEORIGIN
Frame/iframe of content is only allowed from the same site origin.
DENY
Prevent any domain to embed your content using frame/iframe.
ALLOW-FROM
Allow framing the content only on particular URI.
PHP
To sent the header in response by this code.
APACHE
For Apache, we have to insert the code in httpd.conf.
NGINX
For nginx, we have to insert the code in nginx.conf.
EXPRESS.JS
For Express.js, we have to insert the code in middleware statement.
Method 2
Content-Security-Policy: Frame-Ancestors
The frame-ancestors directive can be used in a Content-Security-Policy HTTP response header to indicate whether or not a browser should be allowed to render a page in a <frame>
or <iframe>.
Parameter value
Meaning
none
Prevents all domain from framing the content.
self
Allow the current site to frame the content.
*.somesite.com
Allow any page on somesite.com (using any protocol)
https://myfriend.site.com
Allow the page myfriend.site.com that uses HTTPS
PHP
To sent the header in response by this code.
APACHE
For Apache, we have to insert the code in httpd.conf.
NGINX
For nginx, we have to insert the code in nginx.conf.
EXPRESS.JS
For Express.js, we have to insert the code in middleware statement.
Reference
Last updated
Was this helpful?