From c940659dabec8778b3d9fc47e448c3ab943c4d5c Mon Sep 17 00:00:00 2001 From: Gaurav Dubey Date: Thu, 19 Nov 2026 03:35:48 +0000 Subject: [PATCH 1/3] fix(hooks): create command logs owner-only and rotate them bash-commands.log and cost-tracker.log record every Bash command but were created with the default umask (0664 in the report) and grew without bound. appendLine now opens them with mode 0600, tightens an existing group/world-readable log on the next write, and rotates the log to .1 once it would pass 10 MiB, keeping one rotated copy. This mirrors the metrics.jsonl scheme in scripts/lib/gateguard-metrics.js, inlined because hooks cannot require scripts/lib/. Refs #3846 --- scripts/hooks/post-bash-command-log.js | 36 +++++++- tests/scripts/post-bash-command-log.test.js | 93 ++++++++++++++++++++- 2 files changed, 127 insertions(+), 2 deletions(-) diff --git a/scripts/hooks/post-bash-command-log.js b/scripts/hooks/post-bash-command-log.js index 98249070a..4802e2012 100644 --- a/scripts/hooks/post-bash-command-log.js +++ b/scripts/hooks/post-bash-command-log.js @@ -190,9 +190,42 @@ function sanitizeCommand(command) { .replace(/\bgithub_pat_[A-Za-z0-9_]+\b/g, ''); } +// The logs hold every Bash command, so they are owner-only and size-capped: +// past LOG_MAX_BYTES the log moves to `.1` (replacing the previous one) +// and a fresh log starts. Same scheme as scripts/lib/gateguard-metrics.js, +// inlined because hooks cannot require scripts/lib/ (see #3259). +const LOG_MAX_BYTES = 10 * 1024 * 1024; +const LOG_FILE_MODE = 0o600; + function appendLine(filePath, line) { + const data = `${line}\n`; fs.mkdirSync(path.dirname(filePath), { recursive: true }); - fs.appendFileSync(filePath, `${line}\n`, 'utf8'); + + let stat = null; + try { + stat = fs.statSync(filePath); + } catch { + // No log yet. + } + if (stat && stat.isFile() && stat.size > 0 && stat.size + Buffer.byteLength(data) > LOG_MAX_BYTES) { + fs.renameSync(filePath, `${filePath}.1`); + } + + const fd = fs.openSync(filePath, 'a', LOG_FILE_MODE); + try { + // The mode above only applies when the file is created. Logs written by + // earlier versions were group/world readable, so tighten them here. + // Windows has no POSIX permission bits to tighten. + if (process.platform !== 'win32') { + const current = fs.fstatSync(fd); + if (current.isFile() && (current.mode & 0o077) !== 0) { + fs.fchmodSync(fd, LOG_FILE_MODE); + } + } + fs.writeSync(fd, data); + } finally { + fs.closeSync(fd); + } } // Resolve the Claude profile directory the same way everywhere. @@ -250,6 +283,7 @@ if (require.main === module) { } module.exports = { + LOG_MAX_BYTES, run, sanitizeCommand, }; diff --git a/tests/scripts/post-bash-command-log.test.js b/tests/scripts/post-bash-command-log.test.js index 9537adbee..5c84d53d3 100644 --- a/tests/scripts/post-bash-command-log.test.js +++ b/tests/scripts/post-bash-command-log.test.js @@ -5,7 +5,7 @@ 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); +const { LOG_MAX_BYTES, sanitizeCommand } = require(scriptPath); function test(name, fn) { try { @@ -354,6 +354,97 @@ if ( 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++; + console.log(`\nPassed: ${passed}`); console.log(`Failed: ${failed}`); process.exit(failed > 0 ? 1 : 0); From 9e6272d5dff1e3c24c56ad4034e7f83205fb06fb Mon Sep 17 00:00:00 2001 From: Gaurav Dubey Date: Thu, 19 Nov 2026 03:57:06 +0000 Subject: [PATCH 2/3] fix(hooks): keep recording the command when log rotation fails If the rename to .1 fails (another session rotated first, or Windows refuses the rename), append to the current log instead of dropping the line. Refs #3846 --- scripts/hooks/post-bash-command-log.js | 7 ++++- tests/scripts/post-bash-command-log.test.js | 32 +++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/scripts/hooks/post-bash-command-log.js b/scripts/hooks/post-bash-command-log.js index 4802e2012..dffbba6de 100644 --- a/scripts/hooks/post-bash-command-log.js +++ b/scripts/hooks/post-bash-command-log.js @@ -208,7 +208,12 @@ function appendLine(filePath, line) { // No log yet. } if (stat && stat.isFile() && stat.size > 0 && stat.size + Buffer.byteLength(data) > LOG_MAX_BYTES) { - fs.renameSync(filePath, `${filePath}.1`); + try { + fs.renameSync(filePath, `${filePath}.1`); + } catch { + // Rotation is best effort (another process may have rotated first, or + // Windows may refuse the rename); still record this command. + } } const fd = fs.openSync(filePath, 'a', LOG_FILE_MODE); diff --git a/tests/scripts/post-bash-command-log.test.js b/tests/scripts/post-bash-command-log.test.js index 5c84d53d3..71be45a20 100644 --- a/tests/scripts/post-bash-command-log.test.js +++ b/tests/scripts/post-bash-command-log.test.js @@ -445,6 +445,38 @@ if ( 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 .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); From b427484d1eef05c771527f9018bddf0904afcbcb Mon Sep 17 00:00:00 2001 From: Gaurav Dubey Date: Thu, 19 Nov 2026 04:21:19 +0000 Subject: [PATCH 3/3] fix(hooks): tighten a legacy log before rotating it An existing 0664 log that was already at the cap was renamed to .1 before its mode was tightened, so the rotated copy stayed group/world readable. Tighten the existing log first, and drop the post-open fchmod that only reached the new active log. Refs #3846 --- scripts/hooks/post-bash-command-log.js | 17 +++++++------- tests/scripts/post-bash-command-log.test.js | 26 +++++++++++++++++++++ 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/scripts/hooks/post-bash-command-log.js b/scripts/hooks/post-bash-command-log.js index dffbba6de..606c608d2 100644 --- a/scripts/hooks/post-bash-command-log.js +++ b/scripts/hooks/post-bash-command-log.js @@ -207,6 +207,12 @@ function appendLine(filePath, line) { } catch { // No log yet. } + // Logs written by earlier versions were group/world readable. Tighten an + // existing log before it can be rotated, so the `.1` copy is private too. + // Windows has no POSIX permission bits to tighten. + if (stat && stat.isFile() && process.platform !== 'win32' && (stat.mode & 0o077) !== 0) { + fs.chmodSync(filePath, LOG_FILE_MODE); + } if (stat && stat.isFile() && stat.size > 0 && stat.size + Buffer.byteLength(data) > LOG_MAX_BYTES) { try { fs.renameSync(filePath, `${filePath}.1`); @@ -216,17 +222,10 @@ function appendLine(filePath, line) { } } + // The mode only applies when this call creates the file; an existing log + // was tightened above. const fd = fs.openSync(filePath, 'a', LOG_FILE_MODE); try { - // The mode above only applies when the file is created. Logs written by - // earlier versions were group/world readable, so tighten them here. - // Windows has no POSIX permission bits to tighten. - if (process.platform !== 'win32') { - const current = fs.fstatSync(fd); - if (current.isFile() && (current.mode & 0o077) !== 0) { - fs.fchmodSync(fd, LOG_FILE_MODE); - } - } fs.writeSync(fd, data); } finally { fs.closeSync(fd); diff --git a/tests/scripts/post-bash-command-log.test.js b/tests/scripts/post-bash-command-log.test.js index 71be45a20..7c1fc1166 100644 --- a/tests/scripts/post-bash-command-log.test.js +++ b/tests/scripts/post-bash-command-log.test.js @@ -445,6 +445,32 @@ if ( passed++; else failed++; +if ( + test('a legacy group/world-readable log at the cap rotates to a private .1 (#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, '[legacy] history\n'); + fs.truncateSync(logFile, LOG_MAX_BYTES); + fs.chmodSync(logFile, 0o664); + + const result = runHook('audit', { tool_input: { command: 'echo next' } }, homeDir); + assert.strictEqual(result.status, 0, result.stdout + result.stderr); + + assert.ok(fs.readFileSync(`${logFile}.1`, 'utf8').startsWith('[legacy] history\n')); + assert.strictEqual(fs.statSync(`${logFile}.1`).mode & 0o777, 0o600); + assert.strictEqual(fs.statSync(logFile).mode & 0o777, 0o600); + } 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-'));