↑ ↓ to move · Enter to open · Esc to close

Security

Security and proxies

trustProxy, CORS, request ids, load balancers and misspelled settings.

#trustProxy

Express believes X-Forwarded-For and X-Forwarded-Proto only from proxies you trust. The default trusts nobody, so a client cannot forge its address.

js
export default {
  trustProxy: 1,                       // one reverse proxy in front of the app
  // trustProxy: ['10.0.0.0/8', 'loopback'],   // or name the proxies
};

trustProxy: true is refused at start-up, because it would trust every header from every client. Accepted: a hop count, addresses or subnets, the keywords loopback, linklocal, uniquelocal, { hops }, { proxies }, or a function.

#Cross-origin requests (CORS)

Off by default. A browser app on another origin needs it to call your API:

js
export default {
  security: {
    cors: {
      origin: ['https://app.example.com', /\.example\.com$/],   // '*' (default), origins, RegExps, a function, or a list
      credentials: true,                                       // cookies cross-origin; never together with origin '*'
      paths: ['/api'],                                         // optional: only these URL prefixes
      // methods, allowedHeaders, exposedHeaders, maxAge
    },
  },
};

cors: true is the same as { origin: '*' }, which is right for a public API without cookies. Unsafe settings are refused when the config is read: credentials with '*', origin: true (it would answer every origin), and origins that are not written as https://host (no path, no trailing slash). Preflight requests are answered before the rate limiter and the CSRF check, so they never count against a client. x-request-id is always exposed, so a browser client can quote it in a bug report. Origins you list as strings are also trusted by the CSRF origin check, so an app you allowed to call the API can also post to it.

#Request ids

Every request gets an id (req.id, the x-request-id response header, reqId in log lines), and calls to other services carry it. Who may choose the id is a setting:

js
export default {
  requestId: {
    accept: 'trusted',      // 'trusted' (default) | 'never' | 'always'
    header: 'x-request-id',
    // pattern: /^[\w.-]{8,64}$/,
    // generate: (req) => crypto.randomUUID(),
  },
};

With trusted, an incoming id is used only when the connection comes from a trusted proxy (see trustProxy); otherwise a UUID is generated. An id that is not 8-64 characters of A-Z a-z 0-9 _ . - is always replaced. Signed calls from a known service continue the caller's id (unless accept is 'never'), so one id follows an action through the whole chain.

#Behind a load balancer

Node closes an idle keep-alive connection after 5 seconds, but most load balancers (AWS ALB, nginx upstreams, Cloudflare) keep them for 60 seconds. The balancer then sends a request over a connection that is just being closed and the client sees a sporadic 502. White.js therefore starts with keepAliveTimeout: 65000 and headersTimeout: 66000, and lets you set all three server timeouts:

js
export default {
  server: { keepAliveTimeout: 65_000, headersTimeout: 66_000, requestTimeout: 30_000 },  // ms; null keeps Node's value
};

headersTimeout must be larger than keepAliveTimeout.

#Configuration mistakes

A misspelled setting would be ignored and the default used instead, which is hard to notice. A key that is one or two letters away from a real one is reported at start-up and by white doctor: Unknown config key "plugin": did you mean "plugins"?. Other unknown keys are left alone, because plugins may read their own settings from the config. Invalid values (a port out of range, static.maxAge: 'forever', a cookies.secret that is too short) stop the start-up with every problem listed at once.