mirror of
https://github.com/supabase/supabase.git
synced 2026-05-06 08:56:46 -04:00
0433eeb5f5
Mark provenance of SQL via the branded types SafeSqlFragment and UntrustedSqlFragment. Only SafeSqlFragment should be executed; UntrustedSqlFragments require some kind of implicit user approval (show on screen + user has to click something) before they are promoted to SafeSqlFragment. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Editor and RLS tester show loading states for inferred/generated SQL and include a dedicated user SQL editor for safer edits. * **Refactor** * Platform-wide SQL handling tightened: snippets and AI-generated SQL are treated as untrusted/display-only until promoted, improving safety and consistency. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
28 lines
1.3 KiB
TypeScript
28 lines
1.3 KiB
TypeScript
// Remap `sql` → `unchecked_sql` on SQL snippet content objects as they cross the API boundary.
|
|
// The API stores and returns the field as `sql`; the frontend type uses `unchecked_sql` to make
|
|
// it explicit that this value must never be executed without user confirmation.
|
|
import { untrustedSql } from '@supabase/pg-meta'
|
|
|
|
export function remapSqlContentField<T extends { type: string }>(item: T): T {
|
|
if (item.type !== 'sql') return item
|
|
if (!('content' in item)) return item
|
|
const content = item.content as Record<string, unknown>
|
|
if (!('sql' in content)) return item
|
|
const { sql, ...rest } = content
|
|
return { ...item, content: { ...rest, unchecked_sql: untrustedSql(sql as string) } } as T
|
|
}
|
|
|
|
export function remapSqlContentFields<T extends { type: string }>(items: Array<T>): Array<T> {
|
|
return items.map(remapSqlContentField)
|
|
}
|
|
|
|
// Reverse remap: `unchecked_sql` → `sql` before sending to the API.
|
|
export function unmapSqlContentField<T extends { type: string }>(item: T): T {
|
|
if (item.type !== 'sql') return item
|
|
if (!('content' in item)) return item
|
|
const content = item.content as Record<string, unknown>
|
|
if (!('unchecked_sql' in content)) return item
|
|
const { unchecked_sql, ...rest } = content
|
|
return { ...item, content: { ...rest, sql: unchecked_sql } } as T
|
|
}
|