Someone shares a "Claim Prize" link in a WhatsApp group. You open it and click the big button, but nothing seems to happen. Later you notice a transfer you never confirmed. The button you clicked wasn't the button you saw. And the one who could have stopped it isn't you as a user, it's the developer of the bank's site.
This trick is called Clickjacking (sometimes called UI redressing): you're tricked into clicking something different from what you see. The method: the real page, for example the bank's page, is hidden inside a transparent frame and placed exactly on top of a decoy button. A frame is a box inside a page that shows another page. In HTML, that box is made with the iframe tag. Picture a clear sheet of glass stuck over a poster: you think you're pressing a button on the poster, but your finger lands on the glass covering it. The poster is the decoy page you see, and the glass is the transparent frame holding the bank's page, which receives your click.
How the trap is built
In the Lab, all of this happens inside the Lab's own canvas. There is a "decoy page" and a "bank page", and both belong to the Lab, not to real sites. The decoy page has a big "Claim Prize" button. On top of that button, an iframe loads a "Confirm Transfer" button from the bank page.
This is the CSS for that iframe. Three parts of it build the trap:
.trap {
position: absolute; /* out of the flow, can be placed anywhere */
top: 0; left: 0;
width: 100%; height: 100%;
opacity: 0; /* transparent: the iframe can't be seen */
z-index: 10; /* above the decoy button */
}
position: absolutewithtop/leftplaces the iframe exactly over the decoy button.opacity: 0makes the iframe see-through. Its contents are still there and still clickable, just invisible.z-index: 10puts it on the topmost layer, so a click hits the iframe first, not the button beneath it.
When you click the "Claim Prize" button, your click actually lands on the "Confirm Transfer" button in the transparent iframe. That's what "jacking" (hijacking) means here: your click is taken somewhere else. You're logged in to the bank. If the bank's login cookie uses SameSite=None, that cookie is sent inside the iframe too, so the bank handles that click as though you pressed it yourself. A Lax or Strict cookie isn't sent in another site's iframe, so the bank doesn't recognise you.
The variants share one pattern, with different targets:
- like/follow jacking: liking or following the attacker's account without knowing it;
- payment jacking: clicking a hidden pay button;
- permission jacking: clicking "Allow" on a camera or microphone permission dialog.
Two headers that refuse to be framed
Clickjacking needs one condition: the victim page may be loaded inside another site's iframe. The defence removes that condition: the page is forbidden from being framed (loaded inside a frame). You do it with two headers, sent by the server with the page:
# The old way, still supported by every browser
X-Frame-Options: DENY
# The modern way, through Content-Security-Policy
Content-Security-Policy: frame-ancestors 'none'
X-Frame-Options has two values in use:
DENY: the page may not be framed by anyone, the site itself included.SAMEORIGIN: only a page from the same origin (the same protocol, domain and port) may frame it.
frame-ancestors in CSP is more flexible and replaces X-Frame-Options:
frame-ancestors 'none': the same asDENY.frame-ancestors 'self': the same asSAMEORIGIN.frame-ancestors https://partner.example: only that origin may frame it.
If bank.example sends one of these headers, the browser refuses to show the bank's page inside the iframe. The frame is blank, and the trap fails.
The difference matters: X-Frame-Options can only forbid every site (DENY) or allow only your own origin (SAMEORIGIN), while frame-ancestors can name a few specific origins. Set both so you are safe on old browsers too. When both are sent, frame-ancestors wins: a browser that supports it ignores X-Frame-Options. Note: both headers are sent by the protected page (bank.example), not by the attacker's page. The attacker won't set them.
Setting the header on the server
On an Express server (a server framework for JavaScript), one middleware sets both headers on every response:
app.use((req, res, next) => {
res.setHeader("X-Frame-Options", "DENY");
res.setHeader("Content-Security-Policy", "frame-ancestors 'none'");
next();
});
res.setHeader("X-Frame-Options", "DENY")forbids the page from being framed in any browser.- The
frame-ancestors 'none'line does the same through CSP, for modern browsers. next()passes the request to the next handler (the code that handles the request). Because this middleware is added withapp.usebefore any other handler, every page gets this protection.
💡 Frame-busting JavaScript as a fallback
If for some reason the header can't be set, there's a fallback on the client side (the browser) called frame-busting: a script that forces the page out of the frame. The page checks whether it's inside a frame:
if (window.self !== window.top) {
// This page is being framed: break out.
window.top.location = window.self.location;
}
window.selfis this page's window (the window the page runs in);window.topis the outermost window. If they differ, this page is inside a frame.- The line inside forces the outermost window to go to this page's address, so this page fills the whole tab.
This is only a fallback, because an attacker can bypass it. The header is far stronger: it's the browser itself that refuses to frame, not a script that may not get the chance to run.
⚠️ Why can your page still be framed?
- Forgetting the header altogether. Without
X-Frame-Optionsorframe-ancestors, the page may be framed by anyone. That's the vulnerable default. - Setting the header on the attacker's page. The header goes on the page you protect, not on the page that frames it. You have no control over the attacker's page.
- Thinking transparent means inactive. An iframe with
opacity: 0still receives clicks. Transparent is about being seen or not, not about being active or not. - Relying on frame-busting alone. A script can be bypassed. Make the header the main defence, and the script only a fallback.
TL;DR: Clickjacking hides the victim page in a transparent iframe (a box that shows another page) with opacity: 0 and a high z-index, over a decoy button, so your click lands on a button you can't see. The defence: the protected page sends two headers that forbid it from being framed (loaded inside another site's frame). For every browser: X-Frame-Options: DENY (or SAMEORIGIN, only your own site may frame it). For modern browsers: Content-Security-Policy: frame-ancestors 'none' (or 'self', the same as SAMEORIGIN). frame-ancestors is more flexible because it can name specific origins (sites). Frame-busting JavaScript, a script that forces the page out of a frame, is only a fallback. Don't forget: the header goes on your page, not on the attacker's.
⚡ Clear glass over a poster
Write:
.trap {
position: absolute;
opacity: 0; /* transparent: the iframe can't be seen */
z-index: 10; /* above the decoy button */
}
Guess first: can an iframe with opacity: 0 still be clicked?
Yes. Transparent is about being seen, not about being active. Your click lands on a button inside the iframe, for example a transfer button on the bank's page, that you can't see.
Meaning: Clickjacking hides the victim page in a transparent iframe (a box that shows another page) over a decoy button. It's like clear glass over a poster: you see the decoy button on the poster, but your click hits the glass, which is the victim page. .trap is the iframe's class: opacity: 0 makes it invisible, and z-index: 10 puts it above the decoy button.
⚡ Two headers that refuse to be framed
Write:
X-Frame-Options: DENY
Content-Security-Policy: frame-ancestors 'none'
Meaning: The protected page sends these two headers so that it may not be framed (loaded inside another site's iframe). The browser refuses to show it in the iframe, so the trap fails. The value SAMEORIGIN (for X-Frame-Options) or 'self' (for frame-ancestors) only allows your own origin (site).
⚡ frame-ancestors is more flexible
Meaning: frame-ancestors can name specific origins, such as https://partner.example, and wins over X-Frame-Options when both headers are sent. The header goes on the page you protect, not on the attacker's page.
⚡ Frame-busting is only a fallback
Meaning: Frame-busting is a script that checks window.self !== window.top (whether this page is inside a frame), then forces the page out of the frame. A script like this can be bypassed. The two headers are far stronger, because it's the browser itself that refuses to frame.