🖱️
Security

Clickjacking: protecting your site with X-Frame-Options and CSP

19.11.2026
← All articles

Clickjacking looks like a trivial trick, yet the damage can be real. An attacker builds a page, loads your site inside an <iframe>, makes the frame transparent and places an attractive button on top. The visitor thinks they are clicking "Claim your gift" but actually presses a real button on your site hidden underneath. The browser treats it as a normal click, because it really happens in the visitor's session with their cookies.

What the attack looks like in practice

Picture a Tashkent home appliance shop whose customer account has "Confirm order" and "Change delivery address" buttons. The attacker frames the account page, hides it with opacity: 0 and draws a bright "Get a promo code" button exactly over the confirm button. A customer who is logged in to the shop performs an action they never intended with a single click. No password is stolen and nothing is uploaded to your server; the attack relies purely on the fact that your pages can be displayed inside a page from another domain. That is why the defence is a set of HTTP response headers telling the browser not to render your pages in foreign frames.

X-Frame-Options

The oldest, universally supported option is the X-Frame-Options header. DENY forbids framing entirely, while SAMEORIGIN allows it only for pages on the same scheme, host and port. The old ALLOW-FROM value is not supported by modern browsers, so do not rely on it. SAMEORIGIN suits most sites, because some CMS admin panels frame their own pages; if you have no such need, DENY is stricter.

CSP frame-ancestors

The frame-ancestors directive of Content Security Policy does the same job with more flexibility, since it accepts a list of trusted origins. frame-ancestors 'none' equals DENY and frame-ancestors 'self' matches SAMEORIGIN. When both headers are present, modern browsers follow frame-ancestors and older ones fall back to X-Frame-Options, so sending both is the practical choice. Note that frame-ancestors is ignored in a <meta> tag; it must be a real HTTP header.

Configuring Nginx, Apache and PHP

On a VPS with Nginx, add the headers to the server block; always makes them appear on error pages too:

server {
    server_name example.uz;
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header Content-Security-Policy "frame-ancestors 'self'" always;
}
sudo nginx -t && sudo systemctl reload nginx

On shared hosting with Apache, put this in the site's .htaccess. It needs mod_headers, so wrapping it in IfModule avoids a 500 error if the module is missing:

<IfModule mod_headers.c>
    Header always set X-Frame-Options "SAMEORIGIN"
    Header always set Content-Security-Policy "frame-ancestors 'self'"
</IfModule>

Without server access you can send the headers from PHP before any output:

<?php
header("X-Frame-Options: SAMEORIGIN");
header("Content-Security-Policy: frame-ancestors 'self'");

When your page must be embeddable

Sometimes framing is a genuine requirement. A private clinic may want its online booking form shown on a partner medical portal. X-Frame-Options cannot express that, but CSP can list exact origins while blocking everyone else:

Content-Security-Policy: frame-ancestors 'self' https://portal.misol.uz

Do not combine this with X-Frame-Options: DENY on the same page, or older browsers will block the frame completely. Apply the relaxed policy only to the embeddable page, keep the rest of the site strict and keep the allow list short, without wildcards.

Verifying and avoiding common mistakes

curl -sI https://example.uz | grep -iE "x-frame-options|content-security-policy"

For a hands-on test, create a local HTML file with <iframe src="https://example.uz"> and open it: the frame should stay empty and the console should report that framing was refused. Typical mistakes include protecting only the home page while login and payment pages stay open, using add_header inside a nested Nginx location (which stops inheritance of the outer headers), sending conflicting values from both a CMS plugin and the server, and relying on JavaScript frame-busting scripts that are known to be bypassable. On sayt.uz hosting you can add these headers yourself via .htaccess, and on a VPS the web server configuration is entirely yours.

Related articles

🛡️ Securing a DLE Site: Licensing, the Admin Panel and the uploads Folder 🔐 Adding Passkeys to Your Website: A Practical WebAuthn Guide 🧩 Outdated plugins: the most common reason websites get hacked 🚪 Does Changing the SSH Port Help? Myth vs Reality
🌐 Language
🇺🇿 O'zbek 🇺🇿 Ўзбек 🇷🇺 Русский 🇬🇧 English ✓