fix(db): drop NUL from every request value before any logic runs

The database stores no NUL on either engine, but the application still held
the value a client sent. A key or a check made from it was then a different
value from the one the database compared: `victim@x.test` followed by NUL
was a fresh key for the per-email password-reset throttle while the lookup
found the victim's row, so every spelling mailed a new link and voided the
last one.

Every input now loses its NUL where it enters TREK: the nulFreeRequest
middleware after the body parsers (path, query string, body, cookies), the
first global pipe for what a route parses itself (a multipart body), the
websocket adapter for every frame, and the MCP registry through a new
host-supplied input mapper, since /mcp keeps its raw body. Headers need
nothing: Node refuses a NUL in one with a 400. A string under a key naming a
password stays as sent: it is only ever hashed, and stripping it would lock
out an account whose password was set with one. The stripping in the
database layer stays as the backstop for what does not come from a request.
This commit is contained in:
Maurice
2026-11-17 22:08:45 +01:00
parent fd42d06d55
commit 13e81c04ab
16 changed files with 468 additions and 6 deletions
+13
View File
@@ -1,3 +1,4 @@
import { nulFreeInput } from '../db/nul-free';
import {
demoDenied,
errorResult,
@@ -6,6 +7,7 @@ import {
type McpAccessPolicy,
type McpAccessValidator,
type McpErrorMapper,
type McpInputMapper,
type McpToolGate,
type ToolOptions,
} from '../nest-mcp';
@@ -106,3 +108,14 @@ export function trekDemoToolGate(isDemoUser: (userId: number) => Promise<boolean
*/
export const trekMcpErrorMapper: McpErrorMapper = (err) =>
err instanceof DomainError ? errorResult(err.mcpMessage ?? err.publicMessage) : undefined;
/**
* What a client sends over MCP, without NUL characters, before any tool,
* prompt or resource template reads it: the same rule as every HTTP request
* (`nest/common/nul-free-input.ts`), so a key or a check a tool makes from an
* argument sees the value the database stores. /mcp keeps its raw body for the
* SDK, so the registry is where a tool's input enters TREK. Given to
* McpModule.forRoot (AppModule), which the transport and the in-app assistant
* both attach through.
*/
export const trekMcpInputMapper: McpInputMapper = nulFreeInput;