$ cat docs/middleware/ip-allow.md
IP allowlistLista de IPs permitidos
Restrict an admin area or a callback to known networks.Restrinja uma área de admin ou um callback a redes conhecidas.
What it doesO que faz
ipAllow(options: IpAllowOptions): MiddlewareHandler. It parses every entry at creation (10.0.0.0/8, 2001:db8::/32, single addresses; IPv4-mapped IPv6 counts as IPv4) and lets through only clients whose IP from clientIp() falls in one of them. Anything else, including an unknown IP, gets 403 IP_FORBIDDEN.
ipAllow(options: IpAllowOptions): MiddlewareHandler. Ele lê todas as entradas na criação (10.0.0.0/8, 2001:db8::/32, endereços únicos; IPv6 mapeado de IPv4 conta como IPv4) e só deixa passar clientes cujo IP do clientIp() caia em uma delas. Todo o resto, inclusive IP desconhecido, recebe 403 IP_FORBIDDEN.
Install and orderInstalar e ordenar
pnpm add @tupik/middleware Register clientIp() first; without it, ipAllow throws on the request (fail closed, 500).
Registre o clientIp() antes; sem ele, o ipAllow lança na requisição (falha fechada, 500).
ExampleExemplo
import { createModule } from '@tupik/core';
import { clientIp, ipAllow } from '@tupik/middleware';
const admin = createModule('/admin')
.use(clientIp({ trust: 'cloudflare' }))
.use(ipAllow({ allow: ['203.0.113.0/24', '2001:db8:1::/48'] }))
.get('/', { access: 'authenticated', handler: () => ({ ok: true }) }); APIAPI
| OptionOpção | TypeTipo | Default / behaviorPadrão / comportamento |
|---|---|---|
allow | readonly string[] | Required, non-empty. Addresses or CIDR ranges, IPv4 and IPv6.Obrigatório, não vazio. Endereços ou faixas CIDR, IPv4 e IPv6. |
interface IpAllowOptions { allow: readonly string[] }
interface IpAllowOptions { allow: readonly string[] }
Errors and limitsErros e limites
- An empty list, an invalid address or a prefix out of range throws at creation.
- Unknown or unparsable client IP: 403.
- Lista vazia, endereço inválido ou prefixo fora da faixa lança na criação.
- IP do cliente desconhecido ou ilegível: 403.
When not to use itQuando não usar
An IP is not an identity: shared NATs, VPNs and mobile networks change it. Use it as an extra layer next to authentication, never instead of it.
Um IP não é identidade: NATs compartilhados, VPNs e redes móveis mudam. Use como camada extra junto da autenticação, nunca no lugar dela.