fix(plugins): bind a plugin's own SQLite arguments exactly again, NUL included

5ff5e7660 stripped U+0000 from every argument a plugin bound to its own
SQLite file, but left the rows it had stored alone. A key written with a
NUL (through a bound arg or char(0) in the plugin's SQL) could no longer be
found by the same key, upserts added duplicates, and keys that differ only
in where a NUL sits ('a\0bc', 'ab\0c') collapsed into one and collided.

A plugin's data is the plugin's, so its file binds the arguments exactly as
sent again, as before that commit. TREK's own data still holds no NUL; the
Postgres store, whose text cannot hold the character, refuses it instead.
This commit is contained in:
Maurice
2026-11-17 22:28:26 +01:00
parent 28f93f2539
commit 2b14fe1a2a
2 changed files with 35 additions and 17 deletions
@@ -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)');