import { describe, expect, it } from "vitest"; import { safeReturnTo } from "$lib/auth/returnTo"; describe("safeReturnTo", () => { it("keeps same-origin paths with query and hash", () => { expect(safeReturnTo("/oppi.li/core?tab=pulls#c1")).toBe("/oppi.li/core?tab=pulls#c1"); }); it("rejects network-path references", () => { expect(safeReturnTo("//evil.example")).toBe("/"); expect(safeReturnTo("/\\evil.example")).toBe("/"); expect(safeReturnTo("\\\\evil.example")).toBe("/"); }); it("rejects absolute and non-http targets", () => { expect(safeReturnTo("https://evil.example/repo")).toBe("/"); expect(safeReturnTo("javascript:alert(1)")).toBe("/"); }); it("normalizes scheme-relative input that stays on the origin", () => { // the URL parser reads this as a same-origin path, not a host expect(safeReturnTo("https:/evil.example")).toBe("/evil.example"); }); it("falls back for empty or non-string input", () => { expect(safeReturnTo("")).toBe("/"); expect(safeReturnTo(" ")).toBe("/"); expect(safeReturnTo(undefined)).toBe("/"); expect(safeReturnTo(42)).toBe("/"); }); });