fix(hooks): keep recovery bypass controls out of default denial text (#3844)

Denial reasons are tool output the guarded agent reads. Naming ECC_GATEGUARD=off, ECC_DISABLED_HOOKS and the exemption globs there handed the agent its own bypass. Hint text is now opt-in via ECC_SHOW_RECOVERY_HINTS, and the default denials keep only the factual requirement.

Co-Authored-By: TRAE <noreply@trae.ai>
This commit is contained in:
电大老babyandTRAE committed 2026-11-04 10:32:42 +08:00
1 parent b38be6a7f6
commit 54989037b4
4 files changed
+59 -5

No files matched your search

+11 -3
View File
@@ -145,6 +145,10 @@ function parseInput(inputOrRaw) {
* Exportable run() for in-process execution via run-with-flags.js.
* Avoids the ~50-100ms spawnSync overhead when available.
*/
function showRecoveryHints() {
return /^(1|true|yes|on|enabled)$/i.test(String(process.env.ECC_SHOW_RECOVERY_HINTS || ''));
}
function run(inputOrRaw, options = {}) {
if (options.truncated) {
return {
@@ -152,7 +156,9 @@ function run(inputOrRaw, options = {}) {
stderr:
`BLOCKED: Hook input exceeded ${options.maxStdin || MAX_STDIN} bytes. ` +
'Refusing to bypass config-protection on a truncated payload. ' +
'Retry with a smaller edit or disable the config-protection hook temporarily.'
(showRecoveryHints()
? 'Retry with a smaller edit or disable the config-protection hook temporarily.'
: 'Retry with a smaller edit.')
};
}
@@ -221,8 +227,10 @@ function checkTarget(filePath) {
stderr:
`BLOCKED: Modifying ${basename} is not allowed. ` +
'Fix the source code to satisfy linter/formatter rules instead of ' +
'weakening the config. If this is a legitimate config change, ' +
'disable the config-protection hook temporarily.'
'weakening the config.' +
(showRecoveryHints()
? ' If this is a legitimate config change, disable the config-protection hook temporarily.'
: '')
};
}
+11 -1
View File
@@ -115,6 +115,13 @@ const ROUTINE_POWERSHELL_NARROW_RECOVERY_HINT =
const ECC_DISABLE_VALUES = new Set(['0', 'false', 'off', 'disabled', 'disable']);
const ECC_ENABLE_VALUES = new Set(['1', 'true', 'on', 'enabled', 'enable', 'yes']);
// A denial is fed back to the agent as untrusted tool text. Naming the env vars
// that disable the guard in that same message hands a bypass to the guarded
// agent (#3844), so the recovery controls are opt-in for human operators.
function showRecoveryHints() {
return ECC_ENABLE_VALUES.has(normalizeEnvValue(process.env.ECC_SHOW_RECOVERY_HINTS));
}
// SQL keywords remain a phrase check. Quoted strings are stripped before
// this regex runs so a commit message mentioning "drop table" stays passive.
// `dd if=` used to be a fourth arm here. Matching it as text could not work:
@@ -2108,7 +2115,9 @@ function condensedGateMsg(action, filePath, ordinal, { cls = 'code', targetNote
`${batchSiblingWarning(safe)} ` +
(missNote ? `${missNote} ` : '') +
(targetNote ? `${targetNote} ` : '') +
'(Use GATEGUARD_EXEMPT_GLOBS for path-scoped exemptions; GATEGUARD_FACT_FORCE_MAX_DENIALS caps denials per session; ECC_GATEGUARD=off disables this gate.)'
(showRecoveryHints()
? '(Use GATEGUARD_EXEMPT_GLOBS for path-scoped exemptions; GATEGUARD_FACT_FORCE_MAX_DENIALS caps denials per session; ECC_GATEGUARD=off disables this gate.)'
: '')
);
}
@@ -2183,6 +2192,7 @@ function routineShellMsg(toolName) {
}
function withRecoveryHint(message, hookIds = [EDIT_WRITE_HOOK_ID], narrowRecoveryHint = '') {
if (!showRecoveryHints()) return message;
const disableTargets = hookIds.map(hookId => `\`${hookId}\``).join(' or ');
const recoveryLines = narrowRecoveryHint ? [narrowRecoveryHint, ''] : [];
return [
Executable → Regular
+6 -1
View File
@@ -138,9 +138,14 @@ function truncatedInputResult(hookId, maxStdin) {
if (failOpen) return null;
}
const showRecoveryHints = /^(1|true|yes|on|enabled)$/i.test(
String(process.env.ECC_SHOW_RECOVERY_HINTS || '')
);
return {
stdout: '',
stderr: `BLOCKED: Hook input exceeded ${maxStdin} bytes, so ${hookId} could not safely inspect the complete request. Retry with a smaller tool input or explicitly disable this hook.`,
stderr: showRecoveryHints
? `BLOCKED: Hook input exceeded ${maxStdin} bytes, so ${hookId} could not safely inspect the complete request. Retry with a smaller tool input or explicitly disable this hook.`
: `BLOCKED: Hook input exceeded ${maxStdin} bytes, so ${hookId} could not safely inspect the complete request. Retry with a smaller tool input.`,
exitCode: 2
};
}
+31
View File
@@ -7,6 +7,10 @@ const fs = require('fs');
const path = require('path');
const { spawnSync } = require('child_process');
// These assertions cover the opt-in human-operator hint text (#3844): the
// default denial no longer names the env vars that disable the guard.
process.env.ECC_SHOW_RECOVERY_HINTS = '1';
const runner = path.join(__dirname, '..', '..', 'scripts', 'hooks', 'run-with-flags.js');
const hookScript = path.join(__dirname, '..', '..', 'scripts', 'hooks', 'gateguard-fact-force.js');
const externalStateDir = process.env.GATEGUARD_STATE_DIR;
@@ -7690,6 +7694,33 @@ function runTests() {
else failed++;
}
// --- Test N: the default denial never names the controls that disable the hook (#3844) ---
clearState();
if (
test('default denial does not name recovery bypass controls', () => {
const input = {
session_id: TEST_SESSION_ID,
tool_name: 'Edit',
tool_input: { file_path: '/workspace/app/src/recovery-hint.js', old_string: 'a', new_string: 'b' }
};
const result = runHook(input, { ECC_SHOW_RECOVERY_HINTS: '' });
const output = parseOutput(result.stdout);
assert.ok(output && output.hookSpecificOutput, 'expected a denial');
assert.strictEqual(output.hookSpecificOutput.permissionDecision, 'deny');
const reason = output.hookSpecificOutput.permissionDecisionReason;
for (const leaked of [
'ECC_GATEGUARD=off',
'ECC_DISABLED_HOOKS',
'GATEGUARD_EXEMPT_GLOBS',
'GATEGUARD_BASH_ROUTINE_DISABLED'
]) {
assert.ok(!reason.includes(leaked), `default denial must not name ${leaked}`);
}
})
)
passed++;
else failed++;
const ddResults = runDdRegressionTests();
passed += ddResults.passed;
failed += ddResults.failed;