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
+7
View File
@@ -6,6 +6,7 @@ import {
trekDemoToolGate,
trekMcpAccessPolicy,
trekMcpErrorMapper,
trekMcpInputMapper,
trekMcpValidateAccess,
} from '../mcp/nest-mcp-policy';
import mikroOrmConfig from '../mikro-orm.config';
@@ -35,6 +36,7 @@ import { DemoService } from './common/demo.service';
import { IdempotencyCleanupJob } from './common/idempotency-cleanup.job';
import { IdempotencyInterceptor } from './common/idempotency.interceptor';
import { ManagedGuard } from './common/managed.guard';
import { NulFreeInputPipe } from './common/nul-free-input';
import { TrekExceptionFilter } from './common/trek-exception.filter';
import { ZodValidationPipe } from './common/zod-validation.pipe';
import { ConfigModule } from './config/config.module';
@@ -133,6 +135,7 @@ import { APP_FILTER, APP_GUARD, APP_INTERCEPTOR, APP_PIPE } from '@nestjs/core';
useFactory: (demo: DemoService) => trekDemoToolGate((id) => demo.isDemoUserId(id)),
},
errorMapper: trekMcpErrorMapper,
inputMapper: trekMcpInputMapper,
}),
HealthModule,
PlatformModule,
@@ -264,6 +267,10 @@ import { APP_FILTER, APP_GUARD, APP_INTERCEPTOR, APP_PIPE } from '@nestjs/core';
{ provide: APP_INTERCEPTOR, useClass: SessionRenewalInterceptor },
// Its nightly TTL purge — a provider here because common/ has no module.
IdempotencyCleanupJob,
// First of the global pipes: every handler argument without NUL, which
// after the nulFreeRequest middleware only changes what a route parsed
// itself, a multipart body (common/nul-free-input.ts).
{ provide: APP_PIPE, useClass: NulFreeInputPipe },
// Global Zod validation: any parameter typed with a createZodDto class
// (the <domain>.dto.ts wrappers over @trek/shared schemas) is validated;
// everything else passes through untouched. Paired with the boot gate in