Complete Security Headers

Security headers are the server's instructions to the browser on every response. Set six: Strict-Transport-Security: max-age=31536000; includeSubDomains…

You scan your website with a header checker, and it gets an F. Nothing in the code is wrong, and the page appears fine. All that's missing is a few lines in the response that make the browser treat your page more strictly, to protect your users.

Those lines are security headers: instructions the server attaches to every response, telling the browser how strictly to treat the page. Picture a parcel with handling stickers: "send by the secure route only", "may only contain items from this list", "the contents match the label, don't guess", "don't put it inside another box", "don't write the sender's full address", "don't switch on the camera inside". The courier is the browser: it reads the stickers and follows them. With no stickers, the courier follows its own habits, which are often too loose. The parcel is the response. The six stickers, in order, are HSTS, CSP, nosniff, X-Frame-Options, Referrer-Policy and Permissions-Policy: the six headers in the table below.

The six graded headers

In the Lab, the response from belajarweb.example starts with no security headers at all, so the grader (the checker) gives it an F. The grader checks six things:

Header Value that passes Prevents
Strict-Transport-Security max-age=31536000; includeSubDomains a connection that drops to plain HTTP
Content-Security-Policy default-src 'self' (without 'unsafe-inline') foreign scripts running along (XSS)
X-Frame-Options or CSP frame-ancestors DENY or frame-ancestors 'none' the page being framed by another site (clickjacking)
X-Content-Type-Options nosniff the browser guessing a file's type
Referrer-Policy strict-origin-when-cross-origin or no-referrer the full URL leaking to other sites
Permissions-Policy camera=(), microphone=(), geolocation=() browser features used for no reason

Six passing gets an A, five a B, four a C, three a D, two or fewer an F.

HSTS: always over HTTPS

Without HSTS, someone who types belajarweb.example into the address bar first stops at http://, then gets redirected to https://. That first request isn't encrypted yet, so someone on the same network can divert it. HSTS (HTTP Strict Transport Security) tells the browser to remember: "this site is HTTPS only".

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

Browsers only follow HSTS that arrives over HTTPS. The same header on an http:// response is ignored.

CSP, nosniff and anti-framing

Referrer-Policy and Permissions-Policy

Referrer-Policy controls how much of the page's address is sent in the Referer header (the header's name really is missing one r) when a user clicks a link to another site:

Value What other sites receive
unsafe-url the full URL, path and query included, for example https://belajarweb.example/reset?token=abc
strict-origin-when-cross-origin only the origin, https://belajarweb.example/
no-referrer nothing at all

strict-origin-when-cross-origin is the common balance: your own site still gets the full URL, and other sites get only the origin. From an HTTPS page to an HTTP site, the Referer header is not sent at all. strict-origin-when-cross-origin is also the modern browsers' default, but write it in the header so you don't depend on the default.

Permissions-Policy (formerly Feature-Policy) controls which browser features the page and the iframes inside it may use. camera=() with empty brackets means nobody may use the camera, your own site included. geolocation=(self) means only your own origin may use the location.

Setting them on the server

In Express, one middleware sets them all on every response:

function securityHeaders(req, res, next) {
  res.setHeader("Strict-Transport-Security", "max-age=31536000; includeSubDomains");
  res.setHeader("Content-Security-Policy", "default-src 'self'; frame-ancestors 'none'");
  res.setHeader("X-Content-Type-Options", "nosniff");
  res.setHeader("X-Frame-Options", "DENY");
  res.setHeader("Referrer-Policy", "strict-origin-when-cross-origin");
  res.setHeader("Permissions-Policy", "camera=(), microphone=(), geolocation=()");
  next(); // move on to the next handler
}

app.use(securityHeaders);

In Nginx, write add_header with always, so the header is sent on error responses (404, 500) too:

add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "DENY" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;

Don't rely on the old X-XSS-Protection header. It turned on an old XSS filter in the browser, and modern browsers have removed that filter. If you send it anyway, set its value to 0, and leave XSS protection to CSP. Check the result in DevTools (the Network tab, click a request, the Response Headers part), or with a scanner such as securityheaders.com and Mozilla Observatory.

⚠️ Why is the grade still bad when the headers are written?

TL;DR: Security headers are the server's instructions to the browser on every response. Set six: Strict-Transport-Security: max-age=31536000; includeSubDomains (HTTPS only for a year, subdomains included), Content-Security-Policy without 'unsafe-inline' (limits where scripts come from), X-Frame-Options: DENY or frame-ancestors 'none' (no framing by other sites), X-Content-Type-Options: nosniff (the browser doesn't guess file types), Referrer-Policy: strict-origin-when-cross-origin (controls the page address sent to other sites), and Permissions-Policy: camera=(), microphone=() (controls browser features). In Express, use res.setHeader in one middleware and then call next(). In Nginx, use add_header ... always. X-XSS-Protection is obsolete: if you send it, its value is 0. Send them as HTTP headers, because most of these headers don't work through <meta>.

⚡ Instructions on every response

Meaning: Security headers tell the browser how strictly to treat a page. The grader in the Lab checks six: HSTS (always HTTPS), CSP (which scripts may load), nosniff (don't guess file types), anti-framing (don't appear in another site's iframe), Referrer-Policy, and Permissions-Policy. Six passing gets an A, two or fewer an F.

⚡ HSTS: always over HTTPS

Write:

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

Meaning: The browser remembers for 365 days to always use HTTPS, every subdomain included. HSTS that arrives over http:// is ignored.

⚡ Which browser features may be used

Guess first: the Permissions-Policy header (formerly Feature-Policy) holds values like camera=(). What is it for?

Controlling which browser features the page and the iframes inside it may use. camera=() means nobody may use the camera.

Meaning: Another graded header is Referrer-Policy, which controls the page address sent to other sites. With the value strict-origin-when-cross-origin, your own site gets the full URL, and other sites get only the origin.

⚡ One middleware for all of them

Write:

res.setHeader("X-Content-Type-Options", "nosniff");
res.setHeader("Permissions-Policy", "camera=(), microphone=(), geolocation=()");
next(); // move on to the next handler

Meaning: In Express, this middleware attaches headers to every response with res.setHeader. next() must be called, or the request hangs and is never answered. In Nginx, write add_header with always, so the headers are sent on error responses too.

What you will learn