Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Fix unescaped SQL identifiers in row export (#51256)

Joshen Lim2538f7eb292d
diff --git a/apps/studio/components/interfaces/TableGridEditor/TableEntity.utils.test.ts b/apps/studio/components/interfaces/TableGridEditor/TableEntity.utils.test.ts
index e79948922f9..7e9777caf5d 100644
--- a/apps/studio/components/interfaces/TableGridEditor/TableEntity.utils.test.ts
+++ b/apps/studio/components/interfaces/TableGridEditor/TableEntity.utils.test.ts
@@ -25,7 +25,7 @@ describe('TableEntity.utils: formatTableRowsToSQL', () => {
]
const result = formatTableRowsToSQL(table, rows)
- const expected = `INSERT INTO "public"."people" ("id", "name") VALUES (1, 'Person 1'), (2, 'Person 2'), (3, 'Person 3');`
+ const expected = `INSERT INTO public.people (id, name) VALUES (1, 'Person 1'), (2, 'Person 2'), (3, 'Person 3');`
expect(result).toBe(expected)
})
@@ -49,7 +49,7 @@ describe('TableEntity.utils: formatTableRowsToSQL', () => {
]
const result = formatTableRowsToSQL(table, rows)
- const expected = `INSERT INTO "public"."people" ("id", "name") VALUES (1, 'Person 1'), (2, null), (3, 'Person 3');`
+ const expected = `INSERT INTO public.people (id, name) VALUES (1, 'Person 1'), (2, null), (3, 'Person 3');`
expect(result).toBe(expected)
})
@@ -85,7 +85,7 @@ describe('TableEntity.utils: formatTableRowsToSQL', () => {
},
]
const result = formatTableRowsToSQL(table, rows)
- const expected = `INSERT INTO "public"."demo" ("id", "name", "tags", "metadata") VALUES (2, 'Person 1', ARRAY['tag-a','tag-c'], '{"version": 1}'), (3, 'ONeil', ARRAY['tag-a'], '{"version": 1, "name": "O''Neil"}');`
+ const expected = `INSERT INTO public.demo (id, name, tags, metadata) VALUES (2, 'Person 1', ARRAY['tag-a','tag-c'], '{"version": 1}'), (3, 'ONeil', ARRAY['tag-a'], '{"version": 1, "name": "O''Neil"}');`
expect(result).toBe(expected)
})
@@ -116,7 +116,7 @@ describe('TableEntity.utils: formatTableRowsToSQL', () => {
]
const result = formatTableRowsToSQL(table, rows)
- const expected = `INSERT INTO "storage"."buckets" ("id", "public", "avif_autodetection", "file_size_limit", "allowed_mime_types") VALUES ('emails', true, false, 10485760, ARRAY['image/*','image/o''neil']);`
+ const expected = `INSERT INTO storage.buckets (id, public, avif_autodetection, file_size_limit, allowed_mime_types) VALUES ('emails', true, false, 10485760, ARRAY['image/*','image/o''neil']);`
expect(result).toBe(expected)
})
@@ -133,7 +133,7 @@ describe('TableEntity.utils: formatTableRowsToSQL', () => {
const rows = [{ email: "o'[email protected]" }]
const result = formatTableRowsToSQL(table, rows)
- const expected = `INSERT INTO "public"."users" ("email") VALUES ('o''[email protected]');`
+ const expected = `INSERT INTO public.users (email) VALUES ('o''[email protected]');`
expect(result).toBe(expected)
})
@@ -154,6 +154,26 @@ describe('TableEntity.utils: formatTableRowsToSQL', () => {
expect(result).toBe('')
})
+ it('should escape double quotes in schema, table and column identifiers', () => {
+ const table: SupaTable = {
+ id: 1,
+ type: ENTITY_TYPE.TABLE,
+ columns: [
+ { name: 'id', dataType: 'bigint', format: 'int8', position: 0 },
+ { name: 'we"ird', dataType: 'text', format: 'text', position: 1 },
+ ],
+ name: 'pe"ople',
+ schema: 'pub"lic',
+ comment: undefined,
+ estimateRowCount: 1,
+ }
+ const rows = [{ id: 1, 'we"ird': 'value' }]
+
+ const result = formatTableRowsToSQL(table, rows)
+ const expected = `INSERT INTO "pub""lic"."pe""ople" (id, "we""ird") VALUES (1, 'value');`
+ expect(result).toBe(expected)
+ })
+
it('should remove the idx property', () => {
const table: SupaTable = {
id: 1,
@@ -173,7 +193,7 @@ describe('TableEntity.utils: formatTableRowsToSQL', () => {
]
const result = formatTableRowsToSQL(table, rows)
- const expected = `INSERT INTO "public"."people" ("id", "name") VALUES (1, 'Person 1'), (2, 'Person 2');`
+ const expected = `INSERT INTO public.people (id, name) VALUES (1, 'Person 1'), (2, 'Person 2');`
expect(result).toBe(expected)
})
})
diff --git a/apps/studio/components/interfaces/TableGridEditor/TableEntity.utils.ts b/apps/studio/components/interfaces/TableGridEditor/TableEntity.utils.ts
index dc09cc2b9d3..1b9bbb32e8e 100644
--- a/apps/studio/components/interfaces/TableGridEditor/TableEntity.utils.ts
+++ b/apps/studio/components/interfaces/TableGridEditor/TableEntity.utils.ts
@@ -1,3 +1,5 @@
+import { ident } from '@supabase/pg-meta'
+
import { SupaTable } from '@/components/grid/types'
import { Lint } from '@/data/lint/lint-query'
@@ -37,7 +39,7 @@ export const getTablePoliciesUrl = (
export const formatTableRowsToSQL = (table: SupaTable, rows: any[]) => {
if (rows.length === 0) return ''
- const columns = table.columns.map((col) => `"${col.name}"`).join(', ')
+ const columns = table.columns.map((col) => ident(col.name)).join(', ')
const valuesSets = rows
.map((row) => {
@@ -78,7 +80,7 @@ export const formatTableRowsToSQL = (table: SupaTable, rows: any[]) => {
})
.join(', ')
- return `INSERT INTO "${table.schema}"."${table.name}" (${columns}) VALUES ${valuesSets};`
+ return `INSERT INTO ${ident(table.schema)}.${ident(table.name)} (${columns}) VALUES ${valuesSets};`
}
/**
diff --git a/e2e/studio/features/table-editor.spec.ts b/e2e/studio/features/table-editor.spec.ts
index c46a6cc57dd..5336a1bbf78 100644
--- a/e2e/studio/features/table-editor.spec.ts
+++ b/e2e/studio/features/table-editor.spec.ts
@@ -432,7 +432,7 @@ testRunner('table editor', () => {
const downloadSqlPath = await downloadSql.path()
const sqlContent = fs.readFileSync(downloadSqlPath, 'utf-8')
expect(sqlContent).toContain(
- `INSERT INTO "public"."${tableNameUpdated}" ("id", "created_at", "${columnNameUpdated}") VALUES`
+ `INSERT INTO public.${tableNameUpdated} (id, created_at, ${columnNameUpdated}) VALUES`
)
expect(sqlContent).toContain('789')
expect(sqlContent).toContain('456')
@@ -734,7 +734,7 @@ testRunner('table editor', () => {
const downloadSqlPath = await downloadSql.path()
const sqlContent = fs.readFileSync(downloadSqlPath, 'utf-8')
expect(sqlContent).toBe(
- `INSERT INTO "public"."${tableNameDataActions}" ("id", "created_at", "pw_column") VALUES (4, '2025-01-01 12:00:00+00', 'value 4 to export'), (5, '2025-01-01 12:00:00+00', 'value 5 to export'), (6, '2025-01-01 12:00:00+00', 'value 6 to export');`
+ `INSERT INTO public.${tableNameDataActions} (id, created_at, pw_column) VALUES (4, '2025-01-01 12:00:00+00', 'value 4 to export'), (5, '2025-01-01 12:00:00+00', 'value 5 to export'), (6, '2025-01-01 12:00:00+00', 'value 6 to export');`
)
await page.waitForTimeout(1000) // wait for event processing to complete
fs.unlinkSync(downloadSqlPath)