API Security HTTP Security Headers
HTTP headers carry instructions that travel with every response. Security headers tell browsers and clients how to treat the data they receive. A few well-chosen headers close entire groups of attacks at almost no cost.
Layman's Explanation
A shipping label on a parcel says "Fragile", "This side up", or "Keep dry". The courier follows those notes without opening the box. Security headers act as labels on API responses. They tell the browser, "Use secure connections only", "Do not guess the file type", and "Do not store this reply."
Essential Headers
| Header | Purpose | Typical Value |
|---|---|---|
| Strict-Transport-Security | Forces HTTPS for future visits | max-age=31536000; includeSubDomains |
| X-Content-Type-Options | Stops the browser from guessing content types | nosniff |
| Cache-Control | Prevents storage of private responses | no-store |
| Content-Security-Policy | Limits what content a page may load | default-src 'none' |
| X-Frame-Options | Blocks embedding inside other pages | DENY |
| Referrer-Policy | Controls how much address data leaves with links | no-referrer |
| Content-Type | Declares the real data format | application/json |
Understanding Each Header
Strict-Transport-Security
After one secure visit, the browser refuses plain HTTP for the stated period. Attackers lose the chance to downgrade a connection.
X-Content-Type-Options
Browsers sometimes inspect a response and decide it looks like a script. The nosniff value removes that guessing and forces the browser to trust the declared type.
Cache-Control
Responses with account details, tokens, or personal records must never sit in shared caches or browser history. The no-store value keeps them out.
Content-Security-Policy
JSON APIs rarely need scripts, images, or frames. A strict policy that blocks everything leaves nothing for an attacker to inject.
Content-Type
Always send the correct type and character set. A JSON reply labeled as HTML invites misuse.
Headers to Remove
Some servers announce their software name and version in headers such as Server and X-Powered-By. Those labels help attackers match your stack to known weaknesses. Strip or genericize them at the gateway or web server.
Header Protection Layers
API Response
+-----------------------------------------------+
| Strict-Transport-Security --> locks HTTPS |
| X-Content-Type-Options --> fixes type |
| Cache-Control: no-store --> blocks storing |
| Content-Security-Policy --> blocks content |
| Referrer-Policy --> limits leaks |
+-----------------------------------------------+
| { "status": "ok", "data": ... } |
+-----------------------------------------------+
|
v
Browser applies each instruction
Where to Set Headers
- API gateway sets one policy for every service
- Web server or reverse proxy adds headers globally
- Application middleware covers local development and special routes
Central placement avoids gaps. A new endpoint inherits the policy automatically.
Verification Steps
- Call each endpoint and read the response headers
- Confirm that error responses carry the same headers as successful ones
- Use an online header scanner or a command-line request to compare results
- Add an automated test that fails the build when a required header is missing
Common Mistakes
- Setting headers on the home page only
- Forgetting headers on 404 and 500 responses
- Using a very short Strict-Transport-Security period
- Caching authenticated responses on a CDN
Headers for Special Cases
Permissions-Policy
This header disables browser features such as camera, microphone, and location. APIs that return pages or documents rarely need them, so switching them off reduces exposure.
Cross-Origin Resource Policy
This header tells browsers which origins may load a resource. Setting it to same-origin stops other websites from embedding your responses.
Clear-Site-Data
Logout endpoints can send this header to wipe cached data, cookies, and storage for the site. Users on shared computers benefit most.
Rollout Advice
Start with low-risk headers and watch for broken features. Raise the Strict-Transport-Security period in steps, from a day to a week to a year, so a mistake never locks users out for months.
