Skip to content

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

studio: flip executeSql signature to SafeSqlFragment (7/7) (#46007)

Charis12869fcd9fbb
diff --git a/apps/studio/data/sql/execute-sql-query.ts b/apps/studio/data/sql/execute-sql-query.ts
index 337665f9dda..ebb91391401 100644
--- a/apps/studio/data/sql/execute-sql-query.ts
+++ b/apps/studio/data/sql/execute-sql-query.ts
@@ -1,4 +1,8 @@
-import { ROLE_IMPERSONATION_NO_RESULTS, ROLE_IMPERSONATION_SQL_LINE_COUNT } from '@supabase/pg-meta'
+import {
+ ROLE_IMPERSONATION_NO_RESULTS,
+ ROLE_IMPERSONATION_SQL_LINE_COUNT,
+ SafeSqlFragment,
+} from '@supabase/pg-meta'
import { DEFAULT_PLATFORM_APPLICATION_NAME } from '@supabase/pg-meta/src/constants'
import { QueryKey, useQuery } from '@tanstack/react-query'
@@ -24,7 +28,7 @@ export const COST_THRESHOLD_ERROR = 'Query cost exceeds threshold'
export type ExecuteSqlVariables = {
projectRef?: string
connectionString?: string | null
- sql: string
+ sql: SafeSqlFragment
queryKey?: QueryKey
handleError?: (error: ResponseError) => { result: any }
isRoleImpersonationEnabled?: boolean
diff --git a/apps/studio/lib/ai/tools/studio-tools.ts b/apps/studio/lib/ai/tools/studio-tools.ts
index 8e7667b6428..1825636363a 100644
--- a/apps/studio/lib/ai/tools/studio-tools.ts
+++ b/apps/studio/lib/ai/tools/studio-tools.ts
@@ -1,3 +1,4 @@
+import { acceptUntrustedSql, untrustedSql } from '@supabase/pg-meta'
import { tool } from 'ai'
import { z } from 'zod'
@@ -68,8 +69,11 @@ export const getStudioTools = (ctx: StudioToolsContext = {}) => {
inputSchema: executeSqlInputSchema,
needsApproval: true,
execute: async ({ sql }) => {
+ // The `needsApproval: true` gate on this tool means the user has
+ // explicitly approved this AI-generated SQL before execute runs —
+ // that approval is the user gesture that promotes untrusted to safe.
const { result } = await executeSql(
- { projectRef, connectionString, sql },
+ { projectRef, connectionString, sql: acceptUntrustedSql(untrustedSql(sql)) },
undefined,
authHeaders
)