//! The security member (docs/FORMAT.md, "The security member"): the level rules, the checklist and its audit, the //! kept scope, and the rules the app asks before a card. That an older reader still opens such a vault, and that //! unknown values are refused, are unit tests in src/security.rs, which can see the decrypted body. use std::collections::BTreeSet; use custos_vault::security::{ AUDIT_LIMIT, Action, Auth, Item, KeptScope, Level, SETUP_WINDOW, SshRule, Window, accept_all_allowed, hello_for_approvals, replacing_line_preticked, ssh_needs_auth, totp_offer_allowed, }; use custos_vault::{Error, KdfParams, SecretString, Vault}; const FAST: KdfParams = KdfParams { m_cost_kib: 64, t_cost: 1, p_cost: 1 }; const NOW: i64 = 1_793_750_000_000; const HOUR: i64 = 3_600_000; fn vault() -> Vault { Vault::create(&SecretString::from("pw"), FAST).unwrap() } fn actions(v: &Vault) -> Vec { v.security_view(NOW).audit.iter().map(|e| e.action).collect() } fn s(v: &[&str]) -> Vec { v.iter().map(|x| x.to_string()).collect() } #[test] fn a_vault_that_never_had_setup_is_standard() { let v = vault(); assert_eq!(v.security_level(NOW), Level::Standard); let view = v.security_view(NOW); assert_eq!(view.level, Level::Standard); assert!(view.setup.is_none() && view.setup_remaining.is_none()); assert!(view.ticked.is_empty() && view.audit.is_empty() && view.kept_scope.is_none()); } #[test] fn setup_lasts_two_hours_from_its_start() { assert_eq!(SETUP_WINDOW, 2 * HOUR); let mut v = vault(); let w = v.security_start_setup(NOW); assert_eq!(w, Window { started: NOW, until: NOW + 2 * HOUR }); assert_eq!(v.security_level(NOW), Level::Setup); assert_eq!(v.security_level(NOW + 2 * HOUR - 1), Level::Setup, "the last millisecond is still Setup"); assert_eq!(v.security_level(NOW + 2 * HOUR), Level::Standard, "`until` itself is not"); assert_eq!(v.security_level(NOW - 1), Level::Standard, "a clock set back before the start is not Setup"); assert_eq!(v.security_view(NOW + HOUR).setup_remaining, Some(HOUR)); assert_eq!(v.security_view(NOW + 2 * HOUR).setup_remaining, None); assert_eq!(actions(&v), [Action::SetupStarted]); } #[test] fn starting_setup_again_while_in_setup_extends_nothing() { let mut v = vault(); let w = v.security_start_setup(NOW); assert_eq!(v.security_start_setup(NOW + HOUR), w); assert_eq!(v.security_level(NOW + 2 * HOUR), Level::Standard); assert_eq!(actions(&v), [Action::SetupStarted], "no second entry"); } #[test] fn finish_setup_ends_it_early_and_is_audited() { let mut v = vault(); assert!(!v.security_finish_setup(NOW), "nothing to finish"); v.security_start_setup(NOW); assert!(v.security_finish_setup(NOW + 10)); assert_eq!(v.security_level(NOW + 10), Level::Standard); assert_eq!(v.security_view(NOW + 10).finished, Some(NOW + 10)); assert!(!v.security_finish_setup(NOW + 20), "once"); assert_eq!(actions(&v), [Action::SetupStarted, Action::SetupFinished]); // A later Connect via SSH starts a new window. let w = v.security_start_setup(NOW + HOUR); assert_eq!(w, Window { started: NOW + HOUR, until: NOW + 3 * HOUR }); assert_eq!(v.security_level(NOW + HOUR), Level::Setup); assert_eq!(v.security_view(NOW + HOUR).finished, None); assert_eq!(actions(&v), [Action::SetupStarted, Action::SetupFinished, Action::SetupStarted]); } #[test] fn expiry_is_audited_once_by_the_first_change_that_notices_it() { let mut v = vault(); v.security_start_setup(NOW); // Reading notices nothing. assert_eq!(v.security_level(NOW + 3 * HOUR), Level::Standard); assert_eq!(actions(&v), [Action::SetupStarted]); v.security_tick(Item::NoSsh, NOW + 3 * HOUR); v.security_record(Action::SshConnected, None, NOW + 3 * HOUR + 1).unwrap(); assert!(!v.security_finish_setup(NOW + 3 * HOUR + 2)); assert_eq!(actions(&v), [Action::SetupStarted, Action::SetupExpired, Action::Ticked, Action::SshConnected]); let view = v.security_view(NOW + 3 * HOUR); assert_eq!(view.finished, Some(NOW + 2 * HOUR), "ended at `until`"); assert_eq!(view.audit[1].at, NOW + 3 * HOUR, "written when noticed"); // Starting again after expiry opens a new window. let w = v.security_start_setup(NOW + 4 * HOUR); assert_eq!(w.started, NOW + 4 * HOUR); assert_eq!(*actions(&v).last().unwrap(), Action::SetupStarted); } #[test] fn ticking_hardens_and_undoing_needs_hello_or_the_password_and_is_audited() { let mut v = vault(); assert!(v.security_tick(Item::HelloApprovals, NOW)); assert!(!v.security_tick(Item::HelloApprovals, NOW + 1), "ticked already: no entry"); assert_eq!(v.security_level(NOW), Level::Hardened); assert!(v.security_tick(Item::NoSsh, NOW + 2)); assert!(v.security_untick(Item::NoSsh, Auth::Hello, NOW + 3)); assert!(!v.security_untick(Item::NoSsh, Auth::Password, NOW + 4), "not ticked: no entry"); assert_eq!(v.security_level(NOW + 5), Level::Hardened); assert!(v.security_untick(Item::HelloApprovals, Auth::Password, NOW + 5)); assert_eq!(v.security_level(NOW + 6), Level::Standard); let audit = v.security_view(NOW + 6).audit; let got: Vec<_> = audit.iter().map(|e| (e.at, e.action, e.item, e.auth)).collect(); assert_eq!( got, [ (NOW, Action::Ticked, Some(Item::HelloApprovals), None), (NOW + 2, Action::Ticked, Some(Item::NoSsh), None), (NOW + 3, Action::Unticked, Some(Item::NoSsh), Some(Auth::Hello)), (NOW + 5, Action::Unticked, Some(Item::HelloApprovals), Some(Auth::Password)), ] ); } #[test] fn ticked_items_hold_during_setup() { let mut v = vault(); v.security_tick(Item::NoSsh, NOW); v.security_tick(Item::TotpOnPhone, NOW); v.security_start_setup(NOW + 1); assert_eq!(v.security_level(NOW + 1), Level::Setup); let ticked = v.security_view(NOW + 1).ticked; assert_eq!(ticked, BTreeSet::from([Item::TotpOnPhone, Item::NoSsh])); assert_eq!(ssh_needs_auth(Level::Setup, &ticked), SshRule::Off, "Setup never undoes hardening"); assert!(!totp_offer_allowed(&ticked)); // After Setup the ticks are still there: Hardened. assert_eq!(v.security_level(NOW + 3 * HOUR), Level::Hardened); } #[test] fn the_state_survives_a_reopen() { let dir = tempfile::tempdir().unwrap(); let path = dir.path().join("v.custos"); let mut v = vault(); v.security_start_setup(NOW); v.security_tick(Item::NarrowScope, NOW + 1); v.security_keep_scope(&s(&["arca/"]), &s(&["arca/", "fabrica/"])).unwrap(); v.security_record(Action::TotpMoved, Some(Item::TotpOnPhone), NOW + 2).unwrap(); v.save(&path).unwrap(); let back = Vault::open_file(&path, &SecretString::from("pw")).unwrap(); assert_eq!(back.security_view(NOW + 3), v.security_view(NOW + 3)); assert_eq!(back.security_level(NOW + 3), Level::Setup); let view = back.security_view(NOW + 3); assert_eq!(view.kept_scope, Some(KeptScope { ask: s(&["arca/"]), offer: s(&["arca/", "fabrica/"]) })); assert_eq!(view.audit.last().unwrap().item, Some(Item::TotpOnPhone)); } #[test] fn the_kept_scope_is_kept_once_and_taken_back() { let mut v = vault(); assert!(v.security_take_kept_scope().is_none()); v.security_keep_scope(&s(&["arca/"]), &s(&["fabrica/"])).unwrap(); // A second keep would lose the first prefixes: refused. assert!(matches!(v.security_keep_scope(&s(&["x/"]), &[]), Err(Error::Security(_)))); let kept = v.security_take_kept_scope().unwrap(); assert_eq!(kept, KeptScope { ask: s(&["arca/"]), offer: s(&["fabrica/"]) }); assert!(v.security_take_kept_scope().is_none()); // The rules for a prefix. for bad in [s(&[""]), s(&["a\nb"]), vec!["x".repeat(201)], (0..65).map(|i| format!("p{i}/")).collect()] { assert!(matches!(v.security_keep_scope(&bad, &[]), Err(Error::Security(_))), "{bad:?}"); assert!(matches!(v.security_keep_scope(&[], &bad), Err(Error::Security(_))), "{bad:?}"); } v.security_keep_scope(&[], &[]).unwrap(); assert_eq!(v.security_take_kept_scope(), Some(KeptScope { ask: vec![], offer: vec![] })); } #[test] fn only_ssh_connected_and_totp_moved_are_recorded_directly() { let mut v = vault(); v.security_record(Action::SshConnected, None, NOW).unwrap(); v.security_record(Action::TotpMoved, None, NOW + 1).unwrap(); for a in [Action::SetupStarted, Action::SetupFinished, Action::SetupExpired, Action::Ticked, Action::Unticked] { assert!(matches!(v.security_record(a, Some(Item::NoSsh), NOW + 2), Err(Error::Security(_))), "{a:?}"); } assert_eq!(actions(&v), [Action::SshConnected, Action::TotpMoved]); } #[test] fn the_audit_keeps_the_newest_5000() { assert_eq!(AUDIT_LIMIT, 5_000); let mut v = vault(); for i in 0..(AUDIT_LIMIT as i64 + 2) { v.security_record(Action::SshConnected, None, NOW + i).unwrap(); } let audit = v.security_view(NOW).audit; assert_eq!(audit.len(), AUDIT_LIMIT); assert_eq!(audit[0].at, NOW + 2); assert_eq!(audit.last().unwrap().at, NOW + AUDIT_LIMIT as i64 + 1); } #[test] fn the_rules_before_a_card() { let none = BTreeSet::new(); // Accept all: only in Setup, only for a setup run with Provisio lines only. assert!(accept_all_allowed(Level::Setup, "setup-1", true)); assert!(!accept_all_allowed(Level::Setup, "setup-1", false), "a signing or approval line"); assert!(!accept_all_allowed(Level::Setup, "r-42", true), "not a setup run"); assert!(!accept_all_allowed(Level::Setup, "Setup-1", true), "the prefix is exact"); assert!(!accept_all_allowed(Level::Standard, "setup-1", true)); assert!(!accept_all_allowed(Level::Hardened, "setup-1", true)); // A line that replaces a stored item is pre-ticked only where Accept all applies. assert!(replacing_line_preticked(Level::Setup, true)); assert!(!replacing_line_preticked(Level::Setup, false)); assert!(!replacing_line_preticked(Level::Standard, true)); assert!(!replacing_line_preticked(Level::Hardened, true)); // Connect via SSH. assert_eq!(ssh_needs_auth(Level::Setup, &none), SshRule::Allowed); assert_eq!(ssh_needs_auth(Level::Standard, &none), SshRule::NeedsAuth); let hello = BTreeSet::from([Item::HelloApprovals]); assert_eq!(ssh_needs_auth(Level::Hardened, &hello), SshRule::NeedsAuth); let off = BTreeSet::from([Item::NoSsh]); for l in [Level::Setup, Level::Standard, Level::Hardened] { assert_eq!(ssh_needs_auth(l, &off), SshRule::Off); } // TOTP offers and Hello for approvals. assert!(totp_offer_allowed(&none) && totp_offer_allowed(&hello)); assert!(!totp_offer_allowed(&BTreeSet::from([Item::TotpOnPhone]))); assert!(hello_for_approvals(&hello) && !hello_for_approvals(&none) && !hello_for_approvals(&off)); } #[test] fn values_are_spelled_as_docs_format_says() { let all = [ Item::TotpOnPhone, Item::HelloApprovals, Item::NarrowScope, Item::RotateSetup, Item::NoSsh, Item::PhoneApproves, ]; assert_eq!( serde_json::to_string(&all).unwrap(), r#"["totp-on-phone","hello-approvals","narrow-scope","rotate-setup","no-ssh","phone-approves"]"# ); let actions = [ Action::SetupStarted, Action::SetupFinished, Action::SetupExpired, Action::Ticked, Action::Unticked, Action::SshConnected, Action::TotpMoved, ]; assert_eq!( serde_json::to_string(&actions).unwrap(), r#"["setup-started","setup-finished","setup-expired","ticked","unticked","ssh-connected","totp-moved"]"# ); assert_eq!(serde_json::to_string(&[Auth::Hello, Auth::Password]).unwrap(), r#"["hello","password"]"#); assert_eq!( serde_json::to_string(&[Level::Setup, Level::Standard, Level::Hardened]).unwrap(), r#"["setup","standard","hardened"]"# ); assert_eq!( serde_json::to_string(&[SshRule::Allowed, SshRule::NeedsAuth, SshRule::Off]).unwrap(), r#"["allowed","needs-auth","off"]"# ); }