Skip to content

fix(mcp): don't read semicolons or EXECUTE inside data literals as destructive SQL - #533

Merged
barryroodt merged 2 commits into
supabase:mainfrom
abhiramshaji-cyber:fix/destructive-sql-literals
Nov 9, 2026
Merged

barryroodt merged 2 commits into
supabase:mainfrom
abhiramshaji-cyber:fix/destructive-sql-literals

Conversation

@abhiramshaji-cyber

Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?

Bug fix. Fixes #531.

What is the current behavior?

Thanks so much @zmcray for the really careful write up in #531, and to everyone in the #457 thread. The point raised there about DO '...' bodies shaped this whole change.

Right now execute_sql asks for a destructive confirmation when a plain INSERT, UPDATE or SELECT only has ; drop, ; delete or ; truncate inside a text value, or has the word Execute inside a value:

  1. An agent sends insert into notes (body) values ('Moved the list to 11/3; drop the two duplicates').
  2. checkDestructiveQueryFromScan removes comments but keeps every literal as is, then runs cleanedSql.split(';').
  3. One piece is drop the two duplicates'), which matches /^\s*(drop|delete|truncate)\s/i.
  4. isDestructiveSql returns true, the client gets a confirmation nobody answers, and in an unattended run the write is lost after the timeout.

The same thing happens with $b$...$b$ values, UPDATE values, select 'a; truncate b', and values ('Execute the plan', '{"proposed": "drop"}') (the execute\s+ check matched inside the first value).

What is the new behavior?

Before the existing checks run, a new maskDataLiterals replaces each string, E string or dollar quoted literal with '', unless that literal is code. A literal stays as is when:

  • its statement starts with DO (so do 'begin perform 1; drop table t; end' is still flagged, like today),
  • EXECUTE appears earlier in the same statement, outside any literal (so EXECUTE 'DROP TABLE films' and the format / concat cases work exactly as today),
  • it comes right after AS, which is where function and procedure bodies go (main flags create function ... as 'begin; delete from t; end' today, so I kept that),
  • or it is unterminated, so incomplete input still fails closed.

Everything after that point (the ; split, the ALTER TABLE check, the EXECUTE checks) is unchanged and just reads the masked text. Comments now become a space instead of disappearing, so delete/**/from t is caught too (main read it as deletefrom).

flowchart TD
  A[SQL text] --> B[lexSql spans]
  B --> C{literal?}
  C -- no --> E[keep text]
  C -- yes --> D{"DO statement, EXECUTE earlier,<br/>right after AS, or unterminated?"}
  D -- yes --> E
  D -- no --> F["replace with ''"]
  E --> G[existing checks: split on ;, ALTER, EXECUTE]
  F --> G
Loading

To make sure nothing real got lost, I ran 341 statements through isDestructiveSql on main and on this branch: every string from destructive-sql.test.ts, plus drop, delete, truncate and alter table ... drop wrapped in DO bodies (quoted, E quoted, dollar quoted, with and without LANGUAGE), EXECUTE strings, function and procedure bodies, comments, unterminated input and data literals. Nothing that main flags in code stopped being flagged. The only changes were 37 statements where the destructive words sat inside a data value.

I know @barryroodt has #426 open to parse SQL properly before confirming, which is the real long term answer here. This is meant as a small interim fix on the current scanner, and I'm very happy to close it if #426 is close to landing.

How to Review

  1. Masking

    • packages/mcp-server-supabase/src/tools/sql-data-literals.ts
    • Which literals stay as code (DO, EXECUTE, AS, unterminated) and which become ''.
  2. Call site

    • packages/mcp-server-supabase/src/tools/destructive-sql.ts
    • checkDestructiveQueryFromScan reads the masked text instead of the comment free text; nothing else moved.
  3. Tests

Review questions

  • Do DO, EXECUTE and AS cover the places where a literal is code for you, or is there one I missed?
  • Do the main execution paths preserve existing behavior?
  • Do failures stop safely (unterminated input stays unmasked)?
  • Do tests protect observable behavior rather than implementation details?

Verification

  • pnpm --filter @supabase/mcp-server-supabase typecheck: clean
  • pnpm --filter @supabase/mcp-server-supabase test:unit --run: 891 passed
  • pnpm format:check: clean
  • The 11 new cases that should change fail on main and pass here; the 14 "still matches" cases pass on both.
  • The main vs branch comparison over 341 statements described above.

Additional context

