Skip to the page

Cookies, CSRF and cross-window attacks · mid

Clickjacking and frame-ancestors

Clickjacking loads your page in an invisible iframe on an attacker's page so the user clicks your buttons without knowing; you stop it with the CSP frame-ancestors directive (or X-Frame-Options), sent as a response header.

The notes

What you must know

  • Attack: evil.example puts bank.example in a transparent iframe over a fake 'Claim prize' button. The click lands on the real 'Confirm transfer' button.
  • Defence: Content-Security-Policy: frame-ancestors 'none' (never framed) or 'self' or a list of partner origins. Browsers that support frame-ancestors ignore X-Frame-Options.
  • X-Frame-Options: DENY or SAMEORIGIN for older browsers. ALLOW-FROM is obsolete and fails open.
  • Both must be HTTP headers; a <meta> tag does not work for either.
  • JS frame-busting (if (top !== self) top.location = self.location) is bypassable (sandboxed iframes, double framing).
  • SameSite=Lax/Strict cookies help when the attack needs the user's session, because the framed page loads without the cookie, but they do not block framing itself.

The red pen

Common traps: mistake → fix

  • Mistake: "Use a frame-busting script"

    Fix: it can be disabled with the iframe sandbox attribute; use headers.

  • Mistake: "X-Frame-Options ALLOW-FROM lets partners embed us"

    Fix: modern browsers ignore it; use frame-ancestors with a list.

Easy to hard

Questions interviewers ask, easy to hard

  1. What is clickjacking and how do you prevent it?
  2. X-Frame-Options vs frame-ancestors: which wins and why keep both?
  3. Our checkout widget must be embeddable by 50 merchants. How do you allow that safely?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview