APIs with Express · Chapter 18 of 29

Security headers

By the end of this chapter you can switch on the browser's own defences with response headers, on every answer your app sends, error pages included, and relax them for one route. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Production
  • 5 tasks
  • Express
  • In the all labs plan

The first 2 chapters of APIs with Express are free. This one comes with the all labs plan.

Why you need this

Bun Maska Co ran a security scanner against Tapri before signing the partner contract, and Meera forwards the report: "Is this bad?" Nothing stops another site from framing the menu page, there is no Content-Security-Policy, and every answer says X-Powered-By: Express.

With what you know, each route would set the same five headers by hand, and Express's own 404 and error pages would still miss them.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    securityHeaders: the basic headers, and no X-Powered-By

    Export securityHeaders(options), which returns a middleware; options may be left out. On every response it sets X-Content-Type-Options: nosniff, X-Frame-Options: DENY and Referrer-Policy: strict-origin-when-cross-origin, removes the X-Powered-By header that Express has already set, and passes the request on. With no csp option it sends no Content-Security-Policy.

    The options: frameOptions (default 'DENY') and referrerPolicy (default 'strict-origin-when-cross-origin') replace those two values, for example 'SAMEORIGIN' and 'no-referrer', and false leaves that header out. nosniff is always sent. csp, a string, is sent as Content-Security-Policy when given ("default-src 'none'" sends exactly that).

  2. Task 2

    buildCsp: a policy from an object of directives

    Export buildCsp(directives), which turns an object of directive name to a list of sources into one Content-Security-Policy string.

    Each directive is its name followed by its sources, separated by single spaces; a directive with an empty list is just its name. Directives are joined by '; ' (a semicolon and a space), in the order of the object, with nothing at the end. An empty object gives ''.

    { 'default-src': ["'self'"], 'img-src': ["'self'", 'data:'], 'upgrade-insecure-requests': [] } gives "default-src 'self'; img-src 'self' data:; upgrade-insecure-requests".

  3. Task 3

    securityHeaders: HSTS over HTTPS only

    securityHeaders takes one more option, hsts: { maxAge, includeSubDomains, preload }, where the last two are optional.

    When hsts is given and the request came over HTTPS (req.secure is true), the middleware sets Strict-Transport-Security to 'max-age=' and the number, then '; includeSubDomains' when that is true, then '; preload' when that is true: { maxAge: 31536000, includeSubDomains: true } gives 'max-age=31536000; includeSubDomains', and { maxAge: 60 } gives 'max-age=60'.

    Over plain HTTP there is no Strict-Transport-Security header, and without the hsts option there is none even over HTTPS. The check runs behind trust proxy and sends X-Forwarded-Proto: https, which Express turns into req.secure.

  4. Task 4

    GET /widget: partner cafés may frame it, nobody else

    In createApp, register securityHeaders before every route, with the CSP built from default-src 'self', img-src 'self' https://images.tapri.in, object-src 'none' and frame-ancestors 'none', which is "default-src 'self'; img-src 'self' https://images.tapri.in; object-src 'none'; frame-ancestors 'none'". GET /menu.html then answers menuPage as HTML with X-Frame-Options DENY, that CSP and no X-Powered-By.

    Add GET /widget. It answers 200 with menuPage as HTML, with no X-Frame-Options and with the CSP "default-src 'self'; frame-ancestors https://kulhadcorner.in https://bunmaska.co" (the cafés from partnerCafes, in that order). It keeps X-Content-Type-Options: nosniff.

  5. Task 5

    createApp: 404 and 500 answers that keep the headers

    In createApp, a path with no route answers 404 with { error: 'Not found' }, and an error answers 500 with { error: 'Something went wrong' }. GET /boom throws new Error('db password is hunter2'), and the word hunter2 must appear nowhere in the answer.

    Both answers keep the headers from task 4: X-Content-Type-Options nosniff, X-Frame-Options DENY, Referrer-Policy strict-origin-when-cross-origin, the main CSP exactly as in task 4, and no X-Powered-By. Express's own 404 and error pages replace the CSP, so they must not answer.

What you will learn

  • The basic headers: X-Content-Type-Options, X-Frame-Options, Referrer-Policy, and removing X-Powered-By.
  • Content-Security-Policy: where scripts, images and frames may come from.
  • HSTS: "use HTTPS from now on", sent only over HTTPS.
  • Every answer: a route that changes a header, and error pages that keep them.

More in Production

Build "Security headers" yourself

Partner cafés want Tapri’s menu online by Friday: you build its API. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of APIs with Express are free. This one comes with the all labs plan.