Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 38 ยท Insecure Design and Misconfiguration

Security Headers and CORS Configuration

Learn the HTTP security headers every site should send (HSTS, CSP, nosniff, frame-ancestors, Referrer-Policy, Permissions-Policy), see sample configs, and audit responses with a Python checker.

HTTP security headers are instructions your server sends with each response that make the browser enforce extra protections. They are cheap, they work across the whole site, and they are among the easiest misconfiguration fixes. A missing header is rarely a vulnerability alone, but each one removes a class of attack or limits the damage of another bug such as XSS.

This lesson covers each important header, example values, and how to check them. Set headers once in your web server, reverse proxy or a framework middleware so that every response, including error pages, carries them.

The essential headers

Strict-Transport-Security (HSTS)

Tells the browser to use HTTPS only for this host for a period.

TEXT
Strict-Transport-Security: max-age=31536000; includeSubDomains

After the first secure visit the browser refuses plain HTTP, defeating downgrade and cookie-stealing attacks on public Wi-Fi. Add preload only after you are sure every subdomain supports HTTPS, since removal from preload lists is slow.

Content-Security-Policy (CSP)

Restricts where scripts, styles, images, frames and connections may load from. It is the strongest defense-in-depth against XSS.

TEXT
Content-Security-Policy: default-src 'self'; script-src 'self'; object-src 'none';
  base-uri 'none'; frame-ancestors 'none'; form-action 'self'

Tips: avoid 'unsafe-inline' and 'unsafe-eval' for scripts, use nonces or hashes for the few inline scripts you need, start with Content-Security-Policy-Report-Only to see what would break, and send reports to a collector.

X-Content-Type-Options

TEXT
X-Content-Type-Options: nosniff

Stops browsers from guessing a file's type, so an uploaded text file is never run as a script.

Clickjacking protection

TEXT
Content-Security-Policy: frame-ancestors 'none'
X-Frame-Options: DENY

frame-ancestors is the modern form; X-Frame-Options is the older fallback. Use 'self' if you embed your own pages.

Referrer-Policy

TEXT
Referrer-Policy: strict-origin-when-cross-origin

Limits how much of the URL (which may contain ids or tokens) is shared with other sites.

Permissions-Policy

TEXT
Permissions-Policy: camera=(), microphone=(), geolocation=()

Turns off browser features your site does not use, so injected code cannot ask for them.

Cross-origin isolation headers

Cross-Origin-Opener-Policy: same-origin and Cross-Origin-Resource-Policy: same-site add protections against cross-site leaks and certain side-channel attacks. They can affect popups and embeds, so test before enabling.

Cache-Control for private data

TEXT
Cache-Control: no-store

for pages and API responses containing personal or session-specific data.

Headers you should remove or avoid

  • Server: Apache/2.4.41 (Ubuntu) and X-Powered-By: PHP/8.1.2 reveal versions. Hide or genericise them (not a fix by itself, but it removes free reconnaissance).
  • Deprecated X-XSS-Protection: modern guidance is to set it to 0 or omit it, since the old browser filter itself caused problems. Use CSP instead.

Example configuration

Nginx:

NGINX
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Content-Security-Policy "default-src 'self'; frame-ancestors 'none'; object-src 'none'; base-uri 'none'" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
server_tokens off;

The always keyword makes nginx send the header on error responses too.

Auditing headers with code

Python A security-header checker

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

This is a stand-in for online header scanners. The sample responses are invented; in a real project you would feed it headers from your own staging site.

Building a CSP step by step

Python Parse and review a CSP

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Remember that frame-ancestors, base-uri and form-action do not fall back to default-src, so they must be set explicitly.

CORS configuration recap

CORS headers (Access-Control-Allow-Origin and friends) are security-relevant configuration too. Use an exact allow-list of origins, never combine a wildcard or reflected origin with credentials, and send Vary: Origin when the value changes per request. The CSRF and CORS lesson shows the failure patterns.

Cookies are headers too

Review every Set-Cookie: HttpOnly, Secure, SameSite, a sensible Max-Age, and prefixes such as __Host- (requires Secure, Path=/ and no Domain, which blocks subdomain tampering).

Testing

  • Browser developer tools (Network tab) show response headers.
  • Online scanners and command-line tools can grade your site; use them only on your own domains.
  • Add an automated check in CI that fetches your staging site and fails when a required header is missing.
  • Use Content-Security-Policy-Report-Only before enforcing a new policy.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which header makes browsers insist on HTTPS for a site?

    HSTS tells the browser to use only HTTPS for the host for the given max-age.
  2. 2. Which directive is the modern way to prevent clickjacking?

    frame-ancestors controls which pages may embed yours. X-Frame-Options is the older equivalent.
  3. 3. Why use Content-Security-Policy-Report-Only first?

    Report-only mode lets you tune the policy safely before enforcement.
  4. 4. What does X-Content-Type-Options: nosniff prevent?

    nosniff makes the browser trust the declared Content-Type.
  5. 5. Which directive does NOT fall back to default-src?

    frame-ancestors, base-uri and form-action must be set explicitly.

Summary

  • Security headers make browsers enforce extra protections: HSTS, CSP, nosniff, framing rules, Referrer-Policy and Permissions-Policy.
  • A strict CSP without unsafe-inline, using nonces or hashes, is the strongest XSS mitigation after output encoding.
  • Remove version-revealing headers and set Cache-Control: no-store for private pages.
  • Configure headers centrally (server or middleware), include error responses, and audit them automatically.
  • Review cookies and CORS as part of the same configuration pass.

Next: Vulnerable and Outdated Components