Clickjacking

Difficulty: Intermediate

Question

What is clickjacking and how do you prevent your site from being used in a clickjacking attack?

Answer

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.

How the attack works

  1. The attacker builds a page, say a "Claim your free Swiggy coupon" game.
  2. They embed your real app in an <iframe> with opacity: 0 and position it so your "Transfer" or "Allow" button sits exactly under their "Click to claim" button.
  3. The victim is already logged in to your site, so the framed page is fully authenticated.
  4. The click lands on your hidden button and performs a real action with the victim's session.

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.

The headers that stop it

HeaderValuesNotes
Content-Security-Policy: frame-ancestors'none', 'self', list of originsModern standard, supports multiple allowed origins
X-Frame-OptionsDENY, SAMEORIGINLegacy 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.

Gotchas

  • frame-ancestors does not work inside a <meta http-equiv> CSP tag. It must be an HTTP response header.
  • The header has to be on every HTML response, including error pages and login pages, not just the home page. Set it in middleware or at the CDN.
  • JavaScript frame-busting (if (top !== self) top.location = self.location) is unreliable: attackers can use the iframe sandbox attribute to block navigation. Use headers instead.
  • If you have an embeddable widget (a payment button or chat widget), serve it from a separate route or domain with its own relaxed policy, and keep the rest of the app on 'none'.

Extra layers in modern browsers

  • 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.
  • For high-risk actions like UPI payments or account deletion, add a confirmation step that requires typing, an OTP or re-authentication. A single hidden click is no longer enough.
Anatomy of a clickjacking page. The user sees the decoy layer but their click lands on the invisible framed site.
  1. User's mouse click: Aimed at what looks like a game button.
  2. Invisible iframe (opacity 0): Your real, logged-in site sits on top.
  3. Hidden 'Confirm transfer' button: Positioned exactly under the cursor.
  4. Attacker's decoy page: Visible 'Claim coupon' button underneath.

Code examples

Anti-framing headers in Express with Helmet

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.

Nginx configuration

# 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.

Key points

  • Clickjacking hides your real site in an iframe and steals a genuine user click
  • Prefer CSP frame-ancestors; also send X-Frame-Options for legacy browsers
  • frame-ancestors is ignored in meta tags; it must be an HTTP header
  • JavaScript frame-busting is bypassable with the sandbox attribute
  • CSRF tokens do not stop clickjacking because the request is legitimate

Common follow-up questions

  • Why does a CSRF token not protect against clickjacking?
  • How would you allow one partner domain to embed a widget but block everyone else?
  • What happens when X-Frame-Options and frame-ancestors disagree?
  • How do SameSite cookies reduce clickjacking risk?

Interview tips

  • Describe the attack visually: transparent iframe, button under the cursor, user already logged in
  • Recommend frame-ancestors first and explain why X-Frame-Options is still sent
  • Point out the meta-tag limitation and error-page coverage; those details separate a strong answer

Concepts covered

Clickjacking, UI Redressing, X-Frame-Options, CSP frame-ancestors, SameSite Cookies

More Web Security (OWASP, JWT, XSS, CSRF) questions