OWASP Security Headers

HTTP security headers are short instructions that a server sends along with every web page. Browsers read them and switch on built-in protections. A few lines of configuration can block clickjacking, reduce the damage of script injection, and force secure connections.

The Instruction Label Comparison

A package ships with labels such as "Fragile" and "This Side Up." Handlers follow the labels without knowing the contents. Security headers act as labels for the browser: they tell it how to handle the page safely.

Where Headers Travel

 Browser → asks for page → Server
 Browser ← page + headers ← Server

 Response example:
 HTTP/1.1 200 OK
 Content-Type: text/html
 Strict-Transport-Security: max-age=31536000; includeSubDomains
 X-Content-Type-Options: nosniff
 Content-Security-Policy: default-src 'self'

Essential Headers

HeaderJob
Strict-Transport-SecurityForces the browser to use HTTPS for the site
Content-Security-PolicyLimits which sources may load scripts, images, and frames
X-Content-Type-OptionsStops the browser from guessing file types
Referrer-PolicyControls how much address data leaves with outgoing links
Permissions-PolicyTurns off browser features such as camera or location for the page
Cross-Origin-Opener-PolicySeparates your window from windows of other sites

Header Details

Strict-Transport-Security

After the first secure visit, the browser refuses plain HTTP for the stated time. Attackers lose the chance to downgrade a connection.

Content-Security-Policy

A Content Security Policy, or CSP, lists trusted sources. A browser that follows a strict CSP refuses to run injected scripts from unknown places. Start with a report-only mode, review the reports, and then enforce the policy.

Content-Security-Policy: default-src 'self'; img-src 'self' https://images.example.com; frame-ancestors 'none'

X-Content-Type-Options

The value nosniff makes the browser trust the declared file type. Uploaded text files then stay text instead of turning into scripts.

Referrer-Policy

The value strict-origin-when-cross-origin works well for most sites. It shares only the site name when users follow links to other sites.

Clickjacking Defense

Clickjacking hides your page inside an invisible frame on a hostile site. The visitor believes they click a harmless button but actually click a control on your page.

 +-------------------------------+
 |  Attractive fake page         |
 |   +-----------------------+   |
 |   | Invisible frame:      |   |
 |   | your "Delete" button  |   |
 |   +-----------------------+   |
 +-------------------------------+

The frame-ancestors directive in CSP blocks framing by other sites. Older browsers understand X-Frame-Options: DENY or SAMEORIGIN, so many sites send both.

Header Rollout Plan

  1. Inventory your pages and the outside resources they load.
  2. Add the simple headers first: nosniff, referrer policy, and frame protection.
  3. Enable HSTS with a short time and raise it after testing.
  4. Deploy CSP in report-only mode and fix the violations.
  5. Switch CSP to enforcement.
  6. Scan the site with an online header checker or ZAP after each change.

Common Mistakes

  • Allowing unsafe-inline in CSP, which cancels much of its value.
  • Setting headers on the home page and forgetting API and error pages.
  • Sending conflicting headers from both the application and the proxy.
  • Exposing version details in headers such as Server or X-Powered-By.

Checking Headers on Your Site

Open the browser developer tools, select the Network tab, and click the main page request. The response headers list every header the server sent. Compare the list with the essential table above. Free online scanners grade the same list and explain missing items in plain language. Run the check on login pages, error pages, and API responses too.

Key Takeaways

  • Headers switch on browser protections with little code.
  • CSP and HSTS give the largest benefit.
  • Frame rules stop clickjacking.
  • Staged rollout prevents broken pages.