Difficulty: Intermediate
What is clickjacking and how do you prevent your site from being used in a clickjacking attack?
Clickjacking (UI redressing) is an attack where a malicious page loads your site inside a transparent or disguised iframe and tricks the user into clicking something on it, such as a "Confirm payment" or "Delete account" button, while they think they are clicking the attacker's page. The fix is to tell the browser your pages must not be framed by other origins, using Content-Security-Policy: frame-ancestors and, for older browsers, X-Frame-Options.
<iframe> with opacity: 0 and position it so your "Transfer" or "Allow" button sits exactly under their "Click to claim" button.Variants include likejacking (hidden social like buttons), cursorjacking (a fake cursor offset from the real one), and drag-and-drop tricks that extract text. Because the click comes from the real user on your real origin, CSRF tokens do not help.
| Header | Values | Notes |
|---|---|---|
Content-Security-Policy: frame-ancestors | 'none', 'self', list of origins | Modern standard, supports multiple allowed origins |
X-Frame-Options | DENY, SAMEORIGIN | Legacy but still widely sent; ALLOW-FROM is obsolete and ignored by modern browsers |
When both are present, modern browsers enforce frame-ancestors and ignore X-Frame-Options. Sending both is a common belt-and-braces setup. Use 'none' / DENY unless you genuinely need same-origin framing.
frame-ancestors does not work inside a <meta http-equiv> CSP tag. It must be an HTTP response header.if (top !== self) top.location = self.location) is unreliable: attackers can use the iframe sandbox attribute to block navigation. Use headers instead.'none'.SameSite=Lax (the default in Chromium when no attribute is set) or Strict cookies are not sent in cross-site iframes, so the framed page often loads logged out. This helps, but do not rely on it alone; it does not cover every browser or SameSite=None cookies.import express from 'express';
import helmet from 'helmet';
const app = express();
app.use(
helmet({
contentSecurityPolicy: {
directives: {
defaultSrc: ["'self'"],
frameAncestors: ["'none'"], // nobody may frame us
},
},
frameguard: { action: 'deny' }, // X-Frame-Options: DENY for old browsers
})
);
app.get('/', (req, res) => res.send('Dashboard'));
app.listen(3000);
Helmet adds both headers to every response, including error pages, which is where hand-written headers are often forgotten.
# Inside the server block; 'always' also applies to 4xx/5xx responses
add_header Content-Security-Policy "frame-ancestors 'self'" always;
add_header X-Frame-Options "SAMEORIGIN" always;
Use 'self'/SAMEORIGIN only if your own pages need to frame each other; otherwise prefer 'none'/DENY.
Clickjacking, UI Redressing, X-Frame-Options, CSP frame-ancestors, SameSite Cookies