.botignore matches a path or any parent, with the path tried first, so !vendor/keep beats vendor/. Policy::check takes a relative path against the root and also checks the path as the file system resolves it, spelled from the root as given, so a link or a root behind a link hides nothing. session_dir: the payload's cwd if absolute, else the process directory, for both hooks; fermata check and the hook make paths absolute.
166 lines
6.7 KiB
Rust
166 lines
6.7 KiB
Rust
use dirigent_fermata::core::{Decision, Op, Policy};
|
|
use std::fs;
|
|
use tempfile::TempDir;
|
|
|
|
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 botignore_blocks_read() {
|
|
let tmp = make_project(".env\n", "");
|
|
let policy = Policy::load(tmp.path()).unwrap();
|
|
let target = tmp.path().join(".env");
|
|
fs::write(&target, "").unwrap();
|
|
let d = policy.check(Op::Read, &target).unwrap();
|
|
assert!(matches!(d, Decision::Deny(_)));
|
|
}
|
|
|
|
#[test]
|
|
fn botignore_blocks_write_too() {
|
|
let tmp = make_project(".env\n", "");
|
|
let policy = Policy::load(tmp.path()).unwrap();
|
|
let target = tmp.path().join(".env");
|
|
let d = policy.check(Op::Write, &target).unwrap();
|
|
assert!(matches!(d, Decision::Deny(_)));
|
|
}
|
|
|
|
#[test]
|
|
fn unmatched_path_allowed() {
|
|
let tmp = make_project(".env\n", "");
|
|
let policy = Policy::load(tmp.path()).unwrap();
|
|
let target = tmp.path().join("src/main.rs");
|
|
fs::create_dir_all(target.parent().unwrap()).unwrap();
|
|
fs::write(&target, "").unwrap();
|
|
let d = policy.check(Op::Read, &target).unwrap();
|
|
assert_eq!(d, Decision::Allow);
|
|
}
|
|
|
|
#[test]
|
|
fn policy_read_block_applies_only_to_read() {
|
|
let tmp = make_project("", "[policy.read]\npatterns = [\"secrets/**\"]\n");
|
|
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 policy_write_block_applies_only_to_write() {
|
|
let tmp = make_project("", "[policy.write]\npatterns = [\"vendor/**\"]\n");
|
|
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();
|
|
assert_eq!(policy.check(Op::Read, &target).unwrap(), Decision::Allow);
|
|
assert!(matches!(policy.check(Op::Write, &target).unwrap(), Decision::Deny(_)));
|
|
}
|
|
|
|
fn denied(policy: &Policy, op: Op, path: impl AsRef<std::path::Path>) -> bool {
|
|
policy.check(op, path.as_ref()).unwrap().is_blocking()
|
|
}
|
|
|
|
/// A relative path is the project's, for `.botignore` and for `[policy]`.
|
|
#[test]
|
|
fn a_relative_path_is_taken_against_the_root() {
|
|
let tmp = make_project(".env\nvendor/\n", "[policy.read]\npatterns = [\"conf/*.yaml\"]\n");
|
|
let policy = Policy::load(tmp.path()).unwrap();
|
|
assert!(denied(&policy, Op::Read, ".env"));
|
|
assert!(denied(&policy, Op::Read, "./.env"));
|
|
assert!(denied(&policy, Op::Read, "src/../.env"));
|
|
assert!(denied(&policy, Op::Write, "vendor/a"));
|
|
assert!(denied(&policy, Op::Read, "conf/a.yaml"));
|
|
assert!(!denied(&policy, Op::Read, "src/a.rs"));
|
|
assert!(!denied(&policy, Op::Write, "conf/a.yaml"));
|
|
}
|
|
|
|
#[test]
|
|
fn an_absolute_path_with_dots_is_resolved_first() {
|
|
let tmp = make_project(".env\n", "");
|
|
let policy = Policy::load(tmp.path()).unwrap();
|
|
assert!(denied(&policy, Op::Read, tmp.path().join("src/../.env")));
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
mod links {
|
|
use super::*;
|
|
use std::os::unix::fs::symlink;
|
|
|
|
#[test]
|
|
fn a_link_to_a_blocked_file_is_stopped() {
|
|
let tmp = make_project(".env\n", "[policy.read]\npatterns = [\"conf/**\"]\n");
|
|
fs::write(tmp.path().join(".env"), "").unwrap();
|
|
fs::create_dir(tmp.path().join("conf")).unwrap();
|
|
fs::write(tmp.path().join("conf/a.yaml"), "").unwrap();
|
|
fs::write(tmp.path().join("plain.txt"), "").unwrap();
|
|
symlink(".env", tmp.path().join("notes")).unwrap();
|
|
symlink("conf/a.yaml", tmp.path().join("settings")).unwrap();
|
|
symlink("plain.txt", tmp.path().join("other")).unwrap();
|
|
|
|
let policy = Policy::load(tmp.path()).unwrap();
|
|
assert!(denied(&policy, Op::Read, tmp.path().join("notes")));
|
|
assert!(denied(&policy, Op::Read, "notes"), "relative");
|
|
assert!(denied(&policy, Op::Read, tmp.path().join("settings")), "[policy.read]");
|
|
assert!(!denied(&policy, Op::Read, tmp.path().join("other")));
|
|
}
|
|
|
|
/// A new file does not exist to resolve, and the link above it does.
|
|
#[test]
|
|
fn a_new_file_below_a_link_is_where_the_link_points() {
|
|
let tmp = make_project("vendor/\n", "");
|
|
fs::create_dir(tmp.path().join("vendor")).unwrap();
|
|
symlink("vendor", tmp.path().join("v")).unwrap();
|
|
let policy = Policy::load(tmp.path()).unwrap();
|
|
assert!(denied(&policy, Op::Write, tmp.path().join("v/new")));
|
|
}
|
|
|
|
/// The matchers hold the root as it was given. A root reached through a link must
|
|
/// still meet a path spelled the real way, and the other way round.
|
|
#[test]
|
|
fn a_root_behind_a_link_meets_both_spellings() {
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
let real = tmp.path().join("real");
|
|
fs::create_dir(&real).unwrap();
|
|
fs::write(real.join(".botignore"), ".env\n").unwrap();
|
|
fs::write(real.join(".env"), "").unwrap();
|
|
fs::write(real.join("a.txt"), "").unwrap();
|
|
symlink(".env", real.join("notes")).unwrap();
|
|
let link = tmp.path().join("link");
|
|
symlink(&real, &link).unwrap();
|
|
|
|
let through_link = Policy::load(&link).unwrap();
|
|
for path in [real.join(".env"), real.join("notes"), link.join(".env"), link.join("notes")] {
|
|
assert!(denied(&through_link, Op::Read, &path), "{}", path.display());
|
|
}
|
|
assert!(!denied(&through_link, Op::Read, real.join("a.txt")));
|
|
|
|
let direct = Policy::load(&real).unwrap();
|
|
assert!(denied(&direct, Op::Read, link.join(".env")));
|
|
assert!(!denied(&direct, Op::Read, link.join("a.txt")));
|
|
}
|
|
|
|
/// A link out of the project is no way around a `[policy]` glob, and `.botignore`
|
|
/// rules stay with their own directory.
|
|
#[test]
|
|
fn a_link_out_of_the_root_keeps_the_rules_scoped() {
|
|
let outside = tempfile::tempdir().unwrap();
|
|
fs::write(outside.path().join("server.key"), "").unwrap();
|
|
fs::write(outside.path().join("a.txt"), "").unwrap();
|
|
let tmp = make_project("a.txt\n", "[policy.read]\npatterns = [\"**/*.key\"]\n");
|
|
symlink(outside.path().join("server.key"), tmp.path().join("k")).unwrap();
|
|
symlink(outside.path().join("a.txt"), tmp.path().join("b.txt")).unwrap();
|
|
let policy = Policy::load(tmp.path()).unwrap();
|
|
assert!(denied(&policy, Op::Read, tmp.path().join("k")));
|
|
assert!(!denied(&policy, Op::Read, tmp.path().join("b.txt")));
|
|
}
|
|
}
|