How to Use the X-frame-options Header to Protect Against Frame-based Attacks

Frame-based attacks, such as clickjacking, pose significant security risks to websites. Attackers can embed your site within frames on malicious pages, tricking users into unintended actions. Implementing the X-Frame-Options header is an effective way to prevent this type of attack.

What is the X-Frame-Options Header?

The X-Frame-Options HTTP response header tells browsers whether a page can be displayed within a frame or iframe. It helps protect your site from clickjacking by restricting framing.

How to Use X-Frame-Options

To utilize the X-Frame-Options header, you need to configure your web server. This configuration varies depending on your server type.

For Apache Servers

Add the following line to your .htaccess file or your site’s configuration:

Header always append X-Frame-Options DENY

For Nginx Servers

Include this line in your server block configuration:

add_header X-Frame-Options "DENY";

Options for X-Frame-Options

  • DENY: Prevents any domain from framing your site.
  • SAMEORIGIN: Allows only pages from the same origin to frame your site.

Alternative: Content Security Policy (CSP)

While X-Frame-Options is effective, modern browsers support the Content Security Policy (CSP) frame-ancestors directive, offering more flexibility. Example:

Content-Security-Policy: frame-ancestors 'self';

Conclusion

Protecting your website from frame-based attacks is crucial. Configuring the X-Frame-Options header is a straightforward and effective method. Remember to test your settings across browsers to ensure your site remains secure and functional.