|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +const cookiesMock = vi.fn(); |
| 4 | + |
| 5 | +vi.mock("next/headers", () => ({ |
| 6 | + cookies: cookiesMock, |
| 7 | +})); |
| 8 | + |
| 9 | +const originalNodeEnv = process.env.NODE_ENV; |
| 10 | + |
| 11 | +function hostileRequest(path: string) { |
| 12 | + return new Request(`https://internal.invalid${path}`, { |
| 13 | + headers: { |
| 14 | + host: "attacker.example.test", |
| 15 | + "x-forwarded-host": "attacker.example.test", |
| 16 | + "x-forwarded-proto": "https", |
| 17 | + }, |
| 18 | + }); |
| 19 | +} |
| 20 | + |
| 21 | +describe("OAuth public origin", () => { |
| 22 | + beforeEach(() => { |
| 23 | + vi.resetModules(); |
| 24 | + cookiesMock.mockReset(); |
| 25 | + cookiesMock.mockResolvedValue({ |
| 26 | + get: vi.fn(), |
| 27 | + set: vi.fn(), |
| 28 | + delete: vi.fn(), |
| 29 | + }); |
| 30 | + process.env.NODE_ENV = "production"; |
| 31 | + process.env.GITHUB_CLIENT_ID = "github-client"; |
| 32 | + process.env.GITHUB_CLIENT_SECRET = "github-secret"; |
| 33 | + process.env.GOOGLE_CLIENT_ID = "google-client"; |
| 34 | + process.env.GOOGLE_CLIENT_SECRET = "google-secret"; |
| 35 | + delete process.env.DIFFAUDIT_PLATFORM_URL; |
| 36 | + }); |
| 37 | + |
| 38 | + afterEach(() => { |
| 39 | + process.env.NODE_ENV = originalNodeEnv; |
| 40 | + delete process.env.GITHUB_CLIENT_ID; |
| 41 | + delete process.env.GITHUB_CLIENT_SECRET; |
| 42 | + delete process.env.GOOGLE_CLIENT_ID; |
| 43 | + delete process.env.GOOGLE_CLIENT_SECRET; |
| 44 | + delete process.env.DIFFAUDIT_PLATFORM_URL; |
| 45 | + }); |
| 46 | + |
| 47 | + it("fails GitHub OAuth closed instead of deriving redirect_uri from Host headers", async () => { |
| 48 | + const route = await import("./github/route"); |
| 49 | + |
| 50 | + const response = await route.GET(hostileRequest("/api/auth/github")); |
| 51 | + const payload = await response.json(); |
| 52 | + |
| 53 | + expect(response.status).toBe(500); |
| 54 | + expect(response.headers.get("location")).toBeNull(); |
| 55 | + expect(payload).toEqual({ message: "Platform public URL is not configured." }); |
| 56 | + expect(cookiesMock).not.toHaveBeenCalled(); |
| 57 | + }); |
| 58 | + |
| 59 | + it("fails Google OAuth closed instead of deriving redirect_uri from Host headers", async () => { |
| 60 | + const route = await import("./google/route"); |
| 61 | + |
| 62 | + const response = await route.GET(hostileRequest("/api/auth/google")); |
| 63 | + const payload = await response.json(); |
| 64 | + |
| 65 | + expect(response.status).toBe(500); |
| 66 | + expect(response.headers.get("location")).toBeNull(); |
| 67 | + expect(payload).toEqual({ message: "Platform public URL is not configured." }); |
| 68 | + expect(cookiesMock).not.toHaveBeenCalled(); |
| 69 | + }); |
| 70 | + |
| 71 | + it("uses the configured public URL for GitHub OAuth redirect_uri", async () => { |
| 72 | + process.env.DIFFAUDIT_PLATFORM_URL = "https://diffaudit.example.test"; |
| 73 | + const route = await import("./github/route"); |
| 74 | + |
| 75 | + const response = await route.GET(hostileRequest("/api/auth/github")); |
| 76 | + const location = response.headers.get("location"); |
| 77 | + const redirect = new URL(location ?? ""); |
| 78 | + |
| 79 | + expect(response.status).toBe(307); |
| 80 | + expect(redirect.origin).toBe("https://github.com"); |
| 81 | + expect(redirect.searchParams.get("redirect_uri")).toBe( |
| 82 | + "https://diffaudit.example.test/api/auth/github/callback", |
| 83 | + ); |
| 84 | + }); |
| 85 | + |
| 86 | + it("uses the configured public URL for unauthenticated connect redirects", async () => { |
| 87 | + process.env.DIFFAUDIT_PLATFORM_URL = "https://diffaudit.example.test"; |
| 88 | + const route = await import("./github/route"); |
| 89 | + |
| 90 | + const response = await route.GET( |
| 91 | + hostileRequest("/api/auth/github?intent=connect&redirectTo=/workspace/account"), |
| 92 | + ); |
| 93 | + const location = response.headers.get("location"); |
| 94 | + const redirect = new URL(location ?? ""); |
| 95 | + |
| 96 | + expect(response.status).toBe(307); |
| 97 | + expect(redirect.origin).toBe("https://diffaudit.example.test"); |
| 98 | + expect(redirect.pathname).toBe("/login"); |
| 99 | + expect(redirect.searchParams.get("redirectTo")).toBe("/workspace/account"); |
| 100 | + }); |
| 101 | + |
| 102 | + it("does not redirect callback errors to Host-derived origins", async () => { |
| 103 | + const route = await import("./github/callback/route"); |
| 104 | + |
| 105 | + const response = await route.GET(hostileRequest("/api/auth/github/callback?error=denied")); |
| 106 | + const payload = await response.json(); |
| 107 | + |
| 108 | + expect(response.status).toBe(500); |
| 109 | + expect(response.headers.get("location")).toBeNull(); |
| 110 | + expect(payload).toEqual({ message: "Platform public URL is not configured." }); |
| 111 | + expect(cookiesMock).not.toHaveBeenCalled(); |
| 112 | + }); |
| 113 | +}); |
0 commit comments