fix(fermata): PostToolUse redacts any response shape, and a refusal fits each tool's schema

Built-in tools and normal MCP results send tool_response as an object or an
array, which the hook read as empty: nothing was redacted under any setting.
The hook now walks any shape, masks every string and returns the same shape.

Enum and literal values of Claude Code's output schemas (read from v2.1.314)
stay when they match exactly, because a mask there fails the schema and the
original is used. A secret that cannot take a mask refuses the output under
every setting. Refusals: Bash and Read answer a minimal schema-valid output,
others keep their shape with exact enum values only.

Also: an unreadable config layer no longer counts as deny, multi-line string
spellings of deny count, vim's other swap names and Kate's are editor files,
the heuristic warning moves to additionalContext, and PreToolUse answers
quietly for tools it has no rules for.
This commit is contained in:
2026-11-06 23:36:57 +01:00
parent 5c3b239f59
commit b69fbf3ae2
10 changed files with 901 additions and 196 deletions
+42 -30
View File
@@ -102,37 +102,29 @@ fn a_file_override_that_cannot_be_parsed_withholds_the_output_under_deny() {
}
/// Claude Code ignores a replacement that does not match a built-in tool's schema
/// and uses the original, so a plain string would withhold nothing here.
/// and uses the original, so a plain string would withhold nothing here. Each tool
/// and shape against its schema: `tests/cli_hook_redaction.rs`.
#[test]
fn an_object_response_is_withheld_in_its_own_shape_under_deny() {
// The control: without `deny` an object response is what it was, not looked at.
// The control: without `deny` the output goes on, with this file's values unknown.
let p = project(&[("secrets.toml", BROKEN_TOML)]);
assert_eq!(p.post(json!({"stdout": RESPONSE})), (json!({}), String::new()));
let (out, stderr) = p.post(json!({"stdout": RESPONSE}));
assert_eq!(out, json!({}));
assert!(stderr.contains("secrets.toml"), "{stderr}");
let p = project(&[(".botsecrets", DENY), ("secrets.toml", BROKEN_TOML)]);
let (out, _) = p.post(json!({"stdout": RESPONSE, "stderr": "", "interrupted": false, "isImage": false}));
let updated = &out["hookSpecificOutput"]["updatedToolOutput"];
assert!(updated["stdout"].as_str().unwrap().contains("secrets.toml"), "{out}");
assert_eq!((&updated["interrupted"], &updated["isImage"]), (&json!(false), &json!(false)));
assert!(out["hookSpecificOutput"]["additionalContext"].as_str().unwrap().contains("secrets.toml"), "{out}");
assert!(!out.to_string().contains(VALUE), "{out}");
let (out, _) = p.post(json!({
"type": "text",
"file": {"filePath": "/p/x", "content": RESPONSE, "numLines": 1, "startLine": 1, "totalLines": 1},
}));
let updated = &out["hookSpecificOutput"]["updatedToolOutput"];
assert_eq!(updated["type"], "text");
assert_eq!(updated["file"]["numLines"], 1);
assert!(updated["file"]["content"].as_str().unwrap().contains("withheld"), "{out}");
assert!(!out.to_string().contains(VALUE), "{out}");
// A list keeps one item, and a value under a tag's name is no tag.
let (out, _) = p.post(json!({"mode": "files_with_matches", "filenames": [VALUE, VALUE], "type": RESPONSE}));
let (out, _) = p.hook(
"post-tool-use",
json!({"tool_name": "Grep", "tool_input": {}, "tool_response": {"mode": "files_with_matches", "numFiles": 2, "filenames": [VALUE, VALUE], "type": RESPONSE}}),
);
let updated = &out["hookSpecificOutput"]["updatedToolOutput"];
assert_eq!(updated["mode"], "files_with_matches");
assert_eq!(updated["numFiles"], 2);
// A list keeps one item, and a value under a tag's name is no tag.
assert_eq!(updated["filenames"].as_array().unwrap().len(), 1);
assert!(updated["type"].as_str().unwrap().contains("withheld"), "{out}");
assert!(out["hookSpecificOutput"]["additionalContext"].as_str().unwrap().contains("secrets.toml"), "{out}");
assert!(!out.to_string().contains(VALUE), "{out}");
}
@@ -181,20 +173,38 @@ fn a_broken_user_global_config_that_says_deny_withholds_the_output() {
assert!(refusal(&p.post(json!(RESPONSE)).0).contains(".botsecrets"));
}
/// A layer whose bytes cannot be read says nothing. It must not stop every project
/// on the machine when it is the user-global one, and another layer's `deny` counts.
#[cfg(unix)]
#[test]
fn a_config_whose_bytes_cannot_be_read_withholds_the_output() {
fn a_config_whose_bytes_cannot_be_read_says_nothing() {
use std::os::unix::fs::PermissionsExt;
let p = project(&[(".botsecrets", "")]);
let file = p.dir.path().join(".botsecrets");
let p = project(&[]);
let dir = p.home.path().join(".config/fermata");
fs::create_dir_all(&dir).unwrap();
let file = dir.join(".botsecrets");
fs::write(&file, "").unwrap();
fs::set_permissions(&file, fs::Permissions::from_mode(0o000)).unwrap();
if fs::read(&file).is_ok() {
return; // Running as root: the file cannot be made unreadable.
}
assert_eq!(p.post(json!(RESPONSE)).0, json!({}));
assert_eq!(verdict(&p.pre_bash()), "allow");
fs::write(p.dir.path().join(".botsecrets"), DENY).unwrap();
assert!(refusal(&p.post(json!(RESPONSE)).0).contains(".botsecrets"));
assert_eq!(verdict(&p.pre_bash()), "deny");
}
/// TOML's multi-line strings are spellings of the setting too.
#[test]
fn a_broken_config_with_a_multi_line_string_deny_withholds_the_output() {
for setting in ["[enforcement]\non_parse_error = \"\"\"deny\"\"\"\n", "[enforcement]\non_parse_error = '''deny'''\n"] {
let broken = format!("{setting}{BROKEN_TOML}");
assert!(refusal(&project(&[(".botsecrets", &broken)]).post(json!(RESPONSE)).0).contains(".botsecrets"));
}
}
/// A broken config that does not say `deny` stays open, as it always has. Only the
/// message changes.
#[test]
@@ -228,11 +238,13 @@ fn other_settings_stay_open_and_quote_nothing() {
/// An editor's file does not take down every tool output of a `deny` project.
#[test]
fn an_editors_swap_file_does_not_withhold_under_deny() {
let p = project(&[(".botsecrets", DENY), (".env", "API_TOKEN=value-on-the-failing-line\n")]);
fs::write(p.dir.path().join(".env.swp"), b"b0VIM \xff\xfe\x00\x01").unwrap();
let (out, _) = p.post(json!(RESPONSE));
let text = out["hookSpecificOutput"]["updatedToolOutput"].as_str().unwrap();
assert!(text.starts_with("output of the tool") && !text.contains(VALUE), "{text}");
for name in [".env.swp", ".env.swm", ".env.kate-swp"] {
let p = project(&[(".botsecrets", DENY), (".env", "API_TOKEN=value-on-the-failing-line\n")]);
fs::write(p.dir.path().join(name), b"b0VIM \xff\xfe\x00\x01").unwrap();
let (out, _) = p.post(json!(RESPONSE));
let text = out["hookSpecificOutput"]["updatedToolOutput"].as_str().unwrap();
assert!(text.starts_with("output of the tool") && !text.contains(VALUE), "{name}: {text}");
}
}
// ---------------------------------------------------------------------------