A couple of things I wasn't sure about and would love your take on:

  • Function bodies after AS '...' are still read as code, matching main. But main only flags them when a ; comes before the destructive word, so create function f() returns void as 'delete from t' language sql isn't flagged on main or here. Should those bodies be rechecked as their own statements, or is creating a function (which doesn't run it yet) fine to leave alone?
  • Strings passed to functions that run SQL, like dblink_exec('...') or cron.schedule(..., '...'), are now treated as data. Main only caught them by accident when the text had a ; before the keyword. Is that okay for now, or would you rather keep a short list of those functions as code?

@abhiramshaji-cyber
abhiramshaji-cyber requested a review from a team as a code owner November 6, 2026 15:51

@barryroodt barryroodt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this! I reran the #531 repros and the code-body cases, and they behave as you described. I really like that it's nicely contained too.

On create function ... as 'delete from t' with no semicolon: fine to leave. Creating the function doesn't run it.
The thing to remember is these confirmation prompts are simply serving as a guardrail, we're never going to catch every case with straight regex. The upcoming parser should increase coverage, but even there we're not guaranteed to catch everything.

I answered the dblink_exec/cron.schedule question inline.

@barryroodt
barryroodt merged commit 0dcdd2d into supabase:main Nov 9, 2026
3 of 5 checks passed
@supabase-releaser supabase-releaser Bot mentioned this pull request Nov 9, 2026
barryroodt pushed a commit that referenced this pull request Nov 13, 2026
🤖 I have created a release *beep* *boop*
---


<details><summary>mcp-utils: 0.10.0</summary>

##
[0.10.0](mcp-utils-v0.9.0...mcp-utils-v0.10.0)
(2026-11-13)


### Features

