The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
fix(studio): detect update-without-where on quoted table names (#45009)
Alaister Younga5f4a59e0e26
diff --git a/apps/studio/components/interfaces/SQLEditor/SQLEditor.constants.ts b/apps/studio/components/interfaces/SQLEditor/SQLEditor.constants.tsindex 3a89760fe14..3671b1123bd 100644--- a/apps/studio/components/interfaces/SQLEditor/SQLEditor.constants.ts+++ b/apps/studio/components/interfaces/SQLEditor/SQLEditor.constants.ts@@ -40,8 +40,12 @@ export const destructiveSqlRegex = [/^(.*;)?\s*(drop|delete|truncate|alter\s+table\s+.*\s+drop\s+column)\s/is,]+// Matches `UPDATE <table> SET ...` where <table> is any combination of bareword+// or double-quoted identifiers, optionally schema-qualified. Quoted identifiers+// can contain any character (including spaces) and use `""` to escape an inner+// quote, mirroring Postgres syntax.export const updateWithoutWhereRegex =- /(?:^|;)\s*update\s+(?:"[\w.]+"\."[\w.]+"|[\w.]+)\s+set\s+[\w\W]+?(?!\s*where\s)/is+ /(?:^|;)\s*update\s+(?:"(?:[^"]|"")+"|[\w]+)(?:\.(?:"(?:[^"]|"")+"|[\w]+))?\s+set\s+[\w\W]+?(?!\s*where\s)/isexport const alterDatabasePreventConnectionStatements = ['alter database postgres connection limit 0',diff --git a/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.test.ts b/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.test.tsindex 70be6d89c71..0541700b8d1 100644--- a/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.test.ts+++ b/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.test.ts@@ -244,6 +244,51 @@ describe('SQLEditor.utils:updateWithoutWhere', () => {expect(match).toBe(true)})+ it('catches update on a single quoted table name without a where clause', () => {+ const match = isUpdateWithoutWhere(`UPDATE "messages" SET id = 1;`)+ expect(match).toBe(true)+ })++ it('does not flag update on a single quoted table name with a where clause', () => {+ const match = isUpdateWithoutWhere(`UPDATE "messages" SET id = 1 WHERE id = 2;`)+ expect(match).toBe(false)+ })++ it('catches update on a quoted schema with a bareword table without a where clause', () => {+ const match = isUpdateWithoutWhere(`UPDATE "public".messages SET id = 1;`)+ expect(match).toBe(true)+ })++ it('catches update on a bareword schema with a quoted table without a where clause', () => {+ const match = isUpdateWithoutWhere(`UPDATE public."messages" SET id = 1;`)+ expect(match).toBe(true)+ })++ it('catches update on a quoted table name containing a space without a where clause', () => {+ const match = isUpdateWithoutWhere(`UPDATE "my table" SET id = 1;`)+ expect(match).toBe(true)+ })++ it('catches update on a quoted table name containing escaped quotes without a where clause', () => {+ const match = isUpdateWithoutWhere(`UPDATE "weird""name" SET id = 1;`)+ expect(match).toBe(true)+ })++ it('catches update where a quoted identifier contains the word where', () => {+ const match = isUpdateWithoutWhere(`UPDATE "where table" SET id = 1;`)+ expect(match).toBe(true)+ })++ it('catches update where a string literal contains the word where', () => {+ const match = isUpdateWithoutWhere(`UPDATE messages SET name = 'where x';`)+ expect(match).toBe(true)+ })++ it('does not flag update where the only "where" sits inside a string literal but a real where clause exists', () => {+ const match = isUpdateWithoutWhere(`UPDATE messages SET name = 'where x' WHERE id = 1;`)+ expect(match).toBe(false)+ })+it('contains both an update query and a delete query, triggers destructive', () => {const match = checkDestructiveQuery(stripIndent`delete from countries; update countries set name = 'hello';diff --git a/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.ts b/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.tsindex 2c022ada17b..c853da6e533 100644--- a/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.ts+++ b/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.ts@@ -91,13 +91,22 @@ export function checkDestructiveQuery(sql: string) {return destructiveSqlRegex.some((regex) => regex.test(cleanedSql))}+// Replace the contents of single-quoted string literals and double-quoted+// identifiers with empty quotes, so a downstream `where` scan can't be fooled+// by tokens like `UPDATE "where table" SET ...` or `SET name = 'where x'`.+// Postgres uses doubled quotes to escape, so `''` and `""` are matched as+// part of the same span rather than terminating it.+const stripQuotedSpans = (sql: string) =>+ sql.replace(/'(?:''|[^'])*'/g, "''").replace(/"(?:""|[^"])*"/g, '""')+// Function to check for UPDATE queries without WHERE clauseexport function isUpdateWithoutWhere(sql: string): boolean {const updateStatements = sql.split(';').filter((statement) => statement.trim().toLowerCase().startsWith('update'))return updateStatements.some(- (statement) => updateWithoutWhereRegex.test(statement) && !/where\s/i.test(statement)+ (statement) =>+ updateWithoutWhereRegex.test(statement) && !/where\s/i.test(stripQuotedSpans(statement)))}