Skip to content
Securityintermediate

What is clickjacking?

Updated Nov 30, 20263 min readBased on official documentation
What is clickjacking?

In short

Clickjacking is an attack where a hidden web page is placed on top of a real one, so a visitor clicks something they did not mean to click. Sites defend against it mainly with the X-Frame-Options response header, which tells browsers whether other sites may display their pages inside a frame.

How the attack works

Clickjacking tricks a person into clicking on a button or link they can see, while the click actually lands on a different page. An attacker loads your site inside an invisible frame on their own page. The frame sits above a fake button or game, so when the visitor clicks, the click goes to your page instead. If the visitor is logged in, the action may run with their real account.

The attack works because browsers let one site embed another site in an iframe by default. Unless a site says otherwise, any other site can display its pages this way.

The defense: X-Frame-Options

The main fix is the X-Frame-Options HTTP response header. Your server sends it along with each page, and the browser reads it before it shows the page. The header tells the browser whether the page may be placed inside a frame, iframe, embed or object element.

The header has two values that you will use in practice:

  • DENY blocks the page from being loaded in any frame, from any site, including your own.
  • SAMEORIGIN allows the page to be framed only when every parent frame comes from the same origin as the page itself.

If the header is missing and your site has no other embedding rule, browsers will allow other sites to embed your pages. Keep in mind that the header must come from the server. Adding the same setting inside a meta tag in HTML has no effect.

Setting the header on your server

Most web servers can add the header with one line of configuration. For Apache, you can add the following to your site configuration:

Header always set X-Frame-Options "SAMEORIGIN"

Nginx uses a similar directive inside the http, server or location block:

add_header X-Frame-Options SAMEORIGIN always;

If your site runs on Express, the Helmet middleware can set the header for you. Helmet is a small package that adds several security headers at once. Use the sameorigin action to keep embedding limited to your own domain.

IIS reads custom headers from the Web.config file, and HAProxy can add the header in its front-end, listen or backend section. The exact syntax depends on your server version, so check the documentation for the software you run.

Choosing between DENY and SAMEORIGIN

Use DENY when your pages should never appear in a frame, such as a login page or a banking form. Use SAMEORIGIN when your own site needs to frame its own pages, for example a dashboard that loads a report from the same domain. Both values stop other sites from embedding your content.

Do not rely on the old ALLOW-FROM value to let a specific third-party site embed your page. Modern browsers ignore that header completely. For that case, use the frame-ancestors directive in the Content-Security-Policy header instead.

Common mistakes

The most common mistake is setting the header in one place but not on every page. Check that static files and error pages also receive it, or set it at the server level so it applies to all responses.

Another mistake is placing the header inside an HTML meta tag. Browsers ignore it there, so your site stays exposed. Always send the header from the server.

Finally, do not assume that a missing header is harmless. If your site has no other frame restriction, browsers will allow embedding by default.

Alternatives and extra protection

The frame-ancestors directive in Content-Security-Policy is the modern replacement. It gives finer control, including a list of allowed parent sites, and it is the recommended choice for new projects. Many teams send both headers during a transition so that older browsers still get protection.

For a wider view of how browsers protect pages, read our guide on HTTP security headers if one is available on the site. Clickjacking is one of several web attacks, and a good setup combines headers with careful design of sensitive actions, such as asking for confirmation before a payment.

FAQ

Is clickjacking only a risk for pages with forms?

No. Any page where a click causes an action can be a target, including settings pages, like buttons and share links. Protecting every page with a frame restriction is the simplest approach.

Does a meta tag with X-Frame-Options protect my site?

No. Browsers only enforce X-Frame-Options when it comes in an HTTP response header. A meta tag with the same setting is ignored, so set the header on your server.

Should I use DENY or SAMEORIGIN?

Use DENY if your page should never be shown in a frame. Use SAMEORIGIN if your own site needs to frame its own pages, such as an internal dashboard.

What replaces ALLOW-FROM?

ALLOW-FROM is obsolete, and modern browsers ignore it. Use the frame-ancestors directive in the Content-Security-Policy header to allow specific sites to embed your page.

Sources

This page is based on the official documentation below. Read it for full detail.

Clickjacking — official documentation ↗
blozhikDeveloper news from across the web, explained. Every story links to its original sources.