Skip to content

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

[FE-3379] feat(studio): warn when db passwords need percent-encoding (#47564)

Alaister Young46b31eb53a51
diff --git a/apps/studio/components/interfaces/ConnectSheet/PasswordEncodingNote.tsx b/apps/studio/components/interfaces/ConnectSheet/PasswordEncodingNote.tsx
new file mode 100644
index 00000000000..7c8ea817b03
--- /dev/null
+++ b/apps/studio/components/interfaces/ConnectSheet/PasswordEncodingNote.tsx
@@ -0,0 +1,19 @@
+import { Admonition } from 'ui-patterns/admonition'
+
+import { InlineLink } from '@/components/ui/InlineLink'
+import { SPECIAL_SYMBOLS_IN_PASSWORDS_DOCS_URL } from '@/lib/constants'
+
+export const PasswordEncodingNote = () => {
+ return (
+ <Admonition
+ type="default"
+ description={
+ <>
+ If your database password contains special characters, you will need to{' '}
+ <InlineLink href={SPECIAL_SYMBOLS_IN_PASSWORDS_DOCS_URL}>percent-encode</InlineLink> them
+ in the connection string.
+ </>
+ }
+ />
+ )
+}
diff --git a/apps/studio/components/interfaces/ConnectSheet/content/steps/direct-connection/content.tsx b/apps/studio/components/interfaces/ConnectSheet/content/steps/direct-connection/content.tsx
index 65157cb03dc..64687afd842 100644
--- a/apps/studio/components/interfaces/ConnectSheet/content/steps/direct-connection/content.tsx
+++ b/apps/studio/components/interfaces/ConnectSheet/content/steps/direct-connection/content.tsx
@@ -26,6 +26,7 @@ import {
PASSWORD_PLACEHOLDER,
resolveConnectionString,
} from '@/components/interfaces/ConnectSheet/ConnectionString.utils'
+import { PasswordEncodingNote } from '@/components/interfaces/ConnectSheet/PasswordEncodingNote'
import { ResetDbPasswordDialog } from '@/components/interfaces/Settings/Database/DatabaseSettings/ResetDbPasswordDialog'
import { usePgbouncerConfigQuery } from '@/data/database/pgbouncer-config-query'
import { useSupavisorConfigurationQuery } from '@/data/database/supavisor-configuration-query'
@@ -237,6 +238,7 @@ function DirectConnectionContent({ state, deploymentMode }: StepContentProps) {
<Badge>{poolerBadge}</Badge>
</div>
)}
+ {connectionString.includes(PASSWORD_PLACEHOLDER) && <PasswordEncodingNote />}
<div className="overflow-hidden rounded-lg border bg-surface-75">
<div data-connect-copy-value={redactedConnectionString}>
<CodeBlock
diff --git a/apps/studio/components/interfaces/ConnectSheet/content/steps/direct-files/content.tsx b/apps/studio/components/interfaces/ConnectSheet/content/steps/direct-files/content.tsx
index f15252b3970..210e33eb96e 100644
--- a/apps/studio/components/interfaces/ConnectSheet/content/steps/direct-files/content.tsx
+++ b/apps/studio/components/interfaces/ConnectSheet/content/steps/direct-files/content.tsx
@@ -16,6 +16,7 @@ import {
PASSWORD_PLACEHOLDER,
resolveConnectionString,
} from '@/components/interfaces/ConnectSheet/ConnectionString.utils'
+import { PasswordEncodingNote } from '@/components/interfaces/ConnectSheet/PasswordEncodingNote'
const DOTNET_CONFIG_COMMAND =
'dotnet add package Microsoft.Extensions.Configuration.Json --version YOUR_DOTNET_VERSION'
@@ -27,6 +28,8 @@ type DirectFilesConfig = {
code: string
}[]
connectionStringFile?: string
+ /** The password ends up inside a connection URL, so special characters must be percent-encoded */
+ passwordInUrl?: boolean
postCommands?: { label: string; command: string }[]
}
@@ -79,6 +82,7 @@ export default sql`,
envFile,
],
connectionStringFile: envFile.name,
+ passwordInUrl: true,
}
case 'golang':
@@ -115,6 +119,7 @@ func main() {
envFile,
],
connectionStringFile: envFile.name,
+ passwordInUrl: true,
}
case 'dotnet':
@@ -161,6 +166,7 @@ connection = psycopg2.connect(DATABASE_URL)`,
envFile,
],
connectionStringFile: envFile.name,
+ passwordInUrl: true,
}
case 'sqlalchemy':
@@ -213,6 +219,7 @@ except Exception as e:
},
],
connectionStringFile: '.env',
+ passwordInUrl: true,
}
default:
@@ -242,6 +249,7 @@ except Exception as e:
return (
<div className="flex flex-col gap-3">
<MultipleCodeBlock files={config.files} value={activeFile} onValueChange={setActiveFile} />
+ {config.passwordInUrl && <PasswordEncodingNote />}
<ConnectionParameters parameters={buildConnectionParameters(connectionParams)} />
{(config.postCommands ?? []).map((command) => (
<div key={command.command} className="flex flex-col gap-2">
diff --git a/apps/studio/components/interfaces/ProjectCreation/DatabasePasswordInput.tsx b/apps/studio/components/interfaces/ProjectCreation/DatabasePasswordInput.tsx
index c5b974310d2..46deeeb3ee9 100644
--- a/apps/studio/components/interfaces/ProjectCreation/DatabasePasswordInput.tsx
+++ b/apps/studio/components/interfaces/ProjectCreation/DatabasePasswordInput.tsx
@@ -3,9 +3,7 @@ import { FormControl, FormField } from 'ui'
import { Input } from 'ui-patterns/DataInputs/Input'
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
-import { DATABASE_PASSWORD_REGEX } from './ProjectCreation.constants'
import { CreateProjectForm } from './ProjectCreation.schema'
-import { SpecialSymbolsCallout } from './SpecialSymbolsCallout'
import Panel from '@/components/ui/Panel'
import { PasswordStrengthBar } from '@/components/ui/PasswordStrengthBar'
import { passwordStrength } from '@/lib/password-strength'
@@ -49,23 +47,17 @@ export const DatabasePasswordInput = ({ form }: DatabasePasswordInputProps) => {
control={form.control}
name="dbPass"
render={({ field }) => {
- const isInvalidDatabasePassword =
- field.value.length > 0 && !field.value.match(DATABASE_PASSWORD_REGEX)
-
return (
<FormItemLayout
label="Database password"
layout="horizontal"
description={
- <>
- {isInvalidDatabasePassword && <SpecialSymbolsCallout />}
- <PasswordStrengthBar
- passwordStrengthScore={form.getValues('dbPassStrength')}
- password={field.value}
- passwordStrengthMessage={form.getValues('dbPassStrengthMessage')}
- generateStrongPassword={generatePassword}
- />
- </>
+ <PasswordStrengthBar
+ passwordStrengthScore={form.getValues('dbPassStrength')}
+ password={field.value}
+ passwordStrengthMessage={form.getValues('dbPassStrengthMessage')}
+ generateStrongPassword={generatePassword}
+ />
}
>
<FormControl>
diff --git a/apps/studio/components/interfaces/ProjectCreation/ProjectCreation.constants.ts b/apps/studio/components/interfaces/ProjectCreation/ProjectCreation.constants.ts
index 517d261d47a..2cdb36b8c86 100644
--- a/apps/studio/components/interfaces/ProjectCreation/ProjectCreation.constants.ts
+++ b/apps/studio/components/interfaces/ProjectCreation/ProjectCreation.constants.ts
@@ -255,6 +255,4 @@ export const COUNTRY_LAT_LON = {
ZW: { lat: -20, lon: 30 },
}
-export const DATABASE_PASSWORD_REGEX = /^[^@:\/]*$/
-
export const sizes: DesiredInstanceSize[] = ['micro', 'small', 'medium']
diff --git a/apps/studio/components/interfaces/ProjectCreation/ProjectCreation.test.ts b/apps/studio/components/interfaces/ProjectCreation/ProjectCreation.test.ts
deleted file mode 100644
index 76babdb88d6..00000000000
--- a/apps/studio/components/interfaces/ProjectCreation/ProjectCreation.test.ts
+++ /dev/null
@@ -1,11 +0,0 @@
-import { expect, test } from 'vitest'
-
-import { DATABASE_PASSWORD_REGEX } from './ProjectCreation.constants'
-
-test('Regex test to surface if password contains @, : or /', () => {
- expect(!'teststring'.match(DATABASE_PASSWORD_REGEX)).toEqual(false)
- expect(!'test@string'.match(DATABASE_PASSWORD_REGEX)).toEqual(true)
- expect(!'te:ststring'.match(DATABASE_PASSWORD_REGEX)).toEqual(true)
- expect(!`tests/tring`.match(DATABASE_PASSWORD_REGEX)).toEqual(true)
- expect(!'!#$%^&*()'.match(DATABASE_PASSWORD_REGEX)).toEqual(false)
-})
diff --git a/apps/studio/components/interfaces/ProjectCreation/SpecialSymbolsCallout.tsx b/apps/studio/components/interfaces/ProjectCreation/SpecialSymbolsCallout.tsx
deleted file mode 100644
index a2af77dac29..00000000000
--- a/apps/studio/components/interfaces/ProjectCreation/SpecialSymbolsCallout.tsx
+++ /dev/null
@@ -1,14 +0,0 @@
-import { InlineLink } from '@/components/ui/InlineLink'
-import { DOCS_URL } from '@/lib/constants'
-
-export const SpecialSymbolsCallout = () => {
- return (
- <p className="mb-2">
- Note: If using the Postgres connection string, you will need to{' '}
- <InlineLink href={`${DOCS_URL}/guides/database/postgres/roles#special-symbols-in-passwords`}>
- percent-encode
- </InlineLink>{' '}
- the password
- </p>
- )
-}
diff --git a/apps/studio/components/ui/PasswordStrengthBar.tsx b/apps/studio/components/ui/PasswordStrengthBar.tsx
index 3d5600adf1f..8c96203bdb8 100644
--- a/apps/studio/components/ui/PasswordStrengthBar.tsx
+++ b/apps/studio/components/ui/PasswordStrengthBar.tsx
@@ -1,6 +1,7 @@
import { InlineLinkClassName } from './InlineLink'
+import { SpecialSymbolsCallout } from './SpecialSymbolsCallout'
import { PASSWORD_STRENGTH_COLOR, PASSWORD_STRENGTH_PERCENTAGE } from '@/lib/constants'
-import { PasswordStrengthScore } from '@/lib/password-strength'
+import { passwordNeedsPercentEncoding, PasswordStrengthScore } from '@/lib/password-strength'
interface Props {
passwordStrengthScore: PasswordStrengthScore
@@ -17,6 +18,7 @@ export const PasswordStrengthBar = ({
}: Props) => {
return (
<>
+ {passwordNeedsPercentEncoding(password) && <SpecialSymbolsCallout />}
{password && (
<div
aria-valuemax={100}
diff --git a/apps/studio/components/ui/SpecialSymbolsCallout.tsx b/apps/studio/components/ui/SpecialSymbolsCallout.tsx
new file mode 100644
index 00000000000..4ba5bee5f26
--- /dev/null
+++ b/apps/studio/components/ui/SpecialSymbolsCallout.tsx
@@ -0,0 +1,12 @@
+import { InlineLink } from '@/components/ui/InlineLink'
+import { SPECIAL_SYMBOLS_IN_PASSWORDS_DOCS_URL } from '@/lib/constants'
+
+export const SpecialSymbolsCallout = () => {
+ return (
+ <p className="mb-2 text-warning">
+ Note: If using the Postgres connection string, you will need to{' '}
+ <InlineLink href={SPECIAL_SYMBOLS_IN_PASSWORDS_DOCS_URL}>percent-encode</InlineLink> the
+ password
+ </p>
+ )
+}
diff --git a/apps/studio/lib/constants/index.ts b/apps/studio/lib/constants/index.ts
index 0ed4e00616c..2dd90f2652d 100644
--- a/apps/studio/lib/constants/index.ts
+++ b/apps/studio/lib/constants/index.ts
@@ -68,6 +68,7 @@ export const POSTHOG_URL =
export const USAGE_APPROACHING_THRESHOLD = 0.75
export const DOCS_URL = process.env.NEXT_PUBLIC_DOCS_URL || 'https://supabase.com/docs'
+export const SPECIAL_SYMBOLS_IN_PASSWORDS_DOCS_URL = `${DOCS_URL}/guides/database/postgres/roles#special-symbols-in-passwords`
export const OPT_IN_TAGS = {
AI_SQL: 'AI_SQL_GENERATOR_OPT_IN',
diff --git a/apps/studio/lib/password-strength.test.ts b/apps/studio/lib/password-strength.test.ts
index 0af4784f0b2..ada6a8a5977 100644
--- a/apps/studio/lib/password-strength.test.ts
+++ b/apps/studio/lib/password-strength.test.ts
@@ -1,6 +1,27 @@
import { describe, expect, it } from 'vitest'
-import { passwordStrength } from './password-strength'
+import { passwordNeedsPercentEncoding, passwordStrength } from './password-strength'
+
+describe('passwordNeedsPercentEncoding', () => {
+ it('returns false for passwords that are safe to use in a connection string', () => {
+ expect(passwordNeedsPercentEncoding('')).toBe(false)
+ expect(passwordNeedsPercentEncoding('teststring')).toBe(false)
+ expect(passwordNeedsPercentEncoding('Str0ngPassword123')).toBe(false)
+ expect(passwordNeedsPercentEncoding('with-safe_chars.~!')).toBe(false)
+ })
+
+ it('returns true for passwords with characters that need percent-encoding', () => {
+ expect(passwordNeedsPercentEncoding('test@string')).toBe(true)
+ expect(passwordNeedsPercentEncoding('te:ststring')).toBe(true)
+ expect(passwordNeedsPercentEncoding('tests/tring')).toBe(true)
+ expect(passwordNeedsPercentEncoding('test#string')).toBe(true)
+ expect(passwordNeedsPercentEncoding('test%string')).toBe(true)
+ expect(passwordNeedsPercentEncoding('test+string')).toBe(true)
+ expect(passwordNeedsPercentEncoding('test?string')).toBe(true)
+ expect(passwordNeedsPercentEncoding('test&string')).toBe(true)
+ expect(passwordNeedsPercentEncoding('test string')).toBe(true)
+ })
+})
describe('passwordStrength', () => {
it('returns empty values for message, warning and strength for empty input', async () => {
diff --git a/apps/studio/lib/password-strength.ts b/apps/studio/lib/password-strength.ts
index 7bc72f622a6..6101ebfa70d 100644
--- a/apps/studio/lib/password-strength.ts
+++ b/apps/studio/lib/password-strength.ts
@@ -4,6 +4,19 @@ import { DEFAULT_MINIMUM_PASSWORD_STRENGTH, PASSWORD_STRENGTH } from '@/lib/cons
// but we need to define it here because we don't to import zxcvbn everywhere
export type PasswordStrengthScore = 0 | 1 | 2 | 3 | 4
+/**
+ * True when the password contains characters that must be percent-encoded
+ * before the password can be used in a connection string URL.
+ */
+export function passwordNeedsPercentEncoding(password: string) {
+ try {
+ return password !== encodeURIComponent(password)
+ } catch {
+ // encodeURIComponent throws on lone surrogates
+ return true
+ }
+}
+
export async function passwordStrength(value: string) {
// [Alaister]: Lazy load zxcvbn to avoid bundling it with the main app (it's pretty chunky)
const zxcvbn = await import('zxcvbn').then((module) => module.default)