const assert = require('assert'); const fs = require('fs'); const os = require('os'); const path = require('path'); const { spawnSync } = require('child_process'); const scriptPath = path.join(__dirname, '..', '..', 'scripts', 'hooks', 'post-bash-command-log.js'); const { sanitizeCommand } = require(scriptPath); function test(name, fn) { try { fn(); console.log(`PASS: ${name}`); return true; } catch (error) { console.log(`FAIL: ${name}`); console.log(` ${error.message}`); return false; } } function runHook(mode, payload, homeDir) { return spawnSync('node', [scriptPath, mode], { input: JSON.stringify(payload), encoding: 'utf8', env: { ...process.env, HOME: homeDir, USERPROFILE: homeDir, }, }); } let passed = 0; let failed = 0; if ( test('sanitizeCommand redacts common secret formats', () => { const input = 'gh pr create --token abc123 Authorization: Bearer hello password=swordfish ghp_abc github_pat_xyz'; const sanitized = sanitizeCommand(input); assert.ok(!sanitized.includes('abc123')); assert.ok(!sanitized.includes('swordfish')); assert.ok(!sanitized.includes('ghp_abc')); assert.ok(!sanitized.includes('github_pat_xyz')); assert.ok(sanitized.includes('--token=')); assert.ok(sanitized.includes('Authorization:')); assert.ok(sanitized.includes('password=')); }) ) passed++; else failed++; // Synthetic secrets, assembled at runtime so secret scanners do not flag // this file. None of these are real credentials. const FAKE_ANTHROPIC_KEY = ['sk', 'ant', 'api03', 'FAKEFAKEFAKEFAKEFAKEFAKEFAKE_x-y'].join('-'); const FAKE_OPENAI_KEY = ['sk', 'proj', 'FAKEfakeFAKEfakeFAKEfakeFAKE'].join('-'); const FAKE_JWT = ['eyJhbGciOiJIUzI1NiJ9', 'eyJzdWIiOiJmYWtlIn0', 'ZmFrZXNpZ25hdHVyZQ'].join('.'); const PEM_BEGIN = ['-----BEGIN', 'PRIVATE', 'KEY-----'].join(' '); const PEM_END = ['-----END', 'PRIVATE', 'KEY-----'].join(' '); const FAKE_VALUE = ['not', 'a', 'real', 'value'].join('-'); if ( test('sanitizeCommand redacts bare sk- API keys wherever they appear (#3846)', () => { const commands = [ `ANTHROPIC_API_KEY=${FAKE_ANTHROPIC_KEY} claude -p hi`, `curl -H "x-api-key: ${FAKE_ANTHROPIC_KEY}" https://api.example.com`, `echo ${FAKE_ANTHROPIC_KEY} > key.txt`, `OPENAI_API_KEY=${FAKE_OPENAI_KEY} node app.js`, ]; for (const command of commands) { const sanitized = sanitizeCommand(command); assert.ok(!sanitized.includes('FAKEFAKE'), `key leaked: ${sanitized}`); assert.ok(!sanitized.includes('FAKEfake'), `key leaked: ${sanitized}`); assert.ok(sanitized.includes(''), `no redaction marker: ${sanitized}`); } assert.strictEqual(sanitizeCommand(`echo ${FAKE_ANTHROPIC_KEY} > key.txt`), 'echo > key.txt'); }) ) passed++; else failed++; if ( test('sanitizeCommand redacts JWTs and PEM private key blocks (#3846)', () => { assert.strictEqual(sanitizeCommand(`export TOKEN_VALUE=${FAKE_JWT}`), 'export TOKEN_VALUE='); const pem = `printf '${PEM_BEGIN}\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASC\n${PEM_END}' > id_rsa`; const sanitizedPem = sanitizeCommand(pem); assert.ok(!sanitizedPem.includes('MIIEvQ'), sanitizedPem); assert.strictEqual(sanitizedPem, "printf '' > id_rsa"); const truncated = sanitizeCommand(`echo "${PEM_BEGIN}\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASC`); assert.ok(!truncated.includes('MIIEvQ'), truncated); }) ) passed++; else failed++; if ( test('sanitizeCommand redacts secret= and api_key= assignments (#3846)', () => { assert.strictEqual( sanitizeCommand(`app --secret=${FAKE_VALUE} --api_key=${FAKE_VALUE} --api-key="${FAKE_VALUE} two"`), 'app --secret= --api_key= --api-key=' ); assert.strictEqual(sanitizeCommand(`CLIENT_SECRET=${FAKE_VALUE} run`), 'CLIENT_SECRET= run'); assert.strictEqual( sanitizeCommand(`curl -H "x-api-key: ${FAKE_VALUE}" https://api.example.com`), 'curl -H "x-api-key: " https://api.example.com' ); }) ) passed++; else failed++; if ( test('sanitizeCommand leaves ordinary commands untouched', () => { const commands = [ 'gh secret set DEPLOY_KEY --repo owner/name', 'git commit -m "rotate the secret handling"', 'npm run task-runner-with-a-very-long-script-name', 'cat config/api_key.example.json', 'grep -rn "eyJ" src/', ]; for (const command of commands) { assert.strictEqual(sanitizeCommand(command), command); } }) ) passed++; else failed++; if ( test('audit mode does not write bare API keys to the log (#3846)', () => { const homeDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-bash-log-')); const payload = { tool_input: { command: `echo ${FAKE_ANTHROPIC_KEY} > key.txt`, }, }; try { const result = runHook('audit', payload, homeDir); assert.strictEqual(result.status, 0, result.stdout + result.stderr); const logContent = fs.readFileSync(path.join(homeDir, '.claude', 'bash-commands.log'), 'utf8'); assert.ok(logContent.includes('echo > key.txt'), logContent); assert.ok(!logContent.includes(FAKE_ANTHROPIC_KEY), logContent); } finally { fs.rmSync(homeDir, { recursive: true, force: true }); } }) ) passed++; else failed++; if ( test('audit mode logs sanitized bash commands and preserves stdout', () => { const homeDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-bash-log-')); const payload = { tool_input: { command: 'git push --token abc123', }, }; try { const result = runHook('audit', payload, homeDir); assert.strictEqual(result.status, 0, result.stdout + result.stderr); assert.strictEqual(result.stdout, JSON.stringify(payload)); const logFile = path.join(homeDir, '.claude', 'bash-commands.log'); const logContent = fs.readFileSync(logFile, 'utf8'); assert.ok(logContent.includes('--token=')); assert.ok(!logContent.includes('abc123')); } finally { fs.rmSync(homeDir, { recursive: true, force: true }); } }) ) passed++; else failed++; if ( test('cost mode writes command metrics log', () => { const homeDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-cost-log-')); const payload = { tool_input: { command: 'npm publish', }, }; try { const result = runHook('cost', payload, homeDir); assert.strictEqual(result.status, 0, result.stdout + result.stderr); const logFile = path.join(homeDir, '.claude', 'cost-tracker.log'); const logContent = fs.readFileSync(logFile, 'utf8'); assert.match(logContent, /tool=Bash command=npm publish/); } finally { fs.rmSync(homeDir, { recursive: true, force: true }); } }) ) passed++; else failed++; console.log(`\nPassed: ${passed}`); console.log(`Failed: ${failed}`); process.exit(failed > 0 ? 1 : 0);