use std::collections::BTreeSet;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Scope<T: Ord + Clone> {
All,
Only(BTreeSet<T>),
}
impl<T: Ord + Clone> Scope<T> {
#[must_use]
pub fn top() -> Self {
Self::All
}
#[must_use]
pub fn none() -> Self {
Self::Only(BTreeSet::new())
}
pub fn only<I: IntoIterator<Item = T>>(items: I) -> Self {
Self::Only(items.into_iter().collect())
}
#[must_use]
pub fn leq(&self, other: &Self) -> bool {
match (self, other) {
(_, Self::All) => true,
(Self::All, Self::Only(_)) => false,
(Self::Only(a), Self::Only(b)) => a.is_subset(b),
}
}
#[must_use]
pub fn meet(&self, other: &Self) -> Self {
match (self, other) {
(Self::All, x) | (x, Self::All) => x.clone(),
(Self::Only(a), Self::Only(b)) => Self::Only(a.intersection(b).cloned().collect()),
}
}
#[must_use]
pub fn permits(&self, item: &T) -> bool {
match self {
Self::All => true,
Self::Only(set) => set.contains(item),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CountBound {
Unlimited,
AtMost(u64),
}
impl CountBound {
#[must_use]
pub fn top() -> Self {
Self::Unlimited
}
#[must_use]
pub fn leq(&self, other: &Self) -> bool {
match (self, other) {
(_, Self::Unlimited) => true,
(Self::Unlimited, Self::AtMost(_)) => false,
(Self::AtMost(a), Self::AtMost(b)) => a <= b,
}
}
#[must_use]
pub fn meet(&self, other: &Self) -> Self {
match (self, other) {
(Self::Unlimited, x) | (x, Self::Unlimited) => *x,
(Self::AtMost(a), Self::AtMost(b)) => Self::AtMost((*a).min(*b)),
}
}
#[must_use]
pub fn permits_one_more(&self, used_so_far: u64) -> bool {
match self {
Self::Unlimited => true,
Self::AtMost(n) => used_so_far < *n,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Caveats {
pub fs_read: Scope<String>,
pub fs_write: Scope<String>,
pub exec: Scope<String>,
pub net: Scope<String>,
pub max_calls: CountBound,
pub valid_for_generation: Scope<u64>,
}
impl Caveats {
#[must_use]
pub fn top() -> Self {
Self {
fs_read: Scope::top(),
fs_write: Scope::top(),
exec: Scope::top(),
net: Scope::top(),
max_calls: CountBound::top(),
valid_for_generation: Scope::top(),
}
}
#[must_use]
pub fn leq(&self, other: &Self) -> bool {
self.fs_read.leq(&other.fs_read)
&& self.fs_write.leq(&other.fs_write)
&& self.exec.leq(&other.exec)
&& self.net.leq(&other.net)
&& self.max_calls.leq(&other.max_calls)
&& self.valid_for_generation.leq(&other.valid_for_generation)
}
#[must_use]
pub fn meet(&self, other: &Self) -> Self {
Self {
fs_read: self.fs_read.meet(&other.fs_read),
fs_write: self.fs_write.meet(&other.fs_write),
exec: self.exec.meet(&other.exec),
net: self.net.meet(&other.net),
max_calls: self.max_calls.meet(&other.max_calls),
valid_for_generation: self.valid_for_generation.meet(&other.valid_for_generation),
}
}
#[must_use]
pub fn permits_fs_read(&self, path: &str) -> bool {
self.fs_read.permits(&path.to_string())
}
#[must_use]
pub fn permits_fs_write(&self, path: &str) -> bool {
self.fs_write.permits(&path.to_string())
}
#[must_use]
pub fn permits_exec(&self, cmd: &str) -> bool {
self.exec.permits(&cmd.to_string())
}
#[must_use]
pub fn permits_net(&self, host: &str) -> bool {
self.net.permits(&host.to_string())
}
}
impl Default for Caveats {
fn default() -> Self {
Self::top()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn scope_all_permits_everything() {
let s: Scope<String> = Scope::All;
assert!(s.permits(&"anything".to_string()));
assert!(s.permits(&"".to_string()));
}
#[test]
fn scope_only_permits_exact_members() {
let s = Scope::only(["a".to_string(), "b".to_string()]);
assert!(s.permits(&"a".to_string()));
assert!(s.permits(&"b".to_string()));
assert!(!s.permits(&"c".to_string()));
assert!(!s.permits(&"".to_string()));
}
#[test]
fn scope_none_permits_nothing() {
let s: Scope<String> = Scope::none();
assert!(!s.permits(&"a".to_string()));
}
#[test]
fn scope_lattice_order() {
let small = Scope::only(["a".to_string()]);
let big = Scope::only(["a".to_string(), "b".to_string()]);
assert!(small.leq(&big));
assert!(small.leq(&Scope::All));
assert!(!Scope::<String>::All.leq(&small));
assert!(!big.leq(&small));
}
#[test]
fn scope_meet_intersects() {
let a = Scope::only(["a".to_string(), "b".to_string()]);
let b = Scope::only(["b".to_string(), "c".to_string()]);
assert_eq!(a.meet(&b), Scope::only(["b".to_string()]));
assert_eq!(a.meet(&Scope::All), a);
}
#[test]
fn count_bound_permits_one_more() {
assert!(CountBound::Unlimited.permits_one_more(0));
assert!(CountBound::Unlimited.permits_one_more(99_999));
assert!(CountBound::AtMost(3).permits_one_more(0));
assert!(CountBound::AtMost(3).permits_one_more(2));
assert!(!CountBound::AtMost(3).permits_one_more(3));
assert!(!CountBound::AtMost(3).permits_one_more(99));
assert!(!CountBound::AtMost(0).permits_one_more(0));
}
#[test]
fn count_bound_meet_is_tighter() {
assert_eq!(
CountBound::AtMost(5).meet(&CountBound::AtMost(3)),
CountBound::AtMost(3)
);
assert_eq!(
CountBound::Unlimited.meet(&CountBound::AtMost(7)),
CountBound::AtMost(7)
);
}
#[test]
fn caveats_top_permits_everything() {
let c = Caveats::top();
assert!(c.permits_fs_read("/anywhere"));
assert!(c.permits_fs_write("/anywhere"));
assert!(c.permits_exec("rm"));
assert!(c.permits_net("evil.example.com"));
assert!(c.max_calls.permits_one_more(1_000_000));
}
#[test]
fn caveats_default_is_top() {
assert_eq!(Caveats::default(), Caveats::top());
}
#[test]
fn caveats_attenuated_denies_outside_scope() {
let c = Caveats {
fs_write: Scope::only(["allowed.rs".to_string()]),
net: Scope::only(["allowed.example.com".to_string()]),
max_calls: CountBound::AtMost(2),
..Caveats::top()
};
assert!(c.permits_fs_write("allowed.rs"));
assert!(!c.permits_fs_write("forbidden.rs"));
assert!(c.permits_net("allowed.example.com"));
assert!(!c.permits_net("evil.example.com"));
assert!(c.max_calls.permits_one_more(1));
assert!(!c.max_calls.permits_one_more(2));
}
#[test]
fn caveats_leq_per_axis() {
let restricted = Caveats {
fs_write: Scope::only(["a.rs".to_string()]),
..Caveats::top()
};
assert!(restricted.leq(&Caveats::top()));
assert!(!Caveats::top().leq(&restricted));
}
#[test]
fn caveats_meet_attenuates_each_axis() {
let a = Caveats {
fs_read: Scope::only(["/repo".to_string(), "/tmp".to_string()]),
max_calls: CountBound::AtMost(10),
..Caveats::top()
};
let b = Caveats {
fs_read: Scope::only(["/repo".to_string()]),
max_calls: CountBound::AtMost(4),
..Caveats::top()
};
let m = a.meet(&b);
assert_eq!(m.fs_read, Scope::only(["/repo".to_string()]));
assert_eq!(m.max_calls, CountBound::AtMost(4));
assert!(m.leq(&a) && m.leq(&b));
}
#[test]
fn caveats_serde_roundtrip() {
let c = Caveats {
exec: Scope::only(["git".to_string(), "cargo".to_string()]),
max_calls: CountBound::AtMost(3),
valid_for_generation: Scope::only([42u64]),
..Caveats::top()
};
let json = serde_json::to_string(&c).unwrap();
let back: Caveats = serde_json::from_str(&json).unwrap();
assert_eq!(c, back);
}
}