Files
supabase/apps/studio/components/interfaces/Auth/RLSTester/SandboxManagement.tsx
Joshen Lim 9dc3998fa0 RLS Tester sandbox environment (#45839)
## Context

Resolves FE-3221

Heavily inspired by what @filipecabaco has done previously here:
https://github.com/supabase/supabase/pull/45360

This PR explores the use of pglite to set up a sandbox for RLS testing,
which will pave the way for testing mutation based queries so to ensure
no disruption to the actual database. Sandbox can be set up within the
RLS tester panel as such:
<img width="500" alt="image"
src="https://github.com/user-attachments/assets/0cfdf8e4-dd99-4dee-ac00-39a32b375c07"
/>

Which the sandbox will mimic the project's database to the bare minimum
required
- entities from the `public` schema are copied over (types, tables,
functions, policies)
- `auth` schema is pseudo setup with `SANDBOX_SETUP_STATEMENTS`
- Enough to support role impersonation + querying tables with references
to the auth schema (e.g users table)
- data is seeded up to 100 rows for each table
- More info RE limitations in the last section below

Once sandbox is ready, you'll see this UI where you can either leave the
sandbox, or re-sync the sandbox from the actual database
<img width="500" alt="image"
src="https://github.com/user-attachments/assets/d07ce55f-5bc8-4722-8ce9-898b9b458f9b"
/>

Changes are currently feature flagged, so won't be available publicly
just yet until things are ironed out and ready

## To test
- [ ] Verify that setting up sandbox works
- [ ] Verify that you can query your sandbox, and queries do not touch
the actual database (can verify that we're not sending HTTP requests to
the /query endpoint)
- [ ] Verify correctness of RLS tester as well, should match correctness
with testing against actual DB
- [ ] Verify that re-syncing sandbox picks up changes
- Can test by updating your policies that will affect the output of your
select query
  - e.g SELECT for `authenticated`, change from just `true` to `false`
- [ ] RLS tester should work as per normal (against actual DB) with the
feature flag off with no additional overhead

Let me know of any edge cases you might run into while testing

## Known quirks that will be addressed subsequently
Leaving these for now just to not bloat this PR further
- Pglite schema needs to be re-synced if updating RLS policies while
testing, to ensure that pglite gets the updated policies. Will think
about how to make this more seamless
- Sandbox has its own limitations, will need to add a dialog to inform
users how the sandbox works and what limitations to note of
- e.g only the auth schema is mimicked - so policies that reference
storage helpers won't work (although i think auth is probably the main
use case and the rest might be niche)
  - We can slowly expand tho where required
- Eventually we'll also move forward with figuring out testing mutation
queries with this sandbox

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* RLS tester gains an isolated Postgres sandbox with schema/seed import,
start/refresh/exit controls, and pre-populated auth data.
* Sandbox management UI with setup, loading, active, and error states;
refresh and destroy actions.

* **Bug Fixes**
* Role impersonation now keeps the PostgREST role set to anon while the
tester sheet is open.

* **Chores**
* Content Security Policy updated to allow sandbox/connectivity
endpoints.

* **Style**
  * Minor sheet styling adjustment (top border).

<!-- review_stack_entry_start -->

[![Review Change
Stack](https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg)](https://app.coderabbit.ai/change-stack/supabase/supabase/pull/45839)

<!-- review_stack_entry_end -->
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-05-18 16:22:12 +07:00

106 lines
3.3 KiB
TypeScript

import { Box, Loader2, LogOut, RefreshCw } from 'lucide-react'
import { Badge, Button } from 'ui'
import { Admonition } from 'ui-patterns'
import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
import { usePostgresSandbox } from '@/state/postgres-sandbox/sandbox'
export const SandboxManagement = () => {
const { status, error, isSyncing, startSandbox, destroySandbox, syncSandbox } =
usePostgresSandbox()
if (status === 'idle') {
return (
<Admonition
type="default"
layout="horizontal"
className="min-h-min border-none [&>div>div>div>div>p]:!mb-0"
actions={[
<Button key="set-up" type="default" onClick={() => startSandbox()}>
Set up sandbox
</Button>,
]}
>
<div className="flex items-center gap-x-2">
<p className="text-foreground !m-0">Set up sandbox for testing</p>
<Badge variant="success">Recommended</Badge>
</div>
<p className="text-foreground-light !m-0">
Ensure that queries do not affect your actual database
</p>
</Admonition>
)
}
if (status === 'loading') {
return (
<Admonition
showIcon={false}
type="default"
className="min-h-min border-none py-2 [&>div>div]:flex [&>div>div]:items-center [&>div>div]:justify-between"
>
<div className="flex items-center gap-x-3">
<div className="bg w-6 h-6 rounded border border-border flex items-center justify-center">
<Loader2 size={14} className="animate-spin" />
</div>
<p className="text-xs !mb-0 font-mono uppercase tracking-tight">Setting up sandbox</p>
</div>
</Admonition>
)
}
if (status === 'error') {
return (
<Admonition
type="warning"
layout="horizontal"
title="Unable to set up sandbox"
description={error ?? 'Please try again'}
className="min-h-min border-none"
actions={[
<Button key="set-up" type="default" onClick={() => startSandbox()}>
Retry set up
</Button>,
]}
/>
)
}
return (
<Admonition
showIcon={false}
type="default"
layout="horizontal"
className="min-h-min border-none py-2 [&>div>div>div>div>p]:!mb-0 [&>div>div]:gap-x-2"
actions={[
<ButtonTooltip
key="destroy"
type="default"
icon={<LogOut />}
className="w-7"
disabled={isSyncing}
tooltip={{ content: { side: 'bottom', text: 'Exit sandbox' } }}
onClick={() => destroySandbox()}
/>,
<ButtonTooltip
key="refresh"
type="default"
icon={<RefreshCw />}
className="w-7"
loading={isSyncing}
tooltip={{ content: { side: 'bottom', text: 'Refresh schema' } }}
onClick={() => syncSandbox()}
/>,
]}
>
<div className="flex items-center gap-x-3">
<div className="bg-brand-300 w-6 h-6 rounded border border-brand-500 flex items-center justify-center">
<Box size={14} className="text-brand" />
</div>
<p className="text-xs text-foreground font-mono uppercase tracking-tight">Sandbox active</p>
<p className="text-xs text-foreground-lighter ">Your database is never modified</p>
</div>
</Admonition>
)
}