|
| 1 | +import { App } from "../app.ts"; |
| 2 | +import type { Method } from "../router.ts"; |
| 3 | +import { csrf, type CsrfOptions } from "./csrf.ts"; |
| 4 | +import { expect } from "@std/expect"; |
| 5 | + |
| 6 | +function testHandler<T>( |
| 7 | + options?: CsrfOptions<T>, |
| 8 | +): (request: Request) => Promise<Response> { |
| 9 | + return new App<T>() |
| 10 | + .use(csrf(options)) |
| 11 | + .all("/", () => new Response("hello")) |
| 12 | + .handler(); |
| 13 | +} |
| 14 | + |
| 15 | +function createTests(trusted: CsrfOptions["origin"]) { |
| 16 | + return async function runTest( |
| 17 | + method: Method, |
| 18 | + expected: 200 | 403, |
| 19 | + test: { |
| 20 | + secFetchSite?: "cross-site" | "same-origin" | "same-site" | "none"; |
| 21 | + origin?: string; |
| 22 | + } = {}, |
| 23 | + ) { |
| 24 | + const handler = testHandler({ origin: trusted }); |
| 25 | + |
| 26 | + const headers = new Headers(); |
| 27 | + if (test.secFetchSite) headers.append("Sec-Fetch-Site", test.secFetchSite); |
| 28 | + if (test.origin) { |
| 29 | + headers.append("Origin", test.origin); |
| 30 | + } |
| 31 | + |
| 32 | + const res = await handler( |
| 33 | + new Request("https://example.com", { method, headers }), |
| 34 | + ); |
| 35 | + await res.body?.cancel(); |
| 36 | + expect(res.status).toEqual(expected); |
| 37 | + }; |
| 38 | +} |
| 39 | + |
| 40 | +Deno.test("CSRF - allow GET/HEAD/OPTIONS", async () => { |
| 41 | + const runTest = createTests("https://example.com"); |
| 42 | + |
| 43 | + await runTest("GET", 200); |
| 44 | + await runTest("HEAD", 200); |
| 45 | + await runTest("OPTIONS", 200); |
| 46 | +}); |
| 47 | + |
| 48 | +Deno.test("CSRF - Sec-Fetch-Site", async () => { |
| 49 | + const runTest = createTests("https://example.com"); |
| 50 | + |
| 51 | + await runTest("POST", 200, { secFetchSite: "same-origin" }); |
| 52 | + await runTest("POST", 200, { secFetchSite: "none" }); |
| 53 | + await runTest("POST", 403, { secFetchSite: "cross-site" }); |
| 54 | + await runTest("POST", 403, { secFetchSite: "same-site" }); |
| 55 | + |
| 56 | + await runTest("POST", 200); |
| 57 | + await runTest("POST", 200, { origin: "https://example.com" }); |
| 58 | + await runTest("POST", 403, { origin: "https://attacker.example.com" }); |
| 59 | + await runTest("POST", 403, { origin: "null" }); |
| 60 | + |
| 61 | + await runTest("GET", 200, { secFetchSite: "cross-site" }); |
| 62 | + await runTest("HEAD", 200, { secFetchSite: "cross-site" }); |
| 63 | + await runTest("OPTIONS", 200, { secFetchSite: "cross-site" }); |
| 64 | + await runTest("PUT", 403, { secFetchSite: "cross-site" }); |
| 65 | +}); |
| 66 | + |
| 67 | +Deno.test("CSRF - cross origin", async () => { |
| 68 | + const runTest = createTests("https://trusted.example.com"); |
| 69 | + |
| 70 | + await runTest("POST", 200, { origin: "https://trusted.example.com" }); |
| 71 | + await runTest("POST", 200, { |
| 72 | + origin: "https://trusted.example.com", |
| 73 | + secFetchSite: "cross-site", |
| 74 | + }); |
| 75 | + |
| 76 | + await runTest("POST", 403, { origin: "https://attacker.example.com" }); |
| 77 | + await runTest("POST", 403, { |
| 78 | + origin: "https://attacker.example.com", |
| 79 | + secFetchSite: "cross-site", |
| 80 | + }); |
| 81 | +}); |
| 82 | + |
| 83 | +Deno.test("CSRF - array origin", async () => { |
| 84 | + const runTest = createTests( |
| 85 | + ["https://example.com", "https://trusted.example.com"], |
| 86 | + ); |
| 87 | + |
| 88 | + await runTest("POST", 200, { origin: "https://trusted.example.com" }); |
| 89 | + await runTest("POST", 200, { origin: "https://example.com" }); |
| 90 | + await runTest("POST", 403, { origin: "https://foo.example.com" }); |
| 91 | +}); |
| 92 | + |
| 93 | +Deno.test("CSRF - function origin", async () => { |
| 94 | + const runTest = createTests( |
| 95 | + (origin) => origin === "https://example.com", |
| 96 | + ); |
| 97 | + |
| 98 | + await runTest("POST", 200, { origin: "https://example.com" }); |
| 99 | + await runTest("POST", 403, { origin: "https://trusted.example.com" }); |
| 100 | + await runTest("POST", 403, { origin: "https://foo.example.com" }); |
| 101 | +}); |
0 commit comments