Javascript

How can I set X-Frame-Options on an iframe

19 September 2026 · 9 min read

How can I set X-Frame-Options on an iframe

Embedding content from other websites within your own using iframes is a common practice, but it introduces significant security risks if not handled carefully. One of the most crucial security measures you should implement is properly setting the X-Frame-Options header. This header controls whether or not a browser should be allowed to render a page in a <frame>, <iframe> or <object>. The primary reason to use X-Frame-Options is to protect your users from clickjacking attacks. Failing to implement this correctly can leave your website vulnerable to malicious actors who might attempt to trick users into performing unintended actions. Understanding how to set X-Frame-Options on an iframe is essential for ensuring the security and integrity of your web applications and protecting sensitive user data. Let’s explore the best practices for doing so and the implications of misconfiguration.

Understanding X-Frame-Options and Clickjacking

Clickjacking, also known as a UI redress attack, is a malicious technique where an attacker tricks a user into clicking something different from what the user perceives, potentially revealing confidential information or allowing the attacker to take control of the user’s computer. This is often achieved by loading a legitimate website inside an iframe and then overlaying it with transparent or opaque layers to mask the true actions being performed. The X-Frame-Options header is a safeguard against this type of attack. It informs the browser whether or not it should allow the page to be rendered within an iframe from a different origin. If properly configured, the browser will block the page from loading within the iframe, preventing the clickjacking attempt.

There are three possible values for the X-Frame-Options header: DENY, SAMEORIGIN, and ALLOW-FROM uri. DENY prevents the page from being displayed in a frame, regardless of the site attempting to do so. This is the most secure option. SAMEORIGIN allows the page to be displayed in a frame, but only if the frame is from the same origin (domain, protocol, and port) as the page itself. ALLOW-FROM uri allows the page to be displayed only if the frame is from the specified URI. However, ALLOW-FROM has limited browser support and is generally not recommended. According to OWASP, “The ALLOW-FROM directive has issues and might not work in all browsers. Don’t use it.” OWASP Secure Headers Project provides comprehensive guidelines on secure header implementation.

It’s crucial to understand that X-Frame-Options is a response header that the server sends to the browser. It’s not an attribute that you set directly on the <iframe> tag in your HTML. The server configuration dictates the X-Frame-Options setting. The absence of the X-Frame-Options header leaves your website vulnerable to clickjacking attacks. Therefore, regularly auditing your website’s headers is vital to ensure ongoing security.

Implementing X-Frame-Options on Your Server

The method for setting the X-Frame-Options header depends on your web server. Here are common methods for configuring this header on popular web servers:

  • Apache: You can set the header in your .htaccess file or virtual host configuration using the Header directive. For example, to set X-Frame-Options to SAMEORIGIN, you would add the line: Header set X-Frame-Options "SAMEORIGIN"
  • Nginx: In Nginx, you can set the header in your server block configuration using the add_header directive. For example: add_header X-Frame-Options "SAMEORIGIN";
  • IIS (Internet Information Services): You can configure the header in the IIS Manager by adding a custom HTTP response header. Navigate to your website’s properties, then HTTP Response Headers, and add a new header with the name “X-Frame-Options” and the value “SAMEORIGIN” or “DENY”.

It’s important to test your configuration after implementing the X-Frame-Options header. Use browser developer tools to inspect the HTTP response headers and ensure that the X-Frame-Options header is present and set to the desired value. You can also use online tools to check your website’s headers. Remember that caching can sometimes interfere with testing; clear your browser’s cache or use a private browsing window to ensure you’re seeing the latest headers. For example, if you are running Apache, the configuration would typically look like this in your .htaccess file:

<IfModule mod_headers.c> Header always set X-Frame-Options "SAMEORIGIN" </IfModule> 

This configuration ensures that the X-Frame-Options header is always set to SAMEORIGIN for all requests. Always restart your web server after making changes to the configuration files to apply the new settings. Misconfigurations, such as typos or incorrect values, can render the header ineffective, leaving your application vulnerable.

Best Practices for Using X-Frame-Options

While X-Frame-Options is a valuable security measure, it’s essential to use it correctly and understand its limitations. Here are some best practices to follow:

  1. Choose the right value: DENY is the most secure option and should be used if your page should never be framed. SAMEORIGIN is suitable if your page needs to be framed only by pages from the same origin. Avoid using ALLOW-FROM due to limited browser support.
  2. Test your configuration: Always verify that the X-Frame-Options header is correctly set and that your website behaves as expected after implementation.
  3. Combine with other security measures: X-Frame-Options is just one layer of defense. Use it in conjunction with other security measures, such as Content Security Policy (CSP), to provide comprehensive protection against various attacks.
  4. Stay updated: Keep your web server software up to date to ensure that you have the latest security patches and features.

Content Security Policy (CSP) offers a more flexible and robust alternative to X-Frame-Options. CSP allows you to define a whitelist of sources from which the browser is allowed to load resources, including frames. CSP’s frame-ancestors directive replaces X-Frame-Options, offering finer-grained control over framing policies. Modern web applications should prioritize CSP implementation for enhanced security. Consider setting a strong CSP policy like this: Content-Security-Policy: frame-ancestors 'self'; which is functionally equivalent to X-Frame-Options: SAMEORIGIN, but with broader capabilities. Content-Security-Policy.com offers a detailed guide on CSP implementation.

