import { describe, expect, it } from 'vitest'; import { createCredentialsGuardHook, isClaudeCredentialsCopyCommand, } from '../src/runner-hooks.js'; describe('isClaudeCredentialsCopyCommand', () => { it('blocks the 2026-11-02 test commands that copied the real login', () => { expect( isClaudeCredentialsCopyCommand( 'mkdir -p oauthtest/a && cp /home/claude/EJClaw/data/claude/.credentials.json oauthtest/a/.credentials.json', ), ).toBe(true); expect( isClaudeCredentialsCopyCommand( `bun -e 'const c=JSON.parse(fs.readFileSync("data/claude/.credentials.json","utf8")); fs.writeFileSync("b/.credentials.json",JSON.stringify(c))'`, ), ).toBe(true); expect( isClaudeCredentialsCopyCommand( 'cat data/claude/.credentials.json > /tmp/x/.credentials.json', ), ).toBe(true); }); it('allows reading or inspecting the credentials file', () => { expect( isClaudeCredentialsCopyCommand( 'ls -la data/sessions/x/services/claude/.claude/.credentials.json', ), ).toBe(false); expect( isClaudeCredentialsCopyCommand( `bun -e 'const a=JSON.parse(require("fs").readFileSync("data/claude/.credentials.json","utf8")); console.log(a.claudeAiOauth.expiresAt)'`, ), ).toBe(false); expect(isClaudeCredentialsCopyCommand('cp a.txt b.txt')).toBe(false); }); }); describe('createCredentialsGuardHook', () => { it('denies a copy and passes other commands through', async () => { const hook = createCredentialsGuardHook(); const signal = new AbortController().signal; const denied = await hook( { hook_event_name: 'PreToolUse', tool_name: 'Bash', tool_input: { command: 'cp data/claude/.credentials.json /tmp/c/' }, } as never, undefined, { signal }, ); expect(denied).toMatchObject({ hookSpecificOutput: { permissionDecision: 'deny' }, }); const allowed = await hook( { hook_event_name: 'PreToolUse', tool_name: 'Bash', tool_input: { command: 'git status' }, } as never, undefined, { signal }, ); expect(allowed).toEqual({}); }); });