Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

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

Maurice13e81c04ab4a
diff --git a/server/CLAUDE.md b/server/CLAUDE.md
index f61092987..a67741c0d 100644
--- a/server/CLAUDE.md
+++ b/server/CLAUDE.md
@@ -109,7 +109,7 @@ SQLite is the default engine and Postgres the second (`TREK_DB_URL`, see Engines
- **The new id**: `.returning('id').executeTakeFirstOrThrow()`. Never `InsertResult.insertId` or `last_insert_rowid()`; both are SQLite's rowid and undefined on Postgres.
- **Time**: `currentTimestamp`/`currentTimestampKysely`, `nowMinusDays`, `nowMinusHours`, `nowPlusSeconds`/`nowPlusSecondsKysely`, `nowDateOffset`, `dateOf`, `dateAdd`, `unixEpochToIsoKysely` from `src/db/dialect/sql-functions.ts` (the MikroORM `raw()` helpers) and `src/db/dialect/kysely-functions.ts` (their Kysely-expression twins). Never `datetime('now')`, `strftime` or `julianday`.
- **Search that ignores case**: `ilike(platform, refs)` builds the `(a LIKE ? ESCAPE '\' OR …)` condition, `ILIKE` on Postgres, where `LIKE` is exact. A bare `LIKE` is for a prefix that is exact on both engines (a settings key, a storage path), and says so in a `// like-exact: <reason>` comment (`lint:dialect`).
-- **No NUL in text**: Postgres text cannot hold U+0000, so TREK stores it on neither engine. `src/db/nul-free.ts` drops it where every value leaves for the database (both MikroORM platforms, both Kysely dialects, a plugin's own SQLite file, the move from SQLite), filters included, so nothing above the database layer handles it and the same request leaves the same row on either engine.
+- **No NUL in text**: Postgres text cannot hold U+0000, so TREK stores it on neither engine. `src/db/nul-free.ts` drops it where every value leaves for the database (both MikroORM platforms, both Kysely dialects, a plugin's own SQLite file, the move from SQLite), filters included, so the same request leaves the same row on either engine. A client's input loses it before any logic runs (`src/nest/common/nul-free-input.ts`: the `nulFreeRequest` middleware for the path, query, body and cookies, the first global pipe for a multipart body, the websocket adapter for every frame, `trekMcpInputMapper` for MCP arguments), so a throttle key, a pre-check or a comparison sees the value the database stores; a string under a key naming a password is left as sent, since it is only ever hashed. New code never strips NUL itself.
- **Dates bound as dates**: SQLite rolls an impossible day (`2024-02-30`) over into the next month where Postgres refuses to cast it; a helper that binds a value either engine reads as a date (`dayDistance`) refuses anything `isCalendarDate` does not accept, on both engines, and its caller checks first.
- **Strings and casts**: `concat`/`concatKysely` instead of `||`, `substring`/`substringKysely`, `lower`/`lowerTrim`, `collateNoCase`, `castInteger`/`castIntegerKysely`, and `startsWithIsoDate`/`startsWithIsoDateKysely` instead of `GLOB`.
- **JSON columns** are read whole and parsed in the service; no `json_extract` in SQL. Each one is declared once in `src/db/json-columns.ts` (its Zod shape and the fallback a reader gets for an empty or broken row) and read with `decodeJson`/`decodeJsonResult`, written from a server-built value with `encodeJson` (`src/utils/json-column.ts`), never with a bare `JSON.parse` at the call site: one column, one answer to a broken row, logged.
diff --git a/server/src/bootstrap.ts b/server/src/bootstrap.ts
index d270422fb..80ba2100e 100644
--- a/server/src/bootstrap.ts
+++ b/server/src/bootstrap.ts
@@ -4,6 +4,7 @@ import { applyGlobalMiddleware, routingCspOrigins } from './middleware/globalMid
import { httpConfig } from './nest/app-config';
import { AppModule } from './nest/app.module';
import { apiDocsEnabled } from './nest/common/api-docs.kill-switch';
+import { nulFreeRequest } from './nest/common/nul-free-input';
import { validateBodyContracts } from './nest/common/validate-body-contracts';
import { validateManagedRoutes } from './nest/common/validate-managed-routes';
import { PUBLIC_ROUTE_ALLOW_LIST, validateRouteGuards } from './nest/common/validate-route-guards';
@@ -49,6 +50,7 @@ import nodeHttp from 'node:http';
* 4b. setupApiDocs — Swagger UI/spec at /api/docs* when TREK_API_DOCS_ENABLED.
* 4c. the named body-parser wrappers — the global 100kb JSON/urlencoded
* parsers, exempting /mcp so the MCP SDK reads the raw stream.
+ * 4d. nulFreeRequest — drops NUL from the path, query, body and cookies.
* 5. app.init() — every domain controller, including the transport surface
* that used to live pre-init: /api/health (FeaturesController), OAuth
* discovery documents (DiscoveryController), the /oauth/consent COOP
@@ -228,6 +230,11 @@ export async function buildApp(): Promise<INestApplication> {
instance.use(function urlencodedParser(req: Request, res: Response, next: NextFunction) {
return isMcp(req) ? next() : urlencoded(req, res, next);
});
+ // Every request value without NUL from here on (nest/common/nul-free-input.ts):
+ // the path, the query string, the parsed body and the cookies, before any
+ // guard, interceptor or controller reads them, so a key or a check made from
+ // a value matches what the database stores for it.
+ instance.use(nulFreeRequest);
if (apiDocsEnabled()) setupApiDocs(app);
// The per-request EntityManager fork every Nest route runs inside (D6). This
// used to be @mikro-orm/nestjs's own middleware, which Nest 11 mounts as an
diff --git a/server/src/db/nul-free.ts b/server/src/db/nul-free.ts
index 9305cfbb6..b2a75cd9b 100644
--- a/server/src/db/nul-free.ts
+++ b/server/src/db/nul-free.ts
@@ -31,6 +31,11 @@ import type { AbortableOperationOptions, CompiledQuery, DatabaseConnection, Dial
* A filter is stripped as well as a write, so a lookup finds what the write
* stored. Text that spells the character as the six characters `\u0000`
* (JSON already encoded to a string) is left alone: it holds no NUL.
+ *
+ * What a client sends loses the character earlier still, before any logic
+ * runs ({@link nulFreeInput}), so the application never holds a value the
+ * database would store differently. The stripping here is the backstop for
+ * everything that does not arrive through a request.
*/
const NUL = '\u0000';
@@ -59,6 +64,55 @@ export function nulFreeJson(value: unknown): unknown {
return value;
}
+/** A key whose string value is a password: hashed or handed on as sent, never stored or compared as text. */
+const PASSWORD_KEY = /password/i;
+
+/**
+ * A request's input as the application handles it: every string in it, object
+ * keys included, without NUL. The HTTP layer, the websocket adapter and the
+ * MCP input mapper pass what a client sends through this before any logic runs
+ * (`nest/common/nul-free-input.ts`), so a throttle, a rate-limit key, a
+ * uniqueness check or an audit line sees the same value the database later
+ * stores and filters by. Without it `[email protected]\u0000` was a new key for
+ * the per-email reset throttle while the lookup found the victim's row.
+ *
+ * A string under a key that names a password is left exactly as sent. A
+ * password never reaches the database as text, only as its bcrypt hash, so
+ * the stores above never meet it, and bcryptjs hashes a NUL like any other
+ * byte. Stripping it here would change the hash input and lock out an account
+ * whose password was set with a NUL before this rule existed; left alone,
+ * signup, login and a password change all hash the same string.
+ *
+ * Returns the same value, by identity, when it holds no NUL. Arrays and plain
+ * objects (null-prototype ones too, the shape a query or multipart parser
+ * builds) are walked; any other value is returned as it is.
+ */
+export function nulFreeInput(value: unknown): unknown {
+ return nulFreeInputValue(value, false);
+}
+
+function nulFreeInputValue(value: unknown, password: boolean): unknown {
+ if (typeof value === 'string') return password ? value : stripNul(value);
+ if (Array.isArray(value)) {
+ const items = value.map((item: unknown) => nulFreeInputValue(item, password));
+ return items.some((item, i) => item !== value[i]) ? items : value;
+ }
+ if (value === null || typeof value !== 'object') return value;
+ const prototype: unknown = Object.getPrototypeOf(value);
+ if (prototype !== Object.prototype && prototype !== null) return value;
+ const entries = Object.entries(value as Record<string, unknown>);
+ const cleaned = entries.map(
+ ([key, item]) => [stripNul(key), nulFreeInputValue(item, PASSWORD_KEY.test(key))] as const,
+ );
+ if (cleaned.every(([key, item], i) => key === entries[i][0] && item === entries[i][1])) return value;
+ // Defined, not assigned, so a key named `__proto__` stays a plain property.
+ const copy = Object.create(prototype as object | null) as Record<string, unknown>;
+ for (const [key, item] of cleaned) {
+ Object.defineProperty(copy, key, { value: item, enumerable: true, writable: true, configurable: true });
+ }
+ return copy;
+}
+
/** `query` with its parameters passed through {@link nulFreeParameter}; the same object when none holds a NUL. */
function nulFreeQuery(query: CompiledQuery): CompiledQuery {
const parameters = query.parameters.map(nulFreeParameter);
diff --git a/server/src/mcp/nest-mcp-policy.ts b/server/src/mcp/nest-mcp-policy.ts
index baf27084c..2790547bc 100644
--- a/server/src/mcp/nest-mcp-policy.ts
+++ b/server/src/mcp/nest-mcp-policy.ts
@@ -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;
diff --git a/server/src/nest-mcp/index.ts b/server/src/nest-mcp/index.ts
index e2882f5b9..eee7151c0 100644
--- a/server/src/nest-mcp/index.ts
+++ b/server/src/nest-mcp/index.ts
@@ -33,6 +33,7 @@ export {
type McpEntry,
type McpEntryKind,
type McpErrorMapper,
+ type McpInputMapper,
type McpModuleOptions,
type McpRegistryListing,
type McpToolGate,
diff --git a/server/src/nest-mcp/mcp-registry.service.ts b/server/src/nest-mcp/mcp-registry.service.ts
index 774ae3a05..39ffcf300 100644
--- a/server/src/nest-mcp/mcp-registry.service.ts
+++ b/server/src/nest-mcp/mcp-registry.service.ts
@@ -22,6 +22,7 @@ export class McpRegistryService extends McpRegistry implements OnModuleInit {
validateAccess: options.validateAccess,
toolGate: toolGate ?? undefined,
errorMapper: options.errorMapper,
+ inputMapper: options.inputMapper,
});
}
diff --git a/server/src/nest-mcp/registry.ts b/server/src/nest-mcp/registry.ts
index b76197574..024d2a619 100644
--- a/server/src/nest-mcp/registry.ts
+++ b/server/src/nest-mcp/registry.ts
@@ -9,6 +9,7 @@ import type {
McpEntry,
McpEntryKind,
McpErrorMapper,
+ McpInputMapper,
McpRegistryListing,
McpToolGate,
PromptOptions,
@@ -44,6 +45,8 @@ export interface McpRegistryOptions {
toolGate?: McpToolGate;
/** Turns an error a registered tool handler threw into its result; see `McpErrorMapper`. */
errorMapper?: McpErrorMapper;
+ /** Rewrites what a client sends before any handler sees it; see `McpInputMapper`. */
+ inputMapper?: McpInputMapper;
}
type AnyHandler = (this: unknown, ...handlerArgs: unknown[]) => unknown;
@@ -115,6 +118,7 @@ export class McpRegistry {
private readonly validateAccess?: McpAccessValidator;
private readonly toolGate?: McpToolGate;
private readonly errorMapper?: McpErrorMapper;
+ private readonly inputMapper: McpInputMapper;
/** Memoised `reservedNames()`; dropped by register() so it can never go stale. */
private reserved?: ReadonlySet<string>;
@@ -123,6 +127,7 @@ export class McpRegistry {
this.validateAccess = options.validateAccess;
this.toolGate = options.toolGate;
this.errorMapper = options.errorMapper;
+ this.inputMapper = options.inputMapper ?? ((input) => input);
}
/**
@@ -386,11 +391,14 @@ export class McpRegistry {
// The SDK invokes the callback as (args, extra) when inputSchema is
// present (even an empty shape) and as (extra) otherwise — normalize so
// the decorated method always receives (args, ctx).
+ // The arguments go through the host's input mapper first.
const cb =
options.inputSchema !== undefined
? (args: unknown, _extra: unknown) => {
opts?.onInvoke?.({ kind: 'tool', name: options.name });
- return invoke(opts, { kind: 'tool', name: options.name }, () => handler.call(instance, args, ctx));
+ return invoke(opts, { kind: 'tool', name: options.name }, () =>
+ handler.call(instance, this.inputMapper(args), ctx),
+ );
}
: (_extra: unknown) => {
opts?.onInvoke?.({ kind: 'tool', name: options.name });
@@ -440,7 +448,7 @@ export class McpRegistry {
(uri: unknown, variables: unknown, _extra: unknown) => {
opts?.onInvoke?.({ kind: 'resourceTemplate', name: options.name });
return invoke(opts, { kind: 'resourceTemplate', name: options.name }, () =>
- handler.call(instance, uri, variables, ctx),
+ handler.call(instance, uri, this.inputMapper(variables), ctx),
);
},
);
@@ -469,7 +477,9 @@ export class McpRegistry {
argsSchema !== undefined
? (args: unknown, _extra: unknown) => {
opts?.onInvoke?.({ kind: 'prompt', name: options.name });
- return invoke(opts, { kind: 'prompt', name: options.name }, () => handler.call(instance, args, ctx));
+ return invoke(opts, { kind: 'prompt', name: options.name }, () =>
+ handler.call(instance, this.inputMapper(args), ctx),
+ );
}
: (_extra: unknown) => {
opts?.onInvoke?.({ kind: 'prompt', name: options.name });
diff --git a/server/src/nest-mcp/types.ts b/server/src/nest-mcp/types.ts
index 73505065e..350eac294 100644
--- a/server/src/nest-mcp/types.ts
+++ b/server/src/nest-mcp/types.ts
@@ -290,6 +290,15 @@ export type McpToolGate = (tool: ToolOptions, ctx: McpContext) => unknown;
*/
export type McpErrorMapper = (err: unknown) => unknown;
+/**
+ * Rewrites what a client sends before a handler receives it: the arguments of
+ * every tool (registered and dynamic) and prompt, and the variables of every
+ * resource template. The host's normalisation seam (TREK drops NUL characters
+ * here, as from every HTTP request); nest-mcp attaches no semantics to it.
+ * Omitted, the input reaches the handler as the SDK parsed it.
+ */
+export type McpInputMapper = (input: unknown) => unknown;
+
/**
* How the module builds its `McpToolGate`: a factory with the providers it
* needs, resolved from the container like any other provider. A gate that
@@ -308,6 +317,8 @@ export interface McpModuleOptions {
toolGate?: McpToolGateProvider;
/** See `McpErrorMapper`. Static: it needs nothing from the container. */
errorMapper?: McpErrorMapper;
+ /** See `McpInputMapper`. Static, like the error mapper. */
+ inputMapper?: McpInputMapper;
}
/** Injection token for the resolved `McpToolGate` (null when none was configured). */
diff --git a/server/src/nest/app.module.ts b/server/src/nest/app.module.ts
index d39dbe77e..860a763ab 100644
--- a/server/src/nest/app.module.ts
+++ b/server/src/nest/app.module.ts
@@ -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
diff --git a/server/src/nest/common/nul-free-input.ts b/server/src/nest/common/nul-free-input.ts
new file mode 100644
index 000000000..7ffb1fa92
--- /dev/null
+++ b/server/src/nest/common/nul-free-input.ts
@@ -0,0 +1,69 @@
+import { nulFreeInput } from '../../db/nul-free';
+import { Injectable, type PipeTransform } from '@nestjs/common';
+
+import type { NextFunction, Request, Response } from 'express';
+
+/**
+ * Where a request's input loses its NUL characters before any logic runs.
+ *
+ * The database stores no U+0000 on either engine (`db/nul-free.ts`), so a
+ * value that still carries one in the application is a different value from
+ * the one a lookup or a write uses: a throttle keyed on it, a rate-limit
+ * bucket, a uniqueness pre-check or a comparison with a stored row sees one
+ * spelling while the database sees another. Normalising at the edge makes the
+ * two agree, and keeps the database's own stripping as the backstop for what
+ * does not arrive through a request (an import, a provider's feed, a plugin).
+ *
+ * The edges, each passing what it parsed through {@link nulFreeInput}:
+ * - {@link nulFreeRequest}, mounted in `bootstrap.ts` right after the body
+ * parsers and before the request context, so every guard, interceptor and
+ * controller after it reads a clean path, query string, body and cookies;
+ * - {@link NulFreeInputPipe}, the first global pipe, for what a route parses
+ * itself after that middleware: the fields and file names of a multipart
+ * upload, which multer sets inside the route's interceptor;
+ * - the websocket adapter, for every frame (`realtime/trek-ws.adapter.ts`);
+ * - the MCP registry's input mapper (`trekMcpInputMapper` in
+ * `mcp/nest-mcp-policy.ts`), for the arguments of every tool, prompt and
+ * resource template, since /mcp keeps its raw body.
+ *
+ * Headers need nothing: Node's HTTP parser answers 400 to a header value that
+ * holds a NUL before TREK sees the request. A cookie is percent-decoded by
+ * cookie-parser, so `%00` in one becomes a NUL, and is cleaned here.
+ */
+
+/** `url` without a percent-encoded NUL, repeated so `%%0000` cannot leave one behind. */
+export function withoutEncodedNul(url: string): string {
+ let out = url;
+ while (out.includes('%00')) out = out.replaceAll('%00', '');
+ return out;
+}
+
+/**
+ * The HTTP edge. The path and the query string are cleaned in the URL itself,
+ * before the router decodes either: Express reads `req.query` from `req.url`
+ * on every access and fills the route parameters from it when a route
+ * matches, so both come out without NUL. A NUL can only reach a URL as `%00`,
+ * since Node refuses a raw one in the request line. `req.originalUrl` keeps
+ * what the client sent, for the access log.
+ */
+export function nulFreeRequest(req: Request, _res: Response, next: NextFunction): void {
+ req.url = withoutEncodedNul(req.url);
+ if (req.body !== undefined) req.body = nulFreeInput(req.body);
+ const withCookies = req as Request & { cookies?: unknown; signedCookies?: unknown };
+ if (withCookies.cookies !== undefined) withCookies.cookies = nulFreeInput(withCookies.cookies);
+ if (withCookies.signedCookies !== undefined) withCookies.signedCookies = nulFreeInput(withCookies.signedCookies);
+ next();
+}
+
+/**
+ * The same rule for every argument a route handler receives. After
+ * {@link nulFreeRequest} it only changes what the route parsed itself (a
+ * multipart body and its files); everything else already holds no NUL and
+ * comes back as the same value.
+ */
+@Injectable()
+export class NulFreeInputPipe implements PipeTransform {
+ transform(value: unknown): unknown {
+ return nulFreeInput(value);
+ }
+}
diff --git a/server/src/nest/realtime/trek-ws.adapter.ts b/server/src/nest/realtime/trek-ws.adapter.ts
index b2a1ccff2..34af762cf 100644
--- a/server/src/nest/realtime/trek-ws.adapter.ts
+++ b/server/src/nest/realtime/trek-ws.adapter.ts
@@ -1,4 +1,5 @@
import { readEnv } from '../../app-config';
+import { nulFreeInput } from '../../db/nul-free';
import { logError } from '../audit/audit-log.logger';
import { traceEntry } from '../audit/entry-trace.logger';
import { isSameHostOrigin } from '../common/same-origin';
@@ -250,7 +251,9 @@ export class TrekWsAdapter extends WsAdapter {
let message: { type?: unknown } | null = null;
try {
- const parsed = JSON.parse(buffer.toString());
+ // Without NUL before any handler reads it, as every HTTP request is
+ // (common/nul-free-input.ts).
+ const parsed = nulFreeInput(JSON.parse(buffer.toString())) as { type?: unknown } | null;
if (parsed && typeof parsed === 'object' && typeof parsed.type === 'string') message = parsed;
} catch {
// Malformed JSON. Still counted below, exactly as before.
diff --git a/server/tests/integration/nul-free-input.test.ts b/server/tests/integration/nul-free-input.test.ts
new file mode 100644
index 000000000..524f508b1
--- /dev/null
+++ b/server/tests/integration/nul-free-input.test.ts
@@ -0,0 +1,99 @@
+/**
+ * A request's NUL characters are gone before any logic runs, on SQLite and on
+ * Postgres alike (src/nest/common/nul-free-input.ts).
+ *
+ * The database stores no NUL on either engine (src/db/nul-free.ts), so a value
+ * the application still held with one was a different value from the one the
+ * database compared: `[email protected]\u0000` was a fresh key for the per-email
+ * password-reset throttle while the lookup found the victim's row, so every
+ * spelling mailed the victim a new link and voided the last one. These run
+ * through buildApp(), the production stack, so the edge is the one that ships.
+ */
+import { buildApp } from '../../src/bootstrap';
+import { PasswordResetTokens } from '../../src/db/entities/PasswordResetTokens.entity';
+import { authCookie } from '../helpers/auth';
+import { makePlace } from '../helpers/factories/places';
+import { countRows } from '../helpers/factories/rows';
+import { makeTrip } from '../helpers/factories/trips';
+import { makeUser } from '../helpers/factories/users';
+import { oneByOne } from '../helpers/one-by-one';
+import { resetDatabase } from '../helpers/reset-database';
+import { resetRateLimits } from '../helpers/test-db';
+import { MikroORM } from '@mikro-orm/core';
+import type { INestApplication } from '@nestjs/common';
+
+import type { Application } from 'express';
+import request from 'supertest';
+import { describe, it, expect, beforeAll, beforeEach, afterAll } from 'vitest';
+
+let nestApp: INestApplication;
+let app: Application;
+let orm: MikroORM;
+
+beforeAll(async () => {
+ nestApp = await buildApp();
+ app = nestApp.getHttpAdapter().getInstance();
+ orm = nestApp.get(MikroORM);
+});
+
+beforeEach(async () => {
+ await resetDatabase(orm);
+ await resetRateLimits(nestApp);
+});
+
+afterAll(async () => {
+ await nestApp.close();
+});
+
+describe('a NUL character in a request, on every engine', () => {
+ it('cannot make a new key for the per-email reset throttle', async () => {
+ const { user } = await makeUser(orm, { email: '[email protected]' });
+ const spellings = [
+ '[email protected]\u0000\u0000',
+ '\[email protected]\u0000\u0000\u0000',
+ ];
+ // Each request from a fresh per-IP bucket, the distributed case the
+ // per-email throttle is there for, and one by one, so it counts in order.
+ const statuses = await oneByOne(spellings, async (email) => {
+ await resetRateLimits(nestApp);
+ return (await request(app).post('/api/auth/forgot-password').send({ email })).status;
+ });
+ expect(statuses).toEqual([200, 200, 200, 200]);
+ // Three links, the throttle's limit; the fourth spelling is the same address.
+ expect(await countRows(orm, PasswordResetTokens, { user: user.id })).toBe(3);
+ });
+
+ it('leaves a password as sent: one set with a NUL signs in with exactly that password', async () => {
+ const password = 'Nul\u0000Password-1';
+ const { user } = await makeUser(orm, { email: '[email protected]', password });
+
+ const exact = await request(app).post('/api/auth/login').send({ email: user.email, password });
+ expect(exact.status).toBe(200);
+ const stripped = await request(app).post('/api/auth/login').send({ email: user.email, password: 'NulPassword-1' });
+ expect(stripped.status).toBe(401);
+ });
+
+ it('cleans the path and the query string before the route reads them', async () => {
+ const { user } = await makeUser(orm);
+ const trip = await makeTrip(orm, user.id);
+ await makePlace(orm, trip.id, { name: 'Spot' });
+
+ // Raw, pre-encoded `%00`: superagent's own encoder need not round-trip a NUL.
+ const res = await request(app)
+ .get(`/api/trips/${trip.id}%00/places?search=Sp%00ot`)
+ .set('Cookie', authCookie(user.id));
+ expect(res.status).toBe(200);
+ expect(res.body.places.map((p: { name: string }) => p.name)).toEqual(['Spot']);
+ });
+
+ it('cleans a cookie, which cookie-parser percent-decodes', async () => {
+ const { user } = await makeUser(orm);
+ const res = await request(app)
+ .get('/api/auth/me')
+ .set('Cookie', `${authCookie(user.id)}%00`);
+ expect(res.status).toBe(200);
+ expect(res.body.user.id).toBe(user.id);
+ });
+});
diff --git a/server/tests/unit/db/nul-free.test.ts b/server/tests/unit/db/nul-free.test.ts
index f6b74faae..ab61b2c56 100644
--- a/server/tests/unit/db/nul-free.test.ts
+++ b/server/tests/unit/db/nul-free.test.ts
@@ -5,7 +5,7 @@
* whole app on SQLite and on Postgres.
*/
import { TrekPostgreSqlPlatform } from '../../../src/db/engine/postgres/postgres-driver';
-import { nulFreeJson, nulFreeParameter, stripNul, withNulFreeParameters } from '../../../src/db/nul-free';
+import { nulFreeInput, nulFreeJson, nulFreeParameter, stripNul, withNulFreeParameters } from '../../../src/db/nul-free';
import {
type AbortableOperationOptions,
@@ -143,3 +143,69 @@ describe('NUL-free values', () => {
expect(platform.convertJsonToDatabaseValue({ k: 'v\u0000' })).toBe('{"k":"v"}');
});
});
+
+describe('nulFreeInput: a request value as the application handles it', () => {
+ it('drops NUL from every string and key, in arrays and nested objects', () => {
+ expect(nulFreeInput('[email protected]\u0000')).toBe('[email protected]');
+ expect(nulFreeInput({ 'na\u0000me': ['a\u0000', { deep: 'b\u0000c' }], n: 1, ok: true, none: null })).toEqual({
+ name: ['a', { deep: 'bc' }],
+ n: 1,
+ ok: true,
+ none: null,
+ });
+ });
+
+ it('returns the very same value when nothing holds a NUL', () => {
+ const body = { email: '[email protected]', tags: ['x', 'y'], nested: { k: 'v' } };
+ expect(nulFreeInput(body)).toBe(body);
+ const list = ['a', 'b'];
+ expect(nulFreeInput(list)).toBe(list);
+ });
+
+ it('leaves a password exactly as sent, under any key that names one', () => {
+ const body = {
+ email: '[email protected]\u0000',
+ password: 'pa\u0000ss',
+ new_password: 'n\u0000ew',
+ current_password: 'c\u0000ur',
+ user: { synology_password: 's\u0000y' },
+ };
+ expect(nulFreeInput(body)).toEqual({
+ email: '[email protected]',
+ password: 'pa\u0000ss',
+ new_password: 'n\u0000ew',
+ current_password: 'c\u0000ur',
+ user: { synology_password: 's\u0000y' },
+ });
+ });
+
+ it('still walks an object under a password key, only its strings stay as sent', () => {
+ expect(nulFreeInput({ passwordReset: { note: 'x\u0000' } })).toEqual({ passwordReset: { note: 'x' } });
+ });
+
+ it('keeps a null prototype, and a `__proto__` key stays a plain property', () => {
+ const fields = Object.create(null) as Record<string, unknown>;
+ fields.title = 't\u0000';
+ const cleaned = nulFreeInput(fields) as Record<string, unknown>;
+ expect(Object.getPrototypeOf(cleaned)).toBeNull();
+ expect(cleaned.title).toBe('t');
+
+ const hostile = JSON.parse('{"__proto__": {"polluted": "y\\u0000"}, "a": "b\\u0000"}') as object;
+ const out = nulFreeInput(hostile) as Record<string, unknown>;
+ expect(Object.getPrototypeOf(out)).toBe(Object.prototype);
+ expect(Object.prototype.hasOwnProperty.call(out, '__proto__')).toBe(true);
+ expect(({} as Record<string, unknown>).polluted).toBeUndefined();
+ expect(out.a).toBe('b');
+ });
+
+ it('leaves anything that is not a string, an array or a plain object alone', () => {
+ const buffer = Buffer.from('a\u0000b');
+ const date = new Date(0);
+ expect(nulFreeInput(buffer)).toBe(buffer);
+ expect(nulFreeInput(date)).toBe(date);
+ expect(nulFreeInput(5)).toBe(5);
+ expect(nulFreeInput(undefined)).toBeUndefined();
+ const file = { originalname: 'pho\u0000to.jpg', buffer };
+ expect(nulFreeInput(file)).toEqual({ originalname: 'photo.jpg', buffer });
+ });
+});
diff --git a/server/tests/unit/nest-mcp/attach.test.ts b/server/tests/unit/nest-mcp/attach.test.ts
index ffa0ce082..76defa1a2 100644
--- a/server/tests/unit/nest-mcp/attach.test.ts
+++ b/server/tests/unit/nest-mcp/attach.test.ts
@@ -233,6 +233,23 @@ describe('McpRegistry.attach', () => {
expect(JSON.parse(textOf(result))).toEqual({ args: {}, userId: 42 });
});
+ it('hands every tool, prompt and resource template input through the input mapper first', async () => {
+ const mapped: unknown[] = [];
+ const inputMapper = (input: unknown) => {
+ mapped.push(input);
+ return JSON.parse(JSON.stringify(input).replaceAll('!', '')) as unknown;
+ };
+ const registry = createTestRegistry([new FixtureMcp(new Greeter())], { accessPolicy: policy, inputMapper });
+ harness = await createAttachHarness(registry, { userId: 5, canRead: true });
+ const tool = await harness.client.callTool({ name: 'loose_tool', arguments: { name: 'tr!ek' } });
+ expect(JSON.parse(textOf(tool))).toEqual({ args: { name: 'trek' }, userId: 5 });
+ const prompt = await harness.client.getPrompt({ name: 'fixture_prompt', arguments: { topic: 'pack!ing' } });
+ expect(prompt.messages[0]?.content).toMatchObject({ text: 'packing for 5' });
+ const item = await harness.client.readResource({ uri: 'test://item/4!2' });
+ expect(item.contents[0]).toMatchObject({ text: 'item 42 for 5' });
+ expect(mapped).toEqual([{ name: 'tr!ek' }, { topic: 'pack!ing' }, { id: '4!2' }]);
+ });
+
it('serves fixed resources with (uri, ctx)', async () => {
harness = await createAttachHarness(buildRegistry(), { userId: 9, canRead: true });
const result = await harness.client.readResource({ uri: 'test://doc' });
diff --git a/server/tests/unit/nest/common/nul-free-input.test.ts b/server/tests/unit/nest/common/nul-free-input.test.ts
new file mode 100644
index 000000000..7afb2c173
--- /dev/null
+++ b/server/tests/unit/nest/common/nul-free-input.test.ts
@@ -0,0 +1,95 @@
+/**
+ * The HTTP edge of the NUL rule (src/nest/common/nul-free-input.ts): what a
+ * request carries loses its NUL characters before any guard or controller
+ * reads it. tests/integration/nul-free-input.test.ts drives the same rule
+ * through the whole app on SQLite and on Postgres.
+ */
+import { NulFreeInputPipe, nulFreeRequest, withoutEncodedNul } from '../../../../src/nest/common/nul-free-input';
+
+import type { Request, Response } from 'express';
+import nodeHttp from 'node:http';
+import net from 'node:net';
+import { describe, expect, it, vi } from 'vitest';
+
+function run(req: Partial<Request> & { cookies?: unknown; signedCookies?: unknown }) {
+ const next = vi.fn();
+ nulFreeRequest(req as Request, {} as Response, next);
+ expect(next).toHaveBeenCalledWith();
+ return req;
+}
+
+describe('withoutEncodedNul', () => {
+ it('drops every %00 from the path and the query string', () => {
+ expect(withoutEncodedNul('/api/trips/7%00/places?search=a%00b&x=%00')).toBe('/api/trips/7/places?search=ab&x=');
+ });
+
+ it('cannot be tricked into leaving one behind', () => {
+ expect(withoutEncodedNul('/a?q=%%0000')).toBe('/a?q=');
+ expect(withoutEncodedNul('/a?q=%%%000000')).toBe('/a?q=');
+ });
+
+ it('keeps an encoded percent sign followed by 00, which decodes to the text %00', () => {
+ expect(withoutEncodedNul('/a?q=%2500')).toBe('/a?q=%2500');
+ });
+});
+
+describe('nulFreeRequest', () => {
+ it('cleans the url, the body and both cookie jars', () => {
+ const req = run({
+ url: '/api/auth/x%00?e=a%00',
+ body: { email: '[email protected]\u0000', password: 'pa\u0000ss' },
+ cookies: { trek_session: 'tok\u0000en' },
+ signedCookies: { s: 'v\u0000' },
+ });
+ expect(req.url).toBe('/api/auth/x?e=a');
+ expect(req.body).toEqual({ email: '[email protected]', password: 'pa\u0000ss' });
+ expect(req.cookies).toEqual({ trek_session: 'token' });
+ expect(req.signedCookies).toEqual({ s: 'v' });
+ });
+
+ it('leaves a request without a body or cookies as it is', () => {
+ const req = run({ url: '/api/health' });
+ expect(req).toEqual({ url: '/api/health' });
+ });
+});
+
+describe('NulFreeInputPipe', () => {
+ it('cleans what a route parsed itself, a multipart body and its file name', () => {
+ const fields = Object.create(null) as Record<string, unknown>;
+ fields.description = 'a\u0000b';
+ const pipe = new NulFreeInputPipe();
+ expect({ ...(pipe.transform(fields) as object) }).toEqual({ description: 'ab' });
+ expect(pipe.transform({ originalname: 'x\u0000.jpg' })).toEqual({ originalname: 'x.jpg' });
+ expect(pipe.transform('7\u0000')).toBe('7');
+ expect(pipe.transform(7)).toBe(7);
+ });
+});
+
+describe('headers', () => {
+ it('never reach TREK with a NUL: Node answers 400 before any middleware runs', async () => {
+ const seen = vi.fn();
+ const server = nodeHttp.createServer((_req, res) => {
+ seen();
+ res.end('ok');
+ });
+ await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));
+ try {
+ const { port } = server.address() as net.AddressInfo;
+ const status = await new Promise<string>((resolve, reject) => {
+ const socket = net.connect(port, '127.0.0.1', () => {
+ socket.write('GET / HTTP/1.1\r\nHost: x\r\nX-Email: a\u0000b\r\nConnection: close\r\n\r\n');
+ });
+ let raw = '';
+ socket.on('data', (chunk: Buffer) => {
+ raw += chunk.toString();
+ });
+ socket.on('end', () => resolve(raw.split('\r\n')[0]));
+ socket.on('error', reject);
+ });
+ expect(status).toBe('HTTP/1.1 400 Bad Request');
+ expect(seen).not.toHaveBeenCalled();
+ } finally {
+ await new Promise((resolve) => server.close(resolve));
+ }
+ });
+});
diff --git a/server/tests/unit/nest/trek-ws-adapter.test.ts b/server/tests/unit/nest/trek-ws-adapter.test.ts
index e98f95d5e..e4d0ae51f 100644
--- a/server/tests/unit/nest/trek-ws-adapter.test.ts
+++ b/server/tests/unit/nest/trek-ws-adapter.test.ts
@@ -138,6 +138,15 @@ describe('TrekWsAdapter wire protocol', () => {
expect(socket.sent).toHaveLength(0); // the answer went nowhere
});
+ it('WSAD-001c: a frame reaches its handler without NUL characters, its type included', () => {
+ // The same rule as every HTTP request (src/nest/common/nul-free-input.ts):
+ // a handler never holds a value the database would store differently.
+ const socket = fakeSocket();
+ adapter.bindMessageHandlers(socket as never, handlers as never, transform);
+ socket.emit('message', frame({ type: 'jo\u0000in', tripId: 7, note: 'a\u0000b', 'k\u0000': ['c\u0000'] }));
+ expect(handled).toEqual([{ type: 'join', tripId: 7, note: 'ab', k: ['c'] }]);
+ });
+
it('WSAD-002: a handler that answers undefined sends nothing', () => {
const socket = fakeSocket();
adapter.bindMessageHandlers(socket as never, handlers as never, transform);