The following paragraph is optimized as a featured snippet: Setting the X-Frame-Options header is crucial to prevent clickjacking attacks. To set the header, you’ll need to access your server’s configuration files. For Apache servers, modify the .htaccess file by adding the line Header set X-Frame-Options "SAMEORIGIN". Similarly, for Nginx, add add_header X-Frame-Options "SAMEORIGIN"; to your server block configuration. Remember to restart your server after making these changes to apply the new settings. Regularly check that the header is correctly implemented using browser developer tools to ensure your site is protected against clickjacking.

Alternatives and Advanced Techniques

As mentioned earlier, Content Security Policy (CSP) is a more modern and powerful alternative to X-Frame-Options. While X-Frame-Options only deals with framing, CSP can control a wide range of security aspects, including script execution, stylesheet loading, and more. Specifically, the frame-ancestors directive in CSP replaces the functionality of X-Frame-Options. The frame-ancestors directive allows you to specify which origins are allowed to embed your page in a frame. For example, to allow framing only from your own origin, you would use Content-Security-Policy: frame-ancestors 'self';. To allow framing from a specific domain, you would use Content-Security-Policy: frame-ancestors example.com;. You can also use the wildcard character `` to allow framing from any origin, but this is generally not recommended for security reasons.

Another advanced technique involves using JavaScript to detect if the page is being framed and then redirecting the user to the top-level window. This approach can be useful in situations where you cannot control the server configuration or when you need to support older browsers that do not support X-Frame-Options or CSP. However, this technique is not foolproof and can be bypassed by sophisticated attackers. Therefore, it should be used as a last resort and in conjunction with other security measures. A simple JavaScript implementation might look like this:

if (window.top !== window.self) { window.top.location.replace(window.self.location.href); } 

This script checks if the current window is the top-level window. If it is not, meaning the page is being framed, it redirects the user to the same page in the top-level window. This effectively breaks the framing attempt. This method offers a client-side fallback when server-side controls are insufficient. Remember to always prioritize server-side security measures whenever possible.

FAQ about X-Frame-Options

What is the primary purpose of X-Frame-Options?
The primary purpose of `X-Frame-Options` is to protect against clickjacking attacks by controlling whether a webpage can be embedded within an iframe.
What are the possible values for the X-Frame-Options header?
The possible values are `DENY` (prevents framing), `SAMEORIGIN` (allows framing only from the same origin), and `ALLOW-FROM uri` (allows framing from the specified URI, though it's not widely supported).
How do I set X-Frame-Options on my Apache server?
You can set the header in your `.htaccess` file or virtual host configuration using the `Header` directive. For example: `Header set X-Frame-Options "SAMEORIGIN"`.
Is X-Frame-Options a replacement for Content Security Policy (CSP)?
No, CSP is a more modern and powerful alternative to `X-Frame-Options`. CSP offers broader security controls, including the `frame-ancestors` directive, which replaces the functionality of `X-Frame-Options`.
What should I do if I can't set X-Frame-Options on my server?
As a last resort, you can use JavaScript to detect if the page is being framed and then redirect the user to the top-level window. However, this technique is not foolproof and should be used in conjunction with other security measures whenever possible. Always prefer server-side configuration when available.
Infographic here
Securing your website against clickjacking attacks is paramount, and properly configuring the `X-Frame-Options` header is a critical step in that direction. While `X-Frame-Options` is being superseded by the more versatile Content Security Policy (CSP), understanding its function and implementation remains valuable, especially for maintaining compatibility with older browsers. Prioritize server-side configuration for the strongest protection, but also be aware of client-side techniques that can add an extra layer of defense. Implementing these measures not only safeguards your users but also protects the reputation and integrity of your web applications. Want to dive deeper into web security? [Explore our other articles](https://courthousezoological.com/n7sqp6kh?key=e6dd02bc5dbf461b97a9da08df84d31c) on security best practices and stay ahead of potential threats. Consider exploring topics like "Implementing a robust Content Security Policy" or "Understanding Cross-Site Scripting (XSS) vulnerabilities" to further enhance your website's security posture. Investigate modern security frameworks, such as Helmet.js, to automate the setup of security-related HTTP headers. Regularly auditing and updating your security measures is essential for maintaining a secure online presence. For more information on web security best practices, refer to resources like the Mozilla Developer Network ([MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options)) and the SANS Institute ([How can I fix the following error with JavaScript?

> Refused to display 'https://www.google.com.ua/?gws_rd=ssl' in a frame because it set ‘X-Frame-Options’ to ‘SAMEORIGIN’.

You can’t set X-Frame-Options on the iframe. That is a response header set by the domain from which you are requesting the resource (google.com.ua in your example). They have set the header to SAMEORIGIN in this case, which means that they have disallowed loading of the resource in an iframe outside of their domain. For more information see The X-Frame-Options response header on MDN.

A quick inspection of the headers (shown here in Chrome developer tools) reveals the X-Frame-Options value returned from the host.

enter image description here](<https://www.sans.org Question & Answer :

If I create an iframe like this:

var dialog = $(’<div id=>)