From dde2c8dbda061699c9df0871821dc410e354eff7 Mon Sep 17 00:00:00 2001 From: huangpz6 Date: Wed, 4 Nov 2026 21:24:45 +0800 Subject: [PATCH] fix(gateguard): treat .claude/settings files as restricted for subagents too (#3862) Removing the exemption gated top-level edits, but subagent Edit/Write/MultiEdit still consult only sensitive or hard-linked targets, so an ordinary settings file bypassed the gate. Classify .claude/settings*.json as sensitive in the shared target class and cover all three subagent edit tools plus both settings file names. Co-Authored-By: TRAE --- scripts/lib/gateguard-target-class.js | 13 +++++++++- tests/hooks/gateguard-fact-force.test.js | 32 +++++++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/scripts/lib/gateguard-target-class.js b/scripts/lib/gateguard-target-class.js index 35a9d1dcf..28100e8d7 100644 --- a/scripts/lib/gateguard-target-class.js +++ b/scripts/lib/gateguard-target-class.js @@ -297,11 +297,22 @@ function isSensitiveTarget(classPath) { } } +// `.claude/settings*.json` holds the switches for GateGuard and the other +// hooks, so it must count as restricted on every edit path — including the +// subagent paths that only deny sensitive or hard-linked targets (#3862). +const CLAUDE_SETTINGS_PATH = /(^|\/)\.claude\/settings(?:\.([^/]+))?\.json$/; + +function isClaudeSettingsPath(classPath) { + return CLAUDE_SETTINGS_PATH.test(String(classPath || '').replace(/\\/g, '/').toLowerCase()); +} + /** Sensitive on the lexical or the real (symlink-resolved) path; any error is sensitive. */ function isSensitiveTargetFor(filePath, data) { try { if (typeof filePath !== 'string' || !filePath) return true; - if (isSensitiveTarget(classPathFor(filePath, data))) return true; + const classPath = classPathFor(filePath, data); + if (isClaudeSettingsPath(classPath)) return true; + if (isSensitiveTarget(classPath)) return true; return isSensitiveRealTarget(filePath, data); } catch (_) { return true; diff --git a/tests/hooks/gateguard-fact-force.test.js b/tests/hooks/gateguard-fact-force.test.js index bdc13b902..d1a16ab11 100644 --- a/tests/hooks/gateguard-fact-force.test.js +++ b/tests/hooks/gateguard-fact-force.test.js @@ -1521,7 +1521,7 @@ function runTests() { tool_name: 'Edit', tool_input: { file_path: '/workspace/app/.claude/settings.json', old_string: '{}', new_string: '{"hooks":[]}' } }; - const result = runHook(input); + const result = runHook(input, { GATEGUARD_EXEMPT_GLOBS: '' }); const output = parseOutput(result.stdout); assert.ok(output, 'should produce valid JSON output'); assert.ok(output.hookSpecificOutput, 'the settings file must not bypass the gate'); @@ -2023,6 +2023,36 @@ function runTests() { passed++; else failed++; + // --- Test 33a: settings files stay gated on subagent Edit/Write/MultiEdit (#3862) --- + clearState(); + if ( + test('subagent settings edits stay gated on every edit tool', () => { + const env = { CLAUDE_SESSION_ID: '', ECC_SESSION_ID: '', GATEGUARD_EXEMPT_GLOBS: '' }; + const settingsPaths = ['/workspace/app/.claude/settings.json', '/workspace/app/.claude/settings.local.json']; + + for (const filePath of settingsPaths) { + const edits = [{ file_path: filePath, old_string: '{}', new_string: '{"hooks":[]}' }]; + const cases = [ + { tool_name: 'MultiEdit', tool_input: { edits } }, + { tool_name: 'Edit', tool_input: { ...edits[0] } }, + { tool_name: 'Write', tool_input: { file_path: filePath, content: '{"hooks":[]}' } } + ]; + for (const toolInput of cases) { + clearState(); + const result = runHook( + { ...toolInput, session_id: 'subagent-fresh-session', agent_id: 'agent-abc-123' }, + env + ); + const output = parseOutput(result.stdout); + assert.ok(output && output.hookSpecificOutput, `${toolInput.tool_name} ${filePath} must be denied`); + assert.strictEqual(output.hookSpecificOutput.permissionDecision, 'deny'); + } + } + }) + ) + passed++; + else failed++; + // --- Test 33: Bash stays gated inside subagents --- clearState(); if (