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:
+6
-6
@@ -84,7 +84,7 @@ fn run_check(op: Op, json: bool, paths: &[PathBuf]) -> ExitCode {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
if !config.bad_patterns().is_empty() {
|
if !config.bad_patterns().is_empty() {
|
||||||
eprintln!("fermata: secrets config: bad patterns: {}", config.bad_patterns().join(", "));
|
eprintln!("fermata: secrets config: bad patterns: {}", config.bad_pattern_sources(&root).join(", "));
|
||||||
return ExitCode::from(2);
|
return ExitCode::from(2);
|
||||||
}
|
}
|
||||||
let d = match policy.check(op, p) {
|
let d = match policy.check(op, p) {
|
||||||
@@ -183,10 +183,10 @@ fn gate_policy(root: &Path) -> Result<Policy, Decision> {
|
|||||||
return Err(refused);
|
return Err(refused);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let bad = SecretsConfig::load_or_default(root).0.bad_patterns().join(", ");
|
let bad = SecretsConfig::load_or_default(root).0.bad_pattern_sources(root).join(", ");
|
||||||
if !bad.is_empty() {
|
if !bad.is_empty() {
|
||||||
eprintln!("fermata: secrets config: bad patterns: {bad}");
|
eprintln!("fermata: secrets config: bad patterns: {bad}");
|
||||||
let refused = gate_failure(root, &format!("the patterns {bad} in the [files] or [policy] rules cannot be used,"));
|
let refused = gate_failure(root, &format!("the rules hold patterns that do not compile ({bad}),"));
|
||||||
if refused.is_blocking() {
|
if refused.is_blocking() {
|
||||||
return Err(refused);
|
return Err(refused);
|
||||||
}
|
}
|
||||||
@@ -388,17 +388,17 @@ fn run_post_tool_use(
|
|||||||
if deny {
|
if deny {
|
||||||
return withhold(match e.path() {
|
return withhold(match e.path() {
|
||||||
Some(path) => format!("{} cannot be parsed, {FIX_OR_CHANGE}", shown_path(path, &root)),
|
Some(path) => format!("{} cannot be parsed, {FIX_OR_CHANGE}", shown_path(path, &root)),
|
||||||
None => format!("the [files] patterns in .botsecrets cannot be used, {FIX_OR_CHANGE}"),
|
None => format!("the patterns {} cannot be used, {FIX_OR_CHANGE}", config.bad_pattern_sources(&root).join(", ")),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if shows_text {
|
if shows_text {
|
||||||
notes.push("[fermata] The [files] patterns in .botsecrets cannot be used, so no value is masked and the read gate is off.".to_string());
|
notes.push("[fermata] The [files] patterns cannot be used, so no value is masked and the read gate is off.".to_string());
|
||||||
}
|
}
|
||||||
return answer(None, ¬es);
|
return answer(None, ¬es);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if shows_text {
|
if shows_text {
|
||||||
notes.extend(config.bad_patterns().iter().map(|p| format!("[fermata] The pattern {p} in [files] or [policy] does not compile, so that pattern is off.")));
|
notes.extend(config.bad_pattern_sources(&root).iter().map(|p| format!("[fermata] The pattern {p} does not compile, so that pattern is off.")));
|
||||||
}
|
}
|
||||||
if shows_text && !manifest.skipped().is_empty() {
|
if shows_text && !manifest.skipped().is_empty() {
|
||||||
let files: Vec<String> = manifest.skipped().iter().map(|path| shown_path(path, &root)).collect();
|
let files: Vec<String> = manifest.skipped().iter().map(|path| shown_path(path, &root)).collect();
|
||||||
|
|||||||
@@ -360,6 +360,21 @@ impl Default for EnforcementConfig {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The patterns of these `[files]` and `[policy]` lists that do not compile, each with its
|
||||||
|
/// section. A command pattern with no glob character is a plain substring.
|
||||||
|
fn bad_globs<'a>(files: Option<&'a Vec<String>>, policy: Option<&'a PolicyConfig>) -> Vec<(&'static str, &'a String)> {
|
||||||
|
let bash = policy.and_then(|p| p.bash.as_ref());
|
||||||
|
let is_glob = |p: &&String| p.contains(['*', '?', '[']);
|
||||||
|
let lists: [(&'static str, Vec<&'a String>); 5] = [
|
||||||
|
("[files]", files.into_iter().flatten().collect()),
|
||||||
|
("[policy.read]", policy.and_then(|p| p.read.as_ref()).into_iter().flat_map(|r| &r.patterns).collect()),
|
||||||
|
("[policy.write]", policy.and_then(|p| p.write.as_ref()).into_iter().flat_map(|r| &r.patterns).collect()),
|
||||||
|
("[policy.bash] deny", bash.into_iter().flat_map(|b| &b.deny).filter(is_glob).collect()),
|
||||||
|
("[policy.bash] ask", bash.into_iter().flat_map(|b| &b.ask).filter(is_glob).collect()),
|
||||||
|
];
|
||||||
|
lists.into_iter().flat_map(|(section, list)| list.into_iter().map(move |pattern| (section, pattern))).filter(|(_, pattern)| Glob::new(pattern).is_err()).collect()
|
||||||
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Partial layer (for merge)
|
// Partial layer (for merge)
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
@@ -610,11 +625,23 @@ impl SecretsConfig {
|
|||||||
/// The globs of `[files]` and `[policy]` that do not compile. While a project stays
|
/// The globs of `[files]` and `[policy]` that do not compile. While a project stays
|
||||||
/// open each costs only itself; under `deny` the hooks and the channel filter refuse.
|
/// open each costs only itself; under `deny` the hooks and the channel filter refuse.
|
||||||
pub fn bad_patterns(&self) -> Vec<String> {
|
pub fn bad_patterns(&self) -> Vec<String> {
|
||||||
let policy = self.policy.as_ref();
|
bad_globs(Some(&self.files.patterns), self.policy.as_ref()).into_iter().map(|(_, pattern)| pattern.clone()).collect()
|
||||||
let paths = [Some(&self.files.patterns), policy.and_then(|p| p.read.as_ref()).map(|r| &r.patterns), policy.and_then(|p| p.write.as_ref()).map(|r| &r.patterns)];
|
}
|
||||||
// A command pattern with no glob character is a plain substring.
|
|
||||||
let commands = policy.and_then(|p| p.bash.as_ref()).into_iter().flat_map(|b| b.deny.iter().chain(&b.ask)).filter(|p| p.contains(['*', '?', '[']));
|
/// [`Self::bad_patterns`] as a person would look for them: each with its section and the
|
||||||
paths.into_iter().flatten().flatten().chain(commands).filter(|p| Glob::new(p).is_err()).cloned().collect()
|
/// most specific layer of `root` that holds it there, as `a[ in [policy.read] of .botsecrets`.
|
||||||
|
pub fn bad_pattern_sources(&self, root: &Path) -> Vec<String> {
|
||||||
|
let layers: Vec<(PathBuf, PartialSecretsConfig)> = Self::layer_files(root).into_iter().rev().filter_map(|file| Some((file.clone(), Self::read_partial(&file).ok()?))).collect();
|
||||||
|
let source = |section: &str, pattern: &String| {
|
||||||
|
layers.iter().find(|(_, layer)| bad_globs(layer.files.as_ref().and_then(|f| f.patterns.as_ref()), layer.policy.as_ref()).contains(&(section, pattern))).map(|(file, _)| file.strip_prefix(root).unwrap_or(file).display().to_string())
|
||||||
|
};
|
||||||
|
bad_globs(Some(&self.files.patterns), self.policy.as_ref())
|
||||||
|
.into_iter()
|
||||||
|
.map(|(section, pattern)| match source(section, pattern) {
|
||||||
|
Some(file) => format!("{pattern} in {section} of {file}"),
|
||||||
|
None => format!("{pattern} in {section}"),
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `[files]` without its bad patterns. With none left the built-in list stands: one
|
/// `[files]` without its bad patterns. With none left the built-in list stands: one
|
||||||
|
|||||||
@@ -211,3 +211,49 @@ fn a_manifest_skips_a_bad_glob_only_while_open() {
|
|||||||
config.enforcement.on_parse_error = ParseErrorAction::Deny;
|
config.enforcement.on_parse_error = ParseErrorAction::Deny;
|
||||||
assert!(Manifest::build(&config, p.dir.path()).is_err());
|
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