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
@@ -11,15 +11,15 @@ import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
/**
* Plan 3c Task 4 review M1 / program rule 22: MikroORM 7 inlines every bound
* value into the SQL text via `platform.quoteValue`/`escape` (see
* `NulSafeSqlitePlatform`'s own docstring for the full mechanism). These
* tests prove the override round-trips a NUL byte at every position SQLite
* itself will accept, by handing the platform's own generated SQL literal to
* a REAL `better-sqlite3` connection and reading the value back — not a
* string-shape assertion, an actual SQLite round-trip.
* `NulSafeSqlitePlatform`'s own docstring for the full mechanism). TREK stores
* no NUL on either engine (`src/db/nul-free.ts`), so the platform drops it from
* every string it inlines. These hand the platform's own SQL literal to a REAL
* `better-sqlite3` connection and read the value back.
*/
describe('NulSafeSqlitePlatform', () => {
const platform = new NulSafeSqlitePlatform();
const db = new Database(':memory:');
const withoutNul = (value: string) => value.replaceAll('\u0000', '');
function roundTrip(value: string): string {
const literal = platform.escape(value);
@@ -27,51 +27,29 @@ describe('NulSafeSqlitePlatform', () => {
return row.v;
}
it('round-trips a string with no NUL unchanged (baseline: unaffected by the override)', () => {
it('round-trips a string with no NUL unchanged', () => {
expect(roundTrip('plain string')).toBe('plain string');
});
it('round-trips a NUL in the middle of a string', () => {
const value = 'nul\u0000name';
expect(roundTrip(value)).toBe(value);
it('drops a NUL wherever it stands, and a string of NULs becomes empty', () => {
expect(roundTrip('nul\u0000name')).toBe('nulname');
expect(roundTrip('\u0000leading')).toBe('leading');
expect(roundTrip('trailing\u0000')).toBe('trailing');
expect(roundTrip('\u0000\u0000\u0000')).toBe('');
});
it('round-trips a NUL at the very start of a string', () => {
const value = '\u0000leading';
expect(roundTrip(value)).toBe(value);
it('keeps the quote and backslash handling of every other string next to a dropped NUL', () => {
for (const value of ["o'brien\u0000's café", 'back\\slash\u0000end\\', "a'\u0000b\\\u0000c''\u0000\\\\d"]) {
expect(roundTrip(value)).toBe(withoutNul(value));
}
});
it('round-trips a NUL at the very end of a string', () => {
const value = 'trailing\u0000';
expect(roundTrip(value)).toBe(value);
});
it('round-trips a string made ENTIRELY of NUL bytes', () => {
expect(roundTrip('\u0000')).toBe('\u0000');
expect(roundTrip('\u0000\u0000\u0000')).toBe('\u0000\u0000\u0000');
});
it('round-trips a NUL next to a single quote (the string-literal escape character)', () => {
const value = "o'brien\u0000's café";
expect(roundTrip(value)).toBe(value);
});
it('round-trips a NUL next to a backslash (not special in a SQLite string literal)', () => {
const value = 'back\\slash\u0000end\\';
expect(roundTrip(value)).toBe(value);
});
it('round-trips multiple NULs mixed with quotes and backslashes', () => {
const value = "a'\u0000b\\\u0000c''\u0000\\\\d";
expect(roundTrip(value)).toBe(value);
});
it('fuzzes every 0x00-0x1F control character: none corrupt the round-trip and none throw', () => {
it('fuzzes every 0x00-0x1F control character: only NUL is dropped and none throw', () => {
for (let code = 0x00; code <= 0x1f; code++) {
const ch = String.fromCharCode(code);
const value = `before${ch}after`;
expect(() => roundTrip(value)).not.toThrow();
expect(roundTrip(value)).toBe(value);
expect(roundTrip(value)).toBe(code === 0 ? 'beforeafter' : value);
}
});
@@ -81,14 +59,19 @@ describe('NulSafeSqlitePlatform', () => {
expect(platform.escape(true)).toBe('true');
});
it('formatQuery (the real call path AbstractSqlConnection.execute uses: formatQuery -> quoteValue -> escape) inlines a NUL-safe literal for a bound `?`, matched via a real table', () => {
it('formatQuery (the real call path AbstractSqlConnection.execute uses: formatQuery -> quoteValue -> escape) stores a value without its NUL and finds it by the same value', () => {
db.exec('CREATE TABLE probe (name TEXT)');
const value = 'search\u0000term';
const insertSql = platform.formatQuery('insert into probe (name) values (?)', [value]);
db.exec(insertSql);
const selectSql = platform.formatQuery('select name from probe where name = ?', [value]);
const row = db.prepare(selectSql).get() as { name: string } | undefined;
expect(row?.name).toBe(value);
db.exec(platform.formatQuery('insert into probe (name) values (?)', [value]));
const row = db.prepare(platform.formatQuery('select name from probe where name = ?', [value])).get() as
{ name: string } | undefined;
expect(row?.name).toBe('searchterm');
});
it('serializes a JSON value with every string in it, keys included, without NUL', () => {
expect(platform.convertJsonToDatabaseValue({ 'k\u0000': ['a\u0000b', 1, { n: 'x\u0000' }] })).toBe(
'{"k":["ab",1,{"n":"x"}]}',
);
});
/**