The agent harness performance optimization system. Skills, instincts, memory, security, and research-first development for Claude Code, Codex, Opencode, Cursor and beyond.
fix(config-protection): guard current config and ignore filenames
affaan-m339db5b0bed9
diff --cc tests/hooks/config-protection.test.jsindex 83c1bd797,87bce5365..787fbd346--- a/tests/hooks/config-protection.test.js+++ b/tests/hooks/config-protection.test.js@@@ -10,21 -10,21 +10,58 @@@ const { spawnSync } = require('child_prconst runner = path.join(__dirname, '..', '..', 'scripts', 'hooks', 'run-with-flags.js');++const SKIPPED = Symbol('skipped');++++function describeError(error) {++ try {++ return String(error && error.message ? error.message : error);++ } catch {++ return 'Unprintable thrown value';++ }++}++++function withOwnedDirectory(directory, action) {++ let value;++ let failed = false;++ let primary;++ try {++ value = action(directory);++ } catch (error) {++ failed = true;++ primary = error;++ }++ try {++ fs.rmSync(directory, { recursive: true, force: true });++ } catch (error) {++ if (!failed) {++ failed = true;++ primary = error;++ } else {++ console.error(` Cleanup error: ${describeError(error)}`);++ }++ }++ if (failed) throw primary;++ return value;++}++function test(name, fn) {try {-- fn();-- console.log(` ✓ ${name}`);-- return true;++ if (fn() === SKIPPED) {++ console.log(` SKIP ${name}`);++ return 'skipped';++ }++ console.log(` PASS ${name}`);++ return 'passed';} catch (error) {-- console.log(` ✗ ${name}`);-- console.log(` Error: ${error.message}`);-- return false;++ console.log(` FAIL ${name}`);++ console.log(` Error: ${describeError(error)}`);++ return 'failed';}}function runHook(input, env = {}) {const rawInput = typeof input === 'string' ? input : JSON.stringify(input);-- const result = spawnSync('node', [runner, 'pre:config-protection', 'scripts/hooks/config-protection.js', 'standard,strict'], {++ const result = spawnSync(process.execPath, [runner, 'pre:config-protection', 'scripts/hooks/config-protection.js', 'standard,strict'], {input: rawInput,encoding: 'utf8',env: {@@@ -45,7 -45,7 +82,7 @@@function runCustomHook(pluginRoot, hookId, relScriptPath, input, env = {}) {const rawInput = typeof input === 'string' ? input : JSON.stringify(input);-- const result = spawnSync('node', [runner, hookId, relScriptPath, 'standard,strict'], {++ const result = spawnSync(process.execPath, [runner, hookId, relScriptPath, 'standard,strict'], {input: rawInput,encoding: 'utf8',env: {@@@ -68,13 -68,13 +105,11 @@@function runTests() {console.log('\n=== Testing config-protection ===\n');-- let passed = 0;-- let failed = 0;++ const results = [];-- if (++ results.push(test('blocks protected config file edits through run-with-flags', () => {-- const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-config-protect-'));-- try {++ return withOwnedDirectory(fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-config-protect-')), tmpDir => {const absPath = path.join(tmpDir, '.eslintrc.js');fs.writeFileSync(absPath, 'module.exports = {};');@@@ -90,19 -90,19 +125,11 @@@assert.strictEqual(result.code, 2, 'Expected protected config edit to be blocked');assert.strictEqual(result.stdout, '', 'Blocked hook should not echo raw input');assert.ok(result.stderr.includes('BLOCKED: Modifying .eslintrc.js is not allowed.'), `Expected block message, got: ${result.stderr}`);-- } finally {-- try {-- fs.rmSync(tmpDir, { recursive: true, force: true });-- } catch {-- // best-effort cleanup-- }-- }++ });})-- )-- passed++;-- else failed++;++ );-- if (++ results.push(test('passes through safe file edits unchanged', () => {const input = {tool_name: 'Write',@@@ -112,17 -112,16 +139,14 @@@}};- const rawInput = JSON.stringify(input);const result = runHook(input);assert.strictEqual(result.code, 0, 'Expected safe file edit to pass');- assert.strictEqual(result.stdout, rawInput, 'Expected exact raw JSON passthrough');+ assert.strictEqual(result.stdout, '', 'Allowed edits should not echo raw hook input');assert.strictEqual(result.stderr, '', 'Expected no stderr for safe edits');})-- )-- passed++;-- else failed++;++ );-- if (++ results.push(test('blocks truncated protected config payloads instead of failing open', () => {const rawInput = JSON.stringify({tool_name: 'Write',@@@ -138,14 -137,14 +162,11 @@@assert.ok(result.stderr.includes('Hook input exceeded 1048576 bytes'), `Expected size warning, got: ${result.stderr}`);assert.ok(result.stderr.includes('truncated payload'), `Expected truncated payload warning, got: ${result.stderr}`);})-- )-- passed++;-- else failed++;++ );-- if (++ results.push(test('allows first-time creation of a protected config file', () => {-- const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-config-protect-'));-- try {++ return withOwnedDirectory(fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-config-protect-')), tmpDir => {const absPath = path.join(tmpDir, 'eslint.config.mjs');const input = {tool_name: 'Write',@@@ -155,27 -154,26 +176,17 @@@}};- const rawInput = JSON.stringify(input);const result = runHook(input);assert.strictEqual(result.code, 0, `Expected exit 0 for first-time creation, got ${result.code}; stderr: ${result.stderr}`);- assert.strictEqual(result.stdout, rawInput, 'Expected raw passthrough when creation is allowed');+ assert.strictEqual(result.stdout, '', 'Allowed creation should not echo raw hook input');assert.strictEqual(result.stderr, '', `Expected no stderr for first-time creation, got: ${result.stderr}`);-- } finally {-- try {-- fs.rmSync(tmpDir, { recursive: true, force: true });-- } catch {-- // best-effort cleanup-- }-- }++ });})-- )-- passed++;-- else failed++;++ );-- if (++ results.push(test('allows first-time creation when the parent directory does not exist yet', () => {-- const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-config-protect-'));-- try {++ return withOwnedDirectory(fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-config-protect-')), tmpDir => {// Path under a non-existent subdirectory — statSync returns ENOENT// on the final segment, which should be treated as "does not exist"// and allow the write. (Agent or CLI is expected to create parents@@@ -189,26 -187,25 +200,16 @@@}};- const rawInput = JSON.stringify(input);const result = runHook(input);assert.strictEqual(result.code, 0, `Expected exit 0 for ENOENT path, got ${result.code}; stderr: ${result.stderr}`);- assert.strictEqual(result.stdout, rawInput, 'Expected raw passthrough when path does not exist');- } finally {- try {- fs.rmSync(tmpDir, { recursive: true, force: true });- } catch {- // best-effort cleanup- }- }+ assert.strictEqual(result.stdout, '', 'Allowed missing paths should not echo raw hook input');- } finally {- try {- fs.rmSync(tmpDir, { recursive: true, force: true });- } catch {- // best-effort cleanup- }- }++ });})-- )-- passed++;-- else failed++;++ );-- if (++ results.push(test('blocks protected paths that exist as a dangling symlink', () => {-- const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-config-protect-'));-- try {++ return withOwnedDirectory(fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-config-protect-')), tmpDir => {const missingTarget = path.join(tmpDir, 'nowhere.js');const linkPath = path.join(tmpDir, '.eslintrc.js');try {@@@ -218,7 -215,7 +219,7 @@@// symlinks. Skip cleanly rather than fail the suite.if (err.code === 'EPERM' || err.code === 'EACCES') {console.log(' (skipped: symlink creation not permitted here)');-- return;++ return SKIPPED;}throw err;}@@@ -235,22 -232,22 +236,13 @@@assert.strictEqual(result.code, 2, `Expected exit 2 for dangling symlink, got ${result.code}; stderr: ${result.stderr}`);assert.strictEqual(result.stdout, '', 'Blocked hook should not echo raw input');assert.ok(result.stderr.includes('BLOCKED: Modifying .eslintrc.js is not allowed.'), `Expected block message, got: ${result.stderr}`);-- } finally {-- try {-- fs.rmSync(tmpDir, { recursive: true, force: true });-- } catch {-- // best-effort cleanup-- }-- }++ });})-- )-- passed++;-- else failed++;++ );-- if (++ results.push(test('blocks case-variant writes that resolve to an existing protected config', () => {-- const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-config-protect-'));-- try {++ return withOwnedDirectory(fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-config-protect-')), tmpDir => {const realPath = path.join(tmpDir, '.eslintrc.js');const variantPath = path.join(tmpDir, '.ESLINTRC.JS');fs.writeFileSync(realPath, 'module.exports = { rules: { "no-explicit-any": "error" } };');@@@ -267,7 -264,7 +259,7 @@@}if (!sameFile) {console.log(' (skipped: case-sensitive filesystem)');-- return;++ return SKIPPED;}const result = runHook({@@@ -280,22 -277,22 +272,13 @@@assert.strictEqual(result.code, 2, `Case-variant write must be blocked: it overwrites ${path.basename(realPath)} on this filesystem. Got ${result.code}; stderr: ${result.stderr}`);assert.strictEqual(result.stdout, '', 'Blocked hook should not echo raw input');-- } finally {-- try {-- fs.rmSync(tmpDir, { recursive: true, force: true });-- } catch {-- // best-effort cleanup-- }-- }++ });})-- )-- passed++;-- else failed++;++ );-- if (++ results.push(test('still blocks writes to an existing protected config file', () => {-- const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-config-protect-'));-- try {++ return withOwnedDirectory(fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-config-protect-')), tmpDir => {const absPath = path.join(tmpDir, '.eslintrc.js');fs.writeFileSync(absPath, 'module.exports = { rules: {} };');@@@ -311,139 -308,25 +294,111 @@@assert.strictEqual(result.code, 2, 'Expected exit 2 when modifying an existing protected config');assert.strictEqual(result.stdout, '', 'Blocked hook should not echo raw input');assert.ok(result.stderr.includes('BLOCKED: Modifying .eslintrc.js is not allowed.'), `Expected block message, got: ${result.stderr}`);-- } finally {-- try {-- fs.rmSync(tmpDir, { recursive: true, force: true });-- } catch {-- // best-effort cleanup- }- }++ });+ })- )- passed++;- else failed++;++ );+- if (++ results.push(+ test('blocks edits to an existing linter ignore file', () => {+ // Adding one path to .eslintignore silences a failing file without+ // touching the code or the config — the exact move this hook exists to+ // stop, and it was allowed. Measured before the fix: .eslintignore,+ // .prettierignore, .stylelintignore and .markdownlintignore all+ // returned exit 0.- const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-config-protect-'));- try {++ return withOwnedDirectory(fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-config-protect-')), tmpDir => {+ for (const name of [+ '.eslintignore',+ '.prettierignore',+ '.stylelintignore',+ '.markdownlintignore'+ ]) {+ const absPath = path.join(tmpDir, name);+ fs.writeFileSync(absPath, 'dist/\n');++ const result = runHook({+ tool_name: 'Edit',+ tool_input: { file_path: absPath, content: 'dist/\nsrc/failing-file.ts\n' }+ });++ assert.strictEqual(result.code, 2, `Expected exit 2 for ${name}, got ${result.code}`);+ assert.ok(+ result.stderr.includes(`BLOCKED: Modifying ${name} is not allowed.`),+ `Expected block message for ${name}, got: ${result.stderr}`+ );}- } finally {- try {- fs.rmSync(tmpDir, { recursive: true, force: true });- } catch {- // best-effort cleanup- }-- }++ });+ })- )- passed++;- else failed++;++ );+- if (++ results.push(+ test('blocks the current stylelint and markdownlint config spellings', () => {+ // Only the legacy `.stylelintrc*` / `.markdownlint.json` names were+ // listed, so a project on the documented `stylelint.config.js` or+ // markdownlint-cli2 had no protection at all.- const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-config-protect-'));- try {++ return withOwnedDirectory(fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-config-protect-')), tmpDir => {+ for (const name of [+ 'stylelint.config.js',++ 'stylelint.config.cjs',+ 'stylelint.config.mjs',+ 'stylelint.config.ts',+ 'stylelint.config.mts',+ 'stylelint.config.cts',++ '.stylelintrc.yaml',+ '.stylelintrc.js',++ '.stylelintrc.cjs',++ '.stylelintrc.mjs',+ '.markdownlint.jsonc',++ '.markdownlint.yml',+ '.markdownlint.cjs',+ '.markdownlint.mjs',- '.markdownlint-cli2.jsonc'++ '.markdownlint-cli2.jsonc',++ '.markdownlint-cli2.yaml',++ '.markdownlint-cli2.cjs',++ '.markdownlint-cli2.mjs',++ '.ESLINTIGNORE'+ ]) {+ const absPath = path.join(tmpDir, name);+ fs.writeFileSync(absPath, '{}');++ const result = runHook({+ tool_name: 'Edit',+ tool_input: { file_path: absPath, content: '{"rules": {}}' }+ });++ assert.strictEqual(result.code, 2, `Expected exit 2 for ${name}, got ${result.code}`);+ }- } finally {- try {- fs.rmSync(tmpDir, { recursive: true, force: true });- } catch {- // best-effort cleanup- }- }++ });})-- )-- passed++;-- else failed++;++ );+- if (++ results.push(+ test('a first-time ignore file and a lookalike name are still allowed', () => {- const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-config-protect-'));- try {++ return withOwnedDirectory(fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-config-protect-')), tmpDir => {+ // Scaffolding a brand-new ignore file is the same legitimate bootstrap+ // path the hook already allows for configs.+ const fresh = runHook({+ tool_name: 'Write',+ tool_input: { file_path: path.join(tmpDir, '.prettierignore'), content: 'dist/\n' }+ });+ assert.strictEqual(fresh.code, 0, `Expected exit 0 for a new ignore file, got ${fresh.code}`);- if (- test('legacy hooks do not echo raw input when they fail without stdout', () => {- const pluginRoot = path.join(__dirname, '..', `tmp-runner-plugin-${Date.now()}`);- const scriptDir = path.join(pluginRoot, 'scripts', 'hooks');- const scriptPath = path.join(scriptDir, 'legacy-block.js');+ // A file that merely looks like one must not be swept up.+ const lookalike = path.join(tmpDir, '.eslintignore.bak');+ fs.writeFileSync(lookalike, 'dist/\n');+ const result = runHook({+ tool_name: 'Edit',+ tool_input: { file_path: lookalike, content: 'dist/\nsrc/\n' }+ });+ assert.strictEqual(result.code, 0, `Expected exit 0 for ${path.basename(lookalike)}`);- } finally {- try {- fs.rmSync(tmpDir, { recursive: true, force: true });- } catch {- // best-effort cleanup- }- }++ });+ })- )- passed++;- else failed++;++ );- if (- try {++ results.push(+ test('legacy hooks do not echo raw input when they fail without stdout', () => {- const pluginRoot = path.join(__dirname, '..', `tmp-runner-plugin-${Date.now()}`);- const scriptDir = path.join(pluginRoot, 'scripts', 'hooks');- const scriptPath = path.join(scriptDir, 'legacy-block.js');-- try {++ return withOwnedDirectory(fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-config-legacy-')), pluginRoot => {++ const scriptDir = path.join(pluginRoot, 'scripts', 'hooks');++ const scriptPath = path.join(scriptDir, 'legacy-block.js');fs.mkdirSync(scriptDir, { recursive: true });fs.writeFileSync(scriptPath, '#!/usr/bin/env node\nprocess.stderr.write("blocked by legacy hook\\n");\nprocess.exit(2);\n');@@@ -459,20 -342,20 +414,15 @@@assert.strictEqual(result.code, 2, 'Expected failing legacy hook exit code to propagate');assert.strictEqual(result.stdout, '', 'Expected failing legacy hook to avoid raw passthrough');assert.ok(result.stderr.includes('blocked by legacy hook'), `Expected legacy hook stderr, got: ${result.stderr}`);-- } finally {-- try {-- fs.rmSync(pluginRoot, { recursive: true, force: true });-- } catch {-- // best-effort cleanup-- }-- }++ });})-- )-- passed++;-- else failed++;++ );-- console.log(`\nResults: Passed: ${passed}, Failed: ${failed}`);-- process.exit(failed > 0 ? 1 : 0);++ const passed = results.filter(result => result === 'passed').length;++ const failed = results.filter(result => result === 'failed').length;++ const skipped = results.filter(result => result === 'skipped').length;++ console.log(`\nResults: Passed: ${passed}, Failed: ${failed}, Skipped: ${skipped}`);++ process.exitCode = failed > 0 ? 1 : 0;}runTests();