Skip to content

Commit 730c9ae

Browse files
committed
refactor: extract validateCallbackUrl to shared util, evict stale MX cache entries on lookup
1 parent 6a05854 commit 730c9ae

File tree

4 files changed

+27
-37
lines changed

4 files changed

+27
-37
lines changed

apps/sim/app/(auth)/login/login-form.tsx

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
ModalHeader,
1616
} from '@/components/emcn'
1717
import { client } from '@/lib/auth/auth-client'
18+
import { validateCallbackUrl } from '@/lib/auth/validate-callback-url'
1819
import { getEnv, isFalsy, isTruthy } from '@/lib/core/config/env'
1920
import { cn } from '@/lib/core/utils/cn'
2021
import { getBaseUrl } from '@/lib/core/utils/urls'
@@ -53,24 +54,6 @@ const PASSWORD_VALIDATIONS = {
5354
},
5455
}
5556

56-
const validateCallbackUrl = (url: string): boolean => {
57-
try {
58-
if (url.startsWith('/')) {
59-
return true
60-
}
61-
62-
const currentOrigin = typeof window !== 'undefined' ? window.location.origin : ''
63-
if (url.startsWith(currentOrigin)) {
64-
return true
65-
}
66-
67-
return false
68-
} catch (error) {
69-
logger.error('Error validating callback URL:', { error, url })
70-
return false
71-
}
72-
}
73-
7457
const validatePassword = (passwordValue: string): string[] => {
7558
const errors: string[] = []
7659

apps/sim/ee/sso/components/sso-form.tsx

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Link from 'next/link'
66
import { useRouter, useSearchParams } from 'next/navigation'
77
import { Button, Input, Label } from '@/components/emcn'
88
import { client } from '@/lib/auth/auth-client'
9+
import { validateCallbackUrl } from '@/lib/auth/validate-callback-url'
910
import { env, isFalsy } from '@/lib/core/config/env'
1011
import { cn } from '@/lib/core/utils/cn'
1112
import { quickValidateEmail } from '@/lib/messaging/email/validation'
@@ -29,24 +30,6 @@ const validateEmailField = (emailValue: string): string[] => {
2930
return errors
3031
}
3132

32-
const validateCallbackUrl = (url: string): boolean => {
33-
try {
34-
if (url.startsWith('/')) {
35-
return true
36-
}
37-
38-
const currentOrigin = typeof window !== 'undefined' ? window.location.origin : ''
39-
if (url.startsWith(currentOrigin)) {
40-
return true
41-
}
42-
43-
return false
44-
} catch (error) {
45-
logger.error('Error validating callback URL:', { error, url })
46-
return false
47-
}
48-
}
49-
5033
export default function SSOForm() {
5134
const router = useRouter()
5235
const searchParams = useSearchParams()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { createLogger } from '@sim/logger'
2+
3+
const logger = createLogger('ValidateCallbackUrl')
4+
5+
/**
6+
* Returns true if the URL is safe to redirect to after authentication.
7+
* Accepts relative paths and absolute URLs matching the current origin.
8+
*/
9+
export function validateCallbackUrl(url: string): boolean {
10+
try {
11+
if (url.startsWith('/')) return true
12+
13+
const currentOrigin = typeof window !== 'undefined' ? window.location.origin : ''
14+
if (url.startsWith(currentOrigin)) return true
15+
16+
return false
17+
} catch (error) {
18+
logger.error('Error validating callback URL:', { error, url })
19+
return false
20+
}
21+
}

apps/sim/lib/messaging/email/validation.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,10 @@ export async function isDisposableMxBackend(email: string): Promise<boolean> {
132132

133133
const now = Date.now()
134134
const cached = mxCache.get(domain)
135-
if (cached && cached.expires > now) return cached.result
135+
if (cached) {
136+
if (cached.expires > now) return cached.result
137+
mxCache.delete(domain)
138+
}
136139

137140
let timeoutId: ReturnType<typeof setTimeout> | undefined
138141
try {

0 commit comments

Comments
 (0)