opencode/services/site/test/serverAction.test.ts
Kit Langton 5a47777cc4
Some checks are pending
bot-nix-hashes / hash (blacksmith-4vcpu-ubuntu-2404, x86_64-linux) (push) Waiting to run
bot-nix-hashes / hash (blacksmith-4vcpu-ubuntu-2404-arm, aarch64-linux) (push) Waiting to run
bot-nix-hashes / hash (macos-latest, aarch64-darwin) (push) Waiting to run
bot-nix-hashes / compute (push) Blocked by required conditions
bot-nix-hashes / publish (push) Blocked by required conditions
check-nix / nix-eval (push) Waiting to run
check-test / affected packages (push) Waiting to run
check-test / unit (linux) (push) Waiting to run
check-test / unit (windows) (push) Waiting to run
check-test / build (linux) (push) Waiting to run
check-test / build (windows) (push) Waiting to run
check-test / e2e (, ) (push) Blocked by required conditions
check-test / e2e (linux) (push) Blocked by required conditions
check-test / e2e (windows) (push) Blocked by required conditions
check / typecheck (push) Waiting to run
deploy-files / deploy (push) Waiting to run
deploy-site / deploy (push) Waiting to run
deploy / deploy (push) Waiting to run
fix(site): stop server functions failing on the dev server behind a proxy (#62677)
2026-11-07 17:12:04 -08:00

61 lines
2.1 KiB
TypeScript

import { describe, expect, test } from "bun:test"
import { sanitizeServerActionRequest } from "../src/lib/server-action"
describe("server action referer", () => {
test("preserves same-origin return locations", () => {
const request = new Request("https://dev.opencode.ai/_server?id=action", {
headers: { referer: "https://dev.opencode.ai/auth?next=%2Fconsole" },
})
expect(sanitizeServerActionRequest(request)).toBe(request)
})
test("replaces unsafe return locations with the request origin", () => {
const referers = ["https://evil.example/phishing-login", "not a url", undefined]
expect(
referers.map((referer) =>
sanitizeServerActionRequest(
new Request("https://dev.opencode.ai/_server?id=action", {
headers: referer === undefined ? undefined : { referer },
}),
).headers.get("referer"),
),
).toEqual(["https://dev.opencode.ai", "https://dev.opencode.ai", "https://dev.opencode.ai"])
})
test("keeps the method and body when it replaces the referer", async () => {
const sanitized = sanitizeServerActionRequest(
new Request("https://dev.opencode.ai/_server?id=action", {
method: "POST",
headers: { referer: "https://evil.example/phishing-login" },
body: "[1]",
}),
)
expect([sanitized.method, sanitized.headers.get("referer"), await sanitized.text()]).toEqual([
"POST",
"https://dev.opencode.ai",
"[1]",
])
})
test("drops the body of a GET it rebuilds", () => {
const request = {
url: "https://dev.opencode.ai/_server?id=action",
method: "GET",
headers: new Headers({ referer: "https://evil.example/phishing-login" }),
body: new Response("[1]").body,
} as unknown as Request
expect(sanitizeServerActionRequest(request).body).toBeNull()
})
test("does not change other routes", () => {
const request = new Request("https://dev.opencode.ai/auth", {
headers: { referer: "https://evil.example/phishing-login" },
})
expect(sanitizeServerActionRequest(request)).toBe(request)
})
})