Files
Danny White 9052601faf fix(studio): validate direct database access permissions (#54506)
## Problem

Direct database access requires Database management read access, but
OAuth app settings currently allow saving an invalid combination. The
consent screen’s readiness checks then fail.

## Solution

Show an inline error when direct database access lacks Database
management read access, and prevent saving through the button, keyboard
shortcut, or form submission. Both controls stay editable, and neither
changes the other automatically.

| After |
| --- |
| <img width="1280" height="1103" alt="OAuth Apps Organization Settings
Office Supabase (1)"
src="https://github.com/user-attachments/assets/6cfd203b-d320-46da-b7b5-9ede2e9d814c"
/> |
| _Immediate inline error, cleared as soon as it’s corrected._ |
| <img width="1280" height="1103" alt="OAuth Apps Organization Settings
Office Supabase"
src="https://github.com/user-attachments/assets/f2637d92-c149-4d1a-9d83-130e0f54ce66"
/> |
| _Follow-up footer pattern if that error (or any inline error) is
ignored._ |

## Review instructions

1. Open the [staging
preview](https://studio-staging-git-dnywh-trusted-partner-permis-56807c-supabase.vercel.app/dashboard)
→ Organization → OAuth Apps. Edit an organisation-bound app, or select
**Publish OAuth app**. If your organisation is not a Trusted Partner,
use the DevTools steps below first.
2. Select Direct database access, then set Database management to No
access or Write. Check that the inline error explains that “Database
management” requires at least read access and Confirm stays enabled.
Click Confirm: saving should be blocked and “Fix the errors above to
continue.” should appear in red above the footer actions.
3. Select Database management Read only or Read and write. Check that
the inline and footer errors clear. Confirm should remain enabled.
4. Repeat in the opposite order, or remove Direct database access.
Neither control should change automatically.


<details>
<summary>Review without a Trusted Partner organisation (Chrome
DevTools)</summary>

1. In the Studio dev toolbar’s **Flags** tab, enable
`enableScopedOAuthPublish` if needed.
2. Open Chrome DevTools → **Network**, then open **Publish OAuth app**
or edit an existing app.
3. Select the **GET** request whose URL ends in
`/platform/organizations/YOUR_ORG_SLUG`, without `/oauth/apps` after it.
Its response contains your organisation’s name and slug.
4. Right-click the request → **Override content**. Choose a local folder
and click **Allow**. Add `"is_trusted_partner": true` to the top-level
JSON object, or change the existing value to `true`. Keep every other
field and save with **Cmd/Ctrl+S**.
5. Reload with DevTools open and reopen the app form. For a new app,
choose **An owner, once for the whole organization** under
**Authorization**. Fill in the required name, website and callback URL
so other validation does not interfere.
6. Under **Application permissions**, select **Direct database access →
Read only** while **Database management** has **No access**. Check that
the inline error appears and **Confirm** stays enabled. Click
**Confirm** and check that saving is blocked, with “Fix the errors above
to continue.” in red above the footer actions.
7. Set **Database management → Read only** or **Read and write**. Check
that the inline and footer errors clear, while **Confirm** remains
enabled. Change it to **Write** and check that the error returns.
Neither permission should change automatically.
8. Set **Direct database access → No access**. Check that the inline and
footer errors clear. Cancel the form without saving.
9. Turn off **Sources → Overrides → Enable Local Overrides** and reload
when finished.

The override only changes the response in your browser. It does not
grant backend Trusted Partner eligibility, so use it to review the UI
validation. Saving an app with direct database access requires a
genuinely eligible organisation.

</details>
2026-12-03 13:39:50 +07:00

88 lines
3.1 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { OAuthScope } from '@supabase/shared-types/out/constants'
import { useId, type Dispatch, type SetStateAction } from 'react'
import { cn } from 'ui'
import { isDatabaseManagementReadRequired } from '../OAuthApps.utils'
import { AccessLevelDropdown, type AccessLevel } from './AccessLevelDropdown'
const accessLevels: AccessLevel[] = ['No access', 'Read only', 'Read and write']
export const DIRECT_DATABASE_ACCESS_ID = 'direct-database-access'
interface DirectDatabaseAccessProps {
isMemberBound: boolean
isEligible: boolean
scopes: OAuthScope[]
setScopes: Dispatch<SetStateAction<OAuthScope[]>>
}
export function DirectDatabaseAccess({
isMemberBound,
isEligible,
scopes,
setScopes,
}: DirectDatabaseAccessProps) {
const errorId = useId()
const hasScopeError = isDatabaseManagementReadRequired(scopes)
let accessLevel: AccessLevel = 'No access'
if (scopes.includes(OAuthScope.DATABASE_TOKEN_ACCESS_READ)) accessLevel = 'Read only'
if (scopes.includes(OAuthScope.DATABASE_TOKEN_ACCESS_WRITE)) accessLevel = 'Read and write'
let description =
'Connect directly to Postgres as your app’s database role using its access token.'
if (isMemberBound) {
description =
'This app uses each person’s permissions. To request direct database access, use an app that an organization owner approves for the whole organization.'
} else if (!isEligible) {
description =
'Direct database access is unavailable for this organization. Remove this permission to save changes.'
}
const handleAccessChange = (level: AccessLevel) => {
setScopes((current) => {
const next: OAuthScope[] = current.filter(
(scope) =>
scope !== OAuthScope.DATABASE_TOKEN_ACCESS_READ &&
scope !== OAuthScope.DATABASE_TOKEN_ACCESS_WRITE
)
if (level === 'Read only' || level === 'Read and write') {
next.push(OAuthScope.DATABASE_TOKEN_ACCESS_READ)
}
if (level === 'Read and write') next.push(OAuthScope.DATABASE_TOKEN_ACCESS_WRITE)
return next
})
}
return (
<div
id={DIRECT_DATABASE_ACCESS_ID}
className="grid grid-cols-1 sm:grid-cols-[minmax(0,1fr)_10rem] items-start gap-3 p-4"
>
<div className="flex min-w-0 flex-col">
<p className="text-sm font-medium text-foreground">Direct database access</p>
<p className="text-sm text-foreground-lighter">{description}</p>
</div>
<div className={isMemberBound ? 'cursor-not-allowed' : undefined}>
<AccessLevelDropdown
invalid={hasScopeError}
describedBy={hasScopeError ? errorId : undefined}
permission="Direct database access"
levels={accessLevels}
value={accessLevel}
disabled={isMemberBound}
disabledLevels={isEligible ? [] : ['Read only', 'Read and write']}
onChange={handleAccessChange}
/>
</div>
<p
id={errorId}
role="alert"
className={cn('text-sm text-destructive sm:col-span-2', !hasScopeError && 'sr-only')}
>
{hasScopeError &&
'This permission also requires “Database management” to have at least read access.'}
</p>
</div>
)
}