Merge origin/main: resolve fermata botignore→botsecrets conflicts
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,182 @@
|
||||
//! Tests for `.botsecrets [policy]` section integration with `Policy`,
|
||||
//! including `fermata.toml` alias support.
|
||||
|
||||
use dirigent_fermata::core::{Decision, Op, Policy};
|
||||
use std::fs;
|
||||
use tempfile::TempDir;
|
||||
|
||||
/// Helper: create a temp project dir with optional `.botignore` and `.botsecrets` files.
|
||||
fn make_project(botignore: &str, botsecrets: &str) -> TempDir {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
if !botignore.is_empty() {
|
||||
fs::write(tmp.path().join(".botignore"), botignore).unwrap();
|
||||
}
|
||||
if !botsecrets.is_empty() {
|
||||
fs::write(tmp.path().join(".botsecrets"), botsecrets).unwrap();
|
||||
}
|
||||
tmp
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn botsecrets_policy_write_works() {
|
||||
let tmp = make_project(
|
||||
"",
|
||||
r#"
|
||||
[policy.write]
|
||||
patterns = ["vendor/**"]
|
||||
"#,
|
||||
);
|
||||
let policy = Policy::load(tmp.path()).unwrap();
|
||||
let target = tmp.path().join("vendor/lib.rs");
|
||||
fs::create_dir_all(target.parent().unwrap()).unwrap();
|
||||
fs::write(&target, "").unwrap();
|
||||
|
||||
// Write to vendor/** should be denied via .botsecrets [policy.write]
|
||||
assert!(matches!(
|
||||
policy.check(Op::Write, &target).unwrap(),
|
||||
Decision::Deny(_)
|
||||
));
|
||||
// Read should still be allowed (no read rules)
|
||||
assert_eq!(policy.check(Op::Read, &target).unwrap(), Decision::Allow);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn botsecrets_policy_read_works() {
|
||||
let tmp = make_project(
|
||||
"",
|
||||
r#"
|
||||
[policy.read]
|
||||
patterns = ["secrets/**"]
|
||||
"#,
|
||||
);
|
||||
let policy = Policy::load(tmp.path()).unwrap();
|
||||
let target = tmp.path().join("secrets/key.pem");
|
||||
fs::create_dir_all(target.parent().unwrap()).unwrap();
|
||||
fs::write(&target, "").unwrap();
|
||||
|
||||
assert!(matches!(
|
||||
policy.check(Op::Read, &target).unwrap(),
|
||||
Decision::Deny(_)
|
||||
));
|
||||
assert_eq!(policy.check(Op::Write, &target).unwrap(), Decision::Allow);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn botsecrets_without_policy_section_does_not_affect_policy() {
|
||||
let tmp = make_project(
|
||||
"",
|
||||
r#"
|
||||
[keys]
|
||||
include = ["MY_CUSTOM_KEY"]
|
||||
"#,
|
||||
);
|
||||
let policy = Policy::load(tmp.path()).unwrap();
|
||||
|
||||
// Everything should be allowed — no policy rules
|
||||
let target = tmp.path().join("vendor/lib.rs");
|
||||
fs::create_dir_all(target.parent().unwrap()).unwrap();
|
||||
fs::write(&target, "").unwrap();
|
||||
assert_eq!(policy.check(Op::Read, &target).unwrap(), Decision::Allow);
|
||||
assert_eq!(policy.check(Op::Write, &target).unwrap(), Decision::Allow);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn botsecrets_policy_bash_deny_works() {
|
||||
let tmp = make_project(
|
||||
"",
|
||||
r#"
|
||||
[policy.bash]
|
||||
deny = ["rm -rf /"]
|
||||
ask = ["git push"]
|
||||
"#,
|
||||
);
|
||||
let policy = Policy::load(tmp.path()).unwrap();
|
||||
|
||||
let d = policy.check_command("rm -rf /").unwrap();
|
||||
assert!(matches!(d, Decision::Deny(_)));
|
||||
|
||||
let d = policy.check_command("git push origin main").unwrap();
|
||||
assert!(matches!(d, Decision::Ask(_)));
|
||||
|
||||
let d = policy.check_command("cargo build").unwrap();
|
||||
assert_eq!(d, Decision::Allow);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_botsecrets_falls_back_to_defaults() {
|
||||
// .botsecrets is malformed — should still load with empty policy (fail-open)
|
||||
let tmp = TempDir::new().unwrap();
|
||||
fs::write(tmp.path().join(".botsecrets"), "this is not valid toml [[[").unwrap();
|
||||
let policy = Policy::load(tmp.path()).unwrap();
|
||||
let target = tmp.path().join("anything.rs");
|
||||
fs::write(&target, "").unwrap();
|
||||
assert_eq!(policy.check(Op::Read, &target).unwrap(), Decision::Allow);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fermata_toml_works_as_alias() {
|
||||
// No .botsecrets, but fermata.toml exists — should be loaded
|
||||
let tmp = TempDir::new().unwrap();
|
||||
fs::write(
|
||||
tmp.path().join("fermata.toml"),
|
||||
r#"
|
||||
[policy.write]
|
||||
patterns = ["generated/**"]
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let policy = Policy::load(tmp.path()).unwrap();
|
||||
let target = tmp.path().join("generated/output.rs");
|
||||
fs::create_dir_all(target.parent().unwrap()).unwrap();
|
||||
fs::write(&target, "").unwrap();
|
||||
|
||||
assert!(matches!(
|
||||
policy.check(Op::Write, &target).unwrap(),
|
||||
Decision::Deny(_)
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn botsecrets_takes_priority_over_fermata_toml() {
|
||||
// Both .botsecrets and fermata.toml exist.
|
||||
// .botsecrets blocks writes to vendor/**, fermata.toml blocks writes to src/**
|
||||
// Only .botsecrets rules should apply.
|
||||
let tmp = TempDir::new().unwrap();
|
||||
fs::write(
|
||||
tmp.path().join(".botsecrets"),
|
||||
r#"
|
||||
[policy.write]
|
||||
patterns = ["vendor/**"]
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
fs::write(
|
||||
tmp.path().join("fermata.toml"),
|
||||
r#"
|
||||
[policy.write]
|
||||
patterns = ["src/**"]
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let policy = Policy::load(tmp.path()).unwrap();
|
||||
|
||||
// vendor/** should be blocked (from .botsecrets)
|
||||
let vendor_target = tmp.path().join("vendor/lib.rs");
|
||||
fs::create_dir_all(vendor_target.parent().unwrap()).unwrap();
|
||||
fs::write(&vendor_target, "").unwrap();
|
||||
assert!(matches!(
|
||||
policy.check(Op::Write, &vendor_target).unwrap(),
|
||||
Decision::Deny(_)
|
||||
));
|
||||
|
||||
// src/** should NOT be blocked (fermata.toml was ignored because .botsecrets exists)
|
||||
let src_target = tmp.path().join("src/main.rs");
|
||||
fs::create_dir_all(src_target.parent().unwrap()).unwrap();
|
||||
fs::write(&src_target, "").unwrap();
|
||||
assert_eq!(
|
||||
policy.check(Op::Write, &src_target).unwrap(),
|
||||
Decision::Allow
|
||||
);
|
||||
}
|
||||
@@ -2,11 +2,13 @@ use dirigent_fermata::core::{Decision, Op, Policy};
|
||||
use std::fs;
|
||||
use tempfile::TempDir;
|
||||
|
||||
fn make_project(botignore: &str, botsecrets_policy: &str) -> TempDir {
|
||||
fn make_project(botignore: &str, botsecrets: &str) -> TempDir {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
fs::write(tmp.path().join(".botignore"), botignore).unwrap();
|
||||
if !botsecrets_policy.is_empty() {
|
||||
fs::write(tmp.path().join(".botsecrets"), botsecrets_policy).unwrap();
|
||||
if !botignore.is_empty() {
|
||||
fs::write(tmp.path().join(".botignore"), botignore).unwrap();
|
||||
}
|
||||
if !botsecrets.is_empty() {
|
||||
fs::write(tmp.path().join(".botsecrets"), botsecrets).unwrap();
|
||||
}
|
||||
tmp
|
||||
}
|
||||
|
||||
+18
-3
@@ -3,11 +3,12 @@ use std::fs;
|
||||
use tempfile::TempDir;
|
||||
|
||||
#[test]
|
||||
fn finds_botsecrets_as_strong_marker() {
|
||||
fn finds_fermata_toml() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let root = tmp.path();
|
||||
fs::create_dir_all(root.join("sub/deep")).unwrap();
|
||||
fs::write(root.join(".botsecrets"), "").unwrap();
|
||||
fs::write(root.join("fermata.toml"), "").unwrap();
|
||||
fs::create_dir_all(root.join(".git")).unwrap();
|
||||
|
||||
let target = root.join("sub/deep/file.rs");
|
||||
fs::write(&target, "").unwrap();
|
||||
@@ -16,6 +17,20 @@ fn finds_botsecrets_as_strong_marker() {
|
||||
assert_eq!(found, root);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn finds_botsecrets() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let root = tmp.path();
|
||||
fs::create_dir_all(root.join("sub")).unwrap();
|
||||
fs::write(root.join(".botsecrets"), "").unwrap();
|
||||
|
||||
let target = root.join("sub/file.rs");
|
||||
fs::write(&target, "").unwrap();
|
||||
|
||||
let found = find_project_root(&target).unwrap();
|
||||
assert_eq!(found, root);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn botignore_alone_does_not_stop_walk() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
@@ -87,7 +102,7 @@ fn walks_up_from_file_path_not_cwd() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let root = tmp.path();
|
||||
fs::create_dir_all(root.join("a/b/c")).unwrap();
|
||||
fs::write(root.join("a/.botsecrets"), "").unwrap();
|
||||
fs::write(root.join("a/fermata.toml"), "").unwrap();
|
||||
|
||||
let target = root.join("a/b/c/file.rs");
|
||||
fs::write(&target, "").unwrap();
|
||||
|
||||
@@ -103,7 +103,7 @@ fn bash_rm_somefile_asks() {
|
||||
|
||||
#[test]
|
||||
fn read_self_reflections_asks() {
|
||||
// Note: A.4 has self-reflections under "ask" — current toml schema uses `[read].patterns`
|
||||
// Note: A.4 has self-reflections under "ask" — current toml schema uses `[policy.read].patterns`
|
||||
// for hard reads. This documents the gap; once toml has a `[read].ask`, switch to Ask.
|
||||
let t = fixture();
|
||||
let p = Policy::load(t.path()).unwrap();
|
||||
|
||||
Reference in New Issue
Block a user