|
| 1 | +import { afterEach, beforeEach, describe, expect, it } from "@effect/vitest"; |
| 2 | +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | + |
| 6 | +import { startServer, type ServerInstance } from "./serve"; |
| 7 | +import { OTC_TTL_MS, makeOtcStore } from "./otc"; |
| 8 | + |
| 9 | +let clientDir: string; |
| 10 | +let dataDir: string; |
| 11 | +let server: ServerInstance | null = null; |
| 12 | + |
| 13 | +const TOKEN = "test-bearer-token"; |
| 14 | + |
| 15 | +const testHandlers = () => ({ |
| 16 | + api: { |
| 17 | + handler: async () => new Response("ok"), |
| 18 | + dispose: async () => {}, |
| 19 | + }, |
| 20 | + mcp: { |
| 21 | + handleRequest: async () => new Response("ok"), |
| 22 | + handleApprovalRequest: async () => new Response("ok"), |
| 23 | + handlePausedRequest: async () => new Response("ok"), |
| 24 | + close: async () => {}, |
| 25 | + }, |
| 26 | +}); |
| 27 | + |
| 28 | +const startTestServer = async (): Promise<string> => { |
| 29 | + server = await startServer({ |
| 30 | + port: 0, |
| 31 | + hostname: "127.0.0.1", |
| 32 | + clientDir, |
| 33 | + authToken: TOKEN, |
| 34 | + handlers: testHandlers(), |
| 35 | + }); |
| 36 | + return `http://127.0.0.1:${server.port}`; |
| 37 | +}; |
| 38 | + |
| 39 | +beforeEach(() => { |
| 40 | + clientDir = mkdtempSync(join(tmpdir(), "exec-otc-serve-")); |
| 41 | + dataDir = mkdtempSync(join(tmpdir(), "exec-otc-data-")); |
| 42 | + process.env.EXECUTOR_DATA_DIR = dataDir; |
| 43 | + process.env.EXECUTOR_SCOPE_DIR = dataDir; |
| 44 | + writeFileSync( |
| 45 | + join(clientDir, "index.html"), |
| 46 | + "<!doctype html><html><body>index-shell</body></html>", |
| 47 | + ); |
| 48 | +}); |
| 49 | + |
| 50 | +afterEach(async () => { |
| 51 | + if (server) { |
| 52 | + await server.stop(); |
| 53 | + server = null; |
| 54 | + } |
| 55 | + delete process.env.EXECUTOR_DATA_DIR; |
| 56 | + delete process.env.EXECUTOR_SCOPE_DIR; |
| 57 | + rmSync(clientDir, { recursive: true, force: true }); |
| 58 | + rmSync(dataDir, { recursive: true, force: true }); |
| 59 | +}); |
| 60 | + |
| 61 | +describe("OTC exchange endpoint", () => { |
| 62 | + it("mints a code via the bearer-gated route and exchanges it once (200 + HttpOnly cookie)", async () => { |
| 63 | + const origin = await startTestServer(); |
| 64 | + const mint = await fetch(`${origin}/api/auth/otc`, { |
| 65 | + method: "POST", |
| 66 | + headers: { authorization: `Bearer ${TOKEN}` }, |
| 67 | + }); |
| 68 | + expect(mint.status).toBe(200); |
| 69 | + const { code } = (await mint.json()) as { code: string }; |
| 70 | + expect(code.length).toBeGreaterThanOrEqual(16); // ≥128 bits base64url |
| 71 | + |
| 72 | + const exchange = await fetch(`${origin}/api/auth/exchange`, { |
| 73 | + method: "POST", |
| 74 | + headers: { "content-type": "application/x-www-form-urlencoded" }, |
| 75 | + body: `code=${encodeURIComponent(code)}`, |
| 76 | + }); |
| 77 | + expect(exchange.status).toBe(200); |
| 78 | + const body = (await exchange.json()) as { token: string }; |
| 79 | + expect(body.token).toBe(TOKEN); |
| 80 | + |
| 81 | + const setCookie = exchange.headers.get("set-cookie") ?? ""; |
| 82 | + expect(setCookie).toContain("executor_session"); |
| 83 | + expect(setCookie).toContain("HttpOnly"); |
| 84 | + expect(setCookie).toContain("SameSite=Strict"); |
| 85 | + }); |
| 86 | + |
| 87 | + it("rejects a replayed code (single-use — second exchange is 400)", async () => { |
| 88 | + const origin = await startTestServer(); |
| 89 | + const mint = await fetch(`${origin}/api/auth/otc`, { |
| 90 | + method: "POST", |
| 91 | + headers: { authorization: `Bearer ${TOKEN}` }, |
| 92 | + }); |
| 93 | + const { code } = (await mint.json()) as { code: string }; |
| 94 | + |
| 95 | + const first = await fetch(`${origin}/api/auth/exchange`, { |
| 96 | + method: "POST", |
| 97 | + headers: { "content-type": "application/x-www-form-urlencoded" }, |
| 98 | + body: `code=${encodeURIComponent(code)}`, |
| 99 | + }); |
| 100 | + expect(first.status).toBe(200); |
| 101 | + |
| 102 | + const replay = await fetch(`${origin}/api/auth/exchange`, { |
| 103 | + method: "POST", |
| 104 | + headers: { "content-type": "application/x-www-form-urlencoded" }, |
| 105 | + body: `code=${encodeURIComponent(code)}`, |
| 106 | + }); |
| 107 | + expect(replay.status).toBe(400); |
| 108 | + }); |
| 109 | + |
| 110 | + it("rejects an unknown code", async () => { |
| 111 | + const origin = await startTestServer(); |
| 112 | + const res = await fetch(`${origin}/api/auth/exchange`, { |
| 113 | + method: "POST", |
| 114 | + headers: { "content-type": "application/x-www-form-urlencoded" }, |
| 115 | + body: "code=never-issued", |
| 116 | + }); |
| 117 | + expect(res.status).toBe(400); |
| 118 | + }); |
| 119 | + |
| 120 | + it("rejects the mint route without a bearer", async () => { |
| 121 | + const origin = await startTestServer(); |
| 122 | + const res = await fetch(`${origin}/api/auth/otc`, { method: "POST" }); |
| 123 | + expect(res.status).toBe(401); |
| 124 | + }); |
| 125 | + |
| 126 | + it("rejects an expired code (TTL honored by the store)", () => { |
| 127 | + let now = 1_000; |
| 128 | + const store = makeOtcStore(() => now); |
| 129 | + const code = store.issue(); |
| 130 | + expect(store.consume(code)).toBe(code); |
| 131 | + |
| 132 | + // Re-issue after expiry — the consumed code must stay dead even after |
| 133 | + // pruning. |
| 134 | + const code2 = store.issue(); |
| 135 | + now = now + OTC_TTL_MS + 1; |
| 136 | + expect(store.consume(code2)).toBeNull(); |
| 137 | + expect(store.consume(code)).toBeNull(); |
| 138 | + }); |
| 139 | +}); |
0 commit comments