Skip to content

Commit 0d75c97

Browse files
authored
fix(cli): read the semicolon from raw SQL in the remote confirm check (#869)
1 parent 6c365f2 commit 0d75c97

2 files changed

Lines changed: 13 additions & 6 deletions

File tree

‎packages/void/src/cli/db.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -818,16 +818,18 @@ async function runSeed(
818818

819819
/**
820820
* Fail-closed check for read-only SQL (SELECT or plain EXPLAIN), which skips the remote
821-
* confirmation prompt. Comments are stripped first so they cannot hide `ANALYZE`.
822-
* Multiple statements and `WITH` (a CTE can write) are rejected, and so is `EXPLAIN ANALYZE`
823-
* in either form, because PostgreSQL executes the analyzed statement.
821+
* confirmation prompt. A semicolon anywhere but the end means a script, and it is read from the
822+
* raw text: a semicolon inside a literal must stay visible, or `SELECT '--' ; DROP TABLE users`
823+
* reads as one comment-only SELECT and skips the prompt. Comments are stripped for the read-only
824+
* test so they cannot hide `ANALYZE`. `WITH` (a CTE can write) is rejected, and so is
825+
* `EXPLAIN ANALYZE` in either form, because PostgreSQL executes the analyzed statement.
824826
*/
825827
function isSafeStatement(sql: string): boolean {
826-
const decommented = sql.replace(/\/\*[\s\S]*?\*\//g, ' ').replace(/--[^\n]*/g, ' ');
827-
const stripped = decommented.replace(/;\s*$/, '');
828-
if (stripped.includes(';')) {
828+
if (sql.replace(/;\s*$/, '').includes(';')) {
829829
return false;
830830
}
831+
const decommented = sql.replace(/\/\*[\s\S]*?\*\//g, ' ').replace(/--[^\n]*/g, ' ');
832+
const stripped = decommented.replace(/;\s*$/, '');
831833
if (/^\s*SELECT\b/i.test(stripped)) {
832834
return true;
833835
}

‎packages/void/test/unit/db-execute-remote.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,11 @@ describe('void db execute --remote (D1)', () => {
466466
['EXPLAIN SELECT * FROM users', false],
467467
['EXPLAIN (VERBOSE) SELECT 1', false],
468468
['-- header\nSELECT 1', false],
469+
['SELECT 1;', false],
470+
["SELECT '--'", false],
471+
["SELECT '--' ; DROP TABLE users", true],
472+
["SELECT '/*' ; DROP TABLE users", true],
473+
['-- a; comment\nSELECT 1', true],
469474
['EXPLAIN ANALYZE DELETE FROM users', true],
470475
['EXPLAIN (ANALYZE) DELETE FROM users', true],
471476
['EXPLAIN (ANALYZE TRUE) UPDATE users SET name = $1', true],

0 commit comments

Comments
 (0)