X-Frame-Options Header
What you'll learn: How to use the X-Frame-Options HTTP header to prevent your web pages from being embedded in frames on untrusted domains, blocking clickjacking attacks.
After understanding how clickjacking and UI redressing work, you need a defense mechanism. The X-Frame-Options header is your first line of defense—a simple HTTP response header that tells browsers whether your page can be displayed in a frame, iframe, or object.
How X-Frame-Options Works
Think of X-Frame-Options like a "No Trespassing" sign for your web page. When a browser receives this header with your page, it checks if the page is being loaded inside a frame. If the framing doesn't meet your specified rules, the browser refuses to render your page in that context.
The header has three possible values:
DENY - Your page cannot be displayed in any frame, regardless of the site attempting to frame it. This is the most restrictive option.
X-Frame-Options: DENY
SAMEORIGIN - Your page can only be framed by pages from the same origin (same scheme, host, and port). This allows you to embed your own pages in frames within your site.
X-Frame-Options: SAMEORIGIN
ALLOW-FROM uri - Your page can be framed only by the specified origin. Note: this directive is obsolete and poorly supported; use Content Security Policy's frame-ancestors directive instead for specific origin allowlists.
When to Use Each Option
Use DENY for sensitive pages like login forms, password resets, or payment pages. Use SAMEORIGIN when your site legitimately uses iframes for its own content but you want to block external framing.
Key Takeaway: The X-Frame-Options header is a browser-enforced protection that prevents your pages from being embedded in frames on untrusted domains, effectively blocking basic clickjacking attacks by controlling the framing context.