- A glob in [files] or [policy] that does not compile costs only that pattern while open, with a note by name beside text output. A [files] list with none left leaves the built-in list. Under deny the refusal stays, and the channel filter stays strict. - A file named `credentials` is on the default list, at any depth. - A word with a separator counts only if the path is there, the shell may expand it, or a redirect names it: packages, branches and hosts' paths are not denied. - timeout, nice, ionice, stdbuf, xargs and busybox are prefixes; `2>&1` stays in its segment. - `bash|sh|zsh -c` and `eval` arguments are read as command lines. - A `<<` with no closing line is no heredoc. A commented line's own comment is dropped. Text holding NUL fails to parse. The unknown-field message says so. Durations are bound to under 6 characters. More source extensions pass the gate. - Removed: HarnessAdapter::post_tool_op (dead since 3C was dropped), tool_op back inline.
463 lines
14 KiB
Rust
463 lines
14 KiB
Rust
use dirigent_fermata::core::secrets::config::{
|
|
GuardAction, HeuristicMode, ParseErrorAction, RedactionStyle, SecretsConfig,
|
|
BUILTIN_KEY_PATTERNS,
|
|
};
|
|
|
|
#[test]
|
|
fn parse_minimal_files_only() {
|
|
let cfg = SecretsConfig::from_toml(
|
|
r#"
|
|
[files]
|
|
patterns = [".env", ".env.*"]
|
|
"#,
|
|
)
|
|
.unwrap();
|
|
assert_eq!(cfg.files.patterns, vec![".env", ".env.*"]);
|
|
// Other sections use defaults
|
|
assert_eq!(cfg.redaction.style, RedactionStyle::Masked);
|
|
assert_eq!(cfg.enforcement.on_parse_error, ParseErrorAction::Allow);
|
|
assert_eq!(cfg.enforcement.on_unparsed_read, GuardAction::Deny);
|
|
assert_eq!(cfg.enforcement.on_mask_write, GuardAction::Deny);
|
|
}
|
|
|
|
#[test]
|
|
fn parse_full_config() {
|
|
let cfg = SecretsConfig::from_toml(
|
|
r#"
|
|
[files]
|
|
patterns = [".env", "secrets.*"]
|
|
|
|
[keys]
|
|
include = ["STRIPE_*", "TWILIO_*"]
|
|
exclude = ["PUBLIC_KEY", "SSH_KEY_PATH"]
|
|
|
|
[redaction]
|
|
style = "typed"
|
|
|
|
[heuristic]
|
|
enabled = false
|
|
mode = "report"
|
|
patterns = ['AKIA[A-Z2-7]{16}']
|
|
|
|
[enforcement]
|
|
on_parse_error = "deny"
|
|
on_unparsed_read = "allow"
|
|
on_mask_write = "allow"
|
|
|
|
[[file]]
|
|
path = "settings.py"
|
|
format = "python-assignments"
|
|
keys = ["SECRET_KEY", "DATABASES.*.PASSWORD"]
|
|
"#,
|
|
)
|
|
.unwrap();
|
|
|
|
assert_eq!(cfg.files.patterns, vec![".env", "secrets.*"]);
|
|
assert_eq!(cfg.keys.include, vec!["STRIPE_*", "TWILIO_*"]);
|
|
assert_eq!(cfg.keys.exclude, vec!["PUBLIC_KEY", "SSH_KEY_PATH"]);
|
|
assert_eq!(cfg.redaction.style, RedactionStyle::Typed);
|
|
assert!(!cfg.heuristic.enabled);
|
|
assert_eq!(cfg.heuristic.mode, HeuristicMode::Report);
|
|
assert_eq!(cfg.heuristic.patterns, vec!["AKIA[A-Z2-7]{16}"]);
|
|
assert_eq!(cfg.enforcement.on_parse_error, ParseErrorAction::Deny);
|
|
assert_eq!(cfg.enforcement.on_unparsed_read, GuardAction::Allow);
|
|
assert_eq!(cfg.enforcement.on_mask_write, GuardAction::Allow);
|
|
assert_eq!(cfg.file_overrides.len(), 1);
|
|
assert_eq!(cfg.file_overrides[0].path, "settings.py");
|
|
assert_eq!(
|
|
cfg.file_overrides[0].format.as_deref(),
|
|
Some("python-assignments")
|
|
);
|
|
assert_eq!(
|
|
cfg.file_overrides[0].keys,
|
|
vec!["SECRET_KEY", "DATABASES.*.PASSWORD"]
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn empty_toml_returns_defaults() {
|
|
let cfg = SecretsConfig::from_toml("").unwrap();
|
|
assert!(!cfg.files.patterns.is_empty());
|
|
assert!(cfg.files.patterns.contains(&".env".to_string()));
|
|
assert_eq!(cfg.redaction.style, RedactionStyle::Masked);
|
|
assert!(cfg.heuristic.enabled);
|
|
assert_eq!(cfg.heuristic.mode, HeuristicMode::Enforce);
|
|
assert_eq!(cfg.enforcement.on_parse_error, ParseErrorAction::Allow);
|
|
assert_eq!(cfg.enforcement.on_unparsed_read, GuardAction::Deny);
|
|
assert_eq!(cfg.enforcement.on_mask_write, GuardAction::Deny);
|
|
assert!(cfg.file_overrides.is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_toml_produces_error() {
|
|
let result = SecretsConfig::from_toml("this is not valid {{ toml");
|
|
assert!(result.is_err());
|
|
let err_msg = result.unwrap_err().to_string();
|
|
assert!(
|
|
err_msg.contains("expected"),
|
|
"error should describe parse issue: {err_msg}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn effective_key_includes_has_builtins() {
|
|
let cfg = SecretsConfig::default();
|
|
let effective = cfg.effective_key_includes();
|
|
for builtin in BUILTIN_KEY_PATTERNS {
|
|
assert!(
|
|
effective.contains(&builtin.to_string()),
|
|
"missing builtin: {builtin}"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn effective_key_includes_adds_user_patterns() {
|
|
let cfg = SecretsConfig::from_toml(
|
|
r#"
|
|
[keys]
|
|
include = ["MY_CUSTOM_SECRET_*"]
|
|
"#,
|
|
)
|
|
.unwrap();
|
|
let effective = cfg.effective_key_includes();
|
|
assert!(effective.contains(&"MY_CUSTOM_SECRET_*".to_string()));
|
|
// Builtins still present
|
|
assert!(effective.contains(&"*PASSWORD*".to_string()));
|
|
}
|
|
|
|
#[test]
|
|
fn effective_key_includes_removes_excluded() {
|
|
let cfg = SecretsConfig::from_toml(
|
|
r#"
|
|
[keys]
|
|
exclude = ["*TOKEN*", "SENTRY_DSN"]
|
|
"#,
|
|
)
|
|
.unwrap();
|
|
let effective = cfg.effective_key_includes();
|
|
assert!(
|
|
!effective.contains(&"*TOKEN*".to_string()),
|
|
"excluded pattern should be removed"
|
|
);
|
|
assert!(
|
|
!effective.contains(&"SENTRY_DSN".to_string()),
|
|
"excluded pattern should be removed"
|
|
);
|
|
// Other builtins still present
|
|
assert!(effective.contains(&"*PASSWORD*".to_string()));
|
|
}
|
|
|
|
#[test]
|
|
fn key_matches_glob_case_insensitive() {
|
|
let cfg = SecretsConfig::default();
|
|
assert!(cfg.key_matches("DATABASE_URL"));
|
|
assert!(cfg.key_matches("database_url"));
|
|
assert!(cfg.key_matches("my_password_here"));
|
|
assert!(cfg.key_matches("MY_PASSWORD_HERE"));
|
|
assert!(cfg.key_matches("STRIPE_SECRET_KEY"));
|
|
assert!(cfg.key_matches("AWS_ACCESS_KEY_ID"));
|
|
}
|
|
|
|
#[test]
|
|
fn key_matches_non_secret_keys() {
|
|
let cfg = SecretsConfig::default();
|
|
assert!(!cfg.key_matches("DEBUG"));
|
|
assert!(!cfg.key_matches("LOG_LEVEL"));
|
|
assert!(!cfg.key_matches("PORT"));
|
|
assert!(!cfg.key_matches("HOST"));
|
|
}
|
|
|
|
#[test]
|
|
fn key_matches_respects_user_include() {
|
|
let cfg = SecretsConfig::from_toml(
|
|
r#"
|
|
[keys]
|
|
include = ["MY_APP_*"]
|
|
"#,
|
|
)
|
|
.unwrap();
|
|
assert!(cfg.key_matches("MY_APP_SETTING"));
|
|
assert!(cfg.key_matches("my_app_setting"));
|
|
}
|
|
|
|
#[test]
|
|
fn key_matches_respects_user_exclude() {
|
|
let cfg = SecretsConfig::from_toml(
|
|
r#"
|
|
[keys]
|
|
exclude = ["*TOKEN*"]
|
|
"#,
|
|
)
|
|
.unwrap();
|
|
// TOKEN patterns were excluded, so GITHUB_TOKEN should no longer match
|
|
// via the *TOKEN* pattern. But it might match via GITHUB_TOKEN literal.
|
|
// Let's check something that only matched *TOKEN*.
|
|
assert!(!cfg.key_matches("MY_TOKEN"));
|
|
// PASSWORD still matches
|
|
assert!(cfg.key_matches("MY_PASSWORD"));
|
|
}
|
|
|
|
#[test]
|
|
fn builtin_file_patterns_present() {
|
|
let cfg = SecretsConfig::default();
|
|
let patterns = &cfg.files.patterns;
|
|
assert!(patterns.contains(&".env".to_string()));
|
|
assert!(patterns.contains(&"*.pem".to_string()));
|
|
assert!(patterns.contains(&".aws/credentials".to_string()));
|
|
assert!(patterns.contains(&"terraform.tfvars".to_string()));
|
|
}
|
|
|
|
#[test]
|
|
fn load_missing_files_returns_defaults() {
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
let cfg = SecretsConfig::load(tmp.path()).unwrap();
|
|
assert_eq!(cfg.files.patterns, SecretsConfig::default().files.patterns);
|
|
assert_eq!(cfg.redaction.style, RedactionStyle::Masked);
|
|
}
|
|
|
|
#[test]
|
|
fn load_project_botsecrets() {
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
std::fs::write(
|
|
tmp.path().join(".botsecrets"),
|
|
r#"
|
|
[redaction]
|
|
style = "named"
|
|
|
|
[keys]
|
|
include = ["CUSTOM_*"]
|
|
"#,
|
|
)
|
|
.unwrap();
|
|
|
|
let cfg = SecretsConfig::load(tmp.path()).unwrap();
|
|
assert_eq!(cfg.redaction.style, RedactionStyle::Named);
|
|
assert!(cfg.effective_key_includes().contains(&"CUSTOM_*".to_string()));
|
|
// File patterns remain at defaults (not overridden)
|
|
assert!(cfg.files.patterns.contains(&".env".to_string()));
|
|
}
|
|
|
|
#[test]
|
|
fn load_local_overrides_project() {
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
std::fs::write(
|
|
tmp.path().join(".botsecrets"),
|
|
r#"
|
|
[redaction]
|
|
style = "named"
|
|
[enforcement]
|
|
on_parse_error = "deny"
|
|
"#,
|
|
)
|
|
.unwrap();
|
|
std::fs::write(
|
|
tmp.path().join(".botsecrets.local"),
|
|
r#"
|
|
[redaction]
|
|
style = "absent"
|
|
"#,
|
|
)
|
|
.unwrap();
|
|
|
|
let cfg = SecretsConfig::load(tmp.path()).unwrap();
|
|
// .local overrides .botsecrets for redaction style
|
|
assert_eq!(cfg.redaction.style, RedactionStyle::Absent);
|
|
// enforcement from .botsecrets is preserved (not in .local)
|
|
assert_eq!(cfg.enforcement.on_parse_error, ParseErrorAction::Deny);
|
|
}
|
|
|
|
#[test]
|
|
fn load_invalid_botsecrets_returns_error() {
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
std::fs::write(tmp.path().join(".botsecrets"), "invalid {{ toml").unwrap();
|
|
let result = SecretsConfig::load(tmp.path());
|
|
assert!(result.is_err());
|
|
let err = result.unwrap_err().to_string();
|
|
assert!(err.contains(".botsecrets"), "error should mention file: {err}");
|
|
}
|
|
|
|
#[test]
|
|
fn merge_keys_accumulate() {
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
std::fs::write(
|
|
tmp.path().join(".botsecrets"),
|
|
r#"
|
|
[keys]
|
|
include = ["FROM_PROJECT"]
|
|
exclude = ["EXCLUDE_PROJECT"]
|
|
"#,
|
|
)
|
|
.unwrap();
|
|
std::fs::write(
|
|
tmp.path().join(".botsecrets.local"),
|
|
r#"
|
|
[keys]
|
|
include = ["FROM_LOCAL"]
|
|
exclude = ["EXCLUDE_LOCAL"]
|
|
"#,
|
|
)
|
|
.unwrap();
|
|
|
|
let cfg = SecretsConfig::load(tmp.path()).unwrap();
|
|
assert!(cfg.keys.include.contains(&"FROM_PROJECT".to_string()));
|
|
assert!(cfg.keys.include.contains(&"FROM_LOCAL".to_string()));
|
|
assert!(cfg.keys.exclude.contains(&"EXCLUDE_PROJECT".to_string()));
|
|
assert!(cfg.keys.exclude.contains(&"EXCLUDE_LOCAL".to_string()));
|
|
}
|
|
|
|
#[test]
|
|
fn merge_file_patterns_replaced_not_appended() {
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
std::fs::write(
|
|
tmp.path().join(".botsecrets"),
|
|
r#"
|
|
[files]
|
|
patterns = ["only-this.env"]
|
|
"#,
|
|
)
|
|
.unwrap();
|
|
|
|
let cfg = SecretsConfig::load(tmp.path()).unwrap();
|
|
assert_eq!(cfg.files.patterns, vec!["only-this.env"]);
|
|
// Defaults should be gone, replaced by the project's list
|
|
assert!(!cfg.files.patterns.contains(&".env".to_string()));
|
|
}
|
|
|
|
#[test]
|
|
fn all_redaction_styles_parse() {
|
|
for (input, expected) in [
|
|
("masked", RedactionStyle::Masked),
|
|
("typed", RedactionStyle::Typed),
|
|
("named", RedactionStyle::Named),
|
|
("absent", RedactionStyle::Absent),
|
|
] {
|
|
let toml_str = format!("[redaction]\nstyle = \"{input}\"");
|
|
let cfg = SecretsConfig::from_toml(&toml_str).unwrap();
|
|
assert_eq!(cfg.redaction.style, expected, "failed for: {input}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn all_enforcement_settings_parse() {
|
|
for (input, parse_error, guard) in [
|
|
("allow", ParseErrorAction::Allow, GuardAction::Allow),
|
|
("deny", ParseErrorAction::Deny, GuardAction::Deny),
|
|
] {
|
|
let toml_str = format!("[enforcement]\non_parse_error = \"{input}\"\non_unparsed_read = \"{input}\"\non_mask_write = \"{input}\"");
|
|
let cfg = SecretsConfig::from_toml(&toml_str).unwrap();
|
|
assert_eq!(cfg.enforcement.on_parse_error, parse_error, "failed for: {input}");
|
|
assert_eq!(cfg.enforcement.on_unparsed_read, guard, "failed for: {input}");
|
|
assert_eq!(cfg.enforcement.on_mask_write, guard, "failed for: {input}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn all_heuristic_modes_parse() {
|
|
for (input, expected) in [
|
|
("enforce", HeuristicMode::Enforce),
|
|
("report", HeuristicMode::Report),
|
|
("disabled", HeuristicMode::Disabled),
|
|
] {
|
|
let toml_str = format!("[heuristic]\nmode = \"{input}\"");
|
|
let cfg = SecretsConfig::from_toml(&toml_str).unwrap();
|
|
assert_eq!(cfg.heuristic.mode, expected, "failed for: {input}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn serialization_roundtrip() {
|
|
let cfg = SecretsConfig::from_toml(
|
|
r#"
|
|
[files]
|
|
patterns = [".env"]
|
|
[redaction]
|
|
style = "typed"
|
|
[enforcement]
|
|
on_parse_error = "allow"
|
|
on_unparsed_read = "allow"
|
|
"#,
|
|
)
|
|
.unwrap();
|
|
|
|
let serialized = toml::to_string(&cfg).unwrap();
|
|
let deserialized: SecretsConfig = toml::from_str(&serialized).unwrap();
|
|
assert_eq!(deserialized.redaction.style, RedactionStyle::Typed);
|
|
assert_eq!(deserialized.enforcement.on_unparsed_read, GuardAction::Allow);
|
|
assert_eq!(deserialized.enforcement.on_mask_write, GuardAction::Deny);
|
|
assert_eq!(
|
|
deserialized.enforcement.on_parse_error,
|
|
ParseErrorAction::Allow
|
|
);
|
|
}
|
|
|
|
/// Every section refuses a field it does not know, with the field's line.
|
|
const UNKNOWN_FIELDS: &[(&str, usize)] = &[
|
|
("nonsense = 1\n", 1),
|
|
("[polcy]\nread = 1\n", 1),
|
|
("[files]\npattern = []\n", 2),
|
|
("[keys]\nexclude = []\ninclud = []\n", 3),
|
|
("[redaction]\nstile = \"named\"\n", 2),
|
|
("[heuristic]\nenable = false\n", 2),
|
|
("[enforcement]\nmode = \"strict\"\n", 2),
|
|
("[enforcement]\non_parse_error = \"mask-entire-file\"\n", 2),
|
|
("[enforcement]\non_unparsed_read = \"ask\"\n", 2),
|
|
("[enforcement]\non_mask_write = \"ask\"\n", 2),
|
|
("[[file]]\npath = \"x\"\nkey = []\n", 3),
|
|
("[policy]\nreads = []\n", 2),
|
|
("[policy.read]\ndeny = [\"x\"]\n", 2),
|
|
("[policy.write]\ndeny = [\"x\"]\n", 2),
|
|
("[policy.bash]\nallow = [\"x\"]\n", 2),
|
|
];
|
|
|
|
#[test]
|
|
fn an_unknown_field_fails_load_with_its_line() {
|
|
for (config, line) in UNKNOWN_FIELDS {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
std::fs::write(dir.path().join(".botsecrets"), config).unwrap();
|
|
let text = SecretsConfig::load(dir.path()).err().unwrap_or_else(|| panic!("loaded: {config}")).to_string();
|
|
assert!(text.ends_with(&format!("invalid config, not TOML or an unknown field, at line {line}")) && text.contains(".botsecrets"), "{config}: {text}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn an_unknown_field_fails_from_toml() {
|
|
for (config, _) in UNKNOWN_FIELDS {
|
|
assert!(SecretsConfig::from_toml(config).is_err(), "{config}");
|
|
}
|
|
}
|
|
|
|
/// A broken layer costs that layer. A layer whose bytes cannot be read is no error, one
|
|
/// that is not UTF-8 is: `cli_hook_fail_closed.rs` has the first, in a home of its own.
|
|
#[test]
|
|
fn load_or_default_skips_only_the_layer_that_fails() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
std::fs::write(dir.path().join(".botsecrets"), "[polcy]\nread = 1\n").unwrap();
|
|
std::fs::write(dir.path().join(".botsecrets.local"), "[keys]\ninclude = [\"FROM_LOCAL\"]\n").unwrap();
|
|
assert!(SecretsConfig::load(dir.path()).is_err(), "`load` stays strict");
|
|
let (cfg, errors) = SecretsConfig::load_or_default(dir.path());
|
|
assert_eq!(cfg.keys.include, vec!["FROM_LOCAL"]);
|
|
assert_eq!(cfg.files.patterns, SecretsConfig::default().files.patterns);
|
|
assert_eq!(errors.len(), 1);
|
|
assert_eq!(errors[0].path(), dir.path().join(".botsecrets"));
|
|
assert!(errors[0].to_string().ends_with("not TOML or an unknown field, at line 1"), "{}", errors[0]);
|
|
|
|
std::fs::write(dir.path().join(".botsecrets"), [0xff, 0xfe, 0x00]).unwrap();
|
|
let (cfg, errors) = SecretsConfig::load_or_default(dir.path());
|
|
assert_eq!(cfg.keys.include, vec!["FROM_LOCAL"]);
|
|
assert_eq!(errors.len(), 1);
|
|
assert_eq!(errors[0].path(), dir.path().join(".botsecrets"));
|
|
}
|
|
|
|
#[test]
|
|
fn a_broken_layer_names_its_line_and_never_quotes_it() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let value = "value-on-the-failing-line";
|
|
std::fs::write(dir.path().join(".botsecrets"), format!("ok = 1\nkey = \"{value}\" broken [\n")).unwrap();
|
|
let error = SecretsConfig::load(dir.path()).unwrap_err();
|
|
let text = error.to_string();
|
|
assert!(text.ends_with("not TOML or an unknown field, at line 2") && !text.contains(value), "{text}");
|
|
assert!(!format!("{error:?}").contains(value));
|
|
assert_eq!(error.path(), dir.path().join(".botsecrets"));
|
|
}
|