mirror of
https://github.com/affaan-m/ECC.git
synced 2026-12-06 23:24:01 +09:00
Merge reviewed PR #4039 into security and evaluation batch
This commit is contained in:
2 files changed
+189
-2
No files matched your search
@@ -190,9 +190,46 @@ function sanitizeCommand(command) {
|
||||
.replace(/\bgithub_pat_[A-Za-z0-9_]+\b/g, '<REDACTED>');
|
||||
}
|
||||
|
||||
// The logs hold every Bash command, so they are owner-only and size-capped:
|
||||
// past LOG_MAX_BYTES the log moves to `<log>.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.
|
||||
}
|
||||
// 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`);
|
||||
} catch {
|
||||
// Rotation is best effort (another process may have rotated first, or
|
||||
// Windows may refuse the rename); still record this command.
|
||||
}
|
||||
}
|
||||
|
||||
// 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 {
|
||||
fs.writeSync(fd, data);
|
||||
} finally {
|
||||
fs.closeSync(fd);
|
||||
}
|
||||
}
|
||||
|
||||
// Resolve the Claude profile directory the same way everywhere.
|
||||
@@ -250,6 +287,7 @@ if (require.main === module) {
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
LOG_MAX_BYTES,
|
||||
run,
|
||||
sanitizeCommand,
|
||||
};
|
||||
@@ -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,155 @@ 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++;
|
||||
|
||||
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-'));
|
||||
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);
|
||||
Reference in new issue
Block a user