/** * Grok Build (formerly Cursor) through the real hook binary (`dist/cli.js`). * * The app reaches node9 two ways, with the same native payloads: * - the import route: the Claude Code hooks it imports from * ~/.claude/settings.json ("Third-Party Imports", on by default), no flag; * - the native route: node9's own ~/.cursor/hooks.json entries, `--agent cursor`. * * Before the fix every Cursor prompt was blocked as an "unrecognised hook * payload" (even in Audit mode), and Cursor tool calls were recorded as * Claude Code. Payloads come from a capture of Grok Build 3.32.24 * (fixtures/cursor-payloads.ts). Requires `npm run build`. */ import { describe, it, expect, beforeAll, beforeEach, afterEach } from 'vitest'; import { spawnSync } from 'child_process'; import fs from 'fs'; import os from 'os'; import path from 'path'; import { FAKE_GH_TOKEN } from './helpers/github-token'; import { cursorBeforeRead, cursorBeforeShell, cursorPostToolShell, cursorPreToolRead, cursorPreToolShell, cursorPreToolWrite, cursorPrompt, cursorSessionStart, cursorStop, } from './fixtures/cursor-payloads'; const CLI = path.resolve(__dirname, '../../dist/cli.js'); interface RunResult { status: number | null; stdout: string; stderr: string; } function run( sub: 'check' | 'log', payload: Record, home: string, args: string[] = [] ): RunResult { const env = { ...process.env }; delete env.NODE9_API_KEY; delete env.NODE9_API_URL; const r = spawnSync(process.execPath, [CLI, sub, ...args], { input: JSON.stringify(payload), encoding: 'utf-8', timeout: 60000, // The app runs user hooks from ~/.cursor, never from the project. cwd: home, env: { ...env, HOME: home, USERPROFILE: home, NODE9_NO_AUTO_DAEMON: '1', NODE9_TESTING: '1', NODE9_NO_TELEMETRY: '1', }, }); expect(r.error).toBeUndefined(); return { status: r.status, stdout: r.stdout ?? '', stderr: r.stderr ?? '' }; } /** `run` with extra environment variables (test seams). */ function runEnv( sub: 'check' | 'log', payload: Record, home: string, args: string[], extra: Record ): RunResult { const saved: Record = {}; for (const [k, v] of Object.entries(extra)) { saved[k] = process.env[k]; process.env[k] = v; } try { return run(sub, payload, home, args); } finally { for (const [k, v] of Object.entries(saved)) { if (v === undefined) delete process.env[k]; else process.env[k] = v; } } } function auditRows(home: string): Array> { const file = path.join(home, '.node9', 'audit.log'); if (!fs.existsSync(file)) return []; return fs .readFileSync(file, 'utf-8') .trim() .split('\n') .filter(Boolean) .map((l) => JSON.parse(l) as Record); } let home: string; let workspace: string; function makeHome(settings: Record = {}): void { home = fs.mkdtempSync(path.join(os.tmpdir(), 'node9-cursor-')); fs.mkdirSync(path.join(home, '.node9'), { recursive: true }); fs.writeFileSync(path.join(home, '.node9', 'config.json'), JSON.stringify({ settings })); workspace = path.join(home, 'grok-test'); fs.mkdirSync(workspace); fs.writeFileSync(path.join(workspace, '.env'), 'FAKE_API_KEY=not-a-real-key\n'); } /** A capture fixture pointed at this test's workspace. */ function inWorkspace(p: Record): Record { return { ...p, workspace_roots: [workspace] }; } beforeAll(() => { if (!fs.existsSync(CLI)) throw new Error('dist/cli.js not found. Run "npm run build".'); }); afterEach(() => { fs.rmSync(home, { recursive: true, force: true }); }); describe('Cursor through the imported Claude Code hooks (no flag)', () => { beforeEach(() => makeHome()); it('a prompt is let through, not blocked as an unrecognised payload', () => { const r = run('check', inWorkspace(cursorPrompt('Run ls -la')), home); expect(r.status).toBe(0); expect(r.stdout).toBe(''); }); it('a prompt is let through in Audit mode too (the reported bug)', () => { fs.rmSync(home, { recursive: true, force: true }); makeHome({ mode: 'audit' }); const r = run('check', inWorkspace(cursorPrompt('hello')), home); expect(r.status).toBe(0); }); it('a prompt carrying a credential is still stopped, with node9 line as plain text', () => { const r = run('check', inWorkspace(cursorPrompt(`deploy with ${FAKE_GH_TOKEN}`)), home); expect(r.status).toBe(2); expect(r.stdout).not.toContain(FAKE_GH_TOKEN); // The app shows this hook's stdout as it is: one readable line, no JSON. expect(r.stdout.trim()).toMatch(/^🚨 Node9 DLP: GitHub Token detected in prompt/); expect(r.stdout).not.toContain('{'); }); it('rm -rf of home is blocked in the Claude shape the app maps, as agent Cursor', () => { const r = run('check', inWorkspace(cursorPreToolShell('rm -rf ~')), home); expect(r.status).toBe(2); const body = JSON.parse(r.stdout) as Record & { hookSpecificOutput: { permissionDecision: string }; }; expect(body.hookSpecificOutput.permissionDecision).toBe('deny'); // Claude Code's own field; the app's preToolUse schema has no such field. expect(body.systemMessage).toBeUndefined(); const row = auditRows(home).at(-1)!; expect(row.agent).toBe('Cursor'); expect(row.tool).toBe('Bash'); expect(row.decision).toBe('deny'); }); it('reading the workspace .env is blocked', () => { const r = run('check', inWorkspace(cursorPreToolRead(path.join(workspace, '.env'))), home); expect(r.status).toBe(2); }); it('a plain write is allowed with no output, and checked against the workspace', () => { const r = run( 'check', inWorkspace(cursorPreToolWrite(path.join(workspace, 'hello.txt'), 'hi\n')), home ); expect(r.status).toBe(0); expect(r.stdout).toBe(''); const row = auditRows(home).at(-1)!; expect(row.agent).toBe('Cursor'); expect(row.workingDir).toBe(workspace); }); it('events node9 does not gate pass silently', () => { for (const p of [cursorBeforeShell('ls'), cursorStop, cursorSessionStart]) { const r = run('check', inWorkspace(p), home); expect(r.status).toBe(0); expect(r.stdout).toBe(''); } }); it('steps aside when node9 native entry for the event is wired: one verdict per call', () => { fs.mkdirSync(path.join(home, '.cursor')); fs.writeFileSync( path.join(home, '.cursor', 'hooks.json'), JSON.stringify({ version: 1, hooks: { preToolUse: [{ command: 'node9 check --agent cursor' }] }, }) ); // The entry alone is not enough: the app may not have loaded it yet (it // reads its hook files when it starts), so the imported call still checks. const unseen = run('check', inWorkspace(cursorPreToolShell('rm -rf ~')), home); expect(unseen.status).toBe(2); // The native PROMPT hook running says nothing about the native preToolUse // entry (it may have been added after the app started): still checked. run('check', inWorkspace(cursorPrompt('hello')), home, ['--agent', 'cursor']); expect(run('check', inWorkspace(cursorPreToolShell('rm -rf ~')), home).status).toBe(2); // Once the native preToolUse hook has run in this session, the imported // call for that event steps aside. run('check', inWorkspace(cursorPreToolShell('ls')), home, ['--agent', 'cursor']); const before = auditRows(home).length; const r = run('check', inWorkspace(cursorPreToolShell('rm -rf ~')), home); expect(r.status).toBe(0); expect(r.stdout).toBe(''); expect(auditRows(home).length).toBe(before); // The native call itself still blocks. const native = run('check', inWorkspace(cursorPreToolShell('rm -rf ~')), home, [ '--agent', 'cursor', ]); expect(JSON.parse(native.stdout).permission).toBe('deny'); }); it('a stale native entry does not count: the imported call still checks', () => { fs.mkdirSync(path.join(home, '.cursor')); fs.writeFileSync( path.join(home, '.cursor', 'hooks.json'), JSON.stringify({ version: 1, hooks: { preToolUse: [{ command: '"/gone/node" "/gone/dist/cli.js" check --agent cursor' }], }, }) ); const r = run('check', inWorkspace(cursorPreToolShell('rm -rf ~')), home); expect(r.status).toBe(2); }); }); describe('Cursor through node9 native entries (--agent cursor)', () => { beforeEach(() => makeHome()); const native = ['--agent', 'cursor']; it('an allowed call says allow: no output would read as a malformed answer', () => { const r = run('check', inWorkspace(cursorPreToolShell('ls -la')), home, native); expect(r.status).toBe(0); expect(JSON.parse(r.stdout)).toEqual({ permission: 'allow' }); }); it('a block is a native deny with exit 0, a line for the person and the reason for the model', () => { const r = run('check', inWorkspace(cursorPreToolShell('rm -rf ~')), home, native); expect(r.status).toBe(0); const body = JSON.parse(r.stdout) as Record; expect(body.permission).toBe('deny'); expect(body.user_message).toMatch(/Node9 blocked/); expect(body.agent_message).toContain(body.user_message); }); it('a clean prompt continues; a prompt with a credential stops with exit 2', () => { const ok = run('check', inWorkspace(cursorPrompt('hello')), home, native); expect(ok.status).toBe(0); expect(JSON.parse(ok.stdout)).toEqual({ continue: true }); const stop = run('check', inWorkspace(cursorPrompt(`token ${FAKE_GH_TOKEN}`)), home, native); expect(stop.status).toBe(2); // The app shows the stdout of a prompt hook that exits 2 as it is, on // the native route too: one readable line, no JSON. expect(stop.stdout.trim()).toMatch(/^🚨 Node9 DLP: GitHub Token detected in prompt/); expect(stop.stdout).not.toContain('{'); expect(stop.stdout).not.toContain(FAKE_GH_TOKEN); }); it('events node9 does not gate get the allow their schema expects', () => { expect( JSON.parse(run('check', inWorkspace(cursorBeforeShell('ls')), home, native).stdout) ).toEqual({ permission: 'allow' }); expect(JSON.parse(run('check', inWorkspace(cursorStop), home, native).stdout)).toEqual({}); }); }); describe('review findings F1, F2: no Cursor call passes unchecked', () => { const native = ['--agent', 'cursor']; /** A preToolUse cut off before its event name, as a half-sent payload. */ function cutBeforeEvent(): string { const text = JSON.stringify(inWorkspace(cursorPreToolShell('rm -rf ~'))); return text.slice(0, text.indexOf('"tool_use_id"') + 30); } function runRaw(raw: string, args: string[]): RunResult { const r = spawnSync(process.execPath, [CLI, 'check', ...args], { input: raw, encoding: 'utf-8', timeout: 60000, cwd: home, env: { ...process.env, HOME: home, USERPROFILE: home, NODE9_NO_AUTO_DAEMON: '1', NODE9_TESTING: '1', NODE9_NO_TELEMETRY: '1', }, }); expect(r.error).toBeUndefined(); return { status: r.status, stdout: r.stdout ?? '', stderr: r.stderr ?? '' }; } it('F1: native route, block policy: a payload cut before its event name is stopped', () => { makeHome({ mode: 'standard', onHookError: 'block' }); const r = runRaw(cutBeforeEvent(), native); // The event is unknown, so the stop is exit 2, which blocks on any event. expect(r.status).toBe(2); expect(r.stdout).toMatch(/Node9 stopped a tool call/); }); it('F1: a native PROMPT cut before its event name is stopped with exit 2 (a prompt ignores permission)', () => { makeHome({ mode: 'standard', onHookError: 'block' }); const text = JSON.stringify(inWorkspace(cursorPrompt('hello'))); const r = runRaw(text.slice(0, text.indexOf('"session_id"')), native); expect(r.status).toBe(2); expect(r.stdout).not.toContain('{'); }); it('an event named after an Object prototype member is not taken for a known one', () => { makeHome({ mode: 'standard' }); for (const event of ['constructor', 'toString', '__proto__']) { const p = { ...inWorkspace(cursorPreToolShell('rm -rf ~')), hook_event_name: event }; expect(run('check', p, home).status).toBe(2); const ls = { ...inWorkspace(cursorPreToolShell('ls')), hook_event_name: event }; expect(JSON.parse(run('check', ls, home, native).stdout)).toEqual({ permission: 'allow' }); } }); it('F1: native route, default policy: it is let through with an explicit allow', () => { makeHome({ mode: 'standard' }); const r = runRaw(cutBeforeEvent(), native); expect(r.status).toBe(0); expect(JSON.parse(r.stdout)).toEqual({ permission: 'allow' }); }); it('F2: a tool call under an event name node9 does not know is checked, on both routes', () => { makeHome({ mode: 'standard' }); for (const event of ['PreToolUse', 'beforeToolRun', '']) { const p = { ...inWorkspace(cursorPreToolShell('rm -rf ~')), hook_event_name: event }; expect(run('check', p, home).status).toBe(2); expect(JSON.parse(run('check', p, home, native).stdout).permission).toBe('deny'); } }); it('a review is never sent to Cursor as ask (the app does not enforce it on preToolUse)', () => { makeHome({ mode: 'standard', approvalTimeoutMs: 300 }); const r = run('check', inWorkspace(cursorPreToolShell('sudo ls')), home); expect(r.stdout).not.toContain('"ask"'); expect(r.status).toBe(2); }); }); describe('Delete and MCP calls (R1, R2)', () => { beforeEach(() => makeHome({ mode: 'standard' })); const del = (filePath: string) => ({ ...inWorkspace(cursorPreToolRead('/x')), tool_name: 'Delete', tool_input: { file_path: filePath }, }); const mcp = (tool: string) => ({ ...inWorkspace(cursorPreToolRead('/x')), tool_name: `MCP:${tool}`, tool_input: {}, }); function writeCursorMcp(servers: Record): void { fs.mkdirSync(path.join(home, '.cursor'), { recursive: true }); fs.writeFileSync( path.join(home, '.cursor', 'mcp.json'), JSON.stringify({ mcpServers: servers }) ); } it('Delete: protected files are blocked, a shell startup file needs approval, a project file is allowed', () => { makeHome({ mode: 'standard', approvalTimeoutMs: 300 }); expect(run('check', del(path.join(workspace, '.env')), home).status).toBe(2); // A delete is a change: the shell-startup rule asks (no one answers here). // The protected-file policy is POSIX-only (protected-files-gate.spec.ts). if (process.platform !== 'win32') { const startup = run('check', del(path.join(home, '.bashrc')), home, ['--agent', 'cursor']); expect(JSON.parse(startup.stdout).permission).toBe('deny'); expect(auditRows(home).some((r) => r.ruleName === 'review-shell-startup-mutation')).toBe( true ); } const ok = run('check', del(path.join(workspace, 'hello.txt')), home, ['--agent', 'cursor']); expect(JSON.parse(ok.stdout)).toEqual({ permission: 'allow' }); const row = auditRows(home).at(-1)!; expect(row.tool).toBe('Delete'); expect(row.agent).toBe('Cursor'); }); it("an MCP call to node9's own server is checked and recorded as an MCP tool", () => { writeCursorMcp({ node9: { command: 'node9', args: ['mcp-server'] } }); const r = run('check', mcp('node9_status'), home, ['--agent', 'cursor']); expect(JSON.parse(r.stdout)).toEqual({ permission: 'allow' }); expect(auditRows(home).at(-1)?.tool).toBe('mcp__node9__node9_status'); }); it('an MCP call to a server node9 cannot name is still checked as MCP', () => { run('check', mcp('list_issues'), home, ['--agent', 'cursor']); expect(auditRows(home).at(-1)?.tool).toBe('mcp__unknown__list_issues'); }); it('an MCP call to a gateway-wrapped server is left to the gateway: no hook row (D5)', () => { writeCursorMcp({ gmail: { command: 'node9', args: ['mcp-gateway', '--config-name', 'gmail', '--upstream', 'npx gmail-mcp'], }, }); fs.writeFileSync( path.join(home, '.node9', 'mcp-tools.json'), JSON.stringify({ a: { name: 'gmail', tools: [{ name: 'send_email' }] } }) ); const before = auditRows(home).length; const r = run('check', mcp('send_email'), home, ['--agent', 'cursor']); expect(JSON.parse(r.stdout)).toEqual({ permission: 'allow' }); expect(auditRows(home).length).toBe(before); }); }); describe('a credential in the content of a file being read (R4)', () => { const native = ['--agent', 'cursor']; const secretFile = () => cursorBeforeRead(path.join(workspace, 'config.sh'), `export GH=${FAKE_GH_TOKEN}\n`); it('Standard: the read is denied, the row names the file and never the content', () => { makeHome({ mode: 'standard' }); const r = run('check', inWorkspace(secretFile()), home, native); expect(r.status).toBe(0); const body = JSON.parse(r.stdout) as Record; expect(body.permission).toBe('deny'); expect(body.user_message).toMatch(/Node9 blocked reading/); expect(r.stdout).not.toContain(FAKE_GH_TOKEN); const row = auditRows(home).at(-1)!; expect(row.decision).toBe('deny'); expect(row.ruleName).toBe('read-content-credential'); expect(JSON.stringify(row)).not.toContain(FAKE_GH_TOKEN); }); it('Audit: the read goes on and is recorded', () => { makeHome({ mode: 'audit' }); const r = run('check', inWorkspace(secretFile()), home, native); expect(JSON.parse(r.stdout)).toEqual({ permission: 'allow' }); expect(auditRows(home).at(-1)?.checkedBy).toBe('observe-mode-dlp-would-block'); }); it('a failure in the read gate is stopped under the block policy, let through by default', () => { makeHome({ mode: 'standard', onHookError: 'block' }); const fault = { NODE9_TEST_HOOK_FAULT: 'read-content' }; const stopped = runEnv('check', inWorkspace(secretFile()), home, native, fault); expect(JSON.parse(stopped.stdout).permission).toBe('deny'); fs.rmSync(home, { recursive: true, force: true }); makeHome({ mode: 'standard' }); const allowed = runEnv('check', inWorkspace(secretFile()), home, native, fault); expect(JSON.parse(allowed.stdout)).toEqual({ permission: 'allow' }); }); it('clean content: allowed, no row', () => { makeHome({ mode: 'standard' }); const r = run( 'check', inWorkspace(cursorBeforeRead(path.join(workspace, 'notes.txt'), 'hello\n')), home, native ); expect(JSON.parse(r.stdout)).toEqual({ permission: 'allow' }); expect(auditRows(home)).toEqual([]); }); }); describe('Audit mode and payloads node9 cannot read (D1)', () => { const unknown = { session_id: 's1', hook_event_name: 'Mystery' }; it('Audit mode: let through, recorded as not checked', () => { makeHome({ mode: 'audit' }); const r = run('check', unknown, home); expect(r.status).toBe(0); const row = auditRows(home).at(-1)!; expect(row.checkedBy).toBe('hook-error'); expect(row.decision).toBe('allow'); }); it('Standard: still stopped', () => { makeHome({ mode: 'standard' }); const r = run('check', unknown, home); expect(r.status).toBe(2); }); }); describe('node9 log for Cursor', () => { beforeEach(() => makeHome()); it('a postToolUse is one row: agent Cursor, tool Bash, the app name kept', () => { const r = run('log', inWorkspace(cursorPostToolShell('ls -la', 'a\n')), home); expect(r.status).toBe(0); const row = auditRows(home).at(-1)!; expect(row.agent).toBe('Cursor'); expect(row.tool).toBe('Bash'); expect(row.agentToolName).toBe('Shell'); }); it('the imported after-run hook steps aside once the native one runs in the session', () => { fs.mkdirSync(path.join(home, '.cursor')); fs.writeFileSync( path.join(home, '.cursor', 'hooks.json'), JSON.stringify({ version: 1, hooks: { beforeSubmitPrompt: [{ command: 'node9 check --agent cursor' }], postToolUse: [{ command: 'node9 log --agent cursor' }], }, }) ); const post = inWorkspace(cursorPostToolShell('ls', 'a\n')); // Before the native postToolUse hook has run in the session, both record. expect(run('log', post, home).status).toBe(0); expect(auditRows(home).filter((r) => r.source === 'post-hook')).toHaveLength(1); expect(run('log', post, home, ['--agent', 'cursor']).status).toBe(0); expect(auditRows(home).filter((r) => r.source === 'post-hook')).toHaveLength(2); // After it: the imported one steps aside. expect(run('log', post, home).status).toBe(0); expect(auditRows(home).filter((r) => r.source === 'post-hook')).toHaveLength(2); }); it('a call under an event name node9 does not know is still recorded (F2)', () => { const p = { ...inWorkspace(cursorPostToolShell('ls', 'a\n')), hook_event_name: 'afterToolRun' }; expect(run('log', p, home).status).toBe(0); expect(auditRows(home).at(-1)?.agent).toBe('Cursor'); }); it('native stop: the turn tokens are kept for cost, no audit row (R3)', () => { const r = run('log', inWorkspace(cursorStop), home, ['--agent', 'cursor']); expect(r.status).toBe(0); expect(auditRows(home)).toEqual([]); const lines = fs .readFileSync(path.join(home, '.node9', 'cost', 'cursor-turns.jsonl'), 'utf8') .trim() .split('\n'); expect(lines).toHaveLength(1); const t = JSON.parse(lines[0]) as Record; expect(t.input).toBe(28975); expect(t.workspace).toBe(workspace); }); it('a stop event writes no row', () => { const r = run('log', inWorkspace(cursorStop), home); expect(r.status).toBe(0); expect(auditRows(home)).toEqual([]); }); });