|
| 1 | +import { NextRequest } from "next/server"; |
| 2 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import { GET } from "./route"; |
| 4 | + |
| 5 | +const mockGetAccessToken = vi.hoisted(() => vi.fn()); |
| 6 | +const mockHeaders = vi.hoisted(() => vi.fn()); |
| 7 | + |
| 8 | +vi.mock("next/headers", () => ({ |
| 9 | + headers: mockHeaders, |
| 10 | +})); |
| 11 | + |
| 12 | +vi.mock("@/lib/auth/auth", () => ({ |
| 13 | + auth: { |
| 14 | + api: { getAccessToken: mockGetAccessToken }, |
| 15 | + }, |
| 16 | +})); |
| 17 | + |
| 18 | +// BASE_URL defaults to "http://localhost:3000" in test (no BETTER_AUTH_URL set) |
| 19 | + |
| 20 | +const INTERNAL_URL = "http://0.0.0.0:3000/api/auth/token-refresh"; |
| 21 | + |
| 22 | +function makeRequest(path = INTERNAL_URL) { |
| 23 | + return new NextRequest(path); |
| 24 | +} |
| 25 | + |
| 26 | +function mockTokenSuccess(cookies: string[] = []) { |
| 27 | + mockGetAccessToken.mockResolvedValue({ |
| 28 | + ok: true, |
| 29 | + status: 200, |
| 30 | + headers: { getSetCookie: () => cookies }, |
| 31 | + }); |
| 32 | +} |
| 33 | + |
| 34 | +function mockTokenFailure(status = 401) { |
| 35 | + mockGetAccessToken.mockResolvedValue({ |
| 36 | + ok: false, |
| 37 | + status, |
| 38 | + headers: { getSetCookie: () => [] }, |
| 39 | + }); |
| 40 | +} |
| 41 | + |
| 42 | +describe("GET /api/auth/token-refresh", () => { |
| 43 | + beforeEach(() => { |
| 44 | + vi.clearAllMocks(); |
| 45 | + mockHeaders.mockResolvedValue(new Headers()); |
| 46 | + }); |
| 47 | + |
| 48 | + describe("redirect URL uses BASE_URL, not request.url", () => { |
| 49 | + it("redirects to BASE_URL/catalog on success (not 0.0.0.0)", async () => { |
| 50 | + mockTokenSuccess(); |
| 51 | + const response = await GET( |
| 52 | + makeRequest(`${INTERNAL_URL}?redirect=%2Fcatalog`), |
| 53 | + ); |
| 54 | + expect(response.headers.get("location")).toBe( |
| 55 | + "http://localhost:3000/catalog", |
| 56 | + ); |
| 57 | + }); |
| 58 | + |
| 59 | + it("redirects to BASE_URL/signin on token refresh failure", async () => { |
| 60 | + vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 61 | + mockTokenFailure(); |
| 62 | + const response = await GET(makeRequest(INTERNAL_URL)); |
| 63 | + expect(response.headers.get("location")).toBe( |
| 64 | + "http://localhost:3000/signin", |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + it("redirects to BASE_URL/signin on unexpected error", async () => { |
| 69 | + vi.spyOn(console, "error").mockImplementation(() => {}); |
| 70 | + mockGetAccessToken.mockRejectedValue(new Error("Network error")); |
| 71 | + const response = await GET(makeRequest(INTERNAL_URL)); |
| 72 | + expect(response.headers.get("location")).toBe( |
| 73 | + "http://localhost:3000/signin", |
| 74 | + ); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe("open redirect protection", () => { |
| 79 | + it("falls back to /catalog when no redirect param", async () => { |
| 80 | + mockTokenSuccess(); |
| 81 | + const response = await GET(makeRequest(INTERNAL_URL)); |
| 82 | + expect(response.headers.get("location")).toBe( |
| 83 | + "http://localhost:3000/catalog", |
| 84 | + ); |
| 85 | + }); |
| 86 | + |
| 87 | + it("falls back to /catalog for redirect starting with //", async () => { |
| 88 | + mockTokenSuccess(); |
| 89 | + const response = await GET( |
| 90 | + makeRequest(`${INTERNAL_URL}?redirect=//evil.com`), |
| 91 | + ); |
| 92 | + expect(response.headers.get("location")).toBe( |
| 93 | + "http://localhost:3000/catalog", |
| 94 | + ); |
| 95 | + }); |
| 96 | + |
| 97 | + it("falls back to /catalog for redirect to external origin", async () => { |
| 98 | + mockTokenSuccess(); |
| 99 | + const response = await GET( |
| 100 | + makeRequest( |
| 101 | + `${INTERNAL_URL}?redirect=${encodeURIComponent("https://evil.com/phishing")}`, |
| 102 | + ), |
| 103 | + ); |
| 104 | + expect(response.headers.get("location")).toBe( |
| 105 | + "http://localhost:3000/catalog", |
| 106 | + ); |
| 107 | + }); |
| 108 | + |
| 109 | + it("accepts valid internal path with query string", async () => { |
| 110 | + mockTokenSuccess(); |
| 111 | + const response = await GET( |
| 112 | + makeRequest( |
| 113 | + `${INTERNAL_URL}?redirect=${encodeURIComponent("/catalog?page=2")}`, |
| 114 | + ), |
| 115 | + ); |
| 116 | + expect(response.headers.get("location")).toBe( |
| 117 | + "http://localhost:3000/catalog?page=2", |
| 118 | + ); |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + describe("Set-Cookie forwarding", () => { |
| 123 | + it("copies Set-Cookie headers from Better Auth response onto the redirect", async () => { |
| 124 | + const cookie = |
| 125 | + "__Secure-better-auth.account_data=newvalue; Path=/; HttpOnly; SameSite=Lax"; |
| 126 | + mockTokenSuccess([cookie]); |
| 127 | + const response = await GET( |
| 128 | + makeRequest(`${INTERNAL_URL}?redirect=%2Fcatalog`), |
| 129 | + ); |
| 130 | + expect(response.headers.get("set-cookie")).toBe(cookie); |
| 131 | + }); |
| 132 | + |
| 133 | + it("forwards multiple Set-Cookie headers", async () => { |
| 134 | + const cookies = [ |
| 135 | + "__Secure-better-auth.account_data=val1; Path=/; HttpOnly", |
| 136 | + "__Secure-better-auth.session_token=val2; Path=/; HttpOnly", |
| 137 | + ]; |
| 138 | + mockTokenSuccess(cookies); |
| 139 | + const response = await GET( |
| 140 | + makeRequest(`${INTERNAL_URL}?redirect=%2Fcatalog`), |
| 141 | + ); |
| 142 | + const setCookieHeader = response.headers.getSetCookie(); |
| 143 | + expect(setCookieHeader).toHaveLength(2); |
| 144 | + expect(setCookieHeader[0]).toBe(cookies[0]); |
| 145 | + expect(setCookieHeader[1]).toBe(cookies[1]); |
| 146 | + }); |
| 147 | + }); |
| 148 | +}); |
0 commit comments