Security
Security headers and a body size limit are on from the start. The rest is one option away, and every rejection answers in the standard error format.
Security headers
Section titled “Security headers”Helmet sets headers such as Content-Security-Policy, Strict-Transport-Security and X-Content-Type-Options on every response. Configure or turn it off:
kaelum({ security: { helmet: { contentSecurityPolicy: false } } });Off by default: cross-origin access is something to allow on purpose.
kaelum({ security: { cors: { origin: ["https://app.example"], credentials: true } } });cors: true allows any origin. Options: origin, methods, allowedHeaders, exposedHeaders, credentials, maxAge.
Rate limiting
Section titled “Rate limiting”kaelum({ security: { rateLimit: { max: 300, windowMs: 60_000 } } });rateLimit: true allows 100 requests per 15 minutes per IP. Over the limit, the answer is 429 with TOO_MANY_REQUESTS and a Retry-After header. Responses carry RateLimit-Limit, RateLimit-Remaining and RateLimit-Reset.
For a single route, such as a login, use the factory:
import { rateLimit } from "kaelum";
app.post("/login", { use: [rateLimit({ max: 5, windowMs: 60_000 })], handler: login });| Option | Default |
|---|---|
max |
100 per window and client |
windowMs |
15 minutes |
keyGenerator |
req.ip |
skip |
none: a function returning true skips the request |
headers |
true |
statusCode, message |
429 and the standard error body |
store |
in memory, per process |
For apps that use cookies for authentication. Two strategies:
Origin check (strategy: "origin", the default) rejects POST, PUT, PATCH and DELETE requests whose Origin or Referer is not allowed. It needs nothing from the client:
kaelum({ security: { csrf: { origin: "https://app.example" } } });Without origin, the request’s own host is the only allowed origin.
Double-submit token (strategy: "token") sets a csrf-token cookie, and the client must echo it in the X-CSRF-Token header:
kaelum({ security: { csrf: { strategy: "token" } } });// in the browserfetch("/items", { method: "POST", headers: { "X-CSRF-Token": readCookie("csrf-token") },});Exclude paths that are called by other servers, such as webhooks:
kaelum({ security: { csrf: { exclude: ["/webhooks/*"] } } });Failures answer 403 with CSRF_FAILED.
IP restriction
Section titled “IP restriction”kaelum({ security: { ipRestriction: { allow: ["10.0.0.0/8"], deny: ["0.0.0.0/0"] }, },});allow always wins over deny. A request matching neither passes. Rules are IPv4 addresses or CIDR ranges. Denied requests get 403 with IP_NOT_ALLOWED, or your own onDenied(req, res).
To protect only part of the app:
import { ipRestriction } from "kaelum";
app.route("/admin", { use: [ipRestriction({ allow: ["192.168.0.0/16"], deny: ["0.0.0.0/0"] })], get: dashboard,});Body size
Section titled “Body size”Bodies over 100 kb are rejected with 413 before they are read. Adjust with body.limit.
Errors do not leak
Section titled “Errors do not leak”Unexpected errors answer a generic 500. The real error goes to the logs, never to the client. See Errors.