Skip to content

Commit fdb10ba

Browse files
authored
feat(hooks): add config protection hook to block linter config manipulation (anomalyco#758)
* feat(hooks): add config protection hook to block linter config manipulation Agents frequently modify linter/formatter configs (.eslintrc, biome.json, .prettierrc, .ruff.toml, etc.) to make checks pass instead of fixing the actual code. This PreToolUse hook intercepts Write/Edit/MultiEdit calls targeting known config files and blocks them with a steering message that directs the agent to fix the source code instead. Covers: ESLint, Prettier, Biome, Ruff, ShellCheck, Stylelint, and Markdownlint configs. Fixes anomalyco#733 * Address review: fix dead code, add missing configs, export run() - Removed pyproject.toml from PROTECTED_FILES (was dead code since it was also in PARTIAL_CONFIG_FILES). Added comment explaining why it's intentionally excluded. - Removed PARTIAL_CONFIG_FILES entirely (no longer needed). - Added missing ESLint v9 TypeScript flat configs: eslint.config.ts, eslint.config.mts, eslint.config.cts - Added missing Prettier ESM config: prettier.config.mjs - Exported run() function for in-process execution via run-with-flags, avoiding the spawnSync overhead (~50-100ms per call). * Handle stdin truncation gracefully, log warning instead of fail-open If stdin exceeds 1MB, the JSON would be malformed and the catch block would silently pass through. Now we detect truncation and log a warning. The in-process run() path is not affected.
1 parent 401dca0 commit fdb10ba

2 files changed

Lines changed: 136 additions & 0 deletions

File tree

‎hooks/hooks.json‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,17 @@
9696
],
9797
"description": "Capture governance events (secrets, policy violations, approval requests). Enable with ECC_GOVERNANCE_CAPTURE=1"
9898
},
99+
{
100+
"matcher": "Write|Edit|MultiEdit",
101+
"hooks": [
102+
{
103+
"type": "command",
104+
"command": "node \"${CLAUDE_PLUGIN_ROOT}/scripts/hooks/run-with-flags.js\" \"pre:config-protection\" \"scripts/hooks/config-protection.js\" \"standard,strict\"",
105+
"timeout": 5
106+
}
107+
],
108+
"description": "Block modifications to linter/formatter config files. Steers agent to fix code instead of weakening configs."
109+
},
99110
{
100111
"matcher": "*",
101112
"hooks": [

‎scripts/hooks/config-protection.js‎

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#!/usr/bin/env node
2+
/**
3+
* Config Protection Hook
4+
*
5+
* Blocks modifications to linter/formatter config files.
6+
* Agents frequently modify these to make checks pass instead of fixing
7+
* the actual code. This hook steers the agent back to fixing the source.
8+
*
9+
* Exit codes:
10+
* 0 = allow (not a config file)
11+
* 2 = block (config file modification attempted)
12+
*/
13+
14+
'use strict';
15+
16+
const path = require('path');
17+
18+
const MAX_STDIN = 1024 * 1024;
19+
let raw = '';
20+
21+
const PROTECTED_FILES = new Set([
22+
// ESLint (legacy + v9 flat config, JS/TS/MJS/CJS)
23+
'.eslintrc',
24+
'.eslintrc.js',
25+
'.eslintrc.cjs',
26+
'.eslintrc.json',
27+
'.eslintrc.yml',
28+
'.eslintrc.yaml',
29+
'eslint.config.js',
30+
'eslint.config.mjs',
31+
'eslint.config.cjs',
32+
'eslint.config.ts',
33+
'eslint.config.mts',
34+
'eslint.config.cts',
35+
// Prettier (all config variants including ESM)
36+
'.prettierrc',
37+
'.prettierrc.js',
38+
'.prettierrc.cjs',
39+
'.prettierrc.json',
40+
'.prettierrc.yml',
41+
'.prettierrc.yaml',
42+
'prettier.config.js',
43+
'prettier.config.cjs',
44+
'prettier.config.mjs',
45+
// Biome
46+
'biome.json',
47+
'biome.jsonc',
48+
// Ruff (Python)
49+
'.ruff.toml',
50+
'ruff.toml',
51+
// Note: pyproject.toml is intentionally NOT included here because it
52+
// contains project metadata alongside linter config. Blocking all edits
53+
// to pyproject.toml would prevent legitimate dependency changes.
54+
// Shell / Style / Markdown
55+
'.shellcheckrc',
56+
'.stylelintrc',
57+
'.stylelintrc.json',
58+
'.stylelintrc.yml',
59+
'.markdownlint.json',
60+
'.markdownlint.yaml',
61+
'.markdownlintrc',
62+
]);
63+
64+
/**
65+
* Exportable run() for in-process execution via run-with-flags.js.
66+
* Avoids the ~50-100ms spawnSync overhead when available.
67+
*/
68+
function run(input) {
69+
const filePath = input?.tool_input?.file_path || input?.tool_input?.file || '';
70+
if (!filePath) return { exitCode: 0 };
71+
72+
const basename = path.basename(filePath);
73+
if (PROTECTED_FILES.has(basename)) {
74+
return {
75+
exitCode: 2,
76+
stderr:
77+
`BLOCKED: Modifying ${basename} is not allowed. ` +
78+
`Fix the source code to satisfy linter/formatter rules instead of ` +
79+
`weakening the config. If this is a legitimate config change, ` +
80+
`disable the config-protection hook temporarily.`,
81+
};
82+
}
83+
84+
return { exitCode: 0 };
85+
}
86+
87+
module.exports = { run };
88+
89+
// Stdin fallback for spawnSync execution
90+
let truncated = false;
91+
process.stdin.setEncoding('utf8');
92+
process.stdin.on('data', chunk => {
93+
if (raw.length < MAX_STDIN) {
94+
const remaining = MAX_STDIN - raw.length;
95+
raw += chunk.substring(0, remaining);
96+
if (chunk.length > remaining) truncated = true;
97+
} else {
98+
truncated = true;
99+
}
100+
});
101+
102+
process.stdin.on('end', () => {
103+
// If stdin was truncated, the JSON is likely malformed. Fail open but
104+
// log a warning so the issue is visible. The run() path (used by
105+
// run-with-flags.js in-process) is not affected by this.
106+
if (truncated) {
107+
process.stderr.write('[config-protection] Warning: stdin exceeded 1MB, skipping check\n');
108+
process.stdout.write(raw);
109+
return;
110+
}
111+
112+
try {
113+
const input = raw.trim() ? JSON.parse(raw) : {};
114+
const result = run(input);
115+
116+
if (result.exitCode === 2) {
117+
process.stderr.write(result.stderr + '\n');
118+
process.exit(2);
119+
}
120+
} catch {
121+
// Keep hook non-blocking on parse errors.
122+
}
123+
124+
process.stdout.write(raw);
125+
});

0 commit comments

Comments
 (0)