mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 08:29:01 +00:00
## Context Grant-gated query factories returned `queryFn: skipToken` when the caller had no grant. TanStack Query keeps one options object per cache entry, overwritten by whichever observer rendered last. When that was a grant-less observer, invalidation refetched with `skipToken` and failed with `Missing queryFn`, breaking the query for every other observer on the key. #53075 patched two call sites; this fixes the factories. ## Changes - New `grantedQueryOptions` helper (`data/granted-query-options.ts`). Without a grant, the factory sets `queryFn: undefined` instead of `skipToken`, so TanStack borrows the query function from a subscribed observer that has one. The caller's `enabled` applies only when the factory has a query function. - `apiKeysQueryOptions`, `revealedApiKeysQueryOptions`, `branchesQueryOptions`, `branchQueryOptions` and `branchDiffQueryOptions` take caller options as a second argument. Call sites change from `useQuery({ ...f(vars), enabled })` to `useQuery(f(vars, { enabled }))`, so callers can't override the grant gate. - New type-aware `fga/no-spread-granted-query-options` lint rule (error). `grantedQueryOptions` returns a type-only brand, and the rule flags any spread or `Object.assign` of a branded value, however it was built or named. - Restores typed linting as a separate pass (`eslint.type-checks.config.cjs`, run by `pnpm lint` via `lint:type-checks`), as #44678 did. Enabling type info in the main config changes what other plugins report, so it stays separate. - `studio-queries` skill documents the pattern. Callers still pass their own grant, so the UI gates on the same grant the request carries. ## Testing - `data/api-keys/api-keys-query.test.tsx`: a granted and a grant-less component share the key, with the grant-less one rendering last; invalidation via the delete mutation refetches successfully. Fails on master with `Missing queryFn`. - `data/granted-query-options.test.ts`: `enabled` gating, factory fields win over caller fields, `select` type inference. - Lint rule cases in `scripts/__tests__/fga-eslint-rules.test.ts`. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
92 lines
3.3 KiB
TypeScript
92 lines
3.3 KiB
TypeScript
import { useQuery } from '@tanstack/react-query'
|
|
import { Dispatch, SetStateAction, useEffect, useEffectEvent, useRef } from 'react'
|
|
import { toast } from 'sonner'
|
|
|
|
import { RealtimeConfig } from './useRealtimeMessages'
|
|
import { RoleImpersonationPopover } from '@/components/interfaces/RoleImpersonationSelector/RoleImpersonationPopover'
|
|
import { apiKeysQueryOptions, getKeys } from '@/data/api-keys/api-keys-query'
|
|
import { getTemporaryAPIKey } from '@/data/api-keys/temp-api-keys-query'
|
|
import { useProjectPostgrestConfigQuery } from '@/data/config/project-postgrest-config-query'
|
|
import { IS_PLATFORM } from '@/lib/constants'
|
|
import { useGrant } from '@/lib/grants'
|
|
import { getRoleImpersonationJWT } from '@/lib/role-impersonation'
|
|
import { useTrack } from '@/lib/telemetry/track'
|
|
import { useRoleImpersonationStateSnapshot } from '@/state/role-impersonation-state'
|
|
|
|
interface RealtimeTokensPopoverProps {
|
|
config: RealtimeConfig
|
|
onChangeConfig: Dispatch<SetStateAction<RealtimeConfig>>
|
|
}
|
|
|
|
export const RealtimeTokensPopover = ({ config, onChangeConfig }: RealtimeTokensPopoverProps) => {
|
|
const snap = useRoleImpersonationStateSnapshot()
|
|
|
|
// Only the publishable/anon key is used, which doesn't need `reveal`. Denied access is shown by
|
|
// the inspector header, and a temporary key is used as a fallback.
|
|
const apiKeysGrant = useGrant('GET /v1/projects/{ref}/api-keys', {
|
|
projectRef: config.projectRef,
|
|
})
|
|
const { data: apiKeysData } = useQuery(
|
|
apiKeysQueryOptions(
|
|
{ projectRef: config.projectRef, grant: apiKeysGrant.grant },
|
|
{ select: getKeys }
|
|
)
|
|
)
|
|
const { data: postgrestConfig } = useProjectPostgrestConfigQuery(
|
|
{ projectRef: config.projectRef },
|
|
{ enabled: IS_PLATFORM }
|
|
)
|
|
|
|
const jwtSecret = postgrestConfig?.jwt_secret
|
|
|
|
const track = useTrack()
|
|
const onRoleUpdated = useEffectEvent(() => {
|
|
track('realtime_inspector_database_role_updated')
|
|
})
|
|
|
|
// only send a telemetry event if the user changes the role. Don't send an event during initial render.
|
|
const isMounted = useRef(false)
|
|
|
|
useEffect(() => {
|
|
if (isMounted.current) {
|
|
onRoleUpdated()
|
|
}
|
|
isMounted.current = true
|
|
}, [snap.role])
|
|
|
|
useEffect(() => {
|
|
const { anonKey, publishableKey } = apiKeysData ?? {}
|
|
const triggerUpdateTokenBearer = async () => {
|
|
let token: string | undefined
|
|
let bearer: string | null = null
|
|
|
|
if (
|
|
config.projectRef !== undefined &&
|
|
jwtSecret !== undefined &&
|
|
snap.role !== undefined &&
|
|
snap.role.type === 'postgrest'
|
|
) {
|
|
token = publishableKey?.api_key ?? anonKey?.api_key
|
|
await getRoleImpersonationJWT(config.projectRef, jwtSecret, snap.role)
|
|
.then((b) => (bearer = b))
|
|
.catch((err) => toast.error(`Failed to get JWT for role: ${err.message}`))
|
|
} else {
|
|
try {
|
|
const data = await getTemporaryAPIKey({ projectRef: config.projectRef, expiry: 3600 })
|
|
token = data.api_key
|
|
} catch (error) {
|
|
token = publishableKey?.api_key
|
|
}
|
|
}
|
|
if (token) {
|
|
onChangeConfig({ ...config, token, bearer })
|
|
}
|
|
}
|
|
|
|
triggerUpdateTokenBearer()
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [snap.role, apiKeysData])
|
|
|
|
return <RoleImpersonationPopover align="start" variant="connected-on-both" />
|
|
}
|