mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
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>
119 lines
4.0 KiB
TypeScript
119 lines
4.0 KiB
TypeScript
// Regex from: https://stackoverflow.com/a/68002755/4807782
|
|
// modified to accept numbers in the body of domain though
|
|
// examples of matches:
|
|
// "vercel.com"
|
|
// "www.vercel.com"
|
|
// "uptime-monitor-fe.vercel.app"
|
|
// "https://uptime-monitor-fe.vercel.app/"
|
|
|
|
// Supports wildcards, port numbers at the end, paths at the end and query params
|
|
const baseUrlRegex =
|
|
/^((ftp|http|https):\/\/)?(www.)?(?!.*(ftp|http|https|www.))[a-zA-Z0-9_*-]+(\.[a-zA-Z0-9_*-]+)+((\/)[\w#]+)*(\/\w+\?[a-zA-Z0-9_]+=\w+(&[a-zA-Z0-9_]+=\w+)*)+(?:\.[a-z]+)*(?::\d+)?(?![^<]*(?:<\/\w+>|\/?>))(.*)?\/?(.)*?$/gm
|
|
|
|
// iOS deep linking scheme https://benoitpasquier.com/deep-linking-url-scheme-ios/
|
|
const appRegex =
|
|
/^[a-z0-9-]+([.][a-z0-9]+)*:\/(\/[-a-z0-9._~!$&'()*+,;=:@%]+)+(?:\.[a-z]+)*(?::\d+)?(?![^<]*(?:<\/\w+>|\/?>))(.*)?\/?(.)*?$/i
|
|
|
|
// Regex from https://stackoverflow.com/a/18696953/4807782
|
|
const localhostRegex =
|
|
/^(?:^|\s)((https?:\/\/)?(?:localhost|[\w-]+(?:\.[\w-]+)+)(:\d+)?([/?#]\S*)?)/i
|
|
|
|
// "chrome-extension://<extension-id>"
|
|
const chromeExtensionRegex = /chrome-extension:\/\/([a-zA-Z]*)/gm
|
|
|
|
// New regex for custom scheme URLs
|
|
const customSchemeRegex = /^([a-zA-Z][a-zA-Z0-9+.-]*):(?:\/{1,3})?([a-zA-Z0-9_.-]*)$/
|
|
|
|
// Exclude simple domain names without protocol
|
|
const excludeSimpleDomainRegex = /^[a-zA-Z0-9-]+\.[a-zA-Z]{2,}$/
|
|
|
|
const invalidUrlCharacterRegex = /[\s\\]/
|
|
|
|
// combine the above regexes, with optional exclusion of options
|
|
// usage: urlRegex() or urlRegex({ excludeSimpleDomains: false })
|
|
export function urlRegex(
|
|
options: { excludeSimpleDomains?: boolean } = { excludeSimpleDomains: true }
|
|
): RegExp {
|
|
const { excludeSimpleDomains } = options
|
|
const excludeSimpleDomainPart = excludeSimpleDomains
|
|
? `(?!${excludeSimpleDomainRegex.source})`
|
|
: ''
|
|
|
|
// anchored so a valid prefix can't carry trailing input, e.g. `http://localhost:3000\`
|
|
return new RegExp(
|
|
`^(?!.*${invalidUrlCharacterRegex.source})${excludeSimpleDomainPart}((${baseUrlRegex.source})|(${localhostRegex.source})|(${appRegex.source})|(${chromeExtensionRegex.source})|(${customSchemeRegex.source}))$`,
|
|
'i'
|
|
)
|
|
}
|
|
|
|
export function getUrlCharacterError(url: string, label = 'URL'): string | undefined {
|
|
const hasWhitespace = /\s/.test(url)
|
|
if (hasWhitespace) {
|
|
return `${label} can't contain spaces`
|
|
}
|
|
|
|
const hasBackslash = url.includes('\\')
|
|
if (hasBackslash) {
|
|
return `${label} can't contain backslashes`
|
|
}
|
|
|
|
return undefined
|
|
}
|
|
|
|
const INVALID_SITE_URL_MESSAGE = 'Please provide a valid URL, such as https://example.com'
|
|
|
|
const UNSAFE_SITE_URL_PROTOCOLS = ['javascript:', 'data:', 'file:', 'vbscript:']
|
|
|
|
export function getSiteUrlError(url: string): string | undefined {
|
|
const characterError = getUrlCharacterError(url, 'Site URL')
|
|
if (characterError) {
|
|
return characterError
|
|
}
|
|
|
|
const hasWildcard = url.includes('*')
|
|
if (hasWildcard) {
|
|
return "Site URL can't contain wildcards"
|
|
}
|
|
|
|
const isParsable = URL.canParse(url)
|
|
if (!isParsable) {
|
|
return INVALID_SITE_URL_MESSAGE
|
|
}
|
|
|
|
const { protocol, hostname } = new URL(url)
|
|
const isUnsafeProtocol = UNSAFE_SITE_URL_PROTOCOLS.includes(protocol)
|
|
if (isUnsafeProtocol) {
|
|
return `Site URL can't use the ${protocol} protocol`
|
|
}
|
|
|
|
// `localhost:3000` parses with `localhost:` as its scheme, so require `scheme://`
|
|
const hasAuthority = url.slice(protocol.length).startsWith('//')
|
|
const isWebUrl = protocol === 'http:' || protocol === 'https:'
|
|
const isMissingHost = !hasAuthority || (isWebUrl && hostname.length === 0)
|
|
if (isMissingHost) {
|
|
return INVALID_SITE_URL_MESSAGE
|
|
}
|
|
|
|
return undefined
|
|
}
|
|
|
|
export function normalizeRedirectUrl(url: string): string {
|
|
return url.trim().replace(/\s*,\s*$/, '')
|
|
}
|
|
|
|
export function parseRedirectUrls(allowList?: string | null): string[] {
|
|
if (!allowList) return []
|
|
|
|
return Array.from(
|
|
new Set(
|
|
allowList
|
|
.split(',')
|
|
.map(normalizeRedirectUrl)
|
|
.filter((url) => url.length > 0)
|
|
)
|
|
)
|
|
}
|
|
|
|
// Use a const string to represent no chars option. Represented as empty string on the backend side.
|
|
export const NO_REQUIRED_CHARACTERS = 'NO_REQUIRED_CHARS'
|