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
max-age=31536000is how long the browser remembers it, in seconds: 31,536,000 seconds = 365 days. A short value likemax-age=300(5 minutes) is forgotten quickly.includeSubDomainsapplies the same rule to every subdomain. Without it,api.belajarweb.exampleisn't forced onto HTTPS.- An extra
preloadis used to add your site to the browsers' built-in HSTS list. The requirements:max-ageof at least a year, andincludeSubDomains.
Browsers only follow HSTS that arrives over HTTPS. The same header on an http:// response is ignored.
CSP, nosniff and anti-framing
- Content-Security-Policy limits where scripts, styles (CSS), images and the rest may load from.
default-src 'self'means only from your own origin, and inline scripts are blocked too. Ifscript-srccontains'unsafe-inline', its main protection against XSS is gone, so the grader marks it yellow. The details are in the Content Security Policy lesson. - X-Content-Type-Options: nosniff tells the browser to trust the server's
Content-Type(the label for the type of content) and not guess. Without that header, if a page loads an uploaded.txtfile as a script, the browser can run its contents even though its label istext/plain. - X-Frame-Options: DENY (the old way) or
frame-ancestors 'none'in CSP (the modern way) forbids the page from being framed by another site's iframe. This is the defence from the Clickjacking lesson.
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);
res.setHeader(name, value)attaches one header to the response.next()must be called. Without it, the request stops in this middleware and is never answered.app.use(securityHeaders)at the very top gives every route the headers. In a real project, thehelmetpackage sets a group of headers like this at once.
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?
- Forgetting
next()in the middleware. The headers are set, but the request hangs and is never answered. - Setting headers through
<meta>. Only CSP and the referrer can go in a meta tag, and CSP in a meta tag ignoresframe-ancestors. HSTS,X-Frame-Options,nosniffandPermissions-Policymust be real HTTP headers. add_headerinside an Nginxlocationblock. Once alocationhas anadd_headerof its own, none of theadd_headerlines from theserverlevel are inherited by thatlocation. Repeat the headers, or keep them in one included file (a file reused in several blocks).- HSTS without
includeSubDomains, or a shortmax-age. Subdomains can still be reached over HTTP, and the browser forgets quickly.
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.