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

HeaderPurposeTypical Value
Strict-Transport-SecurityForces HTTPS for future visitsmax-age=31536000; includeSubDomains
X-Content-Type-OptionsStops the browser from guessing content typesnosniff
Cache-ControlPrevents storage of private responsesno-store
Content-Security-PolicyLimits what content a page may loaddefault-src 'none'
X-Frame-OptionsBlocks embedding inside other pagesDENY
Referrer-PolicyControls how much address data leaves with linksno-referrer
Content-TypeDeclares the real data formatapplication/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

  1. Call each endpoint and read the response headers
  2. Confirm that error responses carry the same headers as successful ones
  3. Use an online header scanner or a command-line request to compare results
  4. 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.