|
| 1 | +import { afterEach, describe, expect, test } from "bun:test"; |
| 2 | +import { |
| 3 | + handleResponses, |
| 4 | + hasUnreadableEncryptedAgentTask, |
| 5 | +} from "../src/server/responses"; |
| 6 | +import type { OcxConfig } from "../src/types"; |
| 7 | + |
| 8 | +const originalFetch = globalThis.fetch; |
| 9 | +const FERNET_TASK = `gAAAA${"Ab1_-".repeat(20)}==`; |
| 10 | +const ROUTING_ENVELOPE = [ |
| 11 | + "Message Type: NEW_TASK", |
| 12 | + "Task name: /root/worker", |
| 13 | + "Sender: /root", |
| 14 | + "Payload:", |
| 15 | + "", |
| 16 | +].join("\n"); |
| 17 | + |
| 18 | +afterEach(() => { |
| 19 | + globalThis.fetch = originalFetch; |
| 20 | +}); |
| 21 | + |
| 22 | +function agentMessage(content: Array<Record<string, unknown>>): unknown[] { |
| 23 | + return [{ |
| 24 | + type: "agent_message", |
| 25 | + author: "/root", |
| 26 | + recipient: "/root/worker", |
| 27 | + content, |
| 28 | + }]; |
| 29 | +} |
| 30 | + |
| 31 | +function routedConfig(): OcxConfig { |
| 32 | + return { |
| 33 | + port: 0, |
| 34 | + defaultProvider: "xai", |
| 35 | + providers: { |
| 36 | + xai: { |
| 37 | + adapter: "openai-chat", |
| 38 | + baseUrl: "https://api.x.ai/v1", |
| 39 | + authMode: "key", |
| 40 | + apiKey: "test-xai-key", |
| 41 | + }, |
| 42 | + }, |
| 43 | + } as OcxConfig; |
| 44 | +} |
| 45 | + |
| 46 | +function nativeConfig(): OcxConfig { |
| 47 | + return { |
| 48 | + port: 0, |
| 49 | + defaultProvider: "openai", |
| 50 | + providers: { |
| 51 | + openai: { |
| 52 | + adapter: "openai-responses", |
| 53 | + baseUrl: "https://chatgpt.com/backend-api/codex", |
| 54 | + authMode: "forward", |
| 55 | + codexAccountMode: "direct", |
| 56 | + }, |
| 57 | + }, |
| 58 | + } as OcxConfig; |
| 59 | +} |
| 60 | + |
| 61 | +async function post( |
| 62 | + config: OcxConfig, |
| 63 | + model: string, |
| 64 | + input: unknown[], |
| 65 | + headers: HeadersInit = {}, |
| 66 | +): Promise<Response> { |
| 67 | + return handleResponses(new Request("http://localhost/v1/responses", { |
| 68 | + method: "POST", |
| 69 | + headers: { |
| 70 | + "content-type": "application/json", |
| 71 | + ...Object.fromEntries(new Headers(headers)), |
| 72 | + }, |
| 73 | + body: JSON.stringify({ model, input, stream: false }), |
| 74 | + }), config, { model: "", provider: "" }); |
| 75 | +} |
| 76 | + |
| 77 | +describe("V2 routed agent-message ciphertext guard", () => { |
| 78 | + test("blocks a pure Fernet-only agent task", () => { |
| 79 | + expect(hasUnreadableEncryptedAgentTask(agentMessage([ |
| 80 | + { type: "encrypted_content", encrypted_content: FERNET_TASK }, |
| 81 | + ]))).toBe(true); |
| 82 | + }); |
| 83 | + |
| 84 | + test("blocks a routing envelope followed only by a Fernet task", () => { |
| 85 | + expect(hasUnreadableEncryptedAgentTask(agentMessage([ |
| 86 | + { type: "input_text", text: ROUTING_ENVELOPE }, |
| 87 | + { type: "encrypted_content", encrypted_content: FERNET_TASK }, |
| 88 | + ]))).toBe(true); |
| 89 | + }); |
| 90 | + |
| 91 | + test("blocks a control preamble mixed into the Fernet slot before sanitization", async () => { |
| 92 | + const input = agentMessage([ |
| 93 | + { type: "input_text", text: ROUTING_ENVELOPE }, |
| 94 | + { |
| 95 | + type: "encrypted_content", |
| 96 | + encrypted_content: `[CXC-LEAF-GUARD] follow the worker boundary.\n\n${FERNET_TASK}`, |
| 97 | + }, |
| 98 | + ]); |
| 99 | + let fetchCalls = 0; |
| 100 | + globalThis.fetch = (async () => { |
| 101 | + fetchCalls += 1; |
| 102 | + throw new Error("provider dispatch must not happen"); |
| 103 | + }) as typeof fetch; |
| 104 | + |
| 105 | + const response = await post(routedConfig(), "xai/grok-4.5", input); |
| 106 | + const raw = await response.text(); |
| 107 | + const json = JSON.parse(raw) as { |
| 108 | + error?: { type?: string; code?: string; message?: string }; |
| 109 | + }; |
| 110 | + |
| 111 | + expect(response.status).toBe(400); |
| 112 | + expect(json.error).toMatchObject({ |
| 113 | + type: "invalid_request_error", |
| 114 | + code: "invalid_request_error", |
| 115 | + }); |
| 116 | + expect(json.error?.message).toContain("encrypted"); |
| 117 | + expect(fetchCalls).toBe(0); |
| 118 | + expect(raw).not.toContain(FERNET_TASK); |
| 119 | + expect(raw).not.toContain("gAAAA"); |
| 120 | + }); |
| 121 | + |
| 122 | + test("allows genuine readable task text after the envelope", () => { |
| 123 | + expect(hasUnreadableEncryptedAgentTask(agentMessage([ |
| 124 | + { |
| 125 | + type: "input_text", |
| 126 | + text: `${ROUTING_ENVELOPE}Implement the focused regression test.`, |
| 127 | + }, |
| 128 | + { type: "encrypted_content", encrypted_content: FERNET_TASK }, |
| 129 | + ]))).toBe(false); |
| 130 | + }); |
| 131 | + |
| 132 | + test("ignores encrypted reasoning and compaction items", () => { |
| 133 | + expect(hasUnreadableEncryptedAgentTask([ |
| 134 | + { type: "reasoning", encrypted_content: FERNET_TASK, summary: [] }, |
| 135 | + { type: "compaction", encrypted_content: FERNET_TASK }, |
| 136 | + ])).toBe(false); |
| 137 | + }); |
| 138 | + |
| 139 | + test("allows the canonical ChatGPT route to forward the encrypted task", async () => { |
| 140 | + let forwardedBody = ""; |
| 141 | + globalThis.fetch = (async (_input, init) => { |
| 142 | + forwardedBody = typeof init?.body === "string" ? init.body : ""; |
| 143 | + return Response.json({ |
| 144 | + id: "resp_native", |
| 145 | + object: "response", |
| 146 | + status: "completed", |
| 147 | + model: "gpt-5.5", |
| 148 | + output: [], |
| 149 | + usage: { input_tokens: 1, output_tokens: 1, total_tokens: 2 }, |
| 150 | + }); |
| 151 | + }) as typeof fetch; |
| 152 | + |
| 153 | + const input = agentMessage([ |
| 154 | + { type: "input_text", text: ROUTING_ENVELOPE }, |
| 155 | + { type: "encrypted_content", encrypted_content: FERNET_TASK }, |
| 156 | + ]); |
| 157 | + const response = await post(nativeConfig(), "gpt-5.5", input, { |
| 158 | + authorization: "Bearer caller-codex-token", |
| 159 | + }); |
| 160 | + |
| 161 | + expect(response.status).toBe(200); |
| 162 | + expect(forwardedBody).toContain(FERNET_TASK); |
| 163 | + }); |
| 164 | +}); |
0 commit comments