Files
supabase/apps/studio/tests/components/Auth/Auth.constants.test.ts
Alaister YoungandAlaister Young d31c7d1603 [FE-5104] fix(studio): validate site url and redirect urls (#53492)
The Auth URL Configuration page saved Site URLs and redirect URLs
containing spaces or backslashes. `SiteUrl` only checked that the value
was non-empty. `urlRegex()` in `Auth.constants.ts` wasn't anchored, so
any valid prefix passed (`https://foo bar.com`,
`http://localhost:3000\`). This isn't a TanStack regression: both gaps
go back to before the migration (#44803, #30138), and the Next and
TanStack routes render the same `SiteUrl`/`AddNewURLModal` components.

**Changed:**
- `urlRegex()` is anchored and rejects whitespace and backslashes. This
also tightens its other callers (provider forms, SMTP host), which now
reject trailing junk after a valid prefix. The localhost branch also
accepts `?`/`#` right after the host, so scheme-less values such as
`localhost:3000?x=1` that pass today still pass.
- New `getUrlCharacterError` and `getSiteUrlError` helpers give specific
inline errors ("can't contain spaces/backslashes/wildcards", "Please
provide a valid URL, such as https://example.com").
- `SiteUrl` still trims, then rejects interior whitespace, backslashes,
wildcards and non-absolute URLs. `getSiteUrlError` also rejects the
`javascript:`, `data:`, `file:` and `vbscript:` protocols and requires
`scheme://`. Without that, a scheme-less `localhost:3000` would parse
with `localhost:` as its scheme. Custom app schemes such as
`myapp://callback` still work. `onSubmitSiteUrl` is now typed instead of
`any`.
- The Azure, GitLab, Keycloak, WorkOS and SAML URL fields in
`AuthProvidersFormValidation.tsx` now `.trim()` before checking
`urlRegex()`, so a trailing space no longer fails the now-anchored
regex.
- `AddNewURLModal` shows the specific character error before falling
back to "Please provide a valid URL".

**Added:**
- Unit tests for `urlRegex()`, `getUrlCharacterError` and
`getSiteUrlError`, including unsafe protocols and scheme-less
`host:port`.
- `AuthProvidersFormValidation.test.ts`: each provider URL field trims
surrounding whitespace and rejects interior spaces.
- Component tests for `SiteUrl` and `AddNewURLModal` covering spaces,
backslashes, wildcards and invalid URLs, plus saving every supported
redirect URL form. The new rejection tests fail without the fix.
- `AddNewURLModal.test.tsx` now uses MSW (`customRender` + `addAPIMock`)
instead of `vi.mock('@/data/...')`.

Addresses
[FE-5104](https://linear.app/supabase/issue/FE-5104/dashboard-allows-spaces-in-the-site-url).

## To test
- [ ] Auth > URL Configuration, set Site URL to `https://my
app.example.com` and save – "Site URL can't contain spaces", nothing
saved
- [ ] Site URL `http://localhost:3000\` – "Site URL can't contain
backslashes"
- [ ] Site URL `https://*.example.com` – "Site URL can't contain
wildcards"
- [ ] Site URL `example.com` or `localhost:3000` – "Please provide a
valid URL, such as https://example.com"
- [ ] Site URL `javascript:alert(1)` – "Site URL can't use the
javascript: protocol"
- [ ] Site URL `myapp://callback` – saves
- [ ] Site URL ` http://localhost:3000 ` – saves as
`http://localhost:3000`
- [ ] Add URL, enter `https://my app.example.com` – "URL can't contain
spaces"; `myapp://callback\` – "URL can't contain backslashes"
- [ ] Sign In / Providers > Keycloak, enter a Realm URL with a trailing
space and save – no "Must be a valid URL" error; saved without the space
- [ ] Add URL, paste `https://*.example.com/** http://localhost:3000/*
com.example.app://callback chrome-extension://abcdefghijklmnop` – splits
into 4 rows and saves all 4

Ran typecheck, lint ratchet, knip, Prettier, and the Auth vitest suites.

---------

Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
2026-11-17 16:31:59 +09:00

124 lines
4.5 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import {
getSiteUrlError,
getUrlCharacterError,
normalizeRedirectUrl,
parseRedirectUrls,
urlRegex,
} from '@/components/interfaces/Auth/Auth.constants'
describe('Auth.constants: urlRegex', () => {
it('should match valid URLs', () => {
const validUrls = [
'http://domain.com',
'https://supabase.io',
'https://new-domain-vercel.com',
'www.test-domain.com',
'exp://exp.host/some-app',
'exp://exp.host/some-app?release-channel=default',
'https://supabase.com/dashboard',
'http://localhost:3000',
'https://supabase.com?name=test',
'https://supabase.com?name=test&description=hello&page=2',
'https://supabase*.com',
'https://supabase.com/*',
'https://new-*-domain.com/*',
'https://new-*-domain.com/*/*/*',
'https://sub-*-domain.new-*-domain.com/*/*/*',
]
validUrls.forEach((url) => {
expect(urlRegex().test(url)).toBe(true)
})
})
it('should not match invalid URLs', () => {
const invalidUrls = [
'supabase',
'mailto:test@gmail.com',
'hello world.com',
'email@domain.com',
'https://my app.example.com',
'https://example.com/auth callback',
'https://a.example.com https://b.example.com',
'http://localhost:3000\\',
'https://example.com\\callback',
'myapp://callback\\',
'not a url chrome-extension://abcdefghijklmnop',
]
invalidUrls.forEach((url) => {
expect(urlRegex().test(url)).toBe(false)
})
})
it('only matches simple domain URLs when excludeSimpleDomains is false', () => {
const simpleDomainUrl = 'smtp-pulse.com'
expect(urlRegex({ excludeSimpleDomains: true }).test(simpleDomainUrl)).toBe(false)
expect(urlRegex({ excludeSimpleDomains: false }).test(simpleDomainUrl)).toBe(true)
})
it('rejects whitespace and backslashes in simple domains too', () => {
expect(urlRegex({ excludeSimpleDomains: false }).test('smtp.example.com\\')).toBe(false)
expect(urlRegex({ excludeSimpleDomains: false }).test('smtp.example .com')).toBe(false)
})
it('normalizes redirect URLs before saving', () => {
expect(normalizeRedirectUrl(' https://example.com/path , ')).toBe('https://example.com/path')
})
it('parses stored redirect URLs into trimmed unique values', () => {
expect(
parseRedirectUrls(
'https://example.com/callback, https://example.com/callback , https://example.com/next'
)
).toEqual(['https://example.com/callback', 'https://example.com/next'])
})
})
describe('Auth.constants: getUrlCharacterError', () => {
it.each([
['https://example.com/*', undefined],
['https://my app.example.com', "URL can't contain spaces"],
['https://example.com/\tcallback', "URL can't contain spaces"],
['https://example.com\\', "URL can't contain backslashes"],
])('%s', (url, expected) => {
expect(getUrlCharacterError(url)).toBe(expected)
})
})
describe('Auth.constants: getSiteUrlError', () => {
it.each([
'https://example.com',
'https://example.com/auth/callback?next=1',
'http://localhost:3000',
'http://127.0.0.1:3000',
'com.example.app://callback',
'myapp://callback',
'myapp://',
'exp://192.168.1.5:19000',
])('accepts %s', (url) => {
expect(getSiteUrlError(url)).toBeUndefined()
})
it.each([
['https://my app.example.com', "Site URL can't contain spaces"],
['http://localhost:3000\\', "Site URL can't contain backslashes"],
['https://*.example.com', "Site URL can't contain wildcards"],
['https://example.com/**', "Site URL can't contain wildcards"],
['example.com', 'Please provide a valid URL, such as https://example.com'],
['/auth/callback', 'Please provide a valid URL, such as https://example.com'],
['localhost:3000', 'Please provide a valid URL, such as https://example.com'],
['example.com:8080/auth', 'Please provide a valid URL, such as https://example.com'],
['https:example.com', 'Please provide a valid URL, such as https://example.com'],
['javascript:alert(1)', "Site URL can't use the javascript: protocol"],
['JavaScript://example.com/%0Aalert(1)', "Site URL can't use the javascript: protocol"],
['data:text/html,hello', "Site URL can't use the data: protocol"],
['file:///tmp', "Site URL can't use the file: protocol"],
['vbscript:msgbox(1)', "Site URL can't use the vbscript: protocol"],
])('rejects %s', (url, expected) => {
expect(getSiteUrlError(url)).toBe(expected)
})
})