Files
TREK/server/src/nest/audit/client-ip.ts
T
Maurice 8e5fdaa877 fix(server): tighten the guard, audit and token paths
Permissions cached an empty map before it had loaded, so a DB error during
the load cached permissive defaults and every later check answered from that
cache - fail-open, and it never expired. The rate limiter grew its per-IP map
forever; the GC the legacy version had was dropped in the fold. Idempotency
did a check-then-insert, so two concurrent duplicates both ran the handler.

Audit rows recorded whatever X-Forwarded-For said, which the client writes,
and the request-body debug logger redacted by exact key match - it missed
refresh tokens, api keys and PKCE verifiers. FORCE_HTTPS built its redirect
from the client's Host header. CSP asked for unsafe-eval on top of the
wasm-unsafe-eval it actually needs.

The OAuth deny path redirected to an unvalidated URI, ws-token minting had
no rate limit at all, and PKCE was skipped when code_verifier was absent
rather than refused. An MCP session resumed with a token whose scopes had
since been reduced kept the capability set it was created with, and the
session cap was checked before the async registration that increments it.

Plugin frame containment was lexical, so a symlink out of the directory was
never noticed, and instance config accepted keys the manifest never
declared.
2026-08-23 11:59:12 +02:00

17 lines
781 B
TypeScript

import { Request } from 'express';
/**
* Pure request helper — no DB, no side effects — kept out of the injectable
* AuditService so getClientIp-only consumers stay plain-function imports
* (same carve-out precedent as files.constants.ts).
*
* It honours `trust proxy` (set in globalMiddleware from TRUST_PROXY, one hop by
* default) rather than reading X-Forwarded-For itself. The leftmost entry of
* that header is whatever the client typed, and this value ends up on the audit
* rows an operator reads after an incident — the one place a caller must not be
* able to dictate. With trust proxy off, req.ip is the socket address anyway.
*/
export function getClientIp(req: Request): string | null {
return req.ip?.trim() || req.socket?.remoteAddress || null;
}