[vault] Security: the security body member (Setup window, checklist, kept scope, append-only audit of 5 000; level and card rules); docs/FORMAT.md with the values older cores refuse
This commit is contained in:
@@ -0,0 +1,623 @@
|
||||
//! The security level and the hardening checklist (docs/FORMAT.md, "The security member"): the vault body member
|
||||
//! `security`. The level is computed from the Setup window and the ticked items, never stored; every change is in an
|
||||
//! append-only audit. Nothing here is secret.
|
||||
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{Error, Vault};
|
||||
|
||||
/// The log keeps the newest this many entries.
|
||||
pub const AUDIT_LIMIT: usize = 5_000;
|
||||
/// How long Setup lasts from *Connect via SSH*, in milliseconds: 2 hours.
|
||||
pub const SETUP_WINDOW: i64 = 2 * 60 * 60 * 1000;
|
||||
|
||||
/// A checklist item.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
pub enum Item {
|
||||
/// Move the Authelia TOTP to my phone.
|
||||
TotpOnPhone,
|
||||
/// Approve with Windows Hello.
|
||||
HelloApprovals,
|
||||
/// Narrow the server scope.
|
||||
NarrowScope,
|
||||
/// Rotate what setup made.
|
||||
RotateSetup,
|
||||
/// Turn off Connect via SSH.
|
||||
NoSsh,
|
||||
/// The phone approves this PC.
|
||||
PhoneApproves,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum Level {
|
||||
/// From *Connect via SSH* until 2 hours later or *Finish setup*.
|
||||
Setup,
|
||||
/// After Setup, and every vault that never had one.
|
||||
Standard,
|
||||
/// Not in Setup, and at least one checklist item ticked.
|
||||
Hardened,
|
||||
}
|
||||
|
||||
/// How an undo was confirmed.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum Auth {
|
||||
Hello,
|
||||
Password,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
pub enum Action {
|
||||
SetupStarted,
|
||||
SetupFinished,
|
||||
/// Written by the first change after the window ended on its own.
|
||||
SetupExpired,
|
||||
Ticked,
|
||||
Unticked,
|
||||
SshConnected,
|
||||
TotpMoved,
|
||||
}
|
||||
|
||||
/// What *Connect via SSH* needs.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
pub enum SshRule {
|
||||
/// Offered, one click (Setup).
|
||||
Allowed,
|
||||
/// Offered after Windows Hello or the master password.
|
||||
NeedsAuth,
|
||||
/// Not offered (*Turn off Connect via SSH* is ticked).
|
||||
Off,
|
||||
}
|
||||
|
||||
/// A Setup window, Unix milliseconds: Setup holds for `started <= now < until`.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct Window {
|
||||
pub started: i64,
|
||||
pub until: i64,
|
||||
}
|
||||
|
||||
/// The server scope prefixes *Narrow the server scope* replaced, kept for its undo.
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct KeptScope {
|
||||
#[serde(default)]
|
||||
pub ask: Vec<String>,
|
||||
#[serde(default)]
|
||||
pub offer: Vec<String>,
|
||||
}
|
||||
|
||||
/// One audit entry. Append-only: nothing edits or removes one.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct SecurityEntry {
|
||||
/// Unix milliseconds.
|
||||
pub at: i64,
|
||||
pub action: Action,
|
||||
/// The checklist item, for `ticked` and `unticked`.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub item: Option<Item>,
|
||||
/// How an undo was confirmed, for `unticked`.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub auth: Option<Auth>,
|
||||
}
|
||||
|
||||
/// Everything *Settings → Security* shows.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct SecurityView {
|
||||
pub level: Level,
|
||||
/// The last Setup window, if there was one.
|
||||
pub setup: Option<Window>,
|
||||
/// When that window ended (Finish setup, or `until` once its expiry was noticed).
|
||||
pub finished: Option<i64>,
|
||||
/// Milliseconds of Setup left, while in Setup.
|
||||
pub setup_remaining: Option<i64>,
|
||||
pub ticked: BTreeSet<Item>,
|
||||
pub kept_scope: Option<KeptScope>,
|
||||
/// Oldest first.
|
||||
pub audit: Vec<SecurityEntry>,
|
||||
}
|
||||
|
||||
/// The member as the vault keeps it, and as it is written.
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub(crate) struct State {
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
setup: Option<Window>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
finished: Option<i64>,
|
||||
#[serde(default, skip_serializing_if = "BTreeSet::is_empty")]
|
||||
ticked: BTreeSet<Item>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
kept_scope: Option<KeptScope>,
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
audit: Vec<SecurityEntry>,
|
||||
}
|
||||
|
||||
/// At most this many prefixes on each side of a kept scope.
|
||||
const KEPT_MAX: usize = 64;
|
||||
|
||||
/// A kept scope prefix: 1 to 200 characters, no control characters. The error names the rule, never the value.
|
||||
fn kept_ok(list: &[String]) -> Result<(), Error> {
|
||||
if list.len() > KEPT_MAX {
|
||||
return Err(Error::Security("a kept scope has at most 64 prefixes on each side"));
|
||||
}
|
||||
if list.iter().any(|p| !(1..=200).contains(&p.chars().count()) || p.chars().any(char::is_control)) {
|
||||
return Err(Error::Security("a kept scope prefix is 1 to 200 characters without control characters"));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn trim(audit: &mut Vec<SecurityEntry>) {
|
||||
if audit.len() > AUDIT_LIMIT {
|
||||
audit.drain(..audit.len() - AUDIT_LIMIT);
|
||||
}
|
||||
}
|
||||
|
||||
impl State {
|
||||
/// The member as read, checked; `None` when it breaks a rule (the vault then does not open).
|
||||
pub(crate) fn from_json(j: Option<State>) -> Option<State> {
|
||||
let mut st = j.unwrap_or_default();
|
||||
if let Some(w) = st.setup
|
||||
&& w.until <= w.started
|
||||
{
|
||||
return None;
|
||||
}
|
||||
if st.finished.is_some() && st.setup.is_none() {
|
||||
return None;
|
||||
}
|
||||
if let Some(k) = &st.kept_scope
|
||||
&& (kept_ok(&k.ask).is_err() || kept_ok(&k.offer).is_err())
|
||||
{
|
||||
return None;
|
||||
}
|
||||
trim(&mut st.audit);
|
||||
Some(st)
|
||||
}
|
||||
|
||||
/// Absent while nothing was ever set, so a vault without it is written exactly as before.
|
||||
pub(crate) fn json(&self) -> Option<State> {
|
||||
(*self != State::default()).then(|| self.clone())
|
||||
}
|
||||
|
||||
/// Setup holds for `started <= now < until` of a window that has not finished.
|
||||
fn in_setup(&self, now: i64) -> bool {
|
||||
self.finished.is_none() && self.setup.is_some_and(|w| w.started <= now && now < w.until)
|
||||
}
|
||||
|
||||
fn level(&self, now: i64) -> Level {
|
||||
if self.in_setup(now) {
|
||||
Level::Setup
|
||||
} else if self.ticked.is_empty() {
|
||||
Level::Standard
|
||||
} else {
|
||||
Level::Hardened
|
||||
}
|
||||
}
|
||||
|
||||
fn push(&mut self, at: i64, action: Action, item: Option<Item>, auth: Option<Auth>) {
|
||||
self.audit.push(SecurityEntry { at, action, item, auth });
|
||||
trim(&mut self.audit);
|
||||
}
|
||||
|
||||
/// Before any change: a window that ended on its own is written down once, as `setup-expired` at `now` and
|
||||
/// `finished` = `until`.
|
||||
fn notice_expiry(&mut self, now: i64) {
|
||||
if let Some(w) = self.setup
|
||||
&& self.finished.is_none()
|
||||
&& now >= w.until
|
||||
{
|
||||
self.finished = Some(w.until);
|
||||
self.push(now, Action::SetupExpired, None, None);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// *Accept all* on a run card: only in Setup, for a setup run (its id starts with `setup-`) that holds Provisio
|
||||
/// lines only.
|
||||
pub fn accept_all_allowed(level: Level, run_id: &str, only_provision: bool) -> bool {
|
||||
level == Level::Setup && run_id.starts_with("setup-") && only_provision
|
||||
}
|
||||
|
||||
/// Whether a line that replaces a stored item starts ticked: only where *Accept all* applies (a setup run in
|
||||
/// Setup); otherwise Mark ticks it himself.
|
||||
pub fn replacing_line_preticked(level: Level, setup_run: bool) -> bool {
|
||||
level == Level::Setup && setup_run
|
||||
}
|
||||
|
||||
/// What *Connect via SSH* needs: off when *Turn off Connect via SSH* is ticked, one click in Setup, Windows Hello or
|
||||
/// the master password otherwise.
|
||||
pub fn ssh_needs_auth(level: Level, ticked: &BTreeSet<Item>) -> SshRule {
|
||||
if ticked.contains(&Item::NoSsh) {
|
||||
SshRule::Off
|
||||
} else if level == Level::Setup {
|
||||
SshRule::Allowed
|
||||
} else {
|
||||
SshRule::NeedsAuth
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether a server may offer a TOTP seed into the vault: not once it was moved to the phone.
|
||||
pub fn totp_offer_allowed(ticked: &BTreeSet<Item>) -> bool {
|
||||
!ticked.contains(&Item::TotpOnPhone)
|
||||
}
|
||||
|
||||
/// Whether *Sign & release*, *Accept*, *Accept all* and approvals ask for Windows Hello first.
|
||||
pub fn hello_for_approvals(ticked: &BTreeSet<Item>) -> bool {
|
||||
ticked.contains(&Item::HelloApprovals)
|
||||
}
|
||||
|
||||
impl Vault {
|
||||
/// The level at `now` (Unix milliseconds).
|
||||
pub fn security_level(&self, now: i64) -> Level {
|
||||
self.security.level(now)
|
||||
}
|
||||
|
||||
/// The level, the Setup window and what is left of it, the ticked items, the kept scope and the audit.
|
||||
pub fn security_view(&self, now: i64) -> SecurityView {
|
||||
let st = &self.security;
|
||||
SecurityView {
|
||||
level: st.level(now),
|
||||
setup: st.setup,
|
||||
finished: st.finished,
|
||||
setup_remaining: st.setup.filter(|_| st.in_setup(now)).map(|w| w.until - now),
|
||||
ticked: st.ticked.clone(),
|
||||
kept_scope: st.kept_scope.clone(),
|
||||
audit: st.audit.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Starts Setup (*Connect via SSH*): a window of 2 hours from `now`, audited `setup-started`. While in Setup it
|
||||
/// changes nothing and returns the window that holds. The caller saves.
|
||||
pub fn security_start_setup(&mut self, now: i64) -> Window {
|
||||
let st = &mut self.security;
|
||||
st.notice_expiry(now);
|
||||
if st.in_setup(now)
|
||||
&& let Some(w) = st.setup
|
||||
{
|
||||
return w;
|
||||
}
|
||||
let w = Window { started: now, until: now.saturating_add(SETUP_WINDOW) };
|
||||
st.setup = Some(w);
|
||||
st.finished = None;
|
||||
st.push(now, Action::SetupStarted, None, None);
|
||||
w
|
||||
}
|
||||
|
||||
/// *Finish setup*: ends the window at `now`, audited `setup-finished`. False when not in Setup. The caller saves.
|
||||
pub fn security_finish_setup(&mut self, now: i64) -> bool {
|
||||
let st = &mut self.security;
|
||||
st.notice_expiry(now);
|
||||
if !st.in_setup(now) {
|
||||
return false;
|
||||
}
|
||||
st.finished = Some(now);
|
||||
st.push(now, Action::SetupFinished, None, None);
|
||||
true
|
||||
}
|
||||
|
||||
/// Ticks a checklist item, audited `ticked`. False (and no entry) when it was ticked already. The caller saves.
|
||||
pub fn security_tick(&mut self, item: Item, now: i64) -> bool {
|
||||
let st = &mut self.security;
|
||||
st.notice_expiry(now);
|
||||
if !st.ticked.insert(item) {
|
||||
return false;
|
||||
}
|
||||
st.push(now, Action::Ticked, Some(item), None);
|
||||
true
|
||||
}
|
||||
|
||||
/// Unticks a checklist item after Windows Hello or the master password, audited `unticked` with how. False (and
|
||||
/// no entry) when it was not ticked. The caller saves.
|
||||
pub fn security_untick(&mut self, item: Item, auth: Auth, now: i64) -> bool {
|
||||
let st = &mut self.security;
|
||||
st.notice_expiry(now);
|
||||
if !st.ticked.remove(&item) {
|
||||
return false;
|
||||
}
|
||||
st.push(now, Action::Unticked, Some(item), Some(auth));
|
||||
true
|
||||
}
|
||||
|
||||
/// Keeps the scope prefixes *Narrow the server scope* replaces. Refused when a scope is kept already (its undo
|
||||
/// has not taken it back), so the first prefixes are never lost. The caller saves.
|
||||
pub fn security_keep_scope(&mut self, ask: &[String], offer: &[String]) -> Result<(), Error> {
|
||||
if self.security.kept_scope.is_some() {
|
||||
return Err(Error::Security("a scope is kept already; take it back first"));
|
||||
}
|
||||
kept_ok(ask)?;
|
||||
kept_ok(offer)?;
|
||||
self.security.kept_scope = Some(KeptScope { ask: ask.to_vec(), offer: offer.to_vec() });
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Takes the kept prefixes back, for the undo. The caller saves.
|
||||
pub fn security_take_kept_scope(&mut self) -> Option<KeptScope> {
|
||||
self.security.kept_scope.take()
|
||||
}
|
||||
|
||||
/// Records `ssh-connected` or `totp-moved`; the other actions are written by the calls above. The caller saves.
|
||||
pub fn security_record(&mut self, action: Action, item: Option<Item>, now: i64) -> Result<(), Error> {
|
||||
if !matches!(action, Action::SshConnected | Action::TotpMoved) {
|
||||
return Err(Error::Security("only ssh-connected and totp-moved are recorded directly"));
|
||||
}
|
||||
let st = &mut self.security;
|
||||
st.notice_expiry(now);
|
||||
st.push(now, action, item, None);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
//! An older reader still opens a vault with the member, and unknown values make the vault refuse to open. They
|
||||
//! live here because only the crate can decrypt the body, change it and seal it again.
|
||||
use super::*;
|
||||
use crate::format::Header;
|
||||
use crate::{FactorJson, Item as VaultItem, ItemType, KdfParams, SecretString, crypto, identity, provision};
|
||||
|
||||
const NOW: i64 = 1_793_750_000_000;
|
||||
|
||||
fn vault() -> Vault {
|
||||
Vault::create(&SecretString::from("pw"), KdfParams { m_cost_kib: 64, t_cost: 1, p_cost: 1 }).unwrap()
|
||||
}
|
||||
|
||||
fn body_json(v: &Vault) -> String {
|
||||
let bytes = v.to_bytes().unwrap();
|
||||
let (header, at) = Header::parse(&bytes).unwrap();
|
||||
let json = crypto::open(&v.key, &header.body_nonce, &bytes[at..], &bytes[..at]).unwrap();
|
||||
String::from_utf8(json.to_vec()).unwrap()
|
||||
}
|
||||
|
||||
/// The vault's file with its body replaced by `json`, sealed with the vault's key as `to_bytes` does.
|
||||
fn with_body(v: &Vault, json: &str) -> Vec<u8> {
|
||||
let bytes = v.to_bytes().unwrap();
|
||||
let (header, at) = Header::parse(&bytes).unwrap();
|
||||
let mut out = bytes[..at].to_vec();
|
||||
out.extend_from_slice(&crypto::seal(&v.key, &header.body_nonce, json.as_bytes(), &bytes[..at]));
|
||||
out
|
||||
}
|
||||
|
||||
// ---- the body as core 0.11.0 parsed it (src/lib.rs, src/signing.rs, src/provision.rs at v0.11.0) ----
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[allow(dead_code)]
|
||||
struct BodyV0_11 {
|
||||
format: u32,
|
||||
items: Vec<VaultItem>,
|
||||
#[serde(default)]
|
||||
factors: Vec<FactorJson>,
|
||||
#[serde(default)]
|
||||
identity: Option<identity::RootJson>,
|
||||
#[serde(default)]
|
||||
signing: Option<SigningV0_11>,
|
||||
#[serde(default)]
|
||||
provision: Option<ProvisionV0_11>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[allow(dead_code)]
|
||||
struct SigningV0_11 {
|
||||
#[serde(default)]
|
||||
keys: Vec<KeyV0_11>,
|
||||
#[serde(default)]
|
||||
audit: Vec<SignEntryV0_11>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
#[allow(dead_code)]
|
||||
struct KeyV0_11 {
|
||||
id: String,
|
||||
kind: OldKeyKind,
|
||||
role: OldRole,
|
||||
name: String,
|
||||
created: i64,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
enum OldKeyKind {
|
||||
Ed25519,
|
||||
RootCa,
|
||||
CodeSigning,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
enum OldRole {
|
||||
Release,
|
||||
Test,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[allow(dead_code)]
|
||||
struct SignEntryV0_11 {
|
||||
at: i64,
|
||||
key: String,
|
||||
kind: OldSignKind,
|
||||
outcome: OldSignOutcome,
|
||||
via: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
enum OldSignKind {
|
||||
Minisign,
|
||||
Manifest,
|
||||
Authenticode,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
enum OldSignOutcome {
|
||||
Approved,
|
||||
Auto,
|
||||
Refused,
|
||||
Expired,
|
||||
OutOfScope,
|
||||
Invalid,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[allow(dead_code)]
|
||||
struct ProvisionV0_11 {
|
||||
#[serde(default)]
|
||||
audit: Vec<ProvisionEntryV0_11>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[allow(dead_code)]
|
||||
struct ProvisionEntryV0_11 {
|
||||
store: OldStore,
|
||||
outcome: OldProvisionOutcome,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
enum OldStore {
|
||||
Vault,
|
||||
Claude,
|
||||
Cloud,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
enum OldProvisionOutcome {
|
||||
Approved,
|
||||
Generated,
|
||||
Accepted,
|
||||
Refused,
|
||||
OutOfScope,
|
||||
Invalid,
|
||||
Expired,
|
||||
}
|
||||
|
||||
fn display_entry(outcome: provision::Outcome) -> provision::ProvisionEntry {
|
||||
provision::ProvisionEntry {
|
||||
at: NOW,
|
||||
direction: provision::Direction::Offer,
|
||||
host: "nixos-vm".into(),
|
||||
item: "arca/authelia-mark".into(),
|
||||
store: provision::Store::Display,
|
||||
run: None,
|
||||
kind: Some("totp".into()),
|
||||
code: "684361".into(),
|
||||
outcome,
|
||||
replaced: false,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_older_reader_opens_a_vault_with_the_security_member() {
|
||||
let mut v = vault();
|
||||
v.add(VaultItem::new(ItemType::SecureNote, "kept", 1));
|
||||
// Never set: the body has no trace of the member, so it is byte-for-byte what 0.11.0 wrote.
|
||||
assert!(!body_json(&v).contains("security"));
|
||||
|
||||
v.security_start_setup(NOW);
|
||||
v.security_tick(Item::HelloApprovals, NOW + 1);
|
||||
v.security_untick(Item::HelloApprovals, Auth::Hello, NOW + 2);
|
||||
v.security_tick(Item::NoSsh, NOW + 3);
|
||||
v.security_keep_scope(&["arca/".to_string()], &[]).unwrap();
|
||||
v.security_record(Action::SshConnected, None, NOW + 4).unwrap();
|
||||
let json = body_json(&v);
|
||||
assert!(json.contains(r#""security":{"setup":{"started":"#), "{json}");
|
||||
assert!(json.contains(r#""ticked":["no-ssh"]"#) && json.contains(r#""keptScope":{"ask":["arca/"]"#), "{json}");
|
||||
// Core 0.11.0's parser takes it and keeps everything it knows.
|
||||
let old: BodyV0_11 = serde_json::from_str(&json).unwrap();
|
||||
assert_eq!(old.items.len(), 1);
|
||||
assert_eq!(old.items[0].title, "kept");
|
||||
// And this core reads it back the same.
|
||||
let back = Vault::open(&v.to_bytes().unwrap(), &SecretString::from("pw")).unwrap();
|
||||
assert_eq!(back.security_view(NOW + 5), v.security_view(NOW + 5));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_older_reader_opens_a_vault_with_the_approval_key_and_runs_but_not_approval_entries() {
|
||||
use crate::signing::{Approval, Context, Request, Role, SignKind, Via};
|
||||
let mut v = vault();
|
||||
v.signing_create(Role::Release, &["*".to_string()], &["*".to_string()], NOW).unwrap();
|
||||
let mut r = Request {
|
||||
key: "domi-release".into(),
|
||||
kind: SignKind::Minisign,
|
||||
digest: vec![1; 64],
|
||||
manifest: None,
|
||||
approval: None,
|
||||
run: Some("r-42".into()),
|
||||
context: Context { file: "a.exe".into(), version: "0.7.0".into(), ..Default::default() },
|
||||
via: Via::Local,
|
||||
};
|
||||
v.signing_sign(&r, Approval::Mark, NOW).unwrap();
|
||||
v.provision_record(provision::ProvisionEntry {
|
||||
store: provision::Store::Vault,
|
||||
outcome: provision::Outcome::Accepted,
|
||||
..display_entry(provision::Outcome::Accepted)
|
||||
})
|
||||
.unwrap();
|
||||
let json = body_json(&v);
|
||||
assert!(json.contains(r#""id":"domi-approve""#) && json.contains(r#""run":"r-42""#), "{json}");
|
||||
assert!(serde_json::from_str::<BodyV0_11>(&json).is_ok(), "the new key and members are ignored");
|
||||
|
||||
// An approval entry is a kind 0.11.0 does not know: it refuses the vault (docs/FORMAT.md).
|
||||
r.key = "domi-approve".into();
|
||||
r.kind = SignKind::Approval;
|
||||
r.digest = vec![];
|
||||
v.signing_refuse(&r, crate::signing::Outcome::Refused, None, NOW + 1);
|
||||
assert!(serde_json::from_str::<BodyV0_11>(&body_json(&v)).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_older_reader_refuses_display_and_confirmed() {
|
||||
for (store, outcome) in [
|
||||
(provision::Store::Display, provision::Outcome::Refused),
|
||||
(provision::Store::Display, provision::Outcome::Confirmed),
|
||||
] {
|
||||
let mut v = vault();
|
||||
v.provision_record(provision::ProvisionEntry { store, ..display_entry(outcome) }).unwrap();
|
||||
assert!(serde_json::from_str::<BodyV0_11>(&body_json(&v)).is_err(), "{store:?} {outcome:?}");
|
||||
// This core reads it.
|
||||
assert!(Vault::open(&v.to_bytes().unwrap(), &SecretString::from("pw")).is_ok());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_or_broken_security_values_make_the_vault_refuse_to_open() {
|
||||
let mut v = vault();
|
||||
v.security_start_setup(NOW);
|
||||
v.security_tick(Item::NoSsh, NOW + 1);
|
||||
v.security_untick(Item::NoSsh, Auth::Password, NOW + 2);
|
||||
v.security_tick(Item::RotateSetup, NOW + 3);
|
||||
v.security_keep_scope(&["arca/".to_string()], &["fabrica/".to_string()]).unwrap();
|
||||
let json = body_json(&v);
|
||||
// The file as written opens.
|
||||
assert!(Vault::open(&with_body(&v, &json), &SecretString::from("pw")).is_ok());
|
||||
let until = format!(r#""until":{}"#, NOW + SETUP_WINDOW);
|
||||
let cases = [
|
||||
(r#""ticked":["rotate-setup"]"#, r#""ticked":["rotate-later"]"#.to_string()),
|
||||
(r#""action":"ticked""#, r#""action":"tocked""#.to_string()),
|
||||
(r#""auth":"password""#, r#""auth":"pin""#.to_string()),
|
||||
(r#""item":"no-ssh""#, r#""item":"no-telnet""#.to_string()),
|
||||
(until.as_str(), format!(r#""until":{NOW}"#)),
|
||||
(r#""ask":["arca/"]"#, r#""ask":["arca/\n"]"#.to_string()),
|
||||
(r#""ask":["arca/"]"#, r#""ask":[""]"#.to_string()),
|
||||
];
|
||||
for (from, to) in cases {
|
||||
assert!(json.contains(from), "{from} in {json}");
|
||||
let bad = json.replacen(from, &to, 1);
|
||||
let r = Vault::open(&with_body(&v, &bad), &SecretString::from("pw"));
|
||||
assert!(matches!(r, Err(Error::Corrupted)), "{to}: {r:?}");
|
||||
}
|
||||
// `finished` without a window.
|
||||
let mut no_window = json.clone();
|
||||
let start = no_window.find(r#""setup":{"#).unwrap();
|
||||
let end = start + no_window[start..].find('}').unwrap() + 2;
|
||||
no_window.replace_range(start..end, r#""finished":5,"#);
|
||||
let r = Vault::open(&with_body(&v, &no_window), &SecretString::from("pw"));
|
||||
assert!(matches!(r, Err(Error::Corrupted)), "{no_window}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user