# HTTP headers: security, caching and CORS

With every request and every response, the browser or server sends **headers**: extra lines containing information about the message. You can’t see them on the page, but they determine a great deal: whether your site is secure, whether it loads quickly, and whether your API is accessible from outside.

## View headers

- **Browser:** F12 → Network → click on a request → Headers.
- **Terminal:** `curl -sI https://jouwsite.nl`

## Headers sent by the browser

| Header | Meaning |
|---|---|
| `Accept` | The format the browser requires (`application/json`, `text/html`) |
| `Accept-Language` | Preferred language |
| `Accept-Encoding` | The compression formats the browser supports (gzip, br) |
| `Authorization` | Login details or token (`Bearer …`) |
| `Cookie` | Cookies, such as your session |
| `Origin` / `Referer` | The site from which the request originates |
| `User-Agent` | Which browser or tool |
| `If-None-Match` / `If-Modified-Since` | "I already have a version; is there anything new?" |
| `Range` | Only part of a file |

## Security headers that every website needs

| Header | Recommended | Protects against |
|---|---|---|
| `Strict-Transport-Security` | `max-age=31536000; includeSubDomains` | Unsecured HTTP connections |
| `Content-Security-Policy` | Start with a strict ‘report-only’ policy | Malicious scripts (XSS) |
| `X-Content-Type-Options` | `nosniff` | Files that are executed as something else |
| `X-Frame-Options` | `SAMEORIGIN` | Loading your site in a hidden frame (clickjacking) |
| `Referrer-Policy` | `strict-origin-when-cross-origin` | Leakage of URLs to other sites |
| `Permissions-Policy` | `camera=(), microphone=(), geolocation=()` | Unauthorised use of device functions |

First, set a **Content-Security-Policy** to `Content-Security-Policy-Report-Only` mode. That way, you can see what might break without your site breaking.

You should also remove any headers that reveal which software you’re running, such as `X-Powered-By: PHP/8.3`.

## Setting up cookies securely

A session cookie should have the following characteristics:

- `Secure`: via HTTPS only
- `HttpOnly`: JavaScript cannot read it
- `SameSite=Lax`: not sent by other sites in the event of suspicious requests

In Laravel: `SESSION_SECURE_COOKIE=true` in your `.env` file.

## Caching: speed

| Header | What it does |
|---|---|
| `Cache-Control` | How long something may be retained and by whom |
| `ETag` | A ‘fingerprint’ of the content |
| `Last-Modified` | When the content was last modified |
| `Expires` | Obsolete version of `max-age` |
| `Vary` | The answer varies depending on … (e.g. language or Origin) |

Rules of thumb:

- **CSS/JS files with a hash in their name** (`app.3f9a.js`, as generated by Vite): `public, max-age=31536000, immutable`.
- **HTML pages:** `no-cache` (the browser checks every time to see if there’s anything new; with ETag, this is quick).
- **Personal data:** `private, no-store`.

If the content has not changed, the server responds with **304 Not Modified** and the browser does not need to download anything again.

## CORS: who is allowed to use your API?

By default, a browser blocks requests from site A to an API on domain B. Using **CORS headers**, the API grants permission:

| Header | Meaning |
|---|---|
| `Access-Control-Allow-Origin` | Which site(s) are permitted |
| `Access-Control-Allow-Methods` | Which methods (GET, POST…) |
| `Access-Control-Allow-Headers` | Which headers the request is permitted to contain |
| `Access-Control-Allow-Credentials` | Whether cookies are permitted |
| `Access-Control-Max-Age` | How long the browser retains the consent |

In Laravel, you configure this via `config/cors.php`:

```php
'paths' => ['api/*', 'sanctum/csrf-cookie'],
'allowed_origins' => ['https://app.jouwdomein.nl'],
'supports_credentials' => true,
```

**Never** use `*` in conjunction with logging in or cookies.

## Other headers you may come across

`Content-Type` (response format), `Content-Length` (size), `Location` (redirect destination), `Set-Cookie`, `WWW-Authenticate` (for 401), `Retry-After` and `X-RateLimit-*` (for 429).

## Need help?

Would you like to know whether your site is sending the correct security headers? I’ll check this as part of the **AI code review**. A new, secure site falls under **Web design and development**.

## Skills

- [HTTP headers review] (/nl/skills/http-headers-review)
- [Chrome DevTools Agent] (/nl/skills/chrome-devtools)
- [Laravel OWASP Security] (/nl/skills/laravel-owasp-security)
- [API Design Principles] (/nl/skills/api-design-principles)

## Related services

- [Webdesign en -ontwikkeling] (/nl/services#service-01)
- [Beoordeling van AI-code] (/nl/services#service-05)

