$ cat docs/middleware/helmet.md
HelmetHelmet
Add a conservative set of security headers to matched responses, without overwriting values the app already set.Adicione um conjunto conservador de headers de segurança às respostas casadas, sem sobrescrever valores que a app já definiu.
What it doesO que faz
helmet(options?: HelmetOptions): MiddlewareHandler. Adds security headers and does not overwrite values
the app already set (only an explicit contentSecurityPolicy option overrides the app's CSP).
helmet(options?: HelmetOptions): MiddlewareHandler. Adiciona headers de segurança e não sobrescreve
valores já definidos pela app (só a opção explícita contentSecurityPolicy sobrescreve a CSP da app).
Install and orderInstalar e ordenar
pnpm add @tupik/middleware
Register it with app.use(helmet()) before routes so every matched response gets the headers. Set HSTS
only when the public site is HTTPS; hsts: true uses max-age=15552000 and
includeSubDomains.
Registre com app.use(helmet()) antes das rotas para que toda resposta casada receba os headers. Defina
HSTS só quando o site público for HTTPS; hsts: true usa max-age=15552000 e
includeSubDomains.
ExampleExemplo
import { helmet } from '@tupik/middleware';
app.use(helmet({
hsts: { maxAge: 31_536_000, includeSubDomains: true, preload: false }, // opt-in
contentSecurityPolicy: "default-src 'self'", // explicit value overrides the app's
referrerPolicy: 'strict-origin-when-cross-origin',
}));import { helmet } from '@tupik/middleware';
app.use(helmet({
hsts: { maxAge: 31_536_000, includeSubDomains: true, preload: false }, // opcional
contentSecurityPolicy: "default-src 'self'", // valor explícito sobrescreve o da app
referrerPolicy: 'strict-origin-when-cross-origin',
})); APIAPI
| Header | Default valueValor padrão |
|---|---|
x-content-type-options | nosniff |
x-frame-options | DENY |
referrer-policy | no-referrer (referrerPolicy) |
cross-origin-opener-policy | same-origin |
cross-origin-resource-policy | same-origin |
permissions-policy | camera=(), microphone=(), geolocation=() |
content-security-policy | default-src 'none'; frame-ancestors 'none' (contentSecurityPolicy, false = off) |
strict-transport-security | off; hsts: true = max-age=15552000; includeSubDomainsdesligado; hsts: true = max-age=15552000; includeSubDomains |
HelmetOptions = { contentSecurityPolicy?: string | false; hsts?: boolean | HstsOptions; referrerPolicy?: string } · HstsOptions = { maxAge: number; includeSubDomains?: boolean; preload?: boolean }
HelmetOptions = { contentSecurityPolicy?: string | false; hsts?: boolean | HstsOptions; referrerPolicy?: string } · HstsOptions = { maxAge: number; includeSubDomains?: boolean; preload?: boolean }
Errors and limitsErros e limites
- helmet does not replace application policy: it only sets defaults, so CSP, CORS and authorization still need deliberate choices.
- Headers do not reach engine-generated 404, 413 or validation errors; add them in
onError/onSendif needed. - A strict CSP can block inline scripts and third-party frames; test the policy before enabling it in production.
- helmet não substitui a política da aplicação: ele só define padrões, então CSP, CORS e autorização ainda exigem escolhas deliberadas.
- Os headers não chegam a 404, 413 ou erros de validação gerados pelo engine; adicione-os em
onError/onSendse precisar. - Uma CSP estrita pode bloquear scripts inline e frames de terceiros; teste a política antes de ativá-la em produção.
When not to use itQuando não usar
If a CDN or reverse proxy already sets the same headers, choose one owner to avoid duplicated or conflicting values. Do not rely on helmet for authentication, input validation or transport security on its own.
Se um CDN ou proxy reverso já define os mesmos headers, escolha um único dono para evitar valores duplicados ou conflitantes. Não conte com o helmet para autenticação, validação de entrada ou segurança de transporte sozinho.