Files
TREK/server/tests/integration/nul-free-input.test.ts
T
Maurice 13e81c04ab 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.
2026-11-17 23:04:11 +01:00

100 lines
4.2 KiB
TypeScript

/**
* 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: `victim@x.test\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: 'nul-throttle@example.test' });
const spellings = [
'nul-throttle@example.test\u0000',
'nul-throttle@example.test\u0000\u0000',
'nul-\u0000throttle@example.test',
'\u0000nul-throttle@example.test\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: 'nul-password@example.test', 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);
});
});