mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 00:19:08 +00:00
## What kind of change does this PR introduce? Feature. ## What is the current behavior? Role passwords are sent as plaintext in the SQL (`create role ... password '...'` and `alter role ... password '...'`), so they can end up in Postgres logs. ## What is the new behavior? ASCII role passwords are hashed in the browser with Web Crypto and sent as a precomputed `SCRAM-SHA-256$...` verifier. Postgres stores a verifier as given, so the plaintext no longer appears in the SQL or the server logs. This applies to every flow that sets a role password, because they all go through the role create and update mutations. Non-ASCII passwords are still sent as plaintext. Postgres normalizes passwords with SASLprep before hashing, and the browser code doesn't apply it, so a verifier built from the raw text could stop the role from logging in. For ASCII, SASLprep changes nothing, so the verifier always matches. The `saslprep` package can't be used here because it reads its table with `fs.readFileSync` at import time. Covering non-ASCII passwords is a follow-up, most likely a server-side API. Studio also falls back to plaintext when `crypto.subtle` isn't available, which browsers only expose in secure contexts. This keeps setting a password working on self-hosted Studio served over plain HTTP. ## Known limitations - The verifier is built with 4096 iterations, the Postgres default. If a project sets `scram_iterations` higher, these roles keep 4096, because Postgres stores a supplied verifier as given. Reading the setting first needs an extra query and a grant for it, so it's left for the follow-up below. - Non-ASCII passwords, and Studio over plain HTTP, still send plaintext. - The follow-up is a server-side path that sets the password without it going through the SQL, which would honor the server's own settings and cover these cases. A verifier isn't secret (it can be attacked offline), but it is a clear improvement over plaintext. ## Testing - Unit tests compare the helper with an independent `node:crypto` implementation, and check that ASCII passwords become verifiers and non-ASCII passwords pass through unchanged. - I also checked against a real Postgres: roles given these verifiers log in with the plaintext password and reject a wrong one. - The `CreateRolePanel` test now expects a verifier and no plaintext in the SQL. Resolves FE-4822.
147 lines
4.8 KiB
TypeScript
147 lines
4.8 KiB
TypeScript
import { fireEvent, screen, waitFor } from '@testing-library/react'
|
|
import userEvent from '@testing-library/user-event'
|
|
import { mockAnimationsApi } from 'jsdom-testing-mocks'
|
|
import { HttpResponse } from 'msw'
|
|
import { beforeEach, describe, expect, test, vi } from 'vitest'
|
|
import z from 'zod'
|
|
|
|
import { CreateRolePanel } from './CreateRolePanel'
|
|
import { ProjectContextProvider } from '@/components/layouts/ProjectLayout/ProjectContext'
|
|
import { customRender } from '@/tests/lib/custom-render'
|
|
import { addAPIMock } from '@/tests/lib/msw'
|
|
import { routerMock } from '@/tests/lib/route-mock'
|
|
|
|
mockAnimationsApi()
|
|
|
|
const FORM_ID = 'create-new-role'
|
|
|
|
const QueryBodySchema = z.object({ query: z.string() })
|
|
|
|
describe('CreateRolePanel', () => {
|
|
let queries: string[]
|
|
|
|
beforeEach(() => {
|
|
queries = []
|
|
routerMock.setCurrentUrl('/project/default/database/roles')
|
|
// useSelectedProjectQuery
|
|
addAPIMock({
|
|
method: 'get',
|
|
path: '/platform/projects/:ref',
|
|
// @ts-expect-error partial project is enough for the connection string lookup
|
|
response: {
|
|
cloud_provider: 'localhost',
|
|
id: 1,
|
|
inserted_at: '2021-08-02T06:40:40.646Z',
|
|
name: 'Default Project',
|
|
organization_id: 1,
|
|
ref: 'default',
|
|
region: 'local',
|
|
status: 'ACTIVE_HEALTHY',
|
|
},
|
|
})
|
|
// useDatabaseRoleCreateMutation executes SQL, and the invalidated roles query lists roles
|
|
addAPIMock({
|
|
method: 'post',
|
|
path: '/platform/pg-meta/:ref/query',
|
|
response: async ({ request }) => {
|
|
const body = QueryBodySchema.parse(await request.json())
|
|
queries.push(body.query)
|
|
return HttpResponse.json<Record<string, unknown>[]>([])
|
|
},
|
|
})
|
|
})
|
|
|
|
const renderPanel = () => {
|
|
const onClose = vi.fn()
|
|
customRender(
|
|
<ProjectContextProvider projectRef="default">
|
|
<CreateRolePanel visible onClose={onClose} />
|
|
</ProjectContextProvider>
|
|
)
|
|
return { onClose }
|
|
}
|
|
|
|
const fillName = async (name: string) => {
|
|
const user = userEvent.setup()
|
|
await user.click(await screen.findByLabelText('Name'))
|
|
await user.paste(name)
|
|
return user
|
|
}
|
|
|
|
const getLoginSwitch = () => screen.getByLabelText('User can login')
|
|
|
|
const getCreateQuery = () => queries.find((query) => query.includes('create role'))
|
|
|
|
test('hides the password field until login is enabled', async () => {
|
|
renderPanel()
|
|
|
|
await screen.findByLabelText('Name')
|
|
expect(screen.queryByLabelText('Password')).not.toBeInTheDocument()
|
|
|
|
await userEvent.click(getLoginSwitch())
|
|
|
|
expect(await screen.findByLabelText('Password')).toBeInTheDocument()
|
|
})
|
|
|
|
test('fills the password field and announces it when a password is generated', async () => {
|
|
renderPanel()
|
|
const user = userEvent.setup()
|
|
await screen.findByLabelText('Name')
|
|
|
|
await user.click(getLoginSwitch())
|
|
const passwordInput = await screen.findByLabelText('Password')
|
|
expect(passwordInput).toHaveValue('')
|
|
|
|
await user.click(screen.getByRole('button', { name: 'Generate a password' }))
|
|
|
|
expect(passwordInput).not.toHaveValue('')
|
|
expect(screen.getByText('Password generated')).toBeInTheDocument()
|
|
|
|
// Editing the generated password clears the announcement
|
|
await user.type(passwordInput, 'x')
|
|
expect(screen.queryByText('Password generated')).not.toBeInTheDocument()
|
|
})
|
|
|
|
test('creates the role with the password when login is enabled', async () => {
|
|
renderPanel()
|
|
const user = await fillName('app_user')
|
|
|
|
await user.click(getLoginSwitch())
|
|
await user.click(await screen.findByLabelText('Password'))
|
|
await user.paste('s3cret')
|
|
fireEvent.submit(document.getElementById(FORM_ID)!)
|
|
|
|
await waitFor(() => expect(getCreateQuery()).toBeDefined())
|
|
// The password is sent as a SCRAM-SHA-256 verifier, not as plaintext
|
|
expect(getCreateQuery()).toContain(`password 'SCRAM-SHA-256$`)
|
|
expect(getCreateQuery()).not.toContain('s3cret')
|
|
})
|
|
|
|
test('omits the password when login is enabled but the field is empty', async () => {
|
|
renderPanel()
|
|
const user = await fillName('app_user')
|
|
|
|
await user.click(getLoginSwitch())
|
|
fireEvent.submit(document.getElementById(FORM_ID)!)
|
|
|
|
await waitFor(() => expect(getCreateQuery()).toBeDefined())
|
|
expect(getCreateQuery()).toContain('login')
|
|
expect(getCreateQuery()).not.toContain('password')
|
|
})
|
|
|
|
test('omits a typed password when login is turned off again', async () => {
|
|
renderPanel()
|
|
const user = await fillName('app_user')
|
|
|
|
await user.click(getLoginSwitch())
|
|
await user.click(await screen.findByLabelText('Password'))
|
|
await user.paste('s3cret')
|
|
await user.click(getLoginSwitch())
|
|
fireEvent.submit(document.getElementById(FORM_ID)!)
|
|
|
|
await waitFor(() => expect(getCreateQuery()).toBeDefined())
|
|
expect(getCreateQuery()).not.toContain('password')
|
|
expect(getCreateQuery()).not.toContain('s3cret')
|
|
})
|
|
})
|