diff --git a/server/src/nest/plugins/host/plugin-data.service.ts b/server/src/nest/plugins/host/plugin-data.service.ts index 4a9869c2b..8a9813b88 100644 --- a/server/src/nest/plugins/host/plugin-data.service.ts +++ b/server/src/nest/plugins/host/plugin-data.service.ts @@ -1,6 +1,5 @@ import { openDatabase } from '../../../db/connection'; import { PLUGIN_DATA_CAPS, type PluginDataStore } from '../../../db/engine/plugin-data'; -import { nulFreeParameter } from '../../../db/nul-free'; import { pluginDataDir, pluginDbFile, pluginsDataRoot } from '../paths'; import { splitAfterSemicolons } from './sql-script'; @@ -11,19 +10,6 @@ import path from 'node:path'; // The port, re-exported for the RPC kit, which may import from this file only (eslint.config.mjs). export type { PluginDataStore }; -/** - * A plugin's bound arguments without NUL, as TREK binds its own and as a - * plugin's schema on Postgres receives them (`db/nul-free.ts`): positional - * values, or the values of an object of named ones. - */ -function nulFreeArgs(args: readonly unknown[]): never[] { - return args.map((arg) => - arg !== null && typeof arg === 'object' && Object.getPrototypeOf(arg) === Object.prototype - ? Object.fromEntries(Object.entries(arg).map(([name, value]) => [name, nulFreeParameter(value)])) - : nulFreeParameter(arg), - ) as never[]; -} - /** * A plugin's own sqlite database (#plugins, db:own): the SQLite side of the * `PluginDataStore` port (`db/engine/plugin-data.ts`; the Postgres side is a @@ -37,6 +23,12 @@ function nulFreeArgs(args: readonly unknown[]): never[] { * (ATTACH another db, VACUUM INTO elsewhere, PRAGMA fiddling) or DoS via * oversize SQL, every read is held to a row cap, and query(), tx() and an exec() * script are held to a time budget (below). + * + * Bound arguments reach SQLite exactly as the plugin sent them, U+0000 (NUL) + * included: the data is the plugin's own, so a key it stored with a NUL is + * found again by the same key, and `'a\0bc'` and `'ab\0c'` stay two keys. TREK + * drops NUL only from its own data (`db/nul-free.ts`); the Postgres store, + * whose text cannot hold the character, refuses such an argument instead. */ const MAX_SQL_LENGTH = PLUGIN_DATA_CAPS.maxSqlLength; @@ -156,7 +148,7 @@ export class PluginDataDb implements PluginDataStore { // clock is read between rows. It is read only once another row has come back, so // a result that has been read to its end is returned, however long that took. const rows: unknown[] = []; - for (const row of this.db.prepare(sql).iterate(...nulFreeArgs(args))) { + for (const row of this.db.prepare(sql).iterate(...(args as never[]))) { if (rows.length > 0) checkTime(); rows.push(row); if (rows.length > maxRows) throw new Error(`query returned more than ${maxRows} rows`); @@ -172,7 +164,7 @@ export class PluginDataDb implements PluginDataStore { exec(sql: string, args: unknown[] = []): { changes: number } { this.guard(sql); if (args.length > 0) { - const info = this.db.prepare(sql).run(...nulFreeArgs(args)); + const info = this.db.prepare(sql).run(...(args as never[])); return { changes: info.changes }; } this.runScript(sql, this.startBudget('exec')); @@ -256,7 +248,7 @@ export class PluginDataDb implements PluginDataStore { const results: Array<{ changes?: number; rows?: unknown[] }> = []; for (const op of batch) { const stmt = this.db.prepare(op.sql); - const args = nulFreeArgs(op.args ?? []); + const args = (op.args ?? []) as never[]; if (stmt.reader) { const rows: unknown[] = []; for (const row of stmt.iterate(...args)) { diff --git a/server/tests/unit/plugins/plugin-data.test.ts b/server/tests/unit/plugins/plugin-data.test.ts index 36f6ca29f..71eda8c29 100644 --- a/server/tests/unit/plugins/plugin-data.test.ts +++ b/server/tests/unit/plugins/plugin-data.test.ts @@ -264,6 +264,32 @@ describe('PluginDataDb', () => { db.close(); }); + it("binds a plugin's NUL exactly: stored rows stay findable and NUL-separated keys never collide", () => { + const db = new PluginDataDb('nul-keys'); + db.exec('CREATE TABLE kv (k TEXT PRIMARY KEY, v TEXT)'); + // A row a plugin wrote with a NUL in its key, through its own SQL as well as through a bound arg. + db.exec("INSERT INTO kv VALUES ('q' || char(0) || 'r', 'spelled')"); + db.exec('INSERT INTO kv VALUES (?, ?)', ['trip-1\u0000slug', 'bound']); + // Keys that differ only in where the NUL sits are two keys. + db.exec('INSERT INTO kv VALUES (?, ?)', ['a\u0000bc', 'one']); + db.exec('INSERT INTO kv VALUES (?, ?)', ['ab\u0000c', 'two']); + db.tx([{ sql: 'INSERT INTO kv VALUES (?, ?)', args: ['x\u0000', 'tx'] }]); + + expect(db.query('SELECT v FROM kv WHERE k = ?', ['q\u0000r'])).toEqual([{ v: 'spelled' }]); + expect(db.query('SELECT v FROM kv WHERE k = ?', ['trip-1\u0000slug'])).toEqual([{ v: 'bound' }]); + expect(db.query('SELECT v FROM kv WHERE k = ?', ['trip-1slug'])).toEqual([]); + expect(db.query('SELECT v FROM kv WHERE k IN (?, ?) ORDER BY v', ['a\u0000bc', 'ab\u0000c'])).toEqual([ + { v: 'one' }, + { v: 'two' }, + ]); + expect(db.tx([{ sql: 'SELECT v FROM kv WHERE k = ?', args: ['x\u0000'] }])).toEqual({ + results: [{ rows: [{ v: 'tx' }] }], + }); + // Stored whole: SQLite's length() of text stops at a NUL, the bytes do not. + expect(db.query('SELECT length(CAST(k AS BLOB)) AS n FROM kv WHERE v = ?', ['bound'])).toEqual([{ n: 11 }]); + db.close(); + }); + it('removePluginData deletes the whole data dir', () => { const db = new PluginDataDb('temp'); db.migrate('001', 'CREATE TABLE t (id INTEGER)');