Files
Clintchiz afe1c413cc
Quality / quality (ubuntu-latest) (push) Failing after 11m28s
Quality / quality (windows-latest) (push) Canceled after 0s
fix: reject RPC requests without CSRF tokens
2026-08-12 19:49:58 +05:30

37 lines
1.0 KiB
TypeScript

import { expect, test } from "bun:test";
import { validateRpcCsrf } from "../src/index.ts";
function request(headers: Record<string, string> = {}): Request {
return new Request("https://app.example/__wrnexus/rpc", {
method: "POST",
headers,
});
}
test("RPC CSRF requires a matching cookie and header from the same origin", () => {
expect(validateRpcCsrf(request())).toBe(false);
expect(validateRpcCsrf(request({ cookie: "wrn-csrf=token" }))).toBe(false);
expect(validateRpcCsrf(request({ "x-csrf-token": "token" }))).toBe(false);
expect(validateRpcCsrf(request({ cookie: "wrn-csrf=token", "x-csrf-token": "forged" }))).toBe(
false,
);
expect(
validateRpcCsrf(
request({
cookie: "wrn-csrf=token",
"x-csrf-token": "token",
origin: "https://evil.example",
}),
),
).toBe(false);
expect(
validateRpcCsrf(
request({
cookie: "wrn-csrf=encoded%20token",
"x-csrf-token": "encoded token",
origin: "https://app.example",
}),
),
).toBe(true);
});