Repository navigation
fix(mcp): confirm UPDATEs without WHERE when a column name ends in where - #557
Conversation
The UPDATE-without-WHERE check matched "where" followed by whitespace anywhere in the statement, so a column such as elsewhere or nowhere counted as a WHERE clause and execute_sql ran the UPDATE without asking. Match where as a whole word instead. Fixes supabase#556
barryroodt
left a comment
There was a problem hiding this comment.
Thanks for the clear before/after table, makes this easy to check! The fix is right, and I confirmed the new cases fail on main.
Please change one thing: \b only treats ASCII as word characters, so UPDATE films SET where$x = 1; now skips confirmation (main asks today), and éwhere slips through as well. The rest of this file uses [\w$\u0080-�] as its identifier boundary (see explainStart). Something like /(?<![\w$\u0080-�])where(?![\w$\u0080-�])/i covers both. Adding where$x and éwhere rows to your test.each would lock that in.
…565) ## What kind of change does this PR introduce? Bug fix ## What is the current behavior? Fixes #564 This is the follow-up named in the review of #524. `execute_sql` and `apply_migration` ask for confirmation before an `UPDATE` without `WHERE`, but not when the table is written `films*` or `ONLY (films)`. The update runs and changes every row. Postgres spells the target of an `UPDATE` four ways (`relation_expr` in `gram.y`): `t`, `t *`, `ONLY t` and `ONLY (t)`. `updateWithoutWhereRegex` matches only the first and the third, so the other two never reach the `WHERE` check. ## What is the new behavior? `updateWithoutWhereRegex` matches all four. Whitespace around `*` and the parentheses is optional, as it is in Postgres. | query | before | after | | ----- | ------ | ----- | | `update public.films* set id = 2` | runs | asks | | `update films * as f set id = 2` | runs | asks | | `update only (films) set id = 2` | runs | asks | | `update only(public.films)f set id = 2` | runs | asks | | `explain analyze update films* set id = 2` | runs | asks | | any of the above with `where id = 1` | runs | runs | `run_notebook` already asks before any cell that isn't a plain read, and its prompt now counts these statements as destructive. No change to tool names, descriptions, or input and output schemas. Not covered: the spellings under Additional context, which miss for other reasons. `UPDATE ONLY t * ...` isn't valid Postgres but now matches, so the server asks and Postgres then rejects the statement. #426 plans a parser for this check, and the regex stays on as its fallback (#532), so the fix still applies once the parser lands. ## How to Review 1. **UPDATE check** - `packages/mcp-server-supabase/src/tools/destructive-sql.ts` - `updateWithoutWhereRegex` is now built from named parts, like `directDropColumnRegex`. `updateIdentifier` and `updateTable` are the old sub-patterns, unchanged. `updateTarget` is new: it adds `\s*\*\s*` after the table and the `only\s*\(...\)` alternative. The whitespace before the alias and before `SET` now sits after the target and after the alias, since `*` and `)` need none. 2. **Tests** - `packages/mcp-server-supabase/src/tools/destructive-sql.test.ts` - Nine new rows in the `isUpdateWithoutWhere` "matches" table, two in its "with WHERE does not match" table, and one `isDestructiveSql` test with the two statements from the review of #524. **Review questions** - [ ] Is a pattern built from named parts fine, or would you rather have a one-line edit of the literal, as in #524? The built pattern is 252 characters long. - [ ] Is it fine that `UPDATE ONLY t * ...` asks before Postgres rejects it? - [ ] Do you want the spellings under Additional context as a follow-up, or as test cases for #426? ## Verification With only the test file applied on `main` (9092ab9), the 9 new "matches" rows and both `isDestructiveSql` cases fail with `expected false to be true`, and the other 145 tests pass. With the change: - `pnpm --filter @supabase/mcp-server-supabase test:unit --run`: 926 passed (34 files) - `pnpm --filter @supabase/mcp-server-supabase test:integration --run`: 7 passed - `pnpm build` (runs `tsc --noEmit` in each package), `pnpm format:check`, `biome ci .` and `pnpm test:packed-platform-consumer`: clean Through the tools, with `setupModern` and a form-capable client that declines, the mocked Management API, and PGlite holding a table and a child table: | query, through `execute_sql` and `apply_migration` | `main` | this branch | | ----- | ------ | ----------- | | `update public.films* set title = 'x'` | not asked, 4 of 4 rows changed | asked, none changed | | `update only (films) set title = 'x'` | not asked, 2 of 2 parent rows changed | asked, none changed | | either query with `where id = 1` | not asked, 1 row changed | not asked, 1 row changed | I also compared the old and the new classifier on every SQL string in the test file, on 1,874,880 combinations of statement parts and on 2,000,000 random token sequences. No statement stopped asking, and each one that started asking is an `UPDATE` with one of the two targets. Those scripts aren't part of this PR. Not run: the e2e project, which needs `ANTHROPIC_API_KEY`. I haven't tested against a hosted project or with a real MCP client. ## Additional context - Spellings that run without asking on `main` and still do after this change: an unquoted name with a character outside `[A-Za-z0-9_]` (`update café set ...`, `update films$x set ...`), whitespace around the dot or a three-part name, no whitespace next to a quoted name or after `SET` (`update "films"set ...`), a Unicode-escaped name (`update U&"films" set ...`), and `WITH ... UPDATE` or an `UPDATE` first in a `DO` body (under Trade-offs in #408). - Studio's `updateWithoutWhereRegex`, which this one was adapted from, has no `ONLY`, alias, `*` or parentheses. This PR doesn't change Studio. - #557 is open against the same function. The two changes don't overlap, and with both applied the file's 162 tests pass. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
…he WHERE check \b only counts ASCII letters, digits and underscore as word characters, so an identifier such as where$x or éwhere still counted as a WHERE clause. Use the same identifier boundary as the rest of the file.
|
Thank you for pointing it out. I changed the check to use the same identifier boundary as |
barryroodt
left a comment
There was a problem hiding this comment.
Thanks for the quick turnaround! That's exactly what I was after.
🤖 I have created a release *beep* *boop* --- <details><summary>mcp-utils: 0.11.1</summary> ## [0.11.1](mcp-utils-v0.11.0...mcp-utils-v0.11.1) (2026-11-26) ### Bug Fixes * **mcp-ui:** shared project card for create-project and create-branch ([#578](#578)) ([efe189f](efe189f)) </details> <details><summary>mcp-server-supabase: 0.20.0</summary> ## [0.20.0](mcp-server-supabase-v0.19.0...mcp-server-supabase-v0.20.0) (2026-11-26) ### Features * **debugging:** add project issues DEBUG-311 ([#546](#546)) ([01f8021](01f8021)) * **mcp-server-supabase:** add optional schemas parameter to generate_typescript_types ([#592](#592)) ([7532f53](7532f53)) ### Bug Fixes * **debugging:** link docs once per issue in get_issues ([#610](#610)) ([74801d8](74801d8)) * **mcp-server-supabase:** detect import maps uploaded with a ./ prefix ([#591](#591)) ([6b8f4dc](6b8f4dc)) * **mcp-server-supabase:** keep a declined or cancelled SQL confirmation final ([#581](#581)) ([03afe8e](03afe8e)) * **mcp-server-supabase:** match Studio's notebook row_limit and clarify skip and blank-input messages ([#593](#593)) ([ad07f1b](ad07f1b)) * **mcp-server-supabase:** skip cost confirmation for $0 projects on clients without forms ([#608](#608)) ([50cfc37](50cfc37)) * **mcp-server-supabase:** stop reusing a removed import map on edge function redeploy ([#588](#588)) ([56093a6](56093a6)) * **mcp-ui:** shared project card for create-project and create-branch ([#578](#578)) ([efe189f](efe189f)) * **mcp:** confirm UPDATEs without WHERE that follow a comment and set a dollar-quoted value ([#612](#612)) ([468907a](468907a)) * **mcp:** confirm UPDATEs without WHERE that use inheritance syntax ([#565](#565)) ([687b3ee](687b3ee)) * **mcp:** confirm UPDATEs without WHERE when a column name ends in where ([#557](#557)) ([57fa34b](57fa34b)) </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>
What kind of change does this PR introduce?
Bug fix
What is the current behavior?
Fixes #556
execute_sqldoes not ask for confirmation for anUPDATEwithoutWHEREwhen the statement contains a word that ends in "where" followed by whitespace, such aselsewhereornowhere. It asks forwhere(id = 1), which has aWHERE.What is the new behavior?
The
UPDATEcheck looks forwhereas a whole word.update films set elsewhere = 1update films set nowhere = 1update films set title = 'x' where(id = 1)update films set title = 'x' where id = 1update films set title = 'x'How to Review
The check
packages/mcp-server-supabase/src/tools/destructive-sql.tsisUpdateWithoutWhereFromScan:/where\s/ibecomes awherematch that is not preceded or followed by an identifier character (a word character,$or a non-ASCII character).Tests
packages/mcp-server-supabase/src/tools/destructive-sql.test.tselsewhere,nowhere,where$xoréwhereand noWHEREmust match. AWHEREafter such an identifier, before a parenthesis, and after a newline must not.Verification
vitest run src/tools/destructive-sql.test.ts: 151 passed.vitest run src/tools: 228 passed.tsc --noEmitandpnpm format:check: clean.execute_sqlthrough the test harness with a mocked database, before the change: theelsewhereandnowherequeries ran without a confirmation.Additional context
A
WHEREinside a subquery or CTE still counts as the statement'sWHERE. This PR does not change that.