Fix STACK-BACKEND-63

This commit is contained in:
Konstantin Wohlwend 2025-01-13 10:51:56 -08:00
parent b7d4277510
commit 4e03a16f4b

View File

@ -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);