From 413f2c96fed0b9e2283aba6b2941e65fe199707f Mon Sep 17 00:00:00 2001 From: 0froq Date: Tue, 17 Nov 2026 21:27:41 +0800 Subject: [PATCH] fix(cli): read the semicolon from raw SQL in the remote confirm check `SELECT '--' ; DROP TABLE users` was stripped as a comment, so the check saw one SELECT and sent the DROP without the confirmation prompt. Read the semicolon from the raw text, where a literal cannot hide it, and keep comment stripping for the read-only prefix test. --- packages/void/src/cli/db.ts | 14 ++++++++------ packages/void/test/unit/db-execute-remote.test.ts | 5 +++++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/packages/void/src/cli/db.ts b/packages/void/src/cli/db.ts index 5e406876c..9ed39807e 100644 --- a/packages/void/src/cli/db.ts +++ b/packages/void/src/cli/db.ts @@ -818,16 +818,18 @@ async function runSeed( /** * Fail-closed check for read-only SQL (SELECT or plain EXPLAIN), which skips the remote - * confirmation prompt. Comments are stripped first so they cannot hide `ANALYZE`. - * Multiple statements and `WITH` (a CTE can write) are rejected, and so is `EXPLAIN ANALYZE` - * in either form, because PostgreSQL executes the analyzed statement. + * confirmation prompt. A semicolon anywhere but the end means a script, and it is read from the + * raw text: a semicolon inside a literal must stay visible, or `SELECT '--' ; DROP TABLE users` + * reads as one comment-only SELECT and skips the prompt. Comments are stripped for the read-only + * test so they cannot hide `ANALYZE`. `WITH` (a CTE can write) is rejected, and so is + * `EXPLAIN ANALYZE` in either form, because PostgreSQL executes the analyzed statement. */ function isSafeStatement(sql: string): boolean { - const decommented = sql.replace(/\/\*[\s\S]*?\*\//g, ' ').replace(/--[^\n]*/g, ' '); - const stripped = decommented.replace(/;\s*$/, ''); - if (stripped.includes(';')) { + if (sql.replace(/;\s*$/, '').includes(';')) { return false; } + const decommented = sql.replace(/\/\*[\s\S]*?\*\//g, ' ').replace(/--[^\n]*/g, ' '); + const stripped = decommented.replace(/;\s*$/, ''); if (/^\s*SELECT\b/i.test(stripped)) { return true; } diff --git a/packages/void/test/unit/db-execute-remote.test.ts b/packages/void/test/unit/db-execute-remote.test.ts index c1eb91173..17db1eb73 100644 --- a/packages/void/test/unit/db-execute-remote.test.ts +++ b/packages/void/test/unit/db-execute-remote.test.ts @@ -427,6 +427,11 @@ describe('void db execute --remote (D1)', () => { ['EXPLAIN SELECT * FROM users', false], ['EXPLAIN (VERBOSE) SELECT 1', false], ['-- header\nSELECT 1', false], + ['SELECT 1;', false], + ["SELECT '--'", false], + ["SELECT '--' ; DROP TABLE users", true], + ["SELECT '/*' ; DROP TABLE users", true], + ['-- a; comment\nSELECT 1', true], ['EXPLAIN ANALYZE DELETE FROM users', true], ['EXPLAIN (ANALYZE) DELETE FROM users', true], ['EXPLAIN (ANALYZE TRUE) UPDATE users SET name = $1', true],