mirror of
https://github.com/affaan-m/ECC.git
synced 2026-11-17 13:32:18 +00:00
fix(hooks): keep no structured tool result in activity history
The tracker serialized any structured result with JSON.stringify before redaction, so an encoding nested in it, such as a double-escaped JSON token, reached tool-usage.jsonl whole, and Claude Code's Bash stdout went in as text. Keep text only from the adapter's tool_output, as main did, and from Claude Code's tool_response only the sizes of stdout and stderr and its interrupted and image flags. Any other structure is left out.
This commit is contained in:
2 files changed
+71
-18
No files matched your search
@@ -12,7 +12,6 @@ const crypto = require('crypto');
|
||||
const path = require('path');
|
||||
const { spawnSync } = require('child_process');
|
||||
const { sanitizeSessionId } = require('../lib/session-bridge');
|
||||
const { getToolResult } = require('./hook-input');
|
||||
const {
|
||||
appendFile,
|
||||
getClaudeDir,
|
||||
@@ -581,25 +580,34 @@ function summarizeInput(toolName, toolInput, filePaths) {
|
||||
return truncateSummary(toolName);
|
||||
}
|
||||
|
||||
// Activity history keeps tool output text only from the adapter's `tool_output`, a
|
||||
// string, number, boolean or `{ output }`, and redacts it. A structured result is never
|
||||
// serialized, since an encoding nested in it escapes the redaction patterns.
|
||||
function summarizeOutput(toolOutput) {
|
||||
if (toolOutput === null || toolOutput === undefined) {
|
||||
return '';
|
||||
if (['string', 'number', 'boolean'].includes(typeof toolOutput)) {
|
||||
return truncateSummary(String(toolOutput));
|
||||
}
|
||||
|
||||
if (typeof toolOutput === 'string') {
|
||||
return truncateSummary(toolOutput);
|
||||
}
|
||||
|
||||
if (typeof toolOutput === 'object' && typeof toolOutput.output === 'string') {
|
||||
if (toolOutput && typeof toolOutput === 'object' && typeof toolOutput.output === 'string') {
|
||||
return truncateSummary(toolOutput.output);
|
||||
}
|
||||
|
||||
// A Bash result from Claude Code: { stdout, stderr, interrupted, isImage }.
|
||||
if (typeof toolOutput === 'object' && (typeof toolOutput.stdout === 'string' || typeof toolOutput.stderr === 'string')) {
|
||||
return truncateSummary([toolOutput.stdout, toolOutput.stderr].filter(part => typeof part === 'string').join('\n'));
|
||||
return '';
|
||||
}
|
||||
|
||||
// Of Claude Code's `tool_response`, only the sizes and flags of a Bash result
|
||||
// ({ stdout, stderr, interrupted, isImage }) are kept. None of them holds output.
|
||||
function summarizeToolResponse(response) {
|
||||
if (!response || typeof response !== 'object') {
|
||||
return '';
|
||||
}
|
||||
|
||||
return truncateSummary(JSON.stringify(toolOutput));
|
||||
const parts = ['stdout', 'stderr']
|
||||
.filter(field => typeof response[field] === 'string')
|
||||
.map(field => `${field}: ${response[field].length} chars`);
|
||||
if (response.interrupted === true) parts.push('interrupted');
|
||||
if (response.isImage === true) parts.push('image');
|
||||
return parts.join(', ');
|
||||
}
|
||||
|
||||
function buildActivityRow(input, env = process.env) {
|
||||
@@ -642,7 +650,9 @@ function buildActivityRow(input, env = process.env) {
|
||||
tool_name: toolName,
|
||||
input_summary: summarizeInput(toolName, toolInput, filePaths),
|
||||
input_params_json: sanitizeInputParams(toolInput),
|
||||
output_summary: summarizeOutput(getToolResult(input)),
|
||||
output_summary: input?.tool_response === undefined
|
||||
? summarizeOutput(input?.tool_output)
|
||||
: summarizeToolResponse(input.tool_response),
|
||||
duration_ms: 0,
|
||||
file_paths: filePaths,
|
||||
file_events: fileEvents,
|
||||
|
||||
@@ -564,9 +564,12 @@ function runTests() {
|
||||
assert.strictEqual(summarizeOutput(null), '');
|
||||
assert.strictEqual(summarizeOutput(undefined), '');
|
||||
assert.strictEqual(summarizeOutput('hello\nworld'), 'hello world');
|
||||
assert.strictEqual(summarizeOutput({ ok: true }), '{"ok":true}');
|
||||
// Claude Code sends the result as tool_response, for Bash { stdout, stderr, ... }.
|
||||
assert.strictEqual(summarizeOutput({ stdout: `token ghp_${'A'.repeat(20)}`, stderr: '' }), 'token <REDACTED>');
|
||||
// A structured result is not serialized: nested encodings would escape redaction.
|
||||
assert.strictEqual(summarizeOutput({ ok: true }), '');
|
||||
assert.strictEqual(summarizeOutput({ stdout: 'out', stderr: '' }), '');
|
||||
assert.strictEqual(summarizeOutput(`token ghp_${'A'.repeat(20)}`), 'token <REDACTED>');
|
||||
// Claude Code sends the result as tool_response, for Bash { stdout, stderr, ... }:
|
||||
// only its sizes and flags are kept.
|
||||
const bashRow = buildActivityRow(
|
||||
{
|
||||
hook_event_name: 'PostToolUse',
|
||||
@@ -577,10 +580,10 @@ function runTests() {
|
||||
},
|
||||
{}
|
||||
);
|
||||
assert.strictEqual(bashRow.output_summary, '12 passing warn: slow');
|
||||
assert.strictEqual(bashRow.output_summary, 'stdout: 11 chars, stderr: 11 chars');
|
||||
// Output such as `cat .env` lands here too, so the secrets governance-capture
|
||||
// detects are redacted as well.
|
||||
assert.strictEqual(summarizeOutput({ stdout: 'api_key = "example-secret-value"\n', stderr: '' }), 'api_key = <REDACTED>');
|
||||
assert.strictEqual(summarizeOutput('api_key = "example-secret-value"\n'), 'api_key = <REDACTED>');
|
||||
assert.strictEqual(summarizeOutput('OPENAI_API_KEY=sk-abc123def456\nDEBUG=1'), 'OPENAI_API_KEY=<REDACTED> DEBUG=1');
|
||||
assert.strictEqual(summarizeOutput('AWS_SECRET_ACCESS_KEY=example-secret-value'), 'AWS_SECRET_ACCESS_KEY=<REDACTED>');
|
||||
assert.strictEqual(summarizeOutput('DOCKER_AUTH_CONFIG={"auths":{"registry":{}}}'), 'DOCKER_AUTH_CONFIG=<REDACTED>');
|
||||
@@ -612,6 +615,46 @@ function runTests() {
|
||||
}
|
||||
}) ? passed++ : failed++);
|
||||
|
||||
(test('saves no part of a structured tool result in activity history', () => {
|
||||
const tmpHome = makeTempDir();
|
||||
const marker = 'S3CR3T-MARK';
|
||||
const env = { ...withTempHome(tmpHome), CLAUDE_HOOK_EVENT_NAME: 'PostToolUse', ECC_SESSION_ID: 'sess-privacy' };
|
||||
const bashOutput = [
|
||||
`{"token": "${marker}-direct"}`,
|
||||
JSON.stringify(JSON.stringify({ token: `${marker}-escaped` })),
|
||||
`Authorization: Bearer ${marker}-header`,
|
||||
`"Authorization": "Bearer ${marker}-quoted"`,
|
||||
`API_TOKEN=${marker}\\ escaped-space`,
|
||||
].join('\n');
|
||||
const payloads = [
|
||||
{ tool_name: 'Bash', tool_input: { command: 'cat .env' }, tool_response: { stdout: bashOutput, stderr: '', interrupted: false, isImage: false } },
|
||||
{
|
||||
tool_name: 'mcp__example__fetch',
|
||||
tool_input: {},
|
||||
tool_response: {
|
||||
content: [{ type: 'text', text: JSON.stringify({ api_key: `${marker}-nested` }) }],
|
||||
headers: { Authorization: `Bearer ${marker}-field` },
|
||||
raw: JSON.stringify(JSON.stringify(`password=${marker}-double`)),
|
||||
},
|
||||
},
|
||||
{ tool_name: 'Read', tool_input: { file_path: 'README.md' }, tool_output: { output: 'adapted result text' } },
|
||||
];
|
||||
for (const payload of payloads) {
|
||||
assert.strictEqual(runScript(payload, env).code, 0);
|
||||
}
|
||||
|
||||
const saved = fs.readFileSync(path.join(tmpHome, '.claude', 'metrics', 'tool-usage.jsonl'), 'utf8');
|
||||
assert.ok(!saved.includes(marker), `activity history kept a credential fragment: ${saved}`);
|
||||
const rows = readMetricRows(tmpHome);
|
||||
assert.deepStrictEqual(rows.map(row => row.output_summary), [
|
||||
`stdout: ${bashOutput.length} chars, stderr: 0 chars`,
|
||||
'',
|
||||
'adapted result text',
|
||||
]);
|
||||
|
||||
fs.rmSync(tmpHome, { recursive: true, force: true });
|
||||
}) ? passed++ : failed++);
|
||||
|
||||
(test('extracts file paths from nested arrays while filtering duplicates and remote URIs', () => {
|
||||
const paths = extractFilePaths({
|
||||
file_paths: [
|
||||
|
||||
Reference in new issue
Block a user