Implementation Guide
A comprehensive guide with code snippets and configuration examples for developers and system administrators.
Clickjacking is caused by allowing third-party websites to embed your site using an iframe. To fix this vulnerability, you need to set HTTP headers that instruct browsers not to allow your website to be iframed.
There are three main solutions to prevent clickjacking:
Traditional header for basic protection
Modern header with granular control
JavaScript to escape iframes
X-Frame-Options is a response header that controls whether a browser should render your page in an iframe. Set it to one of the following values:
No site can load your page in a frame.
X-Frame-Options: DENYOnly same-origin sites can frame your page.
X-Frame-Options: SAMEORIGINNote: The Content-Security-Policy frame-ancestors directive is the modern replacement for X-Frame-Options.
The CSP frame-ancestors directive specifies valid parents that may embed a page using an iframe. This is the recommended modern approach.
No site can frame your page.
Content-Security-Policy: frame-ancestors 'none';Only same-origin can frame your page.
Content-Security-Policy: frame-ancestors 'self';Allow specific trusted domain.
Content-Security-Policy: frame-ancestors https://trusted.com;Use JavaScript to check if your page is embedded in an iframe and escape if necessary. Add this code to your HTML head section.
<style>
html { display: none; }
</style>
<script>
if (self == top) {
document.documentElement.style.display = 'block';
} else {
top.location = self.location;
}
</script>response.setHeader("X-Frame-Options", "DENY");
response.setHeader("Content-Security-Policy", "frame-ancestors 'none'");public void doGet(HttpServletRequest request, HttpServletResponse response) {
response.addHeader("X-Frame-Options", "DENY");
response.addHeader("Content-Security-Policy", "frame-ancestors 'none'");
}<?php
header("X-Frame-Options: DENY");
header("Content-Security-Policy: frame-ancestors 'none'");1. Enable mod_headers
a2enmod headers
2. Add to your VirtualHost config
Header set X-Frame-Options "DENY" Header set Content-Security-Policy "frame-ancestors 'none'"
3. Restart Apache
sudo service apache2 restart
Add to your server block
add_header X-Frame-Options "DENY"; add_header Content-Security-Policy "frame-ancestors 'none'";
Restart Nginx
sudo service nginx restart
Add to wp-config.php
header('X-Frame-Options: SAMEORIGIN');
header("Content-Security-Policy: frame-ancestors 'self'");Verify your clickjacking protection is working correctly.