Skip to content

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

Realtime Inspector adjustments (#27751)

Terry Suttonac96a6ae1129
diff --git a/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/PolicyDetailsV2.tsx b/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/PolicyDetailsV2.tsx
index 14a131c1aa5..a362164f809 100644
--- a/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/PolicyDetailsV2.tsx
+++ b/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/PolicyDetailsV2.tsx
@@ -46,6 +46,7 @@ interface PolicyDetailsV2Props {
roles: string
}>
onUpdateCommand: (command: string) => void
+ authContext: 'database' | 'realtime'
}
export const PolicyDetailsV2 = ({
@@ -55,6 +56,7 @@ export const PolicyDetailsV2 = ({
isEditing,
form,
onUpdateCommand,
+ authContext,
}: PolicyDetailsV2Props) => {
const { project } = useProjectContext()
const [open, setOpen] = useState(false)
@@ -100,7 +102,7 @@ export const PolicyDetailsV2 = ({
return (
<>
<div className="px-5 py-5 flex flex-col gap-y-4 border-b">
- <div className="flex items-start justify-between gap-4 grid grid-cols-12">
+ <div className="items-start justify-between gap-4 grid grid-cols-12">
<FormField_Shadcn_
control={form.control}
name="name"
@@ -130,65 +132,78 @@ export const PolicyDetailsV2 = ({
<code className="text-xs">on</code> clause
</p>
</FormLabel_Shadcn_>
- <FormControl_Shadcn_>
- <Popover_Shadcn_ open={open} onOpenChange={setOpen} modal={false}>
- <PopoverTrigger_Shadcn_ asChild>
- <Button
- type="default"
- className="w-full [&>span]:w-full h-[38px] text-sm"
- iconRight={
- <ChevronsUpDown
- className="text-foreground-muted"
- strokeWidth={2}
- size={14}
- />
- }
+ {authContext === 'database' && (
+ <FormControl_Shadcn_>
+ <Popover_Shadcn_ open={open} onOpenChange={setOpen} modal={false}>
+ <PopoverTrigger_Shadcn_ asChild>
+ <Button
+ type="default"
+ className="w-full [&>span]:w-full h-[38px] text-sm"
+ iconRight={
+ <ChevronsUpDown
+ className="text-foreground-muted"
+ strokeWidth={2}
+ size={14}
+ />
+ }
+ >
+ <div className="w-full flex gap-1">
+ <span className="text-foreground">
+ {schema}.{field.value}
+ </span>
+ </div>
+ </Button>
+ </PopoverTrigger_Shadcn_>
+
+ <PopoverContent_Shadcn_
+ className="p-0"
+ side="bottom"
+ align="start"
+ sameWidthAsTrigger
>
- <div className="w-full flex gap-1">
- <span className="text-foreground">
- {schema}.{field.value}
- </span>
- </div>
- </Button>
- </PopoverTrigger_Shadcn_>
- <PopoverContent_Shadcn_
- className="p-0"
- side="bottom"
- align="start"
- sameWidthAsTrigger
- >
- <Command_Shadcn_>
- <CommandInput_Shadcn_ placeholder="Find a table..." />
- <CommandList_Shadcn_>
- <CommandEmpty_Shadcn_>No tables found</CommandEmpty_Shadcn_>
- <CommandGroup_Shadcn_>
- <ScrollArea className={(tables ?? []).length > 7 ? 'h-[200px]' : ''}>
- {(tables ?? []).map((table) => (
- <CommandItem_Shadcn_
- key={table.id}
- className="cursor-pointer flex items-center justify-between space-x-2 w-full"
- onSelect={() => {
- form.setValue('table', table.name)
- setOpen(false)
- }}
- onClick={() => {
- form.setValue('table', table.name)
- setOpen(false)
- }}
- >
- <span className="flex items-center gap-1.5">
- {field.value === table.name ? <Check size={13} /> : ''}
- {table.name}
- </span>
- </CommandItem_Shadcn_>
- ))}
- </ScrollArea>
- </CommandGroup_Shadcn_>
- </CommandList_Shadcn_>
- </Command_Shadcn_>
- </PopoverContent_Shadcn_>
- </Popover_Shadcn_>
- </FormControl_Shadcn_>
+ <Command_Shadcn_>
+ <CommandInput_Shadcn_ placeholder="Find a table..." />
+ <CommandList_Shadcn_>
+ <CommandEmpty_Shadcn_>No tables found</CommandEmpty_Shadcn_>
+ <CommandGroup_Shadcn_>
+ <ScrollArea className={(tables ?? []).length > 7 ? 'h-[200px]' : ''}>
+ {(tables ?? []).map((table) => (
+ <CommandItem_Shadcn_
+ key={table.id}
+ className="cursor-pointer flex items-center justify-between space-x-2 w-full"
+ onSelect={() => {
+ form.setValue('table', table.name)
+ setOpen(false)
+ }}
+ onClick={() => {
+ form.setValue('table', table.name)
+ setOpen(false)
+ }}
+ >
+ <span className="flex items-center gap-1.5">
+ {field.value === table.name ? <Check size={13} /> : ''}
+ {table.name}
+ </span>
+ </CommandItem_Shadcn_>
+ ))}
+ </ScrollArea>
+ </CommandGroup_Shadcn_>
+ </CommandList_Shadcn_>
+ </Command_Shadcn_>
+ </PopoverContent_Shadcn_>
+ </Popover_Shadcn_>
+ </FormControl_Shadcn_>
+ )}
+ {authContext === 'realtime' && (
+ <FormControl_Shadcn_>
+ <Input_Shadcn_
+ disabled
+ value="messages.realtime"
+ className="bg-control border-control"
+ />
+ </FormControl_Shadcn_>
+ )}
+
<FormMessage_Shadcn_ />
</FormItem_Shadcn_>
)}
@@ -236,7 +251,6 @@ export const PolicyDetailsV2 = ({
</FormItem_Shadcn_>
)}
/>
-
<FormField_Shadcn_
control={form.control}
name="command"
@@ -259,7 +273,11 @@ export const PolicyDetailsV2 = ({
}}
className={`grid grid-cols-10 gap-3 ${isEditing ? 'opacity-50' : ''}`}
>
- {['select', 'insert', 'update', 'delete', 'all'].map((x) => (
+ {[
+ 'select',
+ 'insert',
+ ...(authContext === 'database' ? ['update', 'delete', 'all'] : []),
+ ].map((x) => (
<RadioGroupLargeItem_Shadcn_
key={x}
value={x}
@@ -274,7 +292,6 @@ export const PolicyDetailsV2 = ({
</FormItem_Shadcn_>
)}
/>
-
<FormField_Shadcn_
control={form.control}
name="roles"
diff --git a/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/index.tsx b/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/index.tsx
index 1019262ec9c..79c61a87636 100644
--- a/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/index.tsx
+++ b/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/index.tsx
@@ -74,6 +74,7 @@ interface AIPolicyEditorPanelProps {
selectedTable?: string
selectedPolicy?: PostgresPolicy
onSelectCancel: () => void
+ authContext: 'database' | 'realtime'
}
/**
@@ -86,6 +87,7 @@ export const AIPolicyEditorPanel = memo(function ({
selectedTable,
selectedPolicy,
onSelectCancel,
+ authContext,
}: AIPolicyEditorPanelProps) {
const router = useRouter()
const { ref } = useParams()
@@ -547,6 +549,7 @@ export const AIPolicyEditorPanel = memo(function ({
setFieldError(undefined)
if (!['update', 'all'].includes(command)) setShowCheckBlock(false)
}}
+ authContext={authContext}
/>
<div className="h-full">
<LockedCreateQuerySection
diff --git a/apps/studio/components/interfaces/Auth/Policies/PolicyEditorModal/PolicyEditorModal.constants.ts b/apps/studio/components/interfaces/Auth/Policies/PolicyEditorModal/PolicyEditorModal.constants.ts
index ddcecc68903..f59eb3d10d0 100644
--- a/apps/studio/components/interfaces/Auth/Policies/PolicyEditorModal/PolicyEditorModal.constants.ts
+++ b/apps/studio/components/interfaces/Auth/Policies/PolicyEditorModal/PolicyEditorModal.constants.ts
@@ -39,7 +39,7 @@ for select using (true);`.trim(),
statement: `
create policy "Enable insert for authenticated users only"
on "${schema}"."${table}"
-for insert to authenticated
+for insert to authenticated
with check (true);`.trim(),
name: 'Enable insert for authenticated users only',
definition: '',
@@ -110,7 +110,7 @@ for insert with check (
templateName: 'Policy with table joins',
description: `
Query across tables to build more advanced RLS rules
-
+
Assuming 2 tables called \`teams\` and \`members\`, you can query both tables in the policy to control access to the members table.`,
statement: `
create policy "Members can update team details if they belong to the team"
@@ -130,8 +130,8 @@ on teams for update using (
preview: true,
templateName: 'Policy with security definer functions',
description: `
-Useful in a many-to-many relationship where you want to restrict access to the linking table.
-
+Useful in a many-to-many relationship where you want to restrict access to the linking table.
+
Assuming 2 tables called \`teams\` and \`members\`, you can use a security definer function in combination with a policy to control access to the members table.`.trim(),
statement: `
create or replace function get_teams_for_user(user_id uuid)
@@ -158,7 +158,7 @@ for all using (
templateName: 'Policy to implement Time To Live (TTL)',
description: `
Implement a TTL-like feature that you see in Instagram stories or Snapchat where messages expire after a day.
-
+
Rows under the table are available only if they have been created within the last 24 hours.`,
statement: `
create policy "Stories are live for a day"
@@ -199,14 +199,13 @@ using ( realtime.messages.extension = 'broadcast' );`.trim(),
description: 'This policy allows pushing broadcasts for authenticated users only.',
statement: `
create policy "Allow pushing broadcasts for authenticated users only"
-ON realtime.messages for update
+ON realtime.messages for insert
TO authenticated
-using ( realtime.messages.extension = 'broadcast' )
with check ( realtime.messages.extension = 'broadcast' );`.trim(),
name: 'Allow pushing broadcasts for authenticated users only',
definition: "realtime.messages.extension = 'broadcast'",
check: "realtime.messages.extension = 'broadcast'",
- command: 'UPDATE',
+ command: 'INSERT',
roles: ['authenticated'],
},
{
@@ -231,13 +230,12 @@ using ( realtime.messages.extension = 'broadcast' AND realtime.topic() = 'channe
description: 'This policy allow pushing broadcasts to specific channel.',
statement: `
create policy "Allow pushing broadcasts to specific channel"
-ON realtime.messages for update
-using ( realtime.messages.extension = 'broadcast' AND realtime.topic() = 'channel_name' )
+ON realtime.messages for insert
with check ( realtime.messages.extension = 'broadcast' AND realtime.topic() = 'channel_name' );`.trim(),
name: 'Allow pushing broadcasts to specific channel',
definition: `realtime.messages.extension = 'broadcast' AND realtime.topic() = 'channel_name'`,
check: `realtime.messages.extension = 'broadcast' AND realtime.topic() = 'channel_name'`,
- command: 'UPDATE',
+ command: 'INSERT',
roles: [],
},
{
@@ -265,15 +263,14 @@ using ( realtime.messages.extension = 'presence' );`.trim(),
'This policy enables broadcasting presences on all channels for all authenticated users only.',
statement: `
create policy "Allow broadcasting presences on all channels for authenticated users only"
-ON realtime.messages for update
+ON realtime.messages for insert
TO authenticated
-using ( realtime.messages.extension = 'presence' )
with check ( realtime.messages.extension = 'presence' );
;`.trim(),
name: 'Allow broadcasting presences on all channels for authenticated users only',
definition: "realtime.messages.extension = 'presence'",
check: "realtime.messages.extension = 'presence'",
- command: 'UPDATE',
+ command: 'INSERT',
roles: ['authenticated'],
},
{
@@ -298,14 +295,13 @@ using ( realtime.messages.extension = 'presence' AND realtime.topic() = 'channel
description: 'This policy allows publishing presence to a specific channel.',
statement: `
create policy "Publish presence to a specific channel"
-ON realtime.messages for update
-using ( realtime.messages.extension = 'presence' AND realtime.topic() = 'channel_name' )
+ON realtime.messages for insert
with check ( realtime.messages.extension = 'presence' AND realtime.topic() = 'channel_name' );
;`.trim(),
name: 'Publish presence to a specific channel',
definition: `realtime.messages.extension = 'presence' AND realtime.topic() = 'channel_name'`,
check: `realtime.messages.extension = 'presence' AND realtime.topic() = 'channel_name'`,
- command: 'UPDATE',
+ command: 'INSERT',
roles: [],
},
] as PolicyTemplate[]
diff --git a/apps/studio/components/interfaces/Realtime/Inspector/ChooseChannelPopover/index.tsx b/apps/studio/components/interfaces/Realtime/Inspector/ChooseChannelPopover/index.tsx
index 278c1e313e5..28575b47fb0 100644
--- a/apps/studio/components/interfaces/Realtime/Inspector/ChooseChannelPopover/index.tsx
+++ b/apps/studio/components/interfaces/Realtime/Inspector/ChooseChannelPopover/index.tsx
@@ -174,7 +174,7 @@ export const ChooseChannelPopover = ({ config, onChangeConfig }: ChooseChannelPo
<a
target="_blank"
rel="noreferrer"
- href="https://github.com/orgs/supabase/discussions/22484"
+ href="https://supabase.com/docs/guides/realtime/authorization"
>
Documentation
</a>
diff --git a/apps/studio/components/interfaces/Realtime/Inspector/useRealtimeMessages.ts b/apps/studio/components/interfaces/Realtime/Inspector/useRealtimeMessages.ts
index d70fb65d52a..f9e461aac22 100644
--- a/apps/studio/components/interfaces/Realtime/Inspector/useRealtimeMessages.ts
+++ b/apps/studio/components/interfaces/Realtime/Inspector/useRealtimeMessages.ts
@@ -11,6 +11,7 @@ import { useProjectApiQuery } from 'data/config/project-api-query'
import { uuidv4 } from 'lib/helpers'
import { EMPTY_ARR } from 'lib/void'
import type { LogData } from './Messages.types'
+import { useRoleImpersonationStateSnapshot } from 'state/role-impersonation-state'
function reducer(
state: LogData[],
@@ -93,6 +94,8 @@ export const useRealtimeMessages = (
let [client, setClient] = useState<RealtimeClient>()
let [channel, setChannel] = useState<RealtimeChannel | undefined>()
+ const roleImpersonationState = useRoleImpersonationStateSnapshot()
+
useEffect(() => {
if (!enabled) {
return
@@ -175,8 +178,18 @@ export const useRealtimeMessages = (
// Let LiveView know we connected so we can update the button text
// pushMessageTo('#conn_info', 'broadcast_subscribed', { host: host })
+ const role = roleImpersonationState.role?.role
+ const computedRole =
+ role === undefined
+ ? 'service_role_'
+ : role === 'anon'
+ ? 'anon_role_'
+ : role === 'authenticated'
+ ? 'authenticated_role_'
+ : 'user_name_'
+
if (enablePresence) {
- const name = 'user_name_' + Math.floor(Math.random() * 100)
+ const name = computedRole + Math.floor(Math.random() * 100)
newChannel.send({
type: 'presence',
event: 'TRACK',
@@ -185,12 +198,12 @@ export const useRealtimeMessages = (
}
} else if (status === 'CHANNEL_ERROR') {
toast.error(
- 'Failed to connect to the channel: This may be due to restrictive RLS policies. Check your role and try again.'
+ `Failed to connect to the channel ${channelName}: This may be due to restrictive RLS policies. Check your role and try again.`
)
newChannel.unsubscribe()
setChannel(undefined)
- setRealtimeConfig({ ...config, enabled: false })
+ setRealtimeConfig({ ...config, channelName: '', enabled: false })
}
})
diff --git a/apps/studio/components/interfaces/Realtime/Policies.tsx b/apps/studio/components/interfaces/Realtime/Policies.tsx
index a372455a354..daa7c887744 100644
--- a/apps/studio/components/interfaces/Realtime/Policies.tsx
+++ b/apps/studio/components/interfaces/Realtime/Policies.tsx
@@ -66,6 +66,7 @@ export const RealtimePolicies = () => {
schema="realtime"
selectedPolicy={selectedPolicyToEdit}
onSelectCancel={() => setShowPolicyEditor(false)}
+ authContext="realtime"
/>
</div>
)
diff --git a/apps/studio/pages/project/[ref]/auth/policies.tsx b/apps/studio/pages/project/[ref]/auth/policies.tsx
index 855c0775953..f2e5f902345 100644
--- a/apps/studio/pages/project/[ref]/auth/policies.tsx
+++ b/apps/studio/pages/project/[ref]/auth/policies.tsx
@@ -203,6 +203,7 @@ const AuthPoliciesPage: NextPageWithLayout = () => {
setShowPolicyAiEditor(false)
setSelectedPolicyToEdit(undefined)
}}
+ authContext="database"
/>
</div>
)