mirror of
https://github.com/supabase/supabase.git
synced 2026-12-03 23:18:57 +00:00
## Problem Flavor of #41370 , but we can see that at least some traffic for this search query is searching using phrases like `supabase password reset` and `supabase password` and then accidentally find themselves on the `/docs/guides/auth/passwords` page. You can see this through user flow mappings in PostHog + also in search query traffic in Google Search Console. <img width="1261" height="669" alt="image" src="https://github.com/user-attachments/assets/70af109c-dbc8-42c2-887a-63133f3f82d5" /> <img width="1128" height="669" alt="image" src="https://github.com/user-attachments/assets/52358d87-55a8-4491-ad26-a0d390ebe55b" /> ## Solution Renames the placeholder across docs / studio / www to `[YOUR-DATABASE-PASSWORD]` and adds an admonition to the current `/docs/guides/auth/passwords` to direct folks to the correct place. Might also be a UX fix in the dash.... but poking into it feel like we already have a lot of affordances there already (`Reset database password` button on the direct connection panel + quick search action for `Reset database password`). ## Preview links ### Studio `https://studio-staging-obj7agk56-supabase.vercel.app/dashboard/project/$PROJECT?showConnect=true&connectTab=direct` <img width="1128" height="801" alt="image" src="https://github.com/user-attachments/assets/420c8582-97bd-45ff-914e-ea9bb25aed91" /> ### Docs https://docs-git-kody-devrel-866-database-password-placeholder-supabase.vercel.app/docs/guides/auth/passwords <img width="838" height="463" alt="image" src="https://github.com/user-attachments/assets/c4bb0242-6ff7-4de7-90c7-386818649209" /> --------- Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
155 lines
5.1 KiB
TypeScript
155 lines
5.1 KiB
TypeScript
import type { ConnectionStringMethod } from './Connect.constants'
|
|
import type { ConnectionStringPooler } from './Connect.types'
|
|
|
|
export const DEFAULT_PORT = '5432'
|
|
export const PASSWORD_PLACEHOLDER = '[YOUR-DATABASE-PASSWORD]'
|
|
|
|
/** Appends query params to a connection string, joining with `?` or `&` as needed */
|
|
export const appendConnectionStringParams = (uri: string, params: string) =>
|
|
!uri || !params ? uri : `${uri}${uri.includes('?') ? '&' : '?'}${params}`
|
|
|
|
export type ConnectionParams = {
|
|
host: string
|
|
port: string
|
|
user: string
|
|
database: string
|
|
/** Raw query string including the leading `?`, or '' when the URI has none */
|
|
search: string
|
|
}
|
|
|
|
export const resolveConnectionString = ({
|
|
connectionMethod,
|
|
useSharedPooler,
|
|
connectionStringPooler,
|
|
}: {
|
|
connectionMethod: ConnectionStringMethod
|
|
useSharedPooler: boolean
|
|
connectionStringPooler: ConnectionStringPooler | undefined
|
|
}) => {
|
|
if (!connectionStringPooler) return ''
|
|
|
|
if (connectionMethod === 'direct') {
|
|
return connectionStringPooler.direct ?? ''
|
|
}
|
|
|
|
if (connectionMethod === 'session') {
|
|
return connectionStringPooler.sessionShared ?? ''
|
|
}
|
|
|
|
if (useSharedPooler || !connectionStringPooler.transactionDedicated) {
|
|
return connectionStringPooler.transactionShared ?? ''
|
|
}
|
|
|
|
return connectionStringPooler.transactionDedicated ?? ''
|
|
}
|
|
|
|
export const parseConnectionParams = (connectionString: string): ConnectionParams => {
|
|
if (!connectionString) {
|
|
return {
|
|
host: 'hidden',
|
|
port: DEFAULT_PORT,
|
|
user: 'hidden',
|
|
database: 'hidden',
|
|
search: '',
|
|
}
|
|
}
|
|
|
|
try {
|
|
const parsed = new URL(connectionString)
|
|
// The URL parser percent-encodes characters that aren't valid in user-info
|
|
// (e.g. brackets in the self-hosted `postgres.[POOLER_TENANT_ID]` placeholder).
|
|
// Decode so the displayed string matches the literal we wrote.
|
|
const decode = (value: string) => {
|
|
try {
|
|
return decodeURIComponent(value)
|
|
} catch {
|
|
return value
|
|
}
|
|
}
|
|
return {
|
|
host: parsed.hostname || 'hidden',
|
|
port: parsed.port || DEFAULT_PORT,
|
|
user: parsed.username ? decode(parsed.username) : 'hidden',
|
|
database: parsed.pathname?.replace(/^\//, '') || 'hidden',
|
|
search: parsed.search,
|
|
}
|
|
} catch (error) {
|
|
return {
|
|
host: 'hidden',
|
|
port: DEFAULT_PORT,
|
|
user: 'hidden',
|
|
database: 'hidden',
|
|
search: '',
|
|
}
|
|
}
|
|
}
|
|
|
|
export const buildSafeConnectionString = (
|
|
connectionString: string,
|
|
params: ConnectionParams
|
|
): string => {
|
|
if (!connectionString) return ''
|
|
|
|
return `postgresql://${params.user}:${PASSWORD_PLACEHOLDER}@${params.host}:${params.port}/${params.database}${params.search}`
|
|
}
|
|
|
|
export const buildPsqlCommand = (params: ConnectionParams) =>
|
|
params.search
|
|
? // Query params (e.g. sslmode) can't be expressed as psql flags, so fall
|
|
// back to the URI form — psql prompts for the password.
|
|
`psql "postgresql://${params.user}@${params.host}:${params.port}/${params.database}${params.search}"`
|
|
: `psql -h ${params.host} -p ${params.port} -d ${params.database} -U ${params.user}`
|
|
|
|
export const buildJdbcString = (params: ConnectionParams) => {
|
|
// pgJDBC (42.7.4+) spells libpq's `sslnegotiation` as `sslNegotiation`
|
|
const extraParams = params.search
|
|
? `&${params.search.slice(1).replace('sslnegotiation=', 'sslNegotiation=')}`
|
|
: ''
|
|
return `jdbc:postgresql://${params.host}:${params.port}/${params.database}?user=${params.user}&password=${PASSWORD_PLACEHOLDER}${extraParams}`
|
|
}
|
|
|
|
/**
|
|
* Ensures a connection string's query params carry `sslmode=require` without
|
|
* dropping params the URI already has (e.g. `options=reference%3D...` or
|
|
* `sslnegotiation=direct`).
|
|
*/
|
|
export const withRequiredSslmode = (search: string) => {
|
|
if (!search) return '?sslmode=require'
|
|
if (search.includes('sslmode=')) return search
|
|
return `${search}&sslmode=require`
|
|
}
|
|
|
|
export const buildDotnetConnectionString = (params: ConnectionParams) => {
|
|
// Multigres only accepts direct SSL negotiation; Npgsql (9+) spells it
|
|
// `SSL Negotiation=Direct` and throws on the parameter in older versions,
|
|
// so only emit it when the resolved URI carries the param.
|
|
const sslNegotiation = params.search.includes('sslnegotiation=direct')
|
|
? ';SSL Negotiation=Direct'
|
|
: ''
|
|
return `Host=${params.host};Port=${params.port};Database=${params.database};Username=${params.user};Password=${PASSWORD_PLACEHOLDER};SSL Mode=Require;Trust Server Certificate=true${sslNegotiation}`
|
|
}
|
|
|
|
export const buildConnectionStringWithPassword = (
|
|
connectionString: string,
|
|
password: string
|
|
): string => {
|
|
if (!connectionString || !password) return connectionString
|
|
|
|
const encodedPassword = (() => {
|
|
try {
|
|
return encodeURIComponent(password)
|
|
} catch {
|
|
return password
|
|
}
|
|
})()
|
|
|
|
return connectionString.split(PASSWORD_PLACEHOLDER).join(encodedPassword)
|
|
}
|
|
|
|
export const buildConnectionParameters = (params: ConnectionParams) => [
|
|
{ key: 'host', value: params.host },
|
|
{ key: 'port', value: params.port },
|
|
{ key: 'database', value: params.database },
|
|
{ key: 'user', value: params.user },
|
|
]
|