Ultimate Guide to Preventing Clickjacking with X-Frame-Options and Content Security Policy (CSP)
Clickjacking is a deceptive web-based attack where users are tricked into clicking on something different from what they perceive. Typically, a malicious actor embeds a legitimate website inside a transparent or hidden iframe to perform unauthorized actions such as fund transfers, form submissions, or credential theft.
One of the most effective methods to counter this threat is by setting the X-Frame-Options HTTP header or using a more flexible Content Security Policy (CSP). This comprehensive guide will walk you through everything you need to know to defend your website from clickjacking attacks.
📑 Table of Contents
-
What is Clickjacking?
-
How Clickjacking Attacks Work
-
Understanding the X-Frame-Options Header
-
Configuring X-Frame-Options in Apache
-
Configuring X-Frame-Options in Nginx
-
Setting X-Frame-Options in PHP
-
Alternative to X-Frame-Options: CSP (Content Security Policy)
-
Testing and Verifying Clickjacking Protection
-
Conclusion
🔍 1. What is Clickjacking?
Clickjacking, also known as a "UI redress attack," tricks users into clicking hidden elements of another website. Attackers often layer transparent iframes over legitimate buttons to hijack user actions.
Example Scenario:
-
User clicks what appears to be a “Play” button
-
Actually submits a form or likes a post on another site without knowing
🛠️ 2. How Clickjacking Attacks Work
The attacker embeds your site in an invisible iframe and overlays fake content to capture clicks. This can:
-
Submit forms
-
Change user settings
-
Trigger financial transactions
Basic Attack Flow:
-
Victim visits a malicious or compromised site
-
Site loads your page in a hidden iframe
-
Victim interacts with UI elements that trigger unintended actions on your site
🧰 3. Understanding the X-Frame-Options Header
The X-Frame-Options HTTP response header helps prevent clickjacking by restricting how your website is embedded in frames.
Supported Values:
-
DENY – Blocks all framing regardless of origin
-
SAMEORIGIN – Allows framing only from the same domain
-
ALLOW-FROM uri – (Deprecated) Allows framing from specific URI (limited browser support)
This header is supported by most modern browsers and offers quick protection.
⚙️ 4. Configuring X-Frame-Options in Apache
Edit either the Apache config file or .htaccess
in the web root:
Block all iframe embedding:
Header always set X-Frame-Options "DENY"
Allow same-origin framing only:
Header always set X-Frame-Options "SAMEORIGIN"
Restart Apache:
sudo systemctl restart apache2
🔧 5. Configuring X-Frame-Options in Nginx
Open the Nginx configuration file (usually in /etc/nginx/nginx.conf
or within a site-specific block).
Block all iframe embedding:
add_header X-Frame-Options "DENY";
Allow same-origin only:
add_header X-Frame-Options "SAMEORIGIN";
Restart Nginx:
sudo systemctl restart nginx
🧑💻 6. Setting X-Frame-Options in PHP
For applications where headers are managed in PHP scripts, use this code at the top of your file:
Block all framing:
header('X-Frame-Options: DENY');
Allow same-origin only:
header('X-Frame-Options: SAMEORIGIN');
This is particularly useful in dynamic PHP applications.
🛡️ 7. Alternative to X-Frame-Options: Content Security Policy (CSP)
While X-Frame-Options is effective, it's limited in flexibility. The Content Security Policy (CSP) standard offers better control via the frame-ancestors
directive.
Example CSP Setup:
Apache:
Header always set Content-Security-Policy "frame-ancestors 'self';"
Nginx:
add_header Content-Security-Policy "frame-ancestors 'self';";
With CSP, you can specify multiple trusted domains:
frame-ancestors 'self' https://trusted.com https://partner.site;
🧪 8. Testing and Verifying Clickjacking Protection
✅ Developer Tools (Manual Method)
-
Open browser dev tools (F12)
-
Check HTTP response headers
-
Look for
X-Frame-Options
orContent-Security-Policy
✅ Online Tools
✅ Manual Test Page
Create a test HTML file with an iframe embedding your site:
<iframe src="https://yourwebsite.com" width="600" height="400"></iframe>
If protection is enabled, the iframe won’t load.
✅ 9. Conclusion
Clickjacking is a serious threat that can compromise user trust, data integrity, and security. Implementing X-Frame-Options or CSP frame-ancestors is a crucial line of defense.
Best Practices:
-
Use X-Frame-Options for fast implementation
-
Use CSP for complex and modern policy control
-
Regularly audit your headers using browser tools and online scanners
-
Combine with HTTPS, input sanitization, and secure authentication
🔒 By proactively securing your site against clickjacking, you improve both user safety and your application’s resilience to web-based threats.