Files
ECC/tests/scripts/post-bash-command-log.test.js
T
Gaurav Dubey 9e6272d5df fix(hooks): keep recording the command when log rotation fails
If the rename to <log>.1 fails (another session rotated first, or
Windows refuses the rename), append to the current log instead of
dropping the line.

Refs #3846
2026-11-19 03:57:06 +00:00

483 lines
18 KiB
JavaScript

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 { LOG_MAX_BYTES, 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=<REDACTED>'));
assert.ok(sanitized.includes('Authorization:<REDACTED>'));
assert.ok(sanitized.includes('password=<REDACTED>'));
})
)
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('<REDACTED>'), `no redaction marker: ${sanitized}`);
}
assert.strictEqual(sanitizeCommand(`echo ${FAKE_ANTHROPIC_KEY} > key.txt`), 'echo <REDACTED> > 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=<REDACTED>');
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 '<REDACTED>' > id_rsa");
const truncated = sanitizeCommand(`echo "${PEM_BEGIN}\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASC`);
assert.ok(!truncated.includes('MIIEvQ'), truncated);
})
)
passed++;
else failed++;
if (
test('sanitizeCommand redacts JWTs whose header has whitespace after the brace', () => {
const spacedHeader = Buffer.from('{ "alg": "HS256" }').toString('base64url');
const newlineHeader = Buffer.from('{\n"alg":"HS256"}').toString('base64url');
assert.ok(spacedHeader.startsWith('eyA'), spacedHeader);
assert.ok(newlineHeader.startsWith('ew'), newlineHeader);
for (const header of [spacedHeader, newlineHeader]) {
const token = [header, 'eyJzdWIiOiJmYWtlIn0', 'ZmFrZXNpZ25hdHVyZQ'].join('.');
assert.strictEqual(sanitizeCommand(`export TOKEN_VALUE=${token}`), 'export TOKEN_VALUE=<REDACTED>');
}
// Same shape, but the first segment does not decode to a JWT header.
const notJwt = 'eyesightdata.example.com';
assert.strictEqual(sanitizeCommand(`ping ${notJwt}`), `ping ${notJwt}`);
})
)
passed++;
else failed++;
if (
test('sanitizeCommand redacts whole quoted assignment values, including escapes', () => {
assert.strictEqual(
sanitizeCommand(`app --secret="first\\"${FAKE_VALUE}" --next`),
'app --secret=<REDACTED> --next'
);
assert.strictEqual(
sanitizeCommand(`app --secret=$'first\\'${FAKE_VALUE}' --next`),
'app --secret=<REDACTED> --next'
);
assert.strictEqual(sanitizeCommand(`app --secret=first\\"${FAKE_VALUE} --next`), 'app --secret=<REDACTED> --next');
assert.strictEqual(sanitizeCommand(`app --secret="${FAKE_VALUE} unterminated`), 'app --secret=<REDACTED>');
})
)
passed++;
else failed++;
if (
test('sanitizeCommand redacts the whole Bash word when fragments are joined', () => {
const tail = ['suffix', 'tail', '123'].join('');
const commands = [
`app --secret='prefix-'${tail} --next`,
`app --secret=prefix-"${tail} two" --next`,
`app --secret=first\\ ${tail} --next`,
`app --secret=${FAKE_VALUE}\\`,
];
for (const command of commands) {
const sanitized = sanitizeCommand(command);
assert.ok(!sanitized.includes(tail) && !sanitized.includes(FAKE_VALUE), `value leaked: ${sanitized}`);
}
assert.strictEqual(sanitizeCommand(commands[0]), 'app --secret=<REDACTED> --next');
})
)
passed++;
else failed++;
if (
test('sanitizeCommand keeps the rest of the command when the keyword is searched for', () => {
const commands = ['grep -n "secret =" settings.py', "rg 'api_key:' config/ --type yaml"];
for (const command of commands) {
assert.strictEqual(sanitizeCommand(command), command);
}
assert.strictEqual(
sanitizeCommand(`curl -H "x-api-key: ${FAKE_VALUE}" -d "payload" https://api.example.com`),
'curl -H "x-api-key: <REDACTED>" -d "payload" https://api.example.com'
);
})
)
passed++;
else failed++;
if (
test('sanitizeCommand redacts JWTs joined to a hyphenated or underscored prefix', () => {
assert.strictEqual(
sanitizeCommand(`curl --cookie "session=jwt-${FAKE_JWT}" https://example.com`),
'curl --cookie "session=jwt-<REDACTED>" https://example.com'
);
assert.strictEqual(sanitizeCommand(`echo prefix-${FAKE_JWT}`), 'echo prefix-<REDACTED>');
assert.strictEqual(sanitizeCommand(`echo ey-${FAKE_JWT}`), 'echo ey-<REDACTED>');
assert.strictEqual(sanitizeCommand(`open https://x.example.com/a.${FAKE_JWT}`), 'open https://x.example.com/a.<REDACTED>');
assert.strictEqual(sanitizeCommand('ping eyesightdata.example.com'), 'ping eyesightdata.example.com'); assert.strictEqual(sanitizeCommand(`echo session_${FAKE_JWT}`), 'echo session_<REDACTED>');
assert.strictEqual(sanitizeCommand(`export AUTH=bearer_${FAKE_JWT}`), 'export AUTH=bearer_<REDACTED>');
assert.strictEqual(sanitizeCommand('ls my_eyes.backup.tar'), 'ls my_eyes.backup.tar');
})
)
passed++;
else failed++;
if (
test('sanitizeCommand tries later JWT headers after a prefix candidate fails', () => {
const spacedHeader = Buffer.from('{ "alg": "HS256" }').toString('base64url');
const newlineHeader = Buffer.from('{\n"alg":"HS256"}').toString('base64url');
for (const header of [spacedHeader, newlineHeader]) {
const token = [header, 'eyJzdWIiOiJmYWtlIn0', 'ZmFrZXNpZ25hdHVyZQ'].join('.');
assert.strictEqual(sanitizeCommand(`echo eyewear_session_${token}`), 'echo eyewear_session_<REDACTED>');
assert.strictEqual(sanitizeCommand(`echo ey-eye-${token}`), 'echo ey-eye-<REDACTED>');
}
assert.strictEqual(sanitizeCommand('ls eyewear_session_eyebrows.backup.tar'), 'ls eyewear_session_eyebrows.backup.tar');
})
)
passed++;
else failed++;
if (
test('sanitizeCommand redacts a JWT that follows more decoy header candidates than the decode budget', () => {
const spacedHeader = Buffer.from('{ "alg":"HS256"}').toString('base64url');
const token = [spacedHeader, 'e30', 'c2ln'].join('.');
for (const count of [7, 8, 9, 20]) {
const prefix = 'eyAg-'.repeat(count);
assert.strictEqual(sanitizeCommand(`echo ${prefix}${token}`), `echo ${prefix.slice(0, 5 * Math.min(count, 8))}<REDACTED>`);
}
})
)
passed++;
else failed++;
if (
test('sanitizeCommand stays linear on long runs of plausible JWT header candidates', () => {
const start = Date.now();
sanitizeCommand(`${'eyAg-'.repeat(200000)}x.y.z`);
sanitizeCommand(`${'ewog_'.repeat(200000)}x.y.z`);
const elapsed = Date.now() - start;
assert.ok(elapsed < 2000, `took ${elapsed} ms`);
})
)
passed++;
else failed++;
if (
test('sanitizeCommand stays linear on long ey- runs followed by dots', () => {
const start = Date.now();
sanitizeCommand(`${'ey-'.repeat(300000)}x.y.z`);
sanitizeCommand(`${'ew-'.repeat(300000)}x.y.z`);
sanitizeCommand('ey-ey-ey-ey.'.repeat(80000));
const elapsed = Date.now() - start;
assert.ok(elapsed < 2000, `took ${elapsed} ms`);
})
)
passed++;
else failed++;
if (
test('sanitizeCommand stays linear on long dot-free ey- runs', () => {
const start = Date.now();
sanitizeCommand('ey-'.repeat(350000));
sanitizeCommand('eyJ-'.repeat(250000));
const elapsed = Date.now() - start;
assert.ok(elapsed < 2000, `took ${elapsed} ms`);
})
)
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=<REDACTED> --api_key=<REDACTED> --api-key=<REDACTED>'
);
assert.strictEqual(sanitizeCommand(`CLIENT_SECRET=${FAKE_VALUE} run`), 'CLIENT_SECRET=<REDACTED> run');
assert.strictEqual(
sanitizeCommand(`curl -H "x-api-key: ${FAKE_VALUE}" https://api.example.com`),
'curl -H "x-api-key: <REDACTED>" 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 <REDACTED> > 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=<REDACTED>'));
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++;
// POSIX permission bits do not apply on Windows.
const checkModes = process.platform !== 'win32';
if (
test('audit and cost logs are created readable by the owner only (#3846)', () => {
if (!checkModes) return;
const homeDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-bash-log-'));
try {
for (const [mode, fileName] of [
['audit', 'bash-commands.log'],
['cost', 'cost-tracker.log'],
]) {
const result = runHook(mode, { tool_input: { command: 'ls' } }, homeDir);
assert.strictEqual(result.status, 0, result.stdout + result.stderr);
const logFile = path.join(homeDir, '.claude', fileName);
assert.strictEqual(fs.statSync(logFile).mode & 0o777, 0o600, fileName);
assert.ok(!fs.existsSync(`${logFile}.1`), `${fileName} should not rotate`);
}
} finally {
fs.rmSync(homeDir, { recursive: true, force: true });
}
})
)
passed++;
else failed++;
if (
test('an existing group/world-readable log is tightened on the next write (#3846)', () => {
if (!checkModes) return;
const homeDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-bash-log-'));
const logFile = path.join(homeDir, '.claude', 'bash-commands.log');
try {
fs.mkdirSync(path.dirname(logFile), { recursive: true });
fs.writeFileSync(logFile, '[earlier] git status\n');
fs.chmodSync(logFile, 0o664);
const result = runHook('audit', { tool_input: { command: 'git log' } }, homeDir);
assert.strictEqual(result.status, 0, result.stdout + result.stderr);
assert.strictEqual(fs.statSync(logFile).mode & 0o777, 0o600);
const logContent = fs.readFileSync(logFile, 'utf8');
assert.ok(logContent.startsWith('[earlier] git status\n'), logContent);
assert.ok(logContent.includes('git log'), logContent);
} finally {
fs.rmSync(homeDir, { recursive: true, force: true });
}
})
)
passed++;
else failed++;
if (
test('a log at the size cap rotates to .1 and keeps one rotated copy (#3846)', () => {
const homeDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-bash-log-'));
const logFile = path.join(homeDir, '.claude', 'bash-commands.log');
const rotated = `${logFile}.1`;
try {
fs.mkdirSync(path.dirname(logFile), { recursive: true });
fs.writeFileSync(logFile, '[first] generation\n');
// Sparse fill keeps the fixture cheap while reporting the full size.
fs.truncateSync(logFile, LOG_MAX_BYTES);
let result = runHook('audit', { tool_input: { command: 'echo second' } }, homeDir);
assert.strictEqual(result.status, 0, result.stdout + result.stderr);
assert.strictEqual(fs.statSync(rotated).size, LOG_MAX_BYTES);
assert.ok(fs.readFileSync(rotated, 'utf8').startsWith('[first] generation\n'));
const fresh = fs.readFileSync(logFile, 'utf8');
assert.match(fresh, /^\[[^\]]+\] echo second\n$/);
if (checkModes) assert.strictEqual(fs.statSync(logFile).mode & 0o777, 0o600);
// Filling the new log again replaces the previous rotated copy.
fs.truncateSync(logFile, LOG_MAX_BYTES);
result = runHook('audit', { tool_input: { command: 'echo third' } }, homeDir);
assert.strictEqual(result.status, 0, result.stdout + result.stderr);
assert.ok(fs.readFileSync(rotated, 'utf8').includes('echo second'));
assert.ok(!fs.existsSync(`${logFile}.2`));
assert.match(fs.readFileSync(logFile, 'utf8'), /^\[[^\]]+\] echo third\n$/);
} finally {
fs.rmSync(homeDir, { recursive: true, force: true });
}
})
)
passed++;
else failed++;
if (
test('a failed rotation still records the command (#3846)', () => {
const homeDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-bash-log-'));
const logFile = path.join(homeDir, '.claude', 'bash-commands.log');
try {
// A non-empty directory at <log>.1 makes the rename fail on every platform.
fs.mkdirSync(path.join(`${logFile}.1`, 'blocker'), { recursive: true });
fs.writeFileSync(logFile, '[first] generation\n');
fs.truncateSync(logFile, LOG_MAX_BYTES);
const result = runHook('audit', { tool_input: { command: 'echo kept' } }, homeDir);
assert.strictEqual(result.status, 0, result.stdout + result.stderr);
assert.ok(fs.statSync(`${logFile}.1`).isDirectory());
assert.ok(fs.statSync(logFile).size > LOG_MAX_BYTES);
const fd = fs.openSync(logFile, 'r');
try {
const tail = Buffer.alloc(64);
const read = fs.readSync(fd, tail, 0, tail.length, fs.statSync(logFile).size - tail.length);
assert.ok(tail.subarray(0, read).toString('utf8').endsWith('echo kept\n'));
} finally {
fs.closeSync(fd);
}
} 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);