Skip to content

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

fix(studio): guard two undefined dereferences crashing the table editor and SQL editor (#49412)

claude[bot]235488e66b33
diff --git a/apps/studio/components/grid/components/grid/Grid.utils.tsx b/apps/studio/components/grid/components/grid/Grid.utils.tsx
index ca19a3fea68..afdcd06bbbd 100644
--- a/apps/studio/components/grid/components/grid/Grid.utils.tsx
+++ b/apps/studio/components/grid/components/grid/Grid.utils.tsx
@@ -20,11 +20,10 @@ export function useOnRowsChange(rows: SupaRow[]) {
(_rows: SupaRow[], data: RowsChangeData<SupaRow, unknown>) => {
const rowData = _rows[data.indexes[0]]
const previousRow = rows.find((x) => x.idx == rowData.idx)
- const changedColumn = Object.keys(rowData).find(
- (name) => rowData[name] !== previousRow![name]
- )
+ if (!previousRow) return
- if (!previousRow || !changedColumn) return
+ const changedColumn = Object.keys(rowData).find((name) => rowData[name] !== previousRow[name])
+ if (!changedColumn) return
const enumArrayColumns = snap.originalTable.columns
?.filter((column) => {
diff --git a/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.test.ts b/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.test.ts
index 5689288a9ac..60425bd3e32 100644
--- a/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.test.ts
+++ b/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.test.ts
@@ -185,6 +185,22 @@ describe('SQLEditor.utils.ts:deriveSnippetIdentity', () => {
})
expect(result).toEqual({ id: 'existing-id', isLoading: false })
})
+ test('is loading when the snippets map itself is missing', () => {
+ const result = deriveSnippetIdentity({
+ urlId: 'existing-id',
+ generatedId: 'generated-id',
+ snippets: undefined as unknown as Record<string, { snippet: { content?: unknown } }>,
+ })
+ expect(result).toEqual({ id: 'existing-id', isLoading: true })
+ })
+ test('is loading when the snippets entry has no snippet on it', () => {
+ const result = deriveSnippetIdentity({
+ urlId: 'existing-id',
+ generatedId: 'generated-id',
+ snippets: { 'existing-id': {} as { snippet: { content?: unknown } } },
+ })
+ expect(result).toEqual({ id: 'existing-id', isLoading: true })
+ })
})
const buildDebugSnippet = (uncheckedSql: string) => ({
diff --git a/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.ts b/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.ts
index 040442d6147..406ac4419ca 100644
--- a/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.ts
+++ b/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.ts
@@ -328,7 +328,9 @@ export function deriveSnippetIdentity({
}): { id: string; isLoading: boolean } {
const id = !urlId || urlId === 'new' ? generatedId : urlId
- const snippetIsLoading = !(id in snippets && snippets[id].snippet.content !== undefined)
+ // `snippets` is typed as always present, but has been seen arriving `undefined` at runtime, and
+ // an entry can be missing its `snippet`. Either case means the content isn't there yet.
+ const snippetIsLoading = snippets?.[id]?.snippet?.content === undefined
const isLoading = urlId === 'new' ? false : snippetIsLoading
return { id, isLoading }