Clickjacking & UI Attacks
How attackers overlay invisible iframes to steal clicks — and the one-line header fix
Skills taught: websecurity.clickjacking, websecurity.x-frame-options, websecurity.csp-frame-ancestors, websecurity.ui-redressing
How clickjacking works
Clickjacking (UI redressing) tricks users into clicking on something they didn't intend to click. The attacker embeds your site in a transparent iframe on their page, then positions their content underneath your site's buttons. When the user thinks they're clicking 'Close Ad', they're actually clicking 'Confirm Transfer' on your bank site. The attack requires the victim to be logged into your site and visit the attacker's page.
<!-- evil.com embeds bank.com in a transparent iframe -->
<!DOCTYPE html>
<html>
<style>
iframe {
position: absolute;
top: 0; left: 0;
width: 100%; height: 100%;
opacity: 0.0001; /* Nearly invisible — attacker overlays decoy content */
z-index: 2;
}
.decoy {
position: absolute;
top: 300px;
left: 500px;
z-index: 1;
}
</style>
<body>
<!-- The transparent bank iframe is on top -->
<!-- Positioned so the 'Confirm Transfer' button is directly under the decoy -->
<iframe src="https://bank.com/transfer?amount=10000&to=attacker"></iframe>
<!-- User sees this and thinks they're clicking a prize button -->
<button class="decoy">Click here to claim $100!</button>
</body>
</html>
<!-- User clicks what appears to be a prize button -->
<!-- They're actually clicking 'Confirm Transfer' in the iframe -->
<!-- The bank processes the transfer (user is authenticated via session cookie) -->
Defence — frame-busting headers
// METHOD 1 — X-Frame-Options header (older, widely supported)
// DENY: page cannot be embedded anywhere
// SAMEORIGIN: page can only be embedded by pages on the same origin
// ALLOW-FROM https://trusted.com: only this origin can embed (deprecated)
app.use((req, res, next) => {
res.setHeader('X-Frame-Options', 'DENY');
// or
res.setHeader('X-Frame-Options', 'SAMEORIGIN'); // for same-origin iframes
next();
});
// METHOD 2 — CSP frame-ancestors (modern, more flexible, preferred)
// Supersedes X-Frame-Options — set both for backward compatibility
// Deny all iframes:
res.setHeader('Content-Security-Policy', "frame-ancestors 'none'");
// Allow same origin only:
res.setHeader('Content-Security-Policy', "frame-ancestors 'self'");
// Allow specific trusted origins:
res.setHeader(
'Content-Security-Policy',
"frame-ancestors 'self' https://dashboard.myapp.com"
);
// Using helmet (sets both automatically):
import helmet from 'helmet';
app.use(helmet({
frameguard: { action: 'deny' }, // Sets X-Frame-Options: DENY
contentSecurityPolicy: {
directives: {
frameAncestors: ["'none'"], // Sets CSP frame-ancestors 'none'
},
},
}));
DENY everything (most apps):
Login pages, payment flows, account settings, admin panels
Any page where a click has significant consequences
SAMEORIGIN (same-domain embeds):
Documentation embeds your own code examples
Your app embeds its own sub-pages in an iframe
ALLOW specific origins (partner embeds):
A widget meant to be embedded by paying customers
A YouTube-style embed for third parties
Use CSP frame-ancestors 'self' https://trusted-partner.com
NEVER use frame-ancestors 'self' for:
Payment pages
Pages that take irreversible actions
Authentication pages
These should always be DENY or 'none'
Frequently Asked Questions
Does the X-Frame-Options header need to be set on every page?
It should be set globally (on all responses), not just sensitive pages. Attackers can target any page — even a 'safe' page might have a form or link that can be abused. Add it as middleware that applies to all routes. The only pages that legitimately need to be iframeable are widgets you intentionally share for embedding — exclude those specifically.