Skip to content

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

Chore/replace url with regex (#29499)

Terry Sutton6d78a98b6396
diff --git a/apps/studio/components/interfaces/Auth/Auth.constants.ts b/apps/studio/components/interfaces/Auth/Auth.constants.ts
index 7ddcf7e7424..1708eb6dce0 100644
--- a/apps/studio/components/interfaces/Auth/Auth.constants.ts
+++ b/apps/studio/components/interfaces/Auth/Auth.constants.ts
@@ -23,8 +23,11 @@ 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,}$/
+
// combine the above regexes
export const urlRegex = new RegExp(
- `(${baseUrlRegex.source})|(${localhostRegex.source})|(${appRegex.source})|(${chromeExtensionRegex.source})|(${customSchemeRegex.source})`,
+ `(?!${excludeSimpleDomainRegex.source})((${baseUrlRegex.source})|(${localhostRegex.source})|(${appRegex.source})|(${chromeExtensionRegex.source})|(${customSchemeRegex.source}))`,
'i'
)
diff --git a/apps/studio/components/interfaces/Auth/RedirectUrls/AddNewURLModal.tsx b/apps/studio/components/interfaces/Auth/RedirectUrls/AddNewURLModal.tsx
index 53449175aa6..adbbbac9e04 100644
--- a/apps/studio/components/interfaces/Auth/RedirectUrls/AddNewURLModal.tsx
+++ b/apps/studio/components/interfaces/Auth/RedirectUrls/AddNewURLModal.tsx
@@ -20,6 +20,7 @@ import {
import { Input } from 'ui-patterns/DataInputs/Input'
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
import { Label } from '@ui/components/shadcn/ui/label'
+import { urlRegex } from '../Auth.constants'
const MAX_URLS_LENGTH = 2 * 1024
@@ -39,7 +40,7 @@ export const AddNewURLModal = ({ visible, allowList, onClose }: AddNewURLModalPr
value: z
.string()
.min(1, 'Please provide a value')
- .url('Please provide a valid URL')
+ .regex(urlRegex, 'Please provide a valid URL')
.refine((value) => !allowList.includes(value), {
message: 'URL already exists in the allow list',
}),
@@ -60,9 +61,8 @@ export const AddNewURLModal = ({ visible, allowList, onClose }: AddNewURLModalPr
})
const onSubmit = (data: z.infer<typeof FormSchema>) => {
- const payload = allowList
- .concat(data.urls.map((url) => url.value.replace(/,\s*$/, '')))
- .toString()
+ const dedupedData = [...new Set(data.urls.map((url) => url.value))]
+ const payload = allowList.concat(dedupedData.map((url) => url.replace(/,\s*$/, ''))).toString()
if (payload.length > MAX_URLS_LENGTH) {
return toast.error('Too many redirect URLs, please remove some or try to use wildcards')
diff --git a/apps/studio/components/interfaces/Auth/RedirectUrls/RedirectUrls.tsx b/apps/studio/components/interfaces/Auth/RedirectUrls/RedirectUrls.tsx
index aeedde4b7c1..23b74dc14f8 100644
--- a/apps/studio/components/interfaces/Auth/RedirectUrls/RedirectUrls.tsx
+++ b/apps/studio/components/interfaces/Auth/RedirectUrls/RedirectUrls.tsx
@@ -74,7 +74,7 @@ export const RedirectUrls = () => {
<div className="flex items-center justify-between">
<FormHeader
title="Redirect URLs"
- description={`URLs that auth providers are permitted to redirect to post authentication. Wildcards are allowed, for example, https://*.domain.com`}
+ description="URLs that auth providers are permitted to redirect to post authentication. Wildcards are allowed, for example, https://*.domain.com"
/>
<div className="flex items-center gap-2 mb-6 ml-12">
<Button asChild type="default" icon={<ExternalLink />}>
diff --git a/apps/studio/components/interfaces/LogDrains/LogDrainDestinationSheetForm.tsx b/apps/studio/components/interfaces/LogDrains/LogDrainDestinationSheetForm.tsx
index 1799508830c..c0c26f9a51d 100644
--- a/apps/studio/components/interfaces/LogDrains/LogDrainDestinationSheetForm.tsx
+++ b/apps/studio/components/interfaces/LogDrains/LogDrainDestinationSheetForm.tsx
@@ -37,13 +37,14 @@ import {
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
import { InfoTooltip } from 'ui-patterns/info-tooltip'
import { DATADOG_REGIONS, LOG_DRAIN_TYPES, LogDrainType } from './LogDrains.constants'
+import { urlRegex } from '../Auth/Auth.constants'
const FORM_ID = 'log-drain-destination-form'
const formUnion = z.discriminatedUnion('type', [
z.object({
type: z.literal('webhook'),
- url: z.string().url('Endpoint URL is required and must be a valid URL'),
+ url: z.string().regex(urlRegex, 'Endpoint URL is required and must be a valid URL'),
http: z.enum(['http1', 'http2']),
gzip: z.boolean(),
headers: z.record(z.string(), z.string()).optional(),
diff --git a/apps/studio/tests/components/Auth/Auth.constants.test.ts b/apps/studio/tests/components/Auth/Auth.constants.test.ts
index 88c02873eb0..0f7f5ff85b4 100644
--- a/apps/studio/tests/components/Auth/Auth.constants.test.ts
+++ b/apps/studio/tests/components/Auth/Auth.constants.test.ts
@@ -1,87 +1,41 @@
import { urlRegex } from 'components/interfaces/Auth/Auth.constants'
-
-describe('Auth.constants: domainRegex', () => {
- test('should validate typical URLs', () => {
- const mockInput1 = 'http://domain.com'
- const output1 = urlRegex.test(mockInput1)
- expect(output1).toBe(true)
-
- const mockInput2 = 'https://supabase.io'
- const output2 = urlRegex.test(mockInput2)
- expect(output2).toBe(true)
-
- const mockInput3 = 'https://new-domain-vercel.com'
- const output3 = urlRegex.test(mockInput3)
- expect(output3).toBe(true)
-
- const mockInput4 = 'www.test-domain.com'
- const output4 = urlRegex.test(mockInput4)
- expect(output4).toBe(true)
- })
- test('should validate app-based domains', () => {
- const mockInput1 = 'exp://exp.host/some-app'
- const output1 = urlRegex.test(mockInput1)
- expect(output1).toBe(true)
-
- const mockInput2 = 'exp://exp.host/some-app?release-channel=default'
- const output2 = urlRegex.test(mockInput2)
- expect(output2).toBe(true)
- })
- test('should validate subdomains', () => {
- const mockInput1 = 'https://supabase.com/dashboard'
- const output1 = urlRegex.test(mockInput1)
- expect(output1).toBe(true)
- })
- test('should validate localhost URLs', () => {
- const mockInput1 = 'http://localhost:3000'
- const output1 = urlRegex.test(mockInput1)
- expect(output1).toBe(true)
- })
- test('should validate URLs with query params', () => {
- const mockInput1 = 'https://supabase.com?name=test'
- const output1 = urlRegex.test(mockInput1)
- expect(output1).toBe(true)
-
- const mockInput2 = 'https://supabase.com?name=test&description=hello&page=2'
- const output2 = urlRegex.test(mockInput2)
- expect(output2).toBe(true)
- })
- test('should validate URLs with wildcards', () => {
- const mockInput1 = 'https://supabase*.com'
- const output1 = urlRegex.test(mockInput1)
- expect(output1).toBe(true)
-
- const mockInput2 = 'https://supabase.com/*'
- const output2 = urlRegex.test(mockInput2)
- expect(output2).toBe(true)
-
- const mockInput3 = 'https://new-*-domain.com/*'
- const output3 = urlRegex.test(mockInput3)
- expect(output3).toBe(true)
-
- const mockInput4 = 'https://new-*-domain.com/*/*/*'
- const output4 = urlRegex.test(mockInput4)
- expect(output4).toBe(true)
-
- const mockInput5 = 'https://sub-*-domain.new-*-domain.com/*/*/*'
- const output5 = urlRegex.test(mockInput5)
- expect(output5).toBe(true)
+import { describe, it, expect } from 'vitest'
+
+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)
+ })
})
- test('should invalidate invalid URLs', () => {
- const mockInput1 = 'supabase'
- const output1 = urlRegex.test(mockInput1)
- expect(output1).toBe(false)
- const mockInput2 = 'mailto:[email protected]'
- const output2 = urlRegex.test(mockInput2)
- expect(output2).toBe(false)
+ it('should not match invalid URLs', () => {
+ const invalidUrls = ['supabase', 'mailto:[email protected]', 'hello world.com', '[email protected]']
- const mockInput4 = 'hello world.com'
- const output4 = urlRegex.test(mockInput4)
- expect(output4).toBe(false)
+ const failingInvalidUrls = invalidUrls.filter((url) => urlRegex.test(url))
+ if (failingInvalidUrls.length > 0) {
+ console.log('Failing invalid URLs:', failingInvalidUrls)
+ }
- const mockInput5 = '[email protected]'
- const output5 = urlRegex.test(mockInput5)
- expect(output5).toBe(false)
+ invalidUrls.forEach((url) => {
+ expect(urlRegex.test(url)).toBe(false)
+ })
})
})