mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
Part of FE-4772. ## Why Errors from the pg-meta query endpoint didn't say who wrote the SQL, so a Sentry issue like SUPABASE-APP-KXE couldn't tell a user's query from one Studio composed. That event came from the assistant's `execute_sql` tool and was captured by the Vercel AI integration on the server, so it skipped `reportStudioError` and had no fault tags. The Postgres message also went to Sentry as-is, and it can contain user SQL and data. ## What - **`sqlOrigin` is required on `executeSql`.** `'user'` covers SQL the user or an AI agent wrote, in whole or in part: SQL editor, editor panel, Explorer, AI `execute_sql` and `run_notebook`, report blocks, Query Insights EXPLAIN, the policy, function, and cron editors, and running a stored cron job. Everything else is `'studio'`. The policy, function, and cron mutation hooks and `useDbQuery` take `sqlOrigin` too. - **`classifySqlResponseError`** turns a 400 from the query endpoint into `UserSqlError` (`user`, `user_sql`) or `StudioSqlError` (`studio`, `studio_sql`). Pattern rules (connection timeouts) and other statuses keep their classes. - **`UserSqlError` never reaches Sentry.** Its routing is `SAMPLE_NEVER`, `alwaysCapture` doesn't override it, and `beforeSend` drops it. - **`StudioSqlError` is redacted.** Error classes can set `sentryMessage`; for `StudioSqlError` it is the SQLSTATE and the query's `queryName`, a static name that `executeSql` requires for Studio SQL. The query key is never reported, since its segments can hold IDs, schemas, and search terms. `applyStudioErrorPolicy` applies it from `beforeSend` on the client and the server, so it holds however the error was captured. The server config gets a `beforeSend` for this. Events get `query_name` and `sql_state` tags and are grouped by both, since every SQL error shares the `executeSql` stack. - **A custom `handleError` gets the classified error**, matching its declared type. The SQL editor, editor panel, and policy editor rethrow it, so their errors are now `UserSqlError` instead of being reported as `UnexpectedError`. The error keeps Postgres's `position` for the editor's line highlight. The error message, `formattedError`, and `position` stay on the error for the UI. The query-performance Sentry context no longer carries the raw error message, which bypassed redaction. ## Known gaps - Index advisor queries run users' query text from `pg_stat_statements` but are marked `'studio'`. Redaction keeps the text out of Sentry, but their failures count as Studio faults. - 5xx responses for user SQL are still `PlatformError` and keep their message. - The self-hosted MCP `executeSql` calls `executeQuery` directly and isn't covered. ## Testing - Unit tests for `classifySqlResponseError`, `applyStudioErrorPolicy`, and the client `beforeSend`. - Full Studio vitest suite passes except `useReportDateRange.test.ts` (169 vs 168 hourly buckets across the DST change), which this PR doesn't touch.
112 lines
3.8 KiB
TypeScript
112 lines
3.8 KiB
TypeScript
import { ident, safeSql } from '@supabase/pg-meta/src/pg-format'
|
|
import { useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
import { ScrollArea } from 'ui'
|
|
import { ConfirmationModal } from 'ui-patterns/Dialogs/ConfirmationModal'
|
|
import { GenericSkeletonLoader } from 'ui-patterns/ShimmeringLoader'
|
|
import { SimpleCodeBlock } from 'ui-patterns/SimpleCodeBlock'
|
|
|
|
import { useViewDefinitionQuery } from '@/data/database/view-definition-query'
|
|
import { unguarded } from '@/data/fetchers'
|
|
import { lintKeys } from '@/data/lint/keys'
|
|
import { useExecuteSqlMutation } from '@/data/sql/execute-sql-mutation'
|
|
import { Entity, isViewLike } from '@/data/table-editor/table-editor-types'
|
|
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
|
|
|
|
interface ViewEntityAutofixSecurityModalProps {
|
|
table: Entity
|
|
isAutofixViewSecurityModalOpen: boolean
|
|
setIsAutofixViewSecurityModalOpen: (isAutofixViewSecurityModalOpen: boolean) => void
|
|
}
|
|
|
|
export const ViewEntityAutofixSecurityModal = ({
|
|
table,
|
|
isAutofixViewSecurityModalOpen,
|
|
setIsAutofixViewSecurityModalOpen,
|
|
}: ViewEntityAutofixSecurityModalProps) => {
|
|
const { data: project } = useSelectedProjectQuery()
|
|
const queryClient = useQueryClient()
|
|
const {
|
|
isSuccess,
|
|
isPending: isLoading,
|
|
data,
|
|
} = useViewDefinitionQuery(
|
|
{
|
|
id: table?.id,
|
|
projectRef: project?.ref,
|
|
connectionString: project?.connectionString,
|
|
},
|
|
{
|
|
enabled: isAutofixViewSecurityModalOpen && isViewLike(table),
|
|
}
|
|
)
|
|
|
|
const { mutate: execute } = useExecuteSqlMutation({
|
|
onSuccess: async () => {
|
|
toast.success('View security changed successfully')
|
|
setIsAutofixViewSecurityModalOpen(false)
|
|
await queryClient.invalidateQueries({ queryKey: lintKeys.lint(project?.ref) })
|
|
},
|
|
onError: (error) => {
|
|
toast.error(`Failed to autofix view security: ${error.message}`)
|
|
},
|
|
})
|
|
|
|
function handleConfirm() {
|
|
const sql = safeSql`ALTER VIEW ${ident(table.schema)}.${ident(table.name)} SET (security_invoker = on);`
|
|
execute({
|
|
projectRef: project?.ref,
|
|
connectionString: project?.connectionString,
|
|
sqlOrigin: 'studio',
|
|
queryName: 'view-security-invoker-autofix',
|
|
sql,
|
|
grant: unguarded.migrating,
|
|
})
|
|
}
|
|
|
|
if (!isViewLike(table)) {
|
|
return null
|
|
}
|
|
|
|
return (
|
|
<ConfirmationModal
|
|
visible={isAutofixViewSecurityModalOpen}
|
|
size="xlarge"
|
|
title="Confirm autofixing view security"
|
|
confirmLabel="Confirm"
|
|
onCancel={() => setIsAutofixViewSecurityModalOpen(false)}
|
|
onConfirm={() => handleConfirm()}
|
|
>
|
|
<p className="text-sm text-foreground-light">
|
|
Setting <code>security_invoker=on</code> ensures the View runs with the permissions of the
|
|
querying user, reducing the risk of unintended data exposure.
|
|
</p>
|
|
<div className="flex items-center gap-8 mt-8">
|
|
<div className=" border rounded-md w-1/2">
|
|
<div className="p-4 pb-0 bg-200 font-mono text-sm font-semibold">Existing query</div>
|
|
<ScrollArea className="h-[225px] px-4 py-2">
|
|
{isLoading && <GenericSkeletonLoader />}
|
|
{isSuccess && (
|
|
<SimpleCodeBlock>
|
|
{`create view ${table.schema}.${table.name} as\n ${data}`}
|
|
</SimpleCodeBlock>
|
|
)}
|
|
</ScrollArea>
|
|
</div>
|
|
|
|
<div className=" border rounded-md w-1/2">
|
|
<div className="p-4 pb-0 bg-200 font-mono text-sm font-semibold">Updated query</div>
|
|
<ScrollArea className="h-[225px] px-4 py-2">
|
|
{isLoading && <GenericSkeletonLoader />}
|
|
{isSuccess && (
|
|
<SimpleCodeBlock>
|
|
{`create view ${table.schema}.${table.name} with (security_invoker = on) as\n ${data}`}
|
|
</SimpleCodeBlock>
|
|
)}
|
|
</ScrollArea>
|
|
</div>
|
|
</div>
|
|
</ConfirmationModal>
|
|
)
|
|
}
|