* tag cost-confirmation errors and report caller-input errors as
ToolCallerError ([#543](#543))
([ac366c4](ac366c4))
</details>

<details><summary>mcp-server-supabase: 0.17.0</summary>

##
[0.17.0](mcp-server-supabase-v0.16.1...mcp-server-supabase-v0.17.0)
(2026-11-13)


### ⚠ BREAKING CHANGES

* **elicitation:** split confirmation and secret collection TTLs
([#538](#538))

### Features

* agent feedback capabilities including docs
([#488](#488))
([698ccd5](698ccd5))
* **elicitation:** split confirmation and secret collection TTLs
([#538](#538))
([8a08e48](8a08e48))
* tag cost-confirmation errors and report caller-input errors as
ToolCallerError ([#543](#543))
([ac366c4](ac366c4))


### Bug Fixes

* **content api:** report GraphQL errors that have no locations
([#528](#528))
([6b2fca9](6b2fca9))
* **mcp:** confirm UPDATEs without WHERE that have a leading comment,
ONLY or an alias ([#524](#524))
([7df9fb8](7df9fb8))
* **mcp:** don't read semicolons or EXECUTE inside data literals as
destructive SQL ([#533](#533))
([0dcdd2d](0dcdd2d))
* tell agents the log window, project readiness, docs query and SQL size
rules up front ([#542](#542))
([23f0df0](23f0df0))
</details>

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: supabase-releaser[bot] <223506987+supabase-releaser[bot]@users.noreply.github.com>
barryroodt pushed a commit that referenced this pull request Nov 26, 2026
… a dollar-quoted value (#612)

## What kind of change does this PR introduce?

Bug fix

## What is the current behavior?

Fixes #611

`splitStatements()` keeps `--`, `/*`, and `*/` markers in `blanked`
until callers strip them (#524), so
`valueDollarQuoteStatement.test(blanked)`
(`/^\s*(?:update\s|explain(?![\w$\u0080-\uffff]))/i`) fails whenever a
comment precedes `UPDATE`/`EXPLAIN` or sits directly after `UPDATE`
(`UPDATE/* reset */films`). Leaving dollar-quoted values unmasked causes
`execute_sql`, `apply_migration`, and `run_notebook` (both confirmation
and destructive label) to:

- run comment-prefixed `UPDATE`s without `WHERE` unprompted when a
dollar-quoted value contains `where`, and
- prompt on comment-prefixed `UPDATE ... WHERE` and read-only `EXPLAIN`
/ `EXPLAIN ANALYZE` statements when a dollar-quoted value contains `;`.

Calling `valueDollarQuoteStatement.test(blanked)` on every dollar-quoted
span also repeatedly flattens V8's `+=` rope (`O(N²)` on statements with
many `$$...$$` values).

## What is the new behavior?

`splitStatements()` emits `' '` for `line-comment` and `block-comment`
spans — matching `maskDataLiterals()` in `sql-data-literals.ts` and
taking the alternative raised in #524's review question — and caches
`valueStatement` per statement (resetting on every `;`).
`stripCommentMarkers()` remains for unmasked dollar bodies and the
unterminated fallback.

| Query | `d670445` | This branch |
| --- | --- | --- |
| `/* reset */ UPDATE films SET bio = $$ where 1=1 $$;` | runs | asks |
| `-- reset`<br>`UPDATE films SET bio = $$ where 1=1 $$;` | runs | asks
|
| `UPDATE/* reset */films SET bio = $$ where 1=1 $$;` | runs | asks |
| `/* reset */ UPDATE films SET bio = $$a; b$$ WHERE id = 1;` | asks |
runs |
| `/* plan */ EXPLAIN SELECT $$ ; UPDATE films SET id = 2 $$;` | asks |
runs |
| `/* plan */ EXPLAIN (ANALYZE) UPDATE films SET bio = $$a; b$$ WHERE id
= 1;` | asks | runs |

No changes to tool names, descriptions, or schemas. Keeps the regex
fallback minimal (#532) while #426 / #582 are in flight.

## How to Review

1. **Statement splitting**
   - `packages/mcp-server-supabase/src/tools/destructive-sql.ts`
- `blanked += ' '` in `line-comment` and `block-comment` preserves token
separation (`UPDATE/* reset */films`, `EXPLAIN /**/ ANALYZE`) without
changing `block-comment` `;` splitting or unterminated-comment behavior.
- `valueStatement` resets at every `blanked = ''` site; caching at the
first dollar body is safe because `valueDollarQuoteStatement` checks
only a short leading prefix already settled before any dollar quote
(`sql-lexer.ts` never starts `$` after an identifier character).

2. **Tests**
   - `packages/mcp-server-supabase/src/tools/destructive-sql.test.ts`
- Covers comment-prefixed and glued-comment `UPDATE`s (with/without
`WHERE`), comment-prefixed `EXPLAIN` / `EXPLAIN (ANALYZE FALSE)` /
`EXPLAIN ANALYZE`, an unterminated block comment, and a 40k-comment /
40k-dollar smoke test.

**Review questions**

- [ ] Is normalizing comments to `' '` inside `splitStatements()`
(keeping `stripCommentMarkers()` for unmasked dollar bodies and the
unterminated fallback) the right boundary?
- [ ] Is caching `valueStatement` per statement worth including here?
- [ ] Should a `;` inside a block comment (`UPDATE /* ; */ films SET id
= 2;`) stop splitting statements in a follow-up, or stay fail-closed as
documented?
- [ ] Do the tests guard observable behavior without freezing internals?

## Verification

- **RED on `d670445`:** 16 of 18 new functional assertions fail on
`d670445` and pass here (2 controls pass on both).
- **Suite & build:** `vitest run --project unit` (36 files, 1,048
passed; `destructive-sql.test.ts`: 183 passed), `pnpm build` (4
packages), `tsc --noEmit`, and `biome check` on both touched files — all
clean.
- **Large-input timings (`isDestructiveSql`, `d670445` → branch):** 40k
comments (no-regression) `28.9 ms` → `25.1 ms` (80k: `44.7 ms` → `33.4
ms`); 40k dollar values `3,434.6 ms` → `22.4 ms`; 40k comments + 40k
dollar values `34.3 ms`.
- **Tool path (`execute_sql` and `apply_migration` via built
`dist/index.js`, form-capable client declining, backed by PGlite):** the
3 comment + unfiltered `UPDATE` + `$$ where 1=1 $$` queries ask and
leave both `films` rows untouched (`exec=0`); the 3 safe `UPDATE ...
WHERE` / `EXPLAIN` queries run unprompted (`exec=1`) and mutate 1 row or
0 rows as expected.
- **Not run:** `test:e2e` (needs `ANTHROPIC_API_KEY`) and live hosted
project.

## Additional context

Known remaining cases, not changed here:
- `UPDATE /* ; */ films SET id = 2;` — block-comment `;` still splits
statements as documented (`runs` on both `d670445` and this branch).
- `DO $$ BEGIN PERFORM 1; /* c */ UPDATE films SET id = 2; END $$;` —
`stripCommentMarkers()` leaves comment text inside unmasked dollar
bodies (`runs` on both).
- `INSERT INTO notes (body) VALUES ($b$Kept the deck; update notes set
body = null$b$);` — known false positive (`UPDATE`-path counterpart of
#533).
- `CREATE FUNCTION /*;*/ update (x int) RETURNS void AS $$ SELECT 1;
UPDATE t SET a = 1; $$ LANGUAGE sql;` — now masks its body (`asks` →
`runs`; defining a function does not run it, per #533).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

execute_sql still asks to confirm INSERTs whose text holds "; drop" or "; delete" (the open half of #457)

2 participants