Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix(web): add getSafeNextPath helper for auth redirects
Richie McIlroye4cb4003851b
diff --git a/apps/web/__tests__/unit/safe-next.test.ts b/apps/web/__tests__/unit/safe-next.test.tsnew file mode 100644index 000000000..5cd43c536--- /dev/null+++ b/apps/web/__tests__/unit/safe-next.test.ts@@ -0,0 +1,44 @@+import { describe, expect, it } from "vitest";+import { getSafeNextPath } from "@/app/(org)/safe-next";++const ORIGIN = "https://cap.so";++describe("getSafeNextPath", () => {+ it("returns the default for missing values", () => {+ expect(getSafeNextPath(undefined, ORIGIN)).toBe("/dashboard");+ expect(getSafeNextPath(null, ORIGIN)).toBe("/dashboard");+ expect(getSafeNextPath("", ORIGIN)).toBe("/dashboard");+ });++ it("keeps same-origin paths with query and hash", () => {+ expect(getSafeNextPath("/dashboard/caps?foo=1#bar", ORIGIN)).toBe(+ "/dashboard/caps?foo=1#bar",+ );+ expect(getSafeNextPath("https://cap.so/settings", ORIGIN)).toBe(+ "/settings",+ );+ });++ it("uses the first value when next is repeated", () => {+ expect(getSafeNextPath(["/a", "/b"], ORIGIN)).toBe("/a");+ });++ it("rejects cross-origin and protocol-relative URLs", () => {+ expect(getSafeNextPath("https://evil.com/x", ORIGIN)).toBe("/dashboard");+ expect(getSafeNextPath("//evil.com/x", ORIGIN)).toBe("/dashboard");+ expect(getSafeNextPath("javascript:alert(1)", ORIGIN)).toBe("/dashboard");+ });++ it("rejects paths that normalize to protocol-relative URLs", () => {+ expect(getSafeNextPath("/.//evil.com", ORIGIN)).toBe("/dashboard");+ expect(getSafeNextPath("/..//evil.com", ORIGIN)).toBe("/dashboard");+ expect(getSafeNextPath("/%2e//evil.com", ORIGIN)).toBe("/dashboard");+ expect(getSafeNextPath("/./%2e//evil.com/path?x=1", ORIGIN)).toBe(+ "/dashboard",+ );+ expect(getSafeNextPath("https://cap.so//evil.com", ORIGIN)).toBe(+ "/dashboard",+ );+ expect(getSafeNextPath("/\\evil.com", ORIGIN)).toBe("/dashboard");+ });+});diff --git a/apps/web/app/(org)/safe-next.ts b/apps/web/app/(org)/safe-next.tsnew file mode 100644index 000000000..2e5e001b9--- /dev/null+++ b/apps/web/app/(org)/safe-next.ts@@ -0,0 +1,25 @@+const DEFAULT_AUTH_REDIRECT = "/dashboard";++export const getSafeNextPath = (+ next: string | string[] | null | undefined,+ origin: string,+) => {+ const value = Array.isArray(next) ? next[0] : next;+ if (!value) return DEFAULT_AUTH_REDIRECT;++ try {+ const base = new URL(origin);+ const url = new URL(value, base);+ if (url.origin !== base.origin) return DEFAULT_AUTH_REDIRECT;+ const path = `${url.pathname}${url.search}${url.hash}`;+ // Path normalization can turn inputs like /.//evil.com or+ // https://<origin>//evil.com into //evil.com, which browsers treat as a+ // protocol-relative URL when emitted in a Location header.+ if (!path.startsWith("/") || path.startsWith("//") || path[1] === "\\") {+ return DEFAULT_AUTH_REDIRECT;+ }+ return path;+ } catch {+ return DEFAULT_AUTH_REDIRECT;+ }+};