Catalog My learning Blog
Application Security for Developers: Secure Coding Course · Phase 4 · Lesson 25 · 3 min read

HTTP Security Headers and Content Security Policy

On this page
  1. Why HTTP security headers matter
  2. Security headers that matter, from helmet's defaults
  3. Content Security Policy blocking XSS
  4. Deploying a nonce-based CSP without breaking your site
  5. Deploying HSTS carefully
  6. Check yourself

Why HTTP security headers matter

Browsers can block injected scripts, refuse to load your site in a hostile frame, insist on HTTPS, and stop guessing file types, but only if you ask. You ask with response headers. They're cheap to add and stop whole classes of attacks, or limit the damage when other defenses fail.

Security headers that matter, from helmet's defaults

The Node helmet middleware sets a sensible baseline. From a real response:

Code
content-security-policy: default-src 'self';base-uri 'self';font-src 'self' https: data:;form-action 'self';
                         frame-ancestors 'self';img-src 'self' data:;object-src 'none';script-src 'self';
                         script-src-attr 'none';style-src 'self' https: 'unsafe-inline';upgrade-insecure-requests
strict-transport-security: max-age=31536000; includeSubDomains
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
referrer-policy: no-referrer
cross-origin-opener-policy: same-origin
x-powered-by: (absent)
HeaderProtects against
Content-Security-PolicyXSS and data injection: controls where scripts, styles, images and frames may come from
Strict-Transport-Security (HSTS)Downgrade to HTTP and SSL stripping; the browser uses HTTPS only for the max-age period
X-Content-Type-Options: nosniffBrowsers treating an uploaded text file as script or HTML
frame-ancestors (CSP) / X-Frame-OptionsClickjacking: your page embedded invisibly in an attacker's frame
Referrer-PolicyLeaking full URLs (with tokens or IDs) to other sites
Cross-Origin-Opener-PolicyCross-window attacks from pages you open or that open you
Permissions-PolicyPages or embedded frames using the camera, microphone or geolocation

Removing X-Powered-By hides which framework you run; minor, but free.

Response headers instruct the browser: CSP restricts script sources, HSTS forces HTTPS, nosniff stops type guessing, and frame-ancestors blocks clickjacking.
Response headers instruct the browser: CSP restricts script sources, HSTS forces HTTPS, nosniff stops type guessing, and frame-ancestors blocks clickjacking.

Content Security Policy blocking XSS

CSP is the most powerful header and the hardest to deploy. A policy without 'unsafe-inline' in script-src stops inline scripts and event-handler attributes, which is how most injected XSS executes.

In the XSS test from Day 9, the same vulnerable innerHTML code ran in Chrome twice. Without CSP the cookie was stolen; with a strict policy:

Config
mode=innerHTML         stolen: session=abc123
mode=innerHTML&csp=2   stolen: (none)   console: Executing inline event handler violates the following Content Security Policy directive...

The bug was still there, but the browser refused to run the injected handler.

Deploying a nonce-based CSP without breaking your site

  1. Start in report-only mode: Content-Security-Policy-Report-Only with a report-to endpoint shows what would be blocked.
  2. Prefer a nonce- or hash-based strict policy over long allow-lists of domains, which are often bypassable through JSONP endpoints or script gadgets on allowed hosts:
YAML
Content-Security-Policy:
  script-src 'nonce-R4nd0mPerRequest' 'strict-dynamic';
  object-src 'none';
  base-uri 'none';
  frame-ancestors 'none'

Each page response includes a fresh random nonce, and only <script nonce="..."> tags with that value run; 'strict-dynamic' lets those trusted scripts load their own dependencies.

  1. Move inline scripts and onclick handlers into files or nonce'd blocks.
  2. Enforce once the reports are clean, and keep monitoring them.

Deploying HSTS carefully

max-age=31536000; includeSubDomains tells browsers to use HTTPS for a year on every subdomain. Make sure every subdomain supports HTTPS first. Adding preload and submitting to the browser preload list protects even the first visit, but is hard to undo.

Check your site with Mozilla's HTTP Observatory or securityheaders.com. The browser security headers course goes deeper into each header.

Check yourself

The browser refuses to run inline scripts and event-handler attributes like onerror, which is how most injected scripts execute. Only scripts from allowed sources or with the right nonce run.

It reports what the policy would block without breaking the site, so you can find and fix legitimate inline scripts and third-party sources before enforcing. Enforcing first usually breaks something and gets the policy reverted.

Users being downgraded to unencrypted HTTP, for example by an attacker on public Wi-Fi intercepting the first plain-HTTP request and stripping the redirect to HTTPS. Once seen, the browser uses HTTPS only for that domain.

It stops browsers from guessing a response's type, so a file served as text can't be executed as a script.

It controls which sites may embed your pages in frames, preventing clickjacking, where a hidden frame tricks users into clicking your buttons. It replaces the older X-Frame-Options header with more flexible rules.