fix(fermata): an unknown config field is an error someone sees; a broken layer costs only that layer
deny_unknown_fields on every config struct. [enforcement] mode, EnforcementMode and mask-entire-file are gone, and on_parse_error defaults to allow. New settings on_unparsed_read and on_mask_write, default deny, read by later blocks. SecretsConfig::load stays strict for the channel filter. load_or_default skips only the layers that fail, so a typo keeps the other layers and the default list. Hooks note the broken layer and any skipped() secret file beside output that shows text, not beside every Write (shows_text). Under deny the refusal is unchanged. fermata check exits 2 with the line number. A non-UTF-8 layer counts as a failed layer; a layer whose bytes cannot be read stays silent, as pinned.
This commit is contained in:
@@ -111,10 +111,12 @@ fn a_file_override_that_cannot_be_parsed_withholds_the_output_under_deny() {
|
||||
/// 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` the output goes on, with this file's values unknown.
|
||||
// The control: without `deny` the output goes on, with this file's values unknown,
|
||||
// and a note says which file.
|
||||
let p = project(&[("secrets.toml", BROKEN_TOML)]);
|
||||
let (out, stderr) = p.post(json!({"stdout": RESPONSE}));
|
||||
assert_eq!(out, json!({}));
|
||||
assert!(out["hookSpecificOutput"].get("updatedToolOutput").is_none(), "{out}");
|
||||
assert!(note(&out).contains("secrets.toml") && !note(&out).contains(VALUE), "{out}");
|
||||
assert!(stderr.contains("secrets.toml"), "{stderr}");
|
||||
|
||||
let p = project(&[(".botsecrets", DENY), ("secrets.toml", BROKEN_TOML)]);
|
||||
@@ -210,19 +212,145 @@ fn a_broken_config_with_a_multi_line_string_deny_withholds_the_output() {
|
||||
}
|
||||
}
|
||||
|
||||
/// A broken config that does not say `deny` stays open, as it always has. Only the
|
||||
/// message changes.
|
||||
/// A broken config that does not say `deny` stays open: the default list still masks,
|
||||
/// and a note names the file and the line.
|
||||
#[test]
|
||||
fn a_broken_config_without_deny_stays_open_and_quotes_nothing() {
|
||||
for broken in [BROKEN_TOML.to_string(), format!("# on_parse_error = \"deny\"\n{BROKEN_TOML}")] {
|
||||
let p = project(&[(".botsecrets", &broken)]);
|
||||
let p = project(&[(".botsecrets", &broken), (".env", &format!("API_TOKEN={VALUE}\n"))]);
|
||||
let (out, stderr) = p.post(json!(RESPONSE));
|
||||
assert_eq!(out, json!({}));
|
||||
let text = out["hookSpecificOutput"]["updatedToolOutput"].as_str().unwrap_or_else(|| panic!("not masked: {out}"));
|
||||
assert!(text.starts_with("output of the tool") && !out.to_string().contains(VALUE), "{out}");
|
||||
assert!(note(&out).contains(".botsecrets") && note(&out).contains("line "), "{out}");
|
||||
assert!(stderr.contains(".botsecrets") && stderr.contains("line "), "{stderr}");
|
||||
assert_silent(&p.pre_bash());
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// A layer that cannot be loaded costs only that layer
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const TYPO: &str = "[polcy]\nread = 1\n";
|
||||
const GLOBAL_VALUE: &str = "fake-value-of-the-global-key";
|
||||
const LOCAL_VALUE: &str = "fake-value-of-the-local-key";
|
||||
|
||||
fn note(out: &Value) -> &str {
|
||||
out["hookSpecificOutput"]["additionalContext"].as_str().unwrap_or_else(|| panic!("no note: {out}"))
|
||||
}
|
||||
|
||||
fn tool_output(p: &Project, tool: &str, text: &str) -> Value {
|
||||
let (input, response) = match tool {
|
||||
"Read" => (
|
||||
json!({"file_path": "/p/f"}),
|
||||
json!({"type": "text", "file": {"filePath": "/p/f", "content": text, "numLines": 1, "startLine": 1, "totalLines": 1}}),
|
||||
),
|
||||
"Write" => (
|
||||
json!({"file_path": "/p/f", "content": text}),
|
||||
json!({"type": "create", "filePath": "/p/f", "content": text, "structuredPatch": [], "originalFile": null}),
|
||||
),
|
||||
"Grep" => (json!({"pattern": "x"}), json!({"mode": "content", "numFiles": 1, "filenames": [], "content": text, "numLines": 1})),
|
||||
"Bash" | "PowerShell" => (json!({"command": "x"}), json!({"stdout": text, "stderr": "", "interrupted": false})),
|
||||
_ => (json!({}), json!([{"type": "text", "text": text}])),
|
||||
};
|
||||
p.hook("post-tool-use", json!({"tool_name": tool, "tool_input": input, "tool_response": response})).0
|
||||
}
|
||||
|
||||
/// Output that shows file or command text gets the note, and only that (A-11).
|
||||
const TEXT_TOOLS: &[&str] = &["Read", "Bash", "PowerShell", "Grep", "mcp__srv__tool"];
|
||||
|
||||
#[test]
|
||||
fn a_typo_in_botsecrets_keeps_the_other_layers_and_the_default_list() {
|
||||
let env = format!("API_TOKEN={VALUE}\nMY_GLOBAL={GLOBAL_VALUE}\nMY_LOCAL={LOCAL_VALUE}\n");
|
||||
let p = project(&[(".botsecrets", TYPO), (".botsecrets.local", "[keys]\ninclude = [\"MY_LOCAL\"]\n"), (".env", &env)]);
|
||||
let global = p.home.path().join(".config/fermata");
|
||||
fs::create_dir_all(&global).unwrap();
|
||||
fs::write(global.join(".botsecrets"), "[keys]\ninclude = [\"MY_GLOBAL\"]\n").unwrap();
|
||||
|
||||
let text = format!("{VALUE} {GLOBAL_VALUE} {LOCAL_VALUE}");
|
||||
for tool in TEXT_TOOLS {
|
||||
let out = tool_output(&p, tool, &text);
|
||||
for value in [VALUE, GLOBAL_VALUE, LOCAL_VALUE] {
|
||||
assert!(!out.to_string().contains(value), "{tool}: {out}");
|
||||
}
|
||||
let note = note(&out);
|
||||
assert!(note.contains(".botsecrets") && note.contains("line 1") && note.contains("rules are off"), "{tool}: {note}");
|
||||
assert!(!note.contains("polcy"), "{tool}: {note}");
|
||||
}
|
||||
|
||||
// A Write costs the session no line for it, masked or not.
|
||||
assert_eq!(tool_output(&p, "Write", "clean"), json!({}));
|
||||
let out = tool_output(&p, "Write", VALUE);
|
||||
assert!(!out.to_string().contains(VALUE), "{out}");
|
||||
assert!(out["hookSpecificOutput"].get("additionalContext").is_none(), "{out}");
|
||||
}
|
||||
|
||||
/// A broken user-global layer: the project's own rules stand.
|
||||
#[test]
|
||||
fn a_typo_in_the_user_global_layer_keeps_the_project_layer() {
|
||||
let p = project(&[(".botsecrets", "[keys]\ninclude = [\"MY_LOCAL\"]\n"), (".env", &format!("MY_LOCAL={LOCAL_VALUE}\n"))]);
|
||||
let global = p.home.path().join(".config/fermata");
|
||||
fs::create_dir_all(&global).unwrap();
|
||||
fs::write(global.join(".botsecrets"), TYPO).unwrap();
|
||||
let out = tool_output(&p, "Read", LOCAL_VALUE);
|
||||
assert!(!out.to_string().contains(LOCAL_VALUE), "{out}");
|
||||
assert!(note(&out).contains("fermata/.botsecrets"), "{out}");
|
||||
}
|
||||
|
||||
/// The gate keeps the rules of the layers that load, and says nothing of the others.
|
||||
#[test]
|
||||
fn the_gate_keeps_the_rules_of_the_layers_that_load() {
|
||||
let local = "[policy.read]\npatterns = [\"blocked.txt\"]\n";
|
||||
let p = project(&[(".botsecrets", TYPO), (".botsecrets.local", local), ("blocked.txt", ""), ("safe.txt", "")]);
|
||||
assert!(deny_reason(&p.pre_read("blocked.txt")).contains("blocked.txt"));
|
||||
assert_silent(&p.pre_read("safe.txt"));
|
||||
assert_silent(&p.pre_bash());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_layer_that_is_not_utf8_is_a_broken_layer() {
|
||||
let p = project(&[(".env", &format!("API_TOKEN={VALUE}\n"))]);
|
||||
fs::write(p.dir.path().join(".botsecrets"), [0xff, 0xfe, 0x00]).unwrap();
|
||||
let out = tool_output(&p, "Read", VALUE);
|
||||
assert!(!out.to_string().contains(VALUE), "{out}");
|
||||
assert!(note(&out).contains(".botsecrets") && note(&out).contains("rules are off"), "{out}");
|
||||
assert_eq!(tool_output(&p, "Write", "clean"), json!({}));
|
||||
}
|
||||
|
||||
/// An unknown field under `deny` is the broken config it always was.
|
||||
#[test]
|
||||
fn an_unknown_field_refuses_under_deny() {
|
||||
for config in [format!("{DENY}{TYPO}"), "[enforcement]\nmode = \"strict\"\non_parse_error = \"deny\"\n".to_string()] {
|
||||
let p = project(&[(".botsecrets", &config), ("safe.txt", "")]);
|
||||
assert!(refusal(&p.post(json!(RESPONSE)).0).contains(".botsecrets"), "{config}");
|
||||
assert!(deny_reason(&p.pre_read("safe.txt")).contains(".botsecrets"), "{config}");
|
||||
assert!(deny_reason(&p.pre_bash()).contains(".botsecrets"), "{config}");
|
||||
}
|
||||
}
|
||||
|
||||
/// `skipped()`: a secret file that cannot be parsed, named beside text output only.
|
||||
#[test]
|
||||
fn a_file_that_cannot_be_parsed_is_named_beside_text_output_only() {
|
||||
let config = "[[file]]\npath = \"conf.toml\"\n";
|
||||
let p = project(&[(".botsecrets", config), ("secrets.toml", BROKEN_TOML), ("conf.toml", BROKEN_TOML), (".env", "API_TOKEN=fake-value-that-still-masks\n")]);
|
||||
for tool in TEXT_TOOLS {
|
||||
let out = tool_output(&p, tool, "fake-value-that-still-masks");
|
||||
assert!(!out.to_string().contains("fake-value-that-still-masks"), "{tool}: {out}");
|
||||
let note = note(&out);
|
||||
assert!(note.contains("secrets.toml") && note.contains("conf.toml"), "{tool}: {note}");
|
||||
assert!(!note.contains(VALUE), "{tool}: {note}");
|
||||
}
|
||||
assert_eq!(tool_output(&p, "Write", "clean"), json!({}));
|
||||
}
|
||||
|
||||
/// An editor's file was never a secret file that failed: no note.
|
||||
#[test]
|
||||
fn an_editors_swap_file_gets_no_note() {
|
||||
let p = project(&[]);
|
||||
fs::write(p.dir.path().join(".env.swp"), b"b0VIM \xff\xfe\x00\x01").unwrap();
|
||||
assert_eq!(tool_output(&p, "Read", "clean"), json!({}));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// PostToolUse, other settings
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -230,12 +358,14 @@ fn a_broken_config_without_deny_stays_open_and_quotes_nothing() {
|
||||
/// `hook` itself asserts that stderr quotes nothing.
|
||||
#[test]
|
||||
fn other_settings_stay_open_and_quote_nothing() {
|
||||
for setting in ["", "[enforcement]\non_parse_error = \"allow\"\n", "[enforcement]\non_parse_error = \"mask-entire-file\"\n"] {
|
||||
for setting in ["", "[enforcement]\non_parse_error = \"allow\"\n"] {
|
||||
// A discovered file, and a `[[file]]` override.
|
||||
let config = format!("{setting}[[file]]\npath = \"conf.toml\"\n");
|
||||
let p = project(&[(".botsecrets", &config), ("secrets.toml", BROKEN_TOML), ("conf.toml", BROKEN_TOML)]);
|
||||
let (out, stderr) = p.post(json!("no secrets here"));
|
||||
assert_eq!(out, json!({}), "setting {setting:?}");
|
||||
assert!(out["hookSpecificOutput"].get("updatedToolOutput").is_none(), "setting {setting:?}: {out}");
|
||||
assert!(note(&out).contains("secrets.toml") && note(&out).contains("conf.toml"), "setting {setting:?}: {out}");
|
||||
assert!(!out.to_string().contains(VALUE), "setting {setting:?}: {out}");
|
||||
assert!(stderr.contains("secrets.toml") && stderr.contains("conf.toml"), "{stderr}");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user