5. Web Security Headers & Browser Security
Strict-Transport-Security (HSTS)
HTTP Strict Transport Security (HSTS) is a security mechanism that forces browsers to communicate with a server exclusively over HTTPS. By sending the Strict-Transport-Security header, the server instructs the browser to never attempt a connection via plain HTTP.
The preload directive is a critical security step. It allows domain owners to submit their site to a master list hardcoded into browsers, ensuring the very first connection is HTTPS, mitigating the risk of the initial plain-text request being intercepted via a man-in-the-middle attack.
You can inspect HSTS headers using OpenSSL to verify a server's configuration without needing a browser.
includeSubDomains and preload flags to ensure complete protection across your infrastructure.
openssl s_client -connect example.com:443 -servername example.com | grep -i Strict-Transport-Security
Content-Security-Policy (CSP)
Content-Security-Policy is an HTTP response header that allows site administrators to declare approved sources of content that browsers are allowed to load. It is the primary defense against Cross-Site Scripting (XSS) and data injection attacks.
A policy defines granular control over scripts, styles, images, and frames. For example, 'script-src self https://trusted.cdn.com' restricts scripts to only those served from your origin or a specific trusted CDN.
The following Python script simulates how you might validate if a provided CSP header contains the unsafe-inline directive, which is generally discouraged.
Content-Security-Policy-Report-Only to test your policy before strictly enforcing it.
def check_csp(csp_header): return 'unsafe-inline' in csp_header header = 'default-src https:; script-src https: unsafe-inline' print(f'Unsafe policy detected: {check_csp(header)}')
X-Frame-Options & Clickjacking Prevention
The X-Frame-Options header informs the browser whether a page should be allowed to be rendered in a <frame>, <iframe>, <embed>, or <object>. This is the primary defense against Clickjacking, where an attacker tricks a user into clicking something different from what the user perceives.
Settings include DENY (no rendering allowed) or SAMEORIGIN (allowed only if the iframe resides on the same origin as the content).
Below is a comparison of headers used to control browser security context.
X-Frame-Options works for older browsers, prioritize modern frame-ancestors directive within CSP.
| Header | Function |
|---|---|
| X-Frame-Options | Prevents site from being embedded |
| X-Content-Type-Options | Disables MIME type sniffing |
X-Content-Type-Options & Permissions-Policy
The 'X-Content-Type-Options: nosniff' header prevents the browser from sniffing the MIME type of a response, forcing it to stick to the content type declared by the server. This prevents attacks where an attacker uploads a malicious file with a spoofed file extension.
Permissions-Policy (formerly Feature-Policy) allows site owners to enable, disable, or modify the behavior of browser features like geolocation, microphone access, or camera usage.
You can capture and inspect these headers in raw packets using tcpdump if you are performing a manual audit.
nosniff for user-uploaded content directories to prevent browser-side execution of malicious file types.
tcpdump -i eth0 -A 'tcp port 80' | grep -E 'X-Content-Type|Permissions-Policy'
Knowledge Check
Ready to test your understanding of 5. Web Security Headers & Browser Security?