X-Frame-Options
What is X-Frame-Options?
The X-Frame-Options HTTP response header is a security mechanism that web developers implement to control whether a browser should be allowed to render a page within a <frame>, <iframe>, <embed>, or <object> element. This header instructs the browser on framing policies, helping to protect users from malicious websites that attempt to embed your content without authorization.
The header supports three primary directives:
- DENY – Prevents the page from being displayed in any frame, regardless of the site attempting to embed it
- SAMEORIGIN – Allows the page to be framed only by pages on the same origin (same domain, protocol, and port)
- ALLOW-FROM uri – Permits framing only from a specified URI (deprecated and not widely supported by modern browsers)
Why is X-Frame-Options Important for Web Security?
The primary purpose of X-Frame-Options is to prevent clickjacking attacks, also known as UI redressing attacks. In a clickjacking attack, a malicious website embeds your legitimate page within an invisible iframe and tricks users into clicking on hidden elements. This can lead to unauthorized actions such as:
- Changing account settings without user knowledge
- Making unauthorized purchases or transfers
- Granting permissions to malicious applications
- Sharing sensitive information unintentionally
According to the OWASP Foundation, clickjacking remains a significant threat vector, making X-Frame-Options an essential security control for web applications.
How to Set X-Frame-Options Header?
Implementing X-Frame-Options varies depending on your web server or application framework. Here are common configuration examples:
Apache Configuration
Add the following line to your .htaccess file or server configuration:
Header always set X-Frame-Options "SAMEORIGIN"
NGINX Configuration
Add this directive to your server block:
add_header X-Frame-Options "DENY";
Application-Level Implementation
Most web frameworks allow you to set response headers programmatically. For example, in Node.js with Express, you might use middleware like helmet to automatically add security headers including X-Frame-Options.
When to Use X-Frame-Options SAMEORIGIN?
The SAMEORIGIN directive is ideal when your application legitimately needs to embed its own pages within frames. Common use cases include:
- Internal dashboards that use iframes to display multiple views
- Single-page applications with frame-based navigation
- Preview functionality within content management systems
- Applications that use iframes for isolated component rendering
If your site never needs to be framed, even by itself, DENY provides the strictest protection.
Which X-Frame-Options Directive Should I Use?
Choosing the right directive depends on your application's requirements:
| Directive | Use Case | Security Level |
|---|---|---|
| DENY | Pages that should never be framed | Highest |
| SAMEORIGIN | Pages that need internal framing only | High |
| ALLOW-FROM | Specific third-party embedding (deprecated) | Limited support |
Modern Alternative: Content-Security-Policy
While X-Frame-Options remains effective, particularly for older browsers, the MDN Web Docs recommend using the Content-Security-Policy (CSP) header with the frame-ancestors directive for more flexible and comprehensive framing control. The CSP approach allows multiple origins and supports wildcard patterns, offering broader protection for modern web applications.
For maximum compatibility, consider implementing both X-Frame-Options and CSP frame-ancestors in your security headers.