fix(fermata): a bad pattern's note names its layer and its section
SecretsConfig::bad_pattern_sources finds the most specific layer holding the pattern in that section. Notes, refusals and fermata check say "a[ in [policy.read] of .botsecrets".
This commit is contained in:
@@ -211,3 +211,49 @@ fn a_manifest_skips_a_bad_glob_only_while_open() {
|
||||
config.enforcement.on_parse_error = ParseErrorAction::Deny;
|
||||
assert!(Manifest::build(&config, p.dir.path()).is_err());
|
||||
}
|
||||
|
||||
/// A note names the pattern, its section and the layer it came from. A layer that a more
|
||||
/// specific one replaced is no longer the source.
|
||||
#[test]
|
||||
fn a_note_names_the_layer_and_the_section() {
|
||||
let note = |p: &Project| context(&p.post_bash("x")).to_string();
|
||||
for (name, section, text) in [
|
||||
(".botsecrets", "[files]", "[files]\npatterns = [\"a[\"]\n"),
|
||||
(".botsecrets", "[policy.read]", "[policy.read]\npatterns = [\"a[\"]\n"),
|
||||
(".botsecrets", "[policy.write]", "[policy.write]\npatterns = [\"a[\"]\n"),
|
||||
(".botsecrets", "[policy.bash] deny", "[policy.bash]\ndeny = [\"a[\"]\n"),
|
||||
(".botsecrets", "[policy.bash] ask", "[policy.bash]\nask = [\"a[\"]\n"),
|
||||
("fermata.toml", "[policy.read]", "[policy.read]\npatterns = [\"a[\"]\n"),
|
||||
(".botsecrets.local", "[policy.read]", "[policy.read]\npatterns = [\"a[\"]\n"),
|
||||
] {
|
||||
let p = project(&[(name, text)]);
|
||||
let expected = format!("The pattern a[ in {section} of {name} does not compile");
|
||||
assert!(note(&p).contains(&expected), "{name} {section}: {}", note(&p));
|
||||
}
|
||||
|
||||
let p = project(&[]);
|
||||
let global = p.home.path().join(".config/fermata");
|
||||
fs::create_dir_all(&global).unwrap();
|
||||
fs::write(global.join(".botsecrets"), "[files]\npatterns = [\"a[\"]\n").unwrap();
|
||||
let expected = format!("The pattern a[ in [files] of {} does not compile", global.join(".botsecrets").display());
|
||||
assert!(note(&p).contains(&expected), "{}", note(&p));
|
||||
|
||||
// The project's list replaces the global one: its pattern is the one that stands.
|
||||
fs::write(p.dir.path().join(".botsecrets"), "[files]\npatterns = [\"b[\", \".env\"]\n").unwrap();
|
||||
assert!(note(&p).contains("The pattern b[ in [files] of .botsecrets does not compile"), "{}", note(&p));
|
||||
assert!(!note(&p).contains("a["), "{}", note(&p));
|
||||
|
||||
// The same pattern in two layers' lists: the one that is in force is named.
|
||||
fs::write(p.dir.path().join(".botsecrets"), "[files]\npatterns = [\"a[\", \".env\"]\n").unwrap();
|
||||
assert!(note(&p).contains("The pattern a[ in [files] of .botsecrets does not compile"), "{}", note(&p));
|
||||
}
|
||||
|
||||
/// A refusal under `deny` says the same.
|
||||
#[test]
|
||||
fn a_refusal_names_the_layer_and_the_section() {
|
||||
let p = project(&[(".botsecrets", &format!("{DENY}[policy.read]\npatterns = [\"a[\"]\n"))]);
|
||||
let reason = p.read("safe.txt")["hookSpecificOutput"]["permissionDecisionReason"].as_str().unwrap().to_string();
|
||||
assert!(reason.contains("a[ in [policy.read] of .botsecrets"), "{reason}");
|
||||
let p = project(&[(".botsecrets", &format!("{DENY}[files]\npatterns = [\"a[\"]\n"))]);
|
||||
assert!(p.post_bash("x").to_string().contains("a[ in [files] of .botsecrets"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user