diff --git a/apps/backend/src/lib/redirect-urls.tsx b/apps/backend/src/lib/redirect-urls.tsx index 47ae49fda..08e0b2d36 100644 --- a/apps/backend/src/lib/redirect-urls.tsx +++ b/apps/backend/src/lib/redirect-urls.tsx @@ -1,13 +1,21 @@ -import { isLocalhost } from "@stackframe/stack-shared/dist/utils/urls"; +import { StackAssertionError, captureError } from "@stackframe/stack-shared/dist/utils/errors"; +import { createUrlIfValid, isLocalhost } from "@stackframe/stack-shared/dist/utils/urls"; export function validateRedirectUrl(urlOrString: string | URL, domains: { domain: string, handler_path: string }[], allowLocalhost: boolean): boolean { - const url = new URL(urlOrString); + const url = createUrlIfValid(urlOrString); + if (!url) return false; if (allowLocalhost && isLocalhost(url)) { return true; } return domains.some((domain) => { const testUrl = url; - const baseUrl = new URL(domain.domain); + const baseUrl = createUrlIfValid(domain.domain); + if (!baseUrl) { + captureError("invalid-redirect-domain", new StackAssertionError("Invalid redirect domain; maybe this should be fixed in the database", { + domain: domain.domain, + })); + return false; + } const sameOrigin = baseUrl.protocol === testUrl.protocol && baseUrl.hostname === testUrl.hostname; const isSubPath = testUrl.pathname.startsWith(baseUrl.pathname);