From 87b4eed5c1678e8a52376498f2ca98bd7ed04bce Mon Sep 17 00:00:00 2001 From: Afloat Date: Wed, 11 Nov 2026 21:39:50 +0800 Subject: [PATCH 1/2] fix(metrics): fingerprint and track native notebook edits --- scripts/hooks/ecc-metrics-bridge.js | 14 ++-- tests/hooks/ecc-notebook-metrics.test.js | 84 ++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 tests/hooks/ecc-notebook-metrics.test.js diff --git a/scripts/hooks/ecc-metrics-bridge.js b/scripts/hooks/ecc-metrics-bridge.js index 31ecad948..6d221b424 100644 --- a/scripts/hooks/ecc-metrics-bridge.js +++ b/scripts/hooks/ecc-metrics-bridge.js @@ -63,7 +63,13 @@ function hashToolCall(toolName, toolInput) { key = crypto .createHash('sha256') .update( - stableStringify({ + stableStringify(name === 'NotebookEdit' ? { + notebook_path: toolInput?.notebook_path, + cell_id: toolInput?.cell_id, + new_source: toolInput?.new_source, + edit_mode: toolInput?.edit_mode, + cell_type: toolInput?.cell_type + } : { file_path: toolInput?.file_path, old_string: toolInput?.old_string, new_string: toolInput?.new_string, @@ -87,7 +93,7 @@ function extractFilePaths(toolName, toolInput) { const paths = []; if (!toolInput || typeof toolInput !== 'object') return paths; - const fp = toolInput.file_path; + const fp = /^NotebookEdit$/i.test(toolName) ? toolInput.notebook_path : toolInput.file_path; if (fp && typeof fp === 'string') paths.push(fp); const edits = toolInput.edits; @@ -229,8 +235,8 @@ function run(rawInput) { bridge.last_timestamp = now; if (!bridge.first_timestamp) bridge.first_timestamp = now; - // Track modified files (Write/Edit/MultiEdit only) - const isWriteOp = /^(Write|Edit|MultiEdit)$/i.test(toolName); + // Track modified files, including notebooks edited through NotebookEdit. + const isWriteOp = /^(Write|Edit|MultiEdit|NotebookEdit)$/i.test(toolName); if (isWriteOp) { const newPaths = extractFilePaths(toolName, toolInput); const existing = new Set(bridge.files_modified || []); diff --git a/tests/hooks/ecc-notebook-metrics.test.js b/tests/hooks/ecc-notebook-metrics.test.js new file mode 100644 index 000000000..1ae04a9c8 --- /dev/null +++ b/tests/hooks/ecc-notebook-metrics.test.js @@ -0,0 +1,84 @@ +'use strict'; + +const assert = require('node:assert/strict'); +const { spawnSync } = require('node:child_process'); +const fs = require('node:fs'); +const os = require('node:os'); +const path = require('node:path'); +const test = require('node:test'); +const { run, hashToolCall, extractFilePaths } = require('../../scripts/hooks/ecc-metrics-bridge'); +const { detectLoop } = require('../../scripts/hooks/ecc-context-monitor'); + +test('NotebookEdit fingerprints distinguish each native edit field', () => { + const original = { + notebook_path: '/repo/analysis.ipynb', cell_id: 'cell-1', + new_source: 'print(1)', edit_mode: 'replace', cell_type: 'code' + }; + const originalHash = hashToolCall('NotebookEdit', original); + for (const change of [ + { notebook_path: '/repo/other.ipynb' }, { cell_id: 'cell-2' }, + { new_source: 'print(2)' }, { edit_mode: 'insert' }, { cell_type: 'markdown' } + ]) { + assert.notEqual(hashToolCall('NotebookEdit', { ...original, ...change }), originalHash, + `NotebookEdit must distinguish ${Object.keys(change)[0]}`); + } + assert.equal(hashToolCall('NotebookEdit', { ...original }), originalHash); +}); + +test('file fingerprints retain existing tool and missing-input behavior', () => { + assert.match(hashToolCall(null, null), /^[0-9a-f]{8}$/); + assert.equal(hashToolCall('NotebookEdit'), hashToolCall('NotebookEdit', {})); + assert.equal(hashToolCall('Bash'), hashToolCall('Bash', { command: '' })); + assert.notEqual(hashToolCall('Read', { file_path: 'a.js' }), hashToolCall('Read', { file_path: 'b.js' })); + assert.notEqual( + hashToolCall('MultiEdit', { edits: [{ old_string: 'a', new_string: 'b' }] }), + hashToolCall('MultiEdit', { edits: [{ old_string: 'a', new_string: 'c' }] }) + ); +}); + +test('NotebookEdit extracts its notebook_path and ignores invalid paths', () => { + assert.deepEqual(extractFilePaths('NotebookEdit', { notebook_path: '/repo/a.ipynb' }), ['/repo/a.ipynb']); + assert.deepEqual(extractFilePaths('NotebookEdit', { notebook_path: 42 }), []); +}); + +test('NotebookEdit tracks notebook scope without false loop warnings', t => { + const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-notebook-metrics-')); + t.after(() => fs.rmSync(tmpHome, { recursive: true, force: true })); + const temporaryDirectory = path.join(tmpHome, 'tmp'); + fs.mkdirSync(temporaryDirectory); + const sessionId = `notebook-edits-${process.pid}`; + const bridgeScript = path.resolve(__dirname, '../../scripts/hooks/ecc-metrics-bridge.js'); + const bridgePath = path.join(temporaryDirectory, `ecc-metrics-${sessionId}.json`); + const readBridge = () => JSON.parse(fs.readFileSync(bridgePath, 'utf8')); + const invoke = toolInput => { + const input = JSON.stringify({ session_id: sessionId, tool_name: 'NotebookEdit', tool_input: toolInput }); + const result = spawnSync(process.execPath, [bridgeScript], { + input, encoding: 'utf8', cwd: tmpHome, timeout: 5000, + env: { + ...process.env, HOME: tmpHome, USERPROFILE: tmpHome, + ECC_AGENT_DATA_HOME: path.join(tmpHome, '.claude'), + CLAUDE_CONFIG_DIR: path.join(tmpHome, '.claude'), + TMPDIR: temporaryDirectory, TEMP: temporaryDirectory, TMP: temporaryDirectory + } + }); + assert.equal(result.status, 0, result.stderr); + assert.equal(result.stdout, input); + }; + const notebook = path.join(tmpHome, 'analysis.ipynb'); + for (let cell = 0; cell < 5; cell++) { + invoke({ notebook_path: notebook, cell_id: `cell-${cell}`, new_source: 'print(1)' }); + } + const distinct = readBridge(); + assert.equal(detectLoop(distinct.recent_tools).detected, false, + 'Editing different notebook cells must not be treated as a stuck loop'); + assert.deepEqual(distinct.files_modified, [notebook]); + assert.equal(distinct.files_modified_count, 1); + const repeated = { notebook_path: notebook, cell_id: 'same-cell', new_source: 'print(2)' }; + for (let repeat = 0; repeat < 5; repeat++) invoke(repeated); + assert.equal(detectLoop(readBridge().recent_tools).detected, true, + 'Identical repeated notebook edits must still be detected'); +}); + +test('malformed hook input remains unchanged', () => { + assert.equal(run('{invalid'), '{invalid'); +}); From 0619ff16bd03fbf78b17aabfef644d021641f69e Mon Sep 17 00:00:00 2001 From: Afloat Date: Fri, 13 Nov 2026 01:09:07 +0800 Subject: [PATCH 2/2] test(metrics): verify NotebookEdit security boundaries Exercise the real hook with isolated runtime directories. Verify that raw cell content stays out of persisted metrics and is not executed, notebook paths remain inert metadata, and traversal session IDs cannot select a bridge file. Preserve the hook's pass-through input contract. The focused notebook and bridge suites pass all 36 cases. Targeted statement/line, branch and function coverage is 91.9%, 85.18% and 100%. Changed-file ESLint and diff checks pass. --- .../ecc-notebook-metrics-security.test.js | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 tests/hooks/ecc-notebook-metrics-security.test.js diff --git a/tests/hooks/ecc-notebook-metrics-security.test.js b/tests/hooks/ecc-notebook-metrics-security.test.js new file mode 100644 index 000000000..bb7f03b86 --- /dev/null +++ b/tests/hooks/ecc-notebook-metrics-security.test.js @@ -0,0 +1,105 @@ +'use strict'; + +/** + * Security evidence for NotebookEdit metrics. + * + * Run the real hook with isolated data/config/temp directories. Notebook source + * remains opaque input: metrics persist only a fingerprint and file metadata. + */ +const assert = require('node:assert/strict'); +const { spawnSync } = require('node:child_process'); +const fs = require('node:fs'); +const os = require('node:os'); +const path = require('node:path'); +const test = require('node:test'); + +const bridgeScript = path.resolve(__dirname, '../../scripts/hooks/ecc-metrics-bridge.js'); + +/** Create a private hook workspace without inheriting the caller's session. */ +function createFixture(t) { + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-notebook-security-')); + t.after(() => fs.rmSync(root, { recursive: true, force: true })); + const temporaryDirectory = path.join(root, 'tmp'); + const workspace = path.join(root, 'workspace'); + fs.mkdirSync(temporaryDirectory); + fs.mkdirSync(workspace); + const configDirectory = path.join(root, '.claude'); + + /** Invoke the production entrypoint and preserve its pass-through contract. */ + function invoke(toolInput, sessionId = 'notebook-security') { + const input = JSON.stringify({ + session_id: sessionId, tool_name: 'NotebookEdit', tool_input: toolInput + }); + const result = spawnSync(process.execPath, [bridgeScript], { + input, encoding: 'utf8', cwd: workspace, timeout: 5000, + env: { + ...process.env, HOME: root, USERPROFILE: root, + ECC_AGENT_DATA_HOME: configDirectory, CLAUDE_CONFIG_DIR: configDirectory, + ECC_SESSION_ID: '', CLAUDE_SESSION_ID: '', + TMPDIR: temporaryDirectory, TEMP: temporaryDirectory, TMP: temporaryDirectory + } + }); + assert.equal(result.status, 0, result.stderr); + assert.equal(result.stdout, input); + assert.equal(result.stderr, ''); + } + + return { + root, workspace, temporaryDirectory, invoke, + bridgePath: path.join(temporaryDirectory, 'ecc-metrics-notebook-security.json') + }; +} + +test('NotebookEdit metrics never execute or persist raw cell contents', t => { + const fixture = createFixture(t); + const notebook = path.join(fixture.workspace, 'analysis.ipynb'); + const marker = path.join(fixture.root, 'executed-source'); + const originalNotebook = '{"cells":[]}\n'; + fs.writeFileSync(notebook, originalNotebook); + const source = '/* synthetic-private-cell-source */\n' + + `require('node:fs').writeFileSync(${JSON.stringify(marker)}, 'executed')`; + fixture.invoke({ + notebook_path: notebook, cell_id: 'synthetic-private-cell-id', + new_source: source, edit_mode: 'replace', cell_type: 'code' + }); + + const stored = fs.readFileSync(fixture.bridgePath, 'utf8'); + const bridge = JSON.parse(stored); + assert.equal(bridge.tool_count, 1); + assert.deepEqual(bridge.files_modified, [notebook]); + assert.deepEqual(Object.keys(bridge.recent_tools[0]).sort(), ['hash', 'tool']); + assert.equal(bridge.recent_tools[0].tool, 'NotebookEdit'); + assert.match(bridge.recent_tools[0].hash, /^[0-9a-f]{8}$/); + assert.ok(!stored.includes('synthetic-private-cell-source')); + assert.ok(!stored.includes('synthetic-private-cell-id')); + assert.ok(!stored.includes('writeFileSync')); + assert.equal(fs.existsSync(marker), false); + assert.equal(fs.readFileSync(notebook, 'utf8'), originalNotebook); +}); + +test('NotebookEdit paths are metadata, not filesystem write targets or commands', t => { + const fixture = createFixture(t); + const outsideNotebook = path.join(fixture.root, 'outside.ipynb'); + fs.writeFileSync(outsideNotebook, 'preserve this notebook'); + const paths = ['../outside.ipynb', '../outside.ipynb; echo injected', '$(echo injected).ipynb']; + for (const notebookPath of paths) { + fixture.invoke({ notebook_path: notebookPath, new_source: 'replacement cell' }); + } + fixture.invoke({ notebook_path: { file_path: '../outside.ipynb' }, new_source: 'ignored path' }); + + const bridge = JSON.parse(fs.readFileSync(fixture.bridgePath, 'utf8')); + assert.equal(bridge.tool_count, 4); + assert.deepEqual(bridge.files_modified, paths); + assert.equal(bridge.files_modified_count, paths.length); + assert.equal(fs.readFileSync(outsideNotebook, 'utf8'), 'preserve this notebook'); + assert.deepEqual(fs.readdirSync(fixture.workspace), []); +}); + +test('NotebookEdit cannot use a traversal session ID to choose a bridge file', t => { + const fixture = createFixture(t); + for (const sessionId of ['../outside', '..\\outside', 'normal/../../outside']) { + fixture.invoke({ notebook_path: '../outside.ipynb', new_source: 'private source' }, sessionId); + } + assert.deepEqual(fs.readdirSync(fixture.temporaryDirectory), []); + assert.deepEqual(fs.readdirSync(fixture.root).sort(), ['tmp', 'workspace']); +});