Security-Related HTTP Response Headers
CREATED: 2024 / 04 / 10 Wed
UPDATED: 2024 / 04 / 10 Wed
Security Response Headers
I’m referring to: Referrer-Policy, Strict-Transport-Security, X-Content-Type-Options, X-XSS-Protection, X-Frame-Options, and Content-Security-Policy. These are elements you must always consider when building a website.
Referrer-Policy
The Referrer request header contains the URI of the page the user visited before arriving at the current page.
For example, if you follow a link in an X post to another site, the Referrer header on that other site will contain X’s URI.
Since the URI can include not just the domain but also path and query parameters, visiting a malicious site set up by an attacker could expose that information via the Referrer.
Referrer-Policy lets you control whether the Referrer is exposed in such cases.
For the available options, see the MDN web docs page.
In short: use no-referrer if you don’t want the Referrer exposed at all when navigating away; origin if the origin alone is acceptable; or the default strict-origin-when-cross-origin (or origin-when-cross-origin) if you want different behavior for same-origin vs cross-origin.
Strict Transport Security
HTTP Strict Transport Security, or HSTS for short. After a page receives this response header, subsequent accesses up to the specified expiry are forced to use HTTPS.
The syntax looks like this, enforcing HTTPS access for the period specified in max-age:
Strict-Transport-Security: max-age=<expire-time>
For details, see here. There’s also an option (includeSubDomains) to require HTTPS for subdomains as well.
However, this enforcement only kicks in after the response header is received, so if the very first request is made over HTTP, request data could be intercepted. HSTS preloading solves this.
HSTS Preload
Registering your site on the HSTS preload list allows it to connect via HTTPS from the very first visit. Register at hstspreload.org.
Getting listed can take a few months.
The header must also include the preload directive:
Strict-Transport-Security: max-age=63072000; includeSubDomains; preload
X-Content-Type-Options
In the past, IE determined how to handle a file by its content rather than its MIME type. X-Content-Type-Options was created to prevent this behavior.
Without it, a file with a .png extension could be interpreted as JavaScript if it contained a JS script, potentially executing it in the browser.
In Tokumaru’s live stream, the following scenario was discussed: on a web service with image upload functionality, if a file that looks like a PNG but contains HTML gets uploaded, the browser might interpret it as HTML — which could lead to XSS.
Currently, X-Content-Type-Options: nosniff suppresses this browser behavior.
Although this originated from IE’s behavior, modern mainstream browsers have inherited it — so X-Content-Type-Options is still worth configuring today.
X-Frame-Options
X-Frame-Options protects against clickjacking attacks (reference).
Clickjacking uses an iframe to overlay a transparent, hidden version of the target site over a trap site, tricking users into performing unintended actions.
For example, imagine a trap site saying “Answer this survey and win a prize!” with the target site overlaid invisibly on top. If the “Submit” button is positioned directly over the target site’s “Delete Account” button, the user unknowingly deletes their own account.
The fix is to prevent the target site from being displayed in an iframe on another site at all.
X-Frame-Options controls whether the page can be displayed in an iframe on another site.
It accepts either SAMEORIGIN or DENY:
X-Frame-Options: DENY
X-Frame-Options: SAMEORIGIN
DENY completely prohibits display in an iframe on another site — use this by default unless there’s a specific reason not to.
SAMEORIGIN allows iframe embedding within the same origin.
X-XSS-Protection
Despite sounding important, X-XSS-Protection can honestly just be disabled.
As noted on MDN web docs, this header is non-standard and can actually introduce vulnerabilities, so it should generally be disabled:
X-XSS-Protection: 0
Because X-XSS-Protection partially rewrites the page when it detects an XSS vulnerability, the page may stop functioning correctly.
If you do enable it, at least use X-XSS-Protection: 1; mode=block to block rendering entirely rather than partially rewriting.
This page introduced by Tokumaru was also a helpful reference.
Still, disabling this and setting Content Security Policy is the better approach.
Content Security Policy
Setting Content Security Policy (CSP) lets you restrict what scripts can run and what URIs can be accessed. This prevents scripts embedded by third parties or requests to URIs planted by attackers from executing.
For example, the following CSP restricts resource loading to the site’s own domain only:
Content-Security-Policy: default-src 'self'
CSP directives are listed here — you can restrict content types like JS, CSS, and iframes.
You can also embed a dynamically generated nonce into scripts returned to the client, so that scripts without the same nonce cannot execute.
Embedding a nonce is not mandatory, but it’s worth setting up if there’s any possibility of injected scripts.
Modern frameworks and libraries often handle some of this behind the scenes, but CSP should always be top of mind for XSS protection.
On Cookie Restrictions
Starting this year, Google has been moving toward restricting cookies, and third-party cookies are set to be deprecated in Chrome as well. They’ve already been removed in Firefox and Safari, and Chrome is finally following suit — apparently causing quite a stir in the advertising world. From a security perspective, though, this is mostly a good thing.
When advertising tags embedded in a site generate third-party cookies to track user behavior across the internet and dynamically adjust ad display, it’s not great for user experience — personally, I find it quite unpleasant. If phasing this out improves security, all the better.
References
- What Are Third-Party Cookies? Impact of Deprecation and 3 Countermeasures
- Cookie Restrictions [2023 Edition]: Japan’s Situation, Impact, and Countermeasures
- What Are Cookie Restrictions? Impact on Digital Marketing and Future Countermeasures