fix(db): store no NUL character on either engine

Postgres text cannot hold U+0000, so any write or filter carrying one (a
request body, a query string, an import, a feed, a forwarded mail, a
plugin's own SQL) answered a 500 on Postgres while SQLite stored it. Both
engines now drop the character where every value leaves for the database
(src/db/nul-free.ts): the MikroORM platforms when they inline a string or
serialize a JSON value, the Kysely dialects for every parameter, a
plugin's own SQLite file for its arguments, and the move from SQLite,
which used to refuse such text. The same request now answers the same
status and leaves the same row on both engines, so the places NUL suite
runs on Postgres as well and leaves SQLITE_ONLY.
This commit is contained in:
Maurice
2026-11-17 21:24:00 +01:00
parent 685506d067
commit 5ff5e76603
12 changed files with 374 additions and 157 deletions
+24 -20
View File
@@ -1,13 +1,13 @@
/**
* M1 / program rule 22 (task-4-review.md): a NUL byte in a user string no
* longer 500s on SQLite. The platform override restores the legacy raw bind's
* byte-for-byte round-trip (base 94c6efbbc: 200/201; before that fix, every
* ORM-converted statement 500'd on NUL).
* A NUL character (U+0000) in a user string, on SQLite and on Postgres alike.
*
* Moved out of places.test.ts so the rest of that suite runs on both engines:
* these two cases are about SQLite's value quoting, and Postgres text cannot
* hold U+0000 at all (tests/postgres/lane.ts, SQLITE_ONLY). The control
* characters 0x01-0x1F stay in places.test.ts, where they run on both.
* Postgres text cannot hold it, so a write or a filter carrying one used to
* answer a 500 there while SQLite stored it. TREK stores no NUL on either
* engine now (src/db/nul-free.ts): every string a statement carries loses it,
* so the same request answers the same status and leaves the same row on both
* engines (M1 / program rule 22 first made it a 201 on SQLite). The control
* characters 0x01-0x1F, which both engines store, are swept in
* places.test.ts.
*/
import { buildApp } from '../../src/bootstrap';
import { Places } from '../../src/db/entities/Places.entity';
@@ -19,6 +19,7 @@ import { makeUser } from '../helpers/factories/users';
import { resetDatabase } from '../helpers/reset-database';
import { resetRateLimits } from '../helpers/test-db';
import { MikroORM } from '@mikro-orm/core';
import type { AbstractSqlConnection } from '@mikro-orm/sql';
import type { INestApplication } from '@nestjs/common';
import type { Application } from 'express';
@@ -44,34 +45,37 @@ afterAll(async () => {
await nestApp.close();
});
describe('M1 — NUL-safe value quoting on the SQLite platform (rule 22)', () => {
it("GET ?search=%00 is 200, not 500 (status parity — SQLite's LIKE pattern matcher itself iterates its RHS as NUL-terminated, an independent SQLite limitation this platform fix does not touch, so a NUL search matches everything on BOTH trees, not nothing)", async () => {
describe('a NUL character in a user string, on every engine', () => {
it('GET ?search=%00 is 200 and matches as an empty search', async () => {
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await makePlace(orm, trip.id, { name: 'Spot' });
// A raw, pre-encoded `%00` in the URL — the review's exact literal
// request shape (`GET /api/trips/:id/places?search=%00`). `.query({...})`
// goes through superagent's own `qs` encoder, which is not guaranteed to
// round-trip a NUL character the same way; the literal query string is
// what Express/the route actually receives in production.
// A raw, pre-encoded `%00` in the URL: `.query({...})` goes through
// superagent's own encoder, which need not round-trip a NUL the same way.
const res = await request(app).get(`/api/trips/${trip.id}/places?search=%00`).set('Cookie', authCookie(user.id));
expect(res.status).toBe(200);
expect(res.body.places.map((p: { name: string }) => p.name)).toEqual(['Spot']);
});
it('POST create with a NUL in the name is 201 and the name round-trips byte-for-byte', async () => {
it('POST create with a NUL in the name is 201 and stores the name without it', async () => {
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
const name = 'nul\u0000name';
const res = await request(app)
.post(`/api/trips/${trip.id}/places`)
.set('Cookie', authCookie(user.id))
.send({ name });
.send({ name: 'nul\u0000name' });
expect(res.status).toBe(201);
expect(res.body.place.name).toBe(name);
expect(res.body.place.name).toBe('nulname');
const stored = await findRow(orm, Places, { id: res.body.place.id });
expect(stored?.name).toBe(name);
expect(stored?.name).toBe('nulname');
});
it('a parameter of a Kysely statement loses its NUL too', async () => {
const kysely = (orm.em.getConnection() as AbstractSqlConnection).getClient<object>();
const row = await kysely.selectNoFrom((eb) => eb.val('a\u0000b').as('v')).executeTakeFirstOrThrow();
expect(row.v).toBe('ab');
});
});
+3 -3
View File
@@ -1304,8 +1304,8 @@ describe('A-H1 / A-M1 / B-H1 — verifyTripAccess parses once with toRowId (Task
// ─────────────────────────────────────────────────────────────────────────────
// M1 / program rule 22 (task-4-review.md) — control characters in a user
// string round-trip on every engine. The NUL byte itself is SQLite's case and
// lives in places-nul-sqlite.test.ts: Postgres text cannot hold U+0000.
// string round-trip on every engine. The NUL byte, which neither engine
// stores, has its own suite: places-nul.test.ts.
// ─────────────────────────────────────────────────────────────────────────────
describe('M1 — control characters in a place name (rule 22)', () => {
@@ -1313,7 +1313,7 @@ describe('M1 — control characters in a place name (rule 22)', () => {
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
// 0x00 is covered by places-nul-sqlite.test.ts; this sweeps the rest
// 0x00 is covered by places-nul.test.ts; this sweeps the rest
// of the control-character range the review's fuzz asked for.
for (let code = 0x01; code <= 0x1f; code++) {
const name = `ctrl${String.fromCharCode(code)}char`;