Skip to content

Commit

Permalink
fix: don't show error on relative callbackUrl
Browse files Browse the repository at this point in the history
fixes #4700
  • Loading branch information
balazsorban44 committed Jun 12, 2022
1 parent c0d2517 commit 49a8d51
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions packages/next-auth/src/core/lib/assert.ts
Expand Up @@ -21,9 +21,11 @@ type ConfigError =

let twitterWarned = false

function isValidHttpUrl(url: string) {
function isValidHttpUrl(url: string, baseUrl: string) {
try {
return /^https?:/.test(new URL(url).protocol)
return /^https?:/.test(
new URL(url, url.startsWith("/") ? baseUrl : undefined).protocol
)
} catch {
return false
}
Expand Down Expand Up @@ -57,23 +59,24 @@ export function assertConfig(

const callbackUrlParam = req.query?.callbackUrl as string | undefined

if (callbackUrlParam && !isValidHttpUrl(callbackUrlParam)) {
const url = parseUrl(req.host)

if (callbackUrlParam && !isValidHttpUrl(callbackUrlParam, url.base)) {
return new InvalidCallbackUrl(
`Invalid callback URL. Received: ${callbackUrlParam}`
)
}

// This is below the callbackUrlParam check because it would obscure the error
if (!req.host) return "NEXTAUTH_URL"

const url = parseUrl(req.host)

const { callbackUrl: defaultCallbackUrl } = defaultCookies(
options.useSecureCookies ?? url.base.startsWith("https://")
)
const callbackUrlCookie =
req.cookies?.[options.cookies?.callbackUrl?.name ?? defaultCallbackUrl.name]

if (callbackUrlCookie && !isValidHttpUrl(callbackUrlCookie)) {
if (callbackUrlCookie && !isValidHttpUrl(callbackUrlCookie, url.base)) {
return new InvalidCallbackUrl(
`Invalid callback URL. Received: ${callbackUrlCookie}`
)
Expand Down

0 comments on commit 49a8d51

Please sign in to comment.