|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { mkdir, mkdtemp, rm, symlink, writeFile } from "node:fs/promises"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import type { ToolCall, ToolResult } from "@intx/types/runtime"; |
| 6 | +import { resolveAtMentions } from "../tui/mention-resolution.js"; |
| 7 | +import { |
| 8 | + isSensitivePath, |
| 9 | + isSensitivePathResolved, |
| 10 | + secretGuardPlugin, |
| 11 | +} from "./secret-guard-plugin.js"; |
| 12 | + |
| 13 | +const CODEX_BYTES = |
| 14 | + '{"profiles":{"personal":{"tokens":{"access":"CODEX-FIXTURE-ACCESS"}}}}'; |
| 15 | +const XAI_BYTES = |
| 16 | + '{"profiles":{"personal":{"tokens":{"access":"XAI-FIXTURE-ACCESS"}}}}'; |
| 17 | +const MCP_BYTES = '{"tokens":{"access_token":"MCP-FIXTURE-ACCESS"}}'; |
| 18 | + |
| 19 | +async function fakeHome(): Promise<{ |
| 20 | + home: string; |
| 21 | + codexAuth: string; |
| 22 | + xaiAuth: string; |
| 23 | + settings: string; |
| 24 | + permissions: string; |
| 25 | + mcpAuth: string; |
| 26 | + cleanup: () => Promise<void>; |
| 27 | +}> { |
| 28 | + const home = await mkdtemp(join(tmpdir(), "cl7789-fake-home-")); |
| 29 | + const configDir = join(home, ".corbits"); |
| 30 | + const mcpDir = join(configDir, "mcp-auth"); |
| 31 | + await mkdir(mcpDir, { recursive: true }); |
| 32 | + const codexAuth = join(configDir, "codex-auth.json"); |
| 33 | + const xaiAuth = join(configDir, "xai-auth.json"); |
| 34 | + const settings = join(configDir, "settings.json"); |
| 35 | + const permissions = join(configDir, "permissions.json"); |
| 36 | + const mcpAuth = join( |
| 37 | + mcpDir, |
| 38 | + "my-server-9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00.json", |
| 39 | + ); |
| 40 | + await writeFile(codexAuth, `${CODEX_BYTES}\n`); |
| 41 | + await writeFile(xaiAuth, `${XAI_BYTES}\n`); |
| 42 | + await writeFile(settings, '{"provider":"codex"}\n'); |
| 43 | + await writeFile(permissions, '{"approvals":[]}\n'); |
| 44 | + await writeFile(mcpAuth, `${MCP_BYTES}\n`); |
| 45 | + return { |
| 46 | + home, |
| 47 | + codexAuth, |
| 48 | + xaiAuth, |
| 49 | + settings, |
| 50 | + permissions, |
| 51 | + mcpAuth, |
| 52 | + cleanup: () => rm(home, { recursive: true, force: true }), |
| 53 | + }; |
| 54 | +} |
| 55 | + |
| 56 | +const next = async (call: ToolCall): Promise<ToolResult> => ({ |
| 57 | + callId: call.id, |
| 58 | + content: "ok", |
| 59 | +}); |
| 60 | + |
| 61 | +function denyReason(path: string): Promise<ToolResult> { |
| 62 | + const plugin = secretGuardPlugin(); |
| 63 | + const handler = plugin.middleware ? plugin.middleware(next) : next; |
| 64 | + return handler( |
| 65 | + { id: "c", name: "read_file", arguments: { path } }, |
| 66 | + new AbortController().signal, |
| 67 | + ); |
| 68 | +} |
| 69 | + |
| 70 | +describe("CL-7789 default-install credential surface", () => { |
| 71 | + test("lexical denylist covers every default credential file", async () => { |
| 72 | + const fake = await fakeHome(); |
| 73 | + try { |
| 74 | + for (const path of [ |
| 75 | + fake.codexAuth, |
| 76 | + fake.xaiAuth, |
| 77 | + fake.settings, |
| 78 | + fake.permissions, |
| 79 | + fake.mcpAuth, |
| 80 | + ]) { |
| 81 | + expect(isSensitivePath(path)).toBe(true); |
| 82 | + expect(isSensitivePathResolved(path)).toBe(true); |
| 83 | + } |
| 84 | + } finally { |
| 85 | + await fake.cleanup(); |
| 86 | + } |
| 87 | + }); |
| 88 | + |
| 89 | + test("read_file hard-denies every default credential file", async () => { |
| 90 | + const fake = await fakeHome(); |
| 91 | + try { |
| 92 | + for (const path of [ |
| 93 | + fake.codexAuth, |
| 94 | + fake.xaiAuth, |
| 95 | + fake.settings, |
| 96 | + fake.permissions, |
| 97 | + fake.mcpAuth, |
| 98 | + ]) { |
| 99 | + const result = await denyReason(path); |
| 100 | + expect(result.isError).toBe(true); |
| 101 | + expect(String(result.content)).toMatch(/sensitive file blocked/); |
| 102 | + } |
| 103 | + } finally { |
| 104 | + await fake.cleanup(); |
| 105 | + } |
| 106 | + }); |
| 107 | + |
| 108 | + test("credential backups and sidecars resolve as sensitive", () => { |
| 109 | + const home = join(tmpdir(), "cl7789-never-created"); |
| 110 | + expect( |
| 111 | + isSensitivePathResolved( |
| 112 | + join(home, ".corbits", "settings.json.bak-2026-01-01"), |
| 113 | + ), |
| 114 | + ).toBe(true); |
| 115 | + expect( |
| 116 | + isSensitivePathResolved(join(home, ".corbits", "codex-auth.json.lock")), |
| 117 | + ).toBe(true); |
| 118 | + expect( |
| 119 | + isSensitivePathResolved( |
| 120 | + join(home, ".corbits", "xai-auth.json.12345.1.tmp"), |
| 121 | + ), |
| 122 | + ).toBe(true); |
| 123 | + }); |
| 124 | + |
| 125 | + test("@mentions of credential files stay blocked without leaking bytes", async () => { |
| 126 | + const fake = await fakeHome(); |
| 127 | + const cwd = await mkdtemp(join(tmpdir(), "cl7789-mention-cwd-")); |
| 128 | + try { |
| 129 | + for (const [path, marker] of [ |
| 130 | + [fake.codexAuth, "CODEX-FIXTURE-ACCESS"], |
| 131 | + [fake.mcpAuth, "MCP-FIXTURE-ACCESS"], |
| 132 | + ] as const) { |
| 133 | + const resolved = await resolveAtMentions(`read @${path}`, cwd); |
| 134 | + expect(resolved).toContain("(blocked: sensitive path)"); |
| 135 | + expect(resolved).not.toContain(marker); |
| 136 | + } |
| 137 | + } finally { |
| 138 | + await fake.cleanup(); |
| 139 | + await rm(cwd, { recursive: true, force: true }); |
| 140 | + } |
| 141 | + }); |
| 142 | + |
| 143 | + test("a symlink to a credential file is blocked by mention and by read", async () => { |
| 144 | + const fake = await fakeHome(); |
| 145 | + const cwd = await mkdtemp(join(tmpdir(), "cl7789-symlink-cwd-")); |
| 146 | + try { |
| 147 | + const link = join(cwd, "link-to-codex-auth"); |
| 148 | + await symlink(fake.codexAuth, link); |
| 149 | + const denied = await denyReason(link); |
| 150 | + expect(denied.isError).toBe(true); |
| 151 | + expect(String(denied.content)).toMatch(/sensitive file blocked/); |
| 152 | + const resolved = await resolveAtMentions(`read @${link}`, cwd); |
| 153 | + expect(resolved).toContain("(blocked: sensitive path)"); |
| 154 | + expect(resolved).not.toContain("CODEX-FIXTURE-ACCESS"); |
| 155 | + } finally { |
| 156 | + await fake.cleanup(); |
| 157 | + await rm(cwd, { recursive: true, force: true }); |
| 158 | + } |
| 159 | + }); |
| 160 | +}); |
0 commit comments