mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-22 03:50:30 +00:00
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:
@@ -1,5 +1,6 @@
|
||||
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';
|
||||
|
||||
@@ -10,6 +11,19 @@ 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
|
||||
@@ -142,7 +156,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(...(args as never[]))) {
|
||||
for (const row of this.db.prepare(sql).iterate(...nulFreeArgs(args))) {
|
||||
if (rows.length > 0) checkTime();
|
||||
rows.push(row);
|
||||
if (rows.length > maxRows) throw new Error(`query returned more than ${maxRows} rows`);
|
||||
@@ -158,7 +172,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(...(args as never[]));
|
||||
const info = this.db.prepare(sql).run(...nulFreeArgs(args));
|
||||
return { changes: info.changes };
|
||||
}
|
||||
this.runScript(sql, this.startBudget('exec'));
|
||||
@@ -242,7 +256,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 = (op.args ?? []) as never[];
|
||||
const args = nulFreeArgs(op.args ?? []);
|
||||
if (stmt.reader) {
|
||||
const rows: unknown[] = [];
|
||||
for (const row of stmt.iterate(...args)) {
|
||||
|
||||
Reference in New Issue
Block a user