diff --git a/server/src/db/engine/postgres/pg-tools.ts b/server/src/db/engine/postgres/pg-tools.ts index b547c8075..9d130ac45 100644 --- a/server/src/db/engine/postgres/pg-tools.ts +++ b/server/src/db/engine/postgres/pg-tools.ts @@ -1,6 +1,7 @@ import { execFile } from 'node:child_process'; import fs from 'node:fs'; import path from 'node:path'; +import readline from 'node:readline'; import { promisify } from 'node:util'; const execFileAsync = promisify(execFile); @@ -266,10 +267,31 @@ async function refuseToolsWithoutRestrict(binDir: string | undefined): Promise { + const createsHistory = new RegExp(`^CREATE TABLE ${escapeRegExp(`${schema.quoted}.mikro_orm_migrations`)} \\(`); + const input = fs.createReadStream(script, { encoding: 'utf8' }); + const lines = readline.createInterface({ input, crlfDelay: Infinity }); + try { + for await (const line of lines) { + if (createsHistory.test(line)) return true; + } + return false; + } finally { + lines.close(); + input.destroy(); + } } /** @@ -295,7 +317,7 @@ export async function pgRestore(url: string, source: string, binDir: string | un ['--no-owner', '--no-privileges', `--schema=${schema.name}`, `--file=${script}`, '--', source], {}, ); - if (!scriptHoldsTrekSchema(script, schema)) { + if (!(await scriptHoldsTrekSchema(script, schema))) { throw new Error(`The backup holds no TREK tables in the schema "${schema.name}"; nothing was replaced.`); } await run( diff --git a/server/tests/unit/db/engine/postgres/pg-tools.test.ts b/server/tests/unit/db/engine/postgres/pg-tools.test.ts index 72783ec65..568d54085 100644 --- a/server/tests/unit/db/engine/postgres/pg-tools.test.ts +++ b/server/tests/unit/db/engine/postgres/pg-tools.test.ts @@ -16,7 +16,7 @@ import { resolvePgTool, } from '../../../../../src/db/engine/postgres/pg-tools'; -import { +import fs, { chmodSync, existsSync, mkdtempSync, @@ -28,7 +28,7 @@ import { } from 'node:fs'; import { tmpdir } from 'node:os'; import path from 'node:path'; -import { afterEach, beforeEach, describe, expect, it } from 'vitest'; +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; const URL_WITH_SECRET = 'postgresql://trek:s3cr%40t@db.example:6543/trek_db?sslmode=require'; @@ -176,6 +176,21 @@ describe('pg-tools', () => { expect(readdirSync(out)).toEqual([]); }); + it('PGTOOLS-005b: the restore script is checked line by line, so its COPY data is never read whole', async () => { + const source = path.join(out, 'in.pgdump'); + fakeTool('pg_restore', { + body: `printf '%s\\n' '\\restrict k1' 'CREATE TABLE public.mikro_orm_migrations (' > "\${4#--file=}"; seq 1 200000 >> "\${4#--file=}"`, + }); + fakeTool('psql', { stdout: PUBLIC_SCHEMA }); + const readWhole = vi.spyOn(fs, 'readFileSync'); + try { + await pgRestore(URL_WITH_SECRET, source, bin); + expect(readWhole.mock.calls.filter(([file]) => String(file).endsWith('restore.sql'))).toEqual([]); + } finally { + readWhole.mockRestore(); + } + }); + it("PGTOOLS-006: reads the dump's recorded migrations from its COPY block, and none from a dump without them", async () => { const copy = [ 'SET statement_timeout = 0;',