Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/app/api/auth/confirm/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import { z } from 'zod'
import { AUTH_URLS } from '@/configs/urls'
import { OtpTypeSchema } from '@/core/modules/auth/models'
import { l } from '@/core/shared/clients/logger/logger'
import { httpUrlSchema } from '@/core/shared/schemas/url'
import { encodedRedirect, isExternalOrigin } from '@/lib/utils/auth'

const confirmSchema = z.object({
token_hash: z.string().min(1),
type: OtpTypeSchema,
next: z.httpUrl(),
next: httpUrlSchema,
})

/**
Expand Down
3 changes: 2 additions & 1 deletion src/core/modules/auth/models.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import z from 'zod'
import { httpUrlSchema } from '@/core/shared/schemas/url'

export const OtpTypeSchema = z.enum([
'signup',
Expand All @@ -14,7 +15,7 @@ export type OtpType = z.infer<typeof OtpTypeSchema>
export const ConfirmEmailInputSchema = z.object({
token_hash: z.string().min(1),
type: OtpTypeSchema,
next: z.httpUrl(),
next: httpUrlSchema,
})

export type ConfirmEmailInput = z.infer<typeof ConfirmEmailInputSchema>
Expand Down
13 changes: 13 additions & 0 deletions src/core/shared/schemas/url.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { z } from 'zod'

/**
* Validates that a string is a well-formed HTTP or HTTPS URL.
*
* Unlike `z.httpUrl()`, this also accepts localhost / 127.0.0.1 URLs so the
* email-verification flow works against a local Supabase setup in development.
*
* The schema only validates URL structure — redirect safety is enforced
* downstream by `isExternalOrigin()` and `buildRedirectUrl()` in the auth
* route handlers, which reconstruct the redirect using the dashboard's own
* origin and preserve only `pathname` + `searchParams` from the input.
*/
export const httpUrlSchema = z.url({ protocol: /^https?$/ })
Comment thread
drankou marked this conversation as resolved.

export const relativeUrlSchema = z
.string()
.trim()
Expand Down
73 changes: 73 additions & 0 deletions tests/unit/url-schema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { describe, expect, it } from 'vitest'
import { httpUrlSchema } from '@/core/shared/schemas/url'

describe('httpUrlSchema', () => {
describe('accepts valid http/https URLs', () => {
it('accepts https production URLs', () => {
expect(httpUrlSchema.safeParse('https://e2b.dev/dashboard').success).toBe(
true
)
})

it('accepts https URLs with paths and query params', () => {
expect(
httpUrlSchema.safeParse('https://e2b.dev/dashboard?tab=settings')
.success
).toBe(true)
})

it('accepts http localhost URLs', () => {
expect(
httpUrlSchema.safeParse('http://localhost:3000/dashboard').success
).toBe(true)
})

it('accepts http localhost without port', () => {
expect(httpUrlSchema.safeParse('http://localhost').success).toBe(true)
})

it('accepts http 127.0.0.1 URLs', () => {
expect(httpUrlSchema.safeParse('http://127.0.0.1:3000').success).toBe(
true
)
})

it('accepts https URLs with subdomains', () => {
expect(
httpUrlSchema.safeParse('https://app.e2b.dev/dashboard').success
).toBe(true)
})
})

describe('rejects non-http(s) schemes', () => {
it('rejects mailto URLs', () => {
expect(httpUrlSchema.safeParse('mailto:user@example.com').success).toBe(
false
)
})

it('rejects ftp URLs', () => {
expect(httpUrlSchema.safeParse('ftp://files.example.com').success).toBe(
false
)
})

it('rejects file URLs', () => {
expect(httpUrlSchema.safeParse('file:///etc/passwd').success).toBe(false)
})

it('rejects javascript URLs', () => {
expect(httpUrlSchema.safeParse('javascript:alert(1)').success).toBe(false)
})
})

describe('rejects invalid inputs', () => {
it('rejects plain strings', () => {
expect(httpUrlSchema.safeParse('not-a-url').success).toBe(false)
})

it('rejects empty strings', () => {
expect(httpUrlSchema.safeParse('').success).toBe(false)
})
})
})
Loading