|
| 1 | +import { describe, expect, test } from "bun:test" |
| 2 | +import { sanitizeServerActionRequest } from "../src/lib/server-action" |
| 3 | + |
| 4 | +describe("server action referer", () => { |
| 5 | + test("preserves same-origin return locations", () => { |
| 6 | + const request = new Request("https://dev.opencode.ai/_server?id=action", { |
| 7 | + headers: { referer: "https://dev.opencode.ai/auth?next=%2Fconsole" }, |
| 8 | + }) |
| 9 | + |
| 10 | + expect(sanitizeServerActionRequest(request)).toBe(request) |
| 11 | + }) |
| 12 | + |
| 13 | + test("replaces unsafe return locations with the request origin", () => { |
| 14 | + const referers = ["https://evil.example/phishing-login", "not a url", undefined] |
| 15 | + |
| 16 | + expect( |
| 17 | + referers.map((referer) => |
| 18 | + sanitizeServerActionRequest( |
| 19 | + new Request("https://dev.opencode.ai/_server?id=action", { |
| 20 | + headers: referer === undefined ? undefined : { referer }, |
| 21 | + }), |
| 22 | + ).headers.get("referer"), |
| 23 | + ), |
| 24 | + ).toEqual(["https://dev.opencode.ai", "https://dev.opencode.ai", "https://dev.opencode.ai"]) |
| 25 | + }) |
| 26 | + |
| 27 | + test("does not change other routes", () => { |
| 28 | + const request = new Request("https://dev.opencode.ai/auth", { |
| 29 | + headers: { referer: "https://evil.example/phishing-login" }, |
| 30 | + }) |
| 31 | + |
| 32 | + expect(sanitizeServerActionRequest(request)).toBe(request) |
| 33 | + }) |
| 34 | +}) |
0 commit comments