Skip to content

armgit / ECC

Public Imported from GitHub
0

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): protect qualified shared linter configurations

affaan-m74023e568908
diff --cc scripts/hooks/config-protection.js
index e6b713541,2da5358c2..e82076575
--- a/scripts/hooks/config-protection.js
+++ b/scripts/hooks/config-protection.js
@@@ -62,40 -62,6 +62,40 @@@ const PROTECTED_FILES = new Set(
'.markdownlintrc'
]);
+/**
+ * Exact basenames only catch a tool's canonical entry point. Real repos split
+ * flat config across files: a shared `eslint.config.base.mjs` holding the
+ * ignore list and rule severities, imported by per-workspace
+ * `eslint.config.mjs` files. That is the common monorepo shape, and matching
- * basenames alone protected the leaves while leaving the trunk — the file that
- * actually carries the rules — freely editable.
++ * basenames alone protected the leaves while leaving the trunk -- the file that
++ * actually carries the rules -- freely editable.
+ *
+ * These patterns cover `<tool>.config.<qualifier>.<ext>` and
+ * `.<tool>rc.<qualifier>.<ext>` for the linters and formatters listed above.
+ * They are case-insensitive for the same reason the Set lookup above is.
+ *
- * Deliberately NOT matched: build and test tooling — `vite.config.ts`,
++ * Deliberately NOT matched: build and test tooling -- `vite.config.ts`,
+ * `vitest.config.ts`, `jest.config.js`, `playwright.config.ts`,
+ * `tsconfig.json`. This hook exists to stop a LINTER config being weakened in
+ * place of fixing the code; editing a bundler or test-runner config is
+ * ordinary work, and sweeping those in would make the hook obstructive.
+ */
+const PROTECTED_PATTERNS = [
- // eslint.config.base.mjs, prettier.config.shared.cjs, stylelint.config.local.js …
++ // eslint.config.base.mjs, prettier.config.shared.cjs, stylelint.config.local.js ...
+ /^(eslint|prettier|stylelint|commitlint|oxlint|biome)\.config(\.[A-Za-z0-9_-]+)*\.(js|mjs|cjs|ts|mts|cts)$/i,
- // .eslintrc.base.json, .prettierrc.shared.yml …
++ // .eslintrc.base.json, .prettierrc.shared.yml ...
+ /^\.(eslintrc|prettierrc|stylelintrc|markdownlintrc)(\.[A-Za-z0-9_-]+)*\.(js|cjs|mjs|json|jsonc|yml|yaml|toml)$/i,
+ // biome.base.json, biome.shared.jsonc
+ /^biome(\.[A-Za-z0-9_-]+)*\.jsonc?$/i,
+];
+
+function isProtectedName(basename) {
+ const lower = basename.toLowerCase();
+ return PROTECTED_FILES.has(basename)
+ || PROTECTED_FILES.has(lower)
+ || PROTECTED_PATTERNS.some((re) => re.test(basename));
+}
+
function parseInput(inputOrRaw) {
if (typeof inputOrRaw === 'string') {
try {
diff --cc tests/hooks/config-protection.test.js
index 3e5c02c9d,87bce5365..2ba65638a
--- a/tests/hooks/config-protection.test.js
+++ b/tests/hooks/config-protection.test.js
@@@ -10,21 -10,21 +10,58 @@@ const { spawnSync } = require('child_pr
const 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,25 -308,25 +294,15 @@@
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('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');
@@@ -345,80 -342,20 +318,73 @@@
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++;
++ );
+
- if (
++ results.push(
+ test('blocks shared/base flat configs, not just the canonical entry point', () => {
+ // Monorepos split flat config: a shared `eslint.config.base.mjs` holding
+ // the ignore list and rule severities, imported by per-workspace
+ // `eslint.config.mjs` files. Matching basenames alone protected the
+ // leaves and left the trunk -- the file that carries the rules -- editable.
- const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-config-protect-base-'));
- try {
- const names = ['eslint.config.base.mjs', 'prettier.config.shared.cjs', '.eslintrc.base.json', 'ESLint.Config.Base.MJS'];
++ return withOwnedDirectory(fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-config-protect-base-')), tmpDir => {
++ const names = [
++ 'eslint.config.base.mjs', 'prettier.config.shared.cjs', '.eslintrc.base.json', 'ESLint.Config.Base.MJS',
++ 'stylelint.config.local.ts', 'commitlint.config.shared.cts', 'oxlint.config.base.mts',
++ 'biome.config.shared.js', '.prettierrc.shared.yml', '.stylelintrc.team.toml',
++ '.markdownlintrc.team.jsonc', 'biome.shared.jsonc', 'BIOME.Team.Base.JSON'
++ ];
+ for (const name of names) {
+ const absPath = path.join(tmpDir, name);
+ fs.writeFileSync(absPath, '{}');
+
+ const result = runHook({ tool_name: 'Edit', tool_input: { file_path: absPath } });
+
+ assert.strictEqual(result.code, 2, 'Expected ' + name + ' to be blocked');
+ 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('does not block build or test tooling configs', () => {
+ // Pins the boundary: this hook guards LINTER configs. A future widening
+ // of the patterns must not quietly start blocking ordinary work.
- const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-config-protect-allow-'));
- try {
- const names = ['vite.config.ts', 'vitest.config.ts', 'jest.config.js', 'playwright.config.ts', 'tsconfig.json'];
++ return withOwnedDirectory(fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-config-protect-allow-')), tmpDir => {
++ const names = [
++ 'vite.config.ts', 'vitest.config.ts', 'jest.config.js', 'playwright.config.ts', 'tsconfig.json',
++ 'pyproject.toml', 'package.json', '.eslintignore.bak', 'not-eslint.config.base.mjs',
++ 'eslint.config..mjs', 'eslint.config.base.mjs.bak'
++ ];
+ for (const name of names) {
+ const absPath = path.join(tmpDir, name);
+ fs.writeFileSync(absPath, '{}');
+
+ const result = runHook({ tool_name: 'Edit', tool_input: { file_path: absPath } });
+
+ assert.strictEqual(result.code, 0, 'Expected ' + name + ' to be allowed, stderr: ' + result.stderr);
}
- } finally {
- try {
- fs.rmSync(tmpDir, { recursive: true, force: true });
- } catch {
- // best-effort cleanup
- }
-- }
++
++ const fresh = runHook({
++ tool_name: 'Write',
++ tool_input: { file_path: path.join(tmpDir, 'eslint.config.new.mjs'), content: 'export default [];' }
++ });
++ assert.strictEqual(fresh.code, 0, 'Expected first-time qualified config creation to be allowed');
++ assert.strictEqual(fresh.stdout, '', 'Allowed qualified creation should not echo raw hook input');
++ });
})
-- )
-- 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();