1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//! The role a path argument plays, out of `pathgate.rs`.
use serde::Deserialize;
/// What to do with a path found in a given argument slot.
#[derive(Deserialize, Clone, Copy, PartialEq, Default, Debug)]
#[serde(rename_all = "lowercase")]
pub(crate) enum Role {
/// Gate by read locus — a disclosing read (`od FILE`, `scp` source, `wget --post-file`).
Read,
/// Gate by read locus, as a SWEEP — the command descends the path rather than reading it as
/// one file (`rg PATTERN DIR`, `ag`, an archiver's source tree).
///
/// The distinction matters only above the workspace, and it is the shield that makes it
/// matter: `rg foo ~` names `~`, which is not a credential store, and then reads
/// `~/.ssh/id_rsa` out of it. A name test can only clear a name someone wrote, so a root that
/// stands for everything beneath it cannot be cleared at all. `read` stays correct for the
/// commands that open exactly the file they are given.
#[serde(rename = "read_tree")]
ReadTree,
/// Gate by write locus — a write-target (`tee FILE`, `curl -o`, a converter's output).
Write,
/// Gate by EXECUTOR locus — a flag whose value selects code to run (`cargo --manifest-path
/// DIR/Cargo.toml` runs that project's build.rs/tests). Denies a foreign or `/tmp` executor
/// (the execution-origin band), where `write` would allow `/tmp`. See
/// docs/design/behavioral-taxonomy-execution-origin.md.
Exec,
/// An executor path that rollup will also accept as inline JavaScript. `--plugin` / `-p`
/// values containing `{`, `=`, or `,` are evaluated as a function body (`commandPlugins.ts`);
/// a package name or a project file has none of those, and is judged as `exec`.
Plugin,
/// Gate as the remote shell rsync starts: a command line, allowed only as ssh or rsh with plain
/// options, or as a program in the project judged by the executor locus. `exec` judges one path
/// and so refuses `ssh -p 22`; this reads the options instead. See `rsync_shell.rs`.
#[serde(rename = "remote_shell")]
RemoteShell,
/// Never gate — a URL, an `-i` identity, a converter's non-disclosing transcode input. The
/// default, so a command declaring only path-bearing flags leaves its positionals ungated.
#[default]
Ignore,
}
impl Role {
/// How much this role withholds, for picking between clauses that both hold.
///
/// Written out rather than derived from declaration order so that reordering the enum — which
/// looks cosmetic — cannot quietly change which role a self-overlapping gate selects. `Exec`
/// outranks `Write` because it withholds strictly more: it refuses `/tmp` and home, where a
/// write target is allowed to land.
pub(crate) fn restrictiveness(self) -> u8 {
match self {
Role::Ignore => 0,
Role::Read => 1,
Role::ReadTree => 2,
Role::Write => 3,
Role::Exec | Role::RemoteShell => 4,
Role::Plugin => 5,
}
}
}
#[cfg(test)]
mod tests {
use super::Role;
use proptest::prelude::*;
const ALL: [(Role, &str); 7] = [
(Role::Ignore, "ignore"),
(Role::Read, "read"),
(Role::ReadTree, "read_tree"),
(Role::Write, "write"),
(Role::Exec, "exec"),
(Role::RemoteShell, "remote_shell"),
(Role::Plugin, "plugin"),
];
/// The rung of each entry of `ALL`: the two executor roles share one, and plugin sits above them.
fn rung(index: usize) -> usize {
match index {
6 => 5,
i => i.min(4),
}
}
proptest! {
/// Every role parses from the name the TOML uses, and a role later on the ladder ignore,
/// read, read_tree, write, the two executor roles, then plugin never withholds less.
#[test]
fn roles_parse_by_name_and_rank_along_the_ladder(a in 0usize..7, b in 0usize..7) {
let ((role_a, name_a), (role_b, _)) = (ALL[a], ALL[b]);
#[derive(serde::Deserialize)]
struct Holder {
role: Role,
}
let parsed: Holder = toml::from_str(&format!("role = \"{name_a}\"")).expect("a role name parses");
prop_assert_eq!(parsed.role, role_a);
if rung(a) < rung(b) {
prop_assert!(role_a.restrictiveness() < role_b.restrictiveness());
} else if rung(a) == rung(b) {
prop_assert_eq!(role_a.restrictiveness(), role_b.restrictiveness());
}
}
}
}