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 e1a5864406
commit f42ef5246c
12 changed files with 374 additions and 157 deletions
+18 -52
View File
@@ -1,3 +1,5 @@
import { nulFreeJson, stripNul } from './nul-free';
import type { TransformContext } from '@mikro-orm/core';
import { SqlitePlatform } from '@mikro-orm/sql';
/**
@@ -5,55 +7,18 @@ import { SqlitePlatform } from '@mikro-orm/sql';
* surfaced by Plan 3c Task 4 review M1: MikroORM 7 never sends the driver a
* parameterised statement. `AbstractSqlConnection.execute` calls
* `platform.formatQuery(sql, params)` (`@mikro-orm/core`'s `Platform.formatQuery`)
* and hands better-sqlite3 the fully INLINED SQL text — `formatQuery` walks the
* `?` placeholders and splices in `this.quoteValue(param)` for each one, so
* every bound value this ORM ever writes is a literal in the statement string,
* not a bound parameter the driver escapes separately. `SqlitePlatform`'s own
* `quoteValue` (`@mikro-orm/sql`'s `AbstractSqlPlatform.quoteValue`, for
* anything that isn't a `Date`) delegates to `escape`, which wraps a string in
* single quotes and doubles embedded `'` — the standard SQL literal shape.
* and hands better-sqlite3 the fully INLINED SQL text: `formatQuery` walks the
* `?` placeholders and splices in `this.quoteValue(param)` for each one, and
* `quoteValue` delegates to `escape` for anything that isn't a `Date`.
*
* A `\u0000` inside that string survives the JS-level quoting untouched (it
* is not `'`, so nothing escapes it), but the C string this literal eventually
* becomes IS NUL-terminated: SQLite's own tokenizer reads the statement text
* as a NUL-terminated buffer, so it stops scanning at the embedded `\0` —
* inside the still-open `'…'` literal, which is exactly why the failure mode
* is always "unterminated string", never anything an attacker could turn into
* an injection (verified in the Task 4 review: every NUL statement in this
* class fails closed with a syntax error, 500, never a different query).
*
* The legacy raw-SQL layer never had this problem: `better-sqlite3` binds `?`
* parameters as real, separately-transmitted values (no inlining, no NUL
* truncation), so a NUL byte in a user string round-tripped byte-for-byte —
* `GET /api/trips/:id/places?search=%00` was 200 and a place named
* `"nul\u0000name"` created (201) with its name intact. Once a route's
* statement moves off `DatabaseService`'s raw bind onto anything this ORM
* inlines (a QB read, `em.insert`, `nativeUpdate` — every one of them,
* regardless of domain), that same input 500s instead, byte-for-byte across
* every domain: this is corollary to rule 22, not specific to any one plan.
*
* The fix restores the round-trip AT THE PLATFORM, once, program-wide: a
* string containing `\u0000` renders not as one `'…'` literal but as a
* parenthesised `||` (SQL string concatenation) chain, splicing in SQLite's
* builtin `char(0)` — which returns a one-character string holding the NUL
* codepoint — everywhere the original string held one. `char()` is a core
* SQLite function (no extension, no `PRAGMA`), and `||` binds tighter than
* every arithmetic/comparison/logical operator that could otherwise appear
* around a bound value, so `(...)`'s parens are redundant for a scalar
* comparison — kept anyway so this expression composes safely as an
* IN-list element or the operand of anything else `escape()`'s caller
* splices it into (`AbstractSqlPlatform.escape` recurses through arrays
* with `value.map(v => this.escape(v)).join(', ')`, so a NUL inside one
* array element must stay a self-contained expression). Splitting on
* `\u0000` and re-quoting each piece with the platform's own `escape` (via
* `super.escape`) reuses the exact same `'`-doubling every non-NUL string
* already goes through, so a NUL next to a quote or a backslash (neither
* of which SQLite string literals treat specially) round-trips exactly as
* it did before this override existed.
*
* Deliberately scoped to `typeof value === 'string'` for the NUL half above:
* that IS the M1 fix, not a general re-implementation of `escape`/
* `quoteValue`.
* **NUL.** A `\u0000` inside a string survives the JS-level quoting, but SQLite
* reads the statement text as a NUL-terminated buffer and stops at it, inside
* the still-open `'…'` literal, so every such statement failed with
* "unterminated string" (a 500, never a different query). This platform once
* spliced SQLite's `char(0)` into the literal to keep the character; TREK now
* stores no NUL on either engine (`nul-free.ts`: Postgres text cannot hold
* one), so `escape` drops it from every string, and `convertJsonToDatabaseValue`
* from every string of a JSON value, the way the Postgres platform does.
*
* **Rule 22 extension (Plan 3c Task 9, close-out review A H1):** non-finite
* numbers hit the SAME inlining mechanism, and rule 21's `toRowId` guards do
@@ -96,14 +61,15 @@ import { SqlitePlatform } from '@mikro-orm/sql';
*/
export class NulSafeSqlitePlatform extends SqlitePlatform {
override escape(value: unknown): string {
if (typeof value === 'string' && value.includes('\u0000')) {
const literals = value.split('\u0000').map((part) => super.escape(part));
return `(${literals.join(' || char(0) || ')})`;
}
if (typeof value === 'string') return super.escape(stripNul(value));
if (typeof value === 'number' && !Number.isFinite(value)) {
if (Number.isNaN(value)) return 'NULL';
return value > 0 ? '9e999' : '-9e999';
}
return super.escape(value);
}
override convertJsonToDatabaseValue(value: unknown, context?: TransformContext): unknown {
return super.convertJsonToDatabaseValue(nulFreeJson(value), context);
}
}