Back to Guides

HTTP Security Headers and How to use them

In the ever-evolving world of cybersecurity, protecting your web applications is paramount. HTTP security headers serve as a crucial first line of defense, instructing browsers on how to behave when interacting with your site. While some headers remain foundational, others have matured or been deprecated. This updated guide details the essential HTTP security headers for today's web applications.

HTTP Security Headers

1. The Cornerstones: Still Highly Relevant and Recommended

These headers are vital for modern web security and should be a priority for all web applications:

  • Content Security Policy (CSP)

    Purpose: CSP is your most powerful tool against Cross-Site Scripting (XSS), clickjacking, and other code injection attacks. It specifies which sources of content (scripts, stylesheets, images, etc.) are trusted and allowed to load on your page.

    Today's Best Practice: Start by implementing CSP in report-only mode (Content-Security-Policy-Report-Only) to log violations without blocking content. This allows you to fine-tune your policy. Once you're confident, switch to enforcement mode (Content-Security-Policy). Leverage directives like nonce or hash for inline scripts/styles and strict-dynamic to simplify policy management while maintaining strong security.

  • Strict-Transport-Security (HSTS)

    Purpose: HSTS forces browsers to interact with your site exclusively over HTTPS, eliminating the need for insecure HTTP redirects. This prevents man-in-the-middle attacks that attempt to downgrade connections to unencrypted HTTP.

    Today's Best Practice: Implement HSTS with a generous max-age (e.g., one year or more) and include the includeSubDomains directive if all your subdomains also use HTTPS. Consider submitting your domain to the HSTS Preload List for immediate HSTS protection, even on the very first visit.

  • X-Content-Type-Options

    Purpose: This header prevents "MIME type sniffing," a browser feature that tries to guess the content type of a file. Without it, an attacker could upload a malicious file (e.g., a JavaScript file disguised as an image) which the browser might then execute.

    Today's Best Practice: Always set this header to X-Content-Type-Options: nosniff. This instructs the browser to strictly adhere to the Content-Type header you send, preventing dangerous MIME sniffing behaviors.

  • Cross-Origin Resource Sharing (CORS)

    Purpose: CORS is a mechanism that uses additional HTTP headers to allow a web application running at one origin to access selected resources from a different origin. It's fundamental for securely managing cross-domain requests.

    Today's Best Practice: Carefully define your Access-Control-Allow-Origin headers to list only the origins that are explicitly allowed to access your resources. Avoid using * (wildcard) for Access-Control-Allow-Origin unless your API is truly public and intended for all origins. Properly handle preflight requests (OPTIONS method) for complex cross-origin requests.

2. Evolved: Still Useful, But With Modern Alternatives

  • X-Frame-Options

    Purpose: This header prevents your page from being loaded in an <iframe>, <frame>, <object>, <embed>, or <applet>, thereby protecting against clickjacking attacks.

    Today's Best Practice: While still effective (especially DENY or SAMEORIGIN), the Content-Security-Policy header with the frame-ancestors directive offers a more flexible and powerful way to control framing. For broader compatibility with older browsers, it's still good practice to include X-Frame-Options in addition to CSP: frame-ancestors.

3. Deprecated: Avoid Using

These headers are no longer recommended due to deprecation or the availability of more robust alternatives:

  • X-XSS-Protection

    Status: Deprecated. This header was an older, browser-specific feature designed to prevent some XSS attacks. However, it's been removed from major browsers like Chrome and Edge and can sometimes introduce new vulnerabilities.

    Replacement: Rely on a strong Content Security Policy (CSP) for comprehensive XSS protection.

  • HTTP Public Key Pinning (HPKP)

    Status: Deprecated. HPKP was intended to prevent man-in-the-middle attacks by forcing browsers to remember (pin) specific cryptographic public keys for a website. However, due to its complexity and the high risk of misconfiguration (which could make a website inaccessible), it was deprecated.

    Replacement: Modern alternatives like Certificate Transparency and robust Certificate Authorities are now the preferred methods for detecting and preventing misissued certificates.

By strategically implementing these updated HTTP security headers, you can significantly enhance the security posture of your web applications. These headers provide essential instructions to web browsers, transforming them into active participants in your defense strategy. Regularly review and update your security configurations to adapt to the dynamic threat landscape and ensure continuous protection for your users and data.

Need help?

chat with an engineer