Skip to main content

mouser_mode_api/
host_api.rs

1//! Calls a plugin makes to the host (EVENTS.md sections 5 and 13). Every
2//! call runs with the logged-in user's rights (or the SSH login's on a
3//! remote target) and never more: elevation is only `Escalate`, typed into
4//! the user's visible terminal after a host-drawn card, so the OS or sudo
5//! asks the user directly and Mouser never sees a password.
6
7use serde_json::Value;
8
9/// Where a call runs.
10#[derive(Debug, Clone, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
11pub enum Target {
12    /// The active tab's session (local or remote).
13    #[default]
14    Active,
15    Local,
16    /// A remote tab, over its existing SSH connection.
17    Tab(u32),
18}
19
20#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
21pub enum WriteMode {
22    Create,
23    Truncate,
24    CreateNew,
25}
26
27#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
28pub enum Fs {
29    Read {
30        path: String,
31        offset: u64,
32        len: Option<u64>,
33    },
34    Write {
35        path: String,
36        #[serde(with = "serde_bytes")]
37        data: Vec<u8>,
38        mode: WriteMode,
39        perm: Option<u32>,
40        atomic: bool,
41    },
42    Append {
43        path: String,
44        #[serde(with = "serde_bytes")]
45        data: Vec<u8>,
46    },
47    Stat {
48        path: String,
49        follow: bool,
50    },
51    List {
52        path: String,
53        glob: Option<String>,
54        hidden: bool,
55        cursor: Option<String>,
56    },
57    Mkdir {
58        path: String,
59        parents: bool,
60        perm: Option<u32>,
61    },
62    /// `to_trash` defaults to true; permanent delete always shows a card.
63    Remove {
64        path: String,
65        recursive: bool,
66        to_trash: bool,
67    },
68    Rename {
69        from: String,
70        to: String,
71        overwrite: bool,
72    },
73    Chmod {
74        path: String,
75        mode: u32,
76    },
77    Watch {
78        path: String,
79        recursive: bool,
80    },
81    Unwatch {
82        id: u64,
83    },
84}
85
86impl Fs {
87    /// Whether the call changes files (always audited; cards outside scope).
88    pub fn mutates(&self) -> bool {
89        !matches!(self, Fs::Read { .. } | Fs::Stat { .. } | Fs::List { .. } | Fs::Watch { .. } | Fs::Unwatch { .. })
90    }
91}
92
93#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
94pub enum Io {
95    Pipes,
96    Pty {
97        cols: u16,
98        rows: u16,
99    },
100    /// Typed into a real tab the user sees (`new`: a new tab).
101    VisiblePty {
102        new: bool,
103    },
104}
105
106#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
107pub struct Spawn {
108    pub program: String,
109    pub args: Vec<String>,
110    pub env_inherit: bool,
111    pub env_set: Vec<(String, String)>,
112    pub cwd: Option<String>,
113    pub io: Io,
114    pub timeout_ms: Option<u64>,
115}
116
117/// Programs that switch user: only allowed through `Io::VisiblePty`.
118pub const ESCALATORS: &[&str] = &["sudo", "doas", "pkexec", "su", "run0"];
119
120impl Spawn {
121    /// Refuse privilege escalation on the plugin's private pipes or pty.
122    pub fn check_escalation(&self) -> Result<(), String> {
123        let base = self.program.rsplit(['/', '\\']).next().unwrap_or(&self.program);
124        let base = base.strip_suffix(".exe").unwrap_or(base);
125        if ESCALATORS.contains(&base) && !matches!(self.io, Io::VisiblePty { .. }) {
126            return Err(format!("{base} must run in the visible terminal (EscalationNeedsVisiblePty)"));
127        }
128        Ok(())
129    }
130}
131
132#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
133pub enum Signal {
134    Term,
135    Kill,
136    Int,
137}
138
139/// A host call (`Out::Request`), answered by `In::Response`.
140#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
141pub enum Call {
142    Fs {
143        target: Target,
144        op: Fs,
145    },
146    Spawn {
147        target: Target,
148        spec: Spawn,
149    },
150    Stdin {
151        proc: u64,
152        #[serde(with = "serde_bytes")]
153        data: Vec<u8>,
154        close: bool,
155    },
156    Kill {
157        proc: u64,
158        signal: Signal,
159    },
160    Resize {
161        proc: u64,
162        cols: u16,
163        rows: u16,
164    },
165    /// Run one command elevated: a card every time (no "Always"), then sudo
166    /// in the visible terminal, UAC on Windows, the OS dialog on macOS.
167    Escalate {
168        target: Target,
169        cmdline: String,
170    },
171    /// A host service method by name (`prefs.get`, `agent.send` ...).
172    Service {
173        method: String,
174        params: Value,
175    },
176    /// Host-drawn approval popup; the answer comes as `approval.result`.
177    Approve {
178        title: String,
179        detail: String,
180        options: Vec<String>,
181    },
182    TimerSet {
183        id: String,
184        every_ms: Option<u64>,
185        at_ms: Option<u64>,
186    },
187    TimerClear {
188        id: String,
189    },
190    ClipboardGet,
191    ClipboardSet(String),
192}
193
194/// Paths no plugin reads or writes without the user's own grant.
195pub const DENY_LIST: &[&str] =
196    &["$HOME/.ssh/**", "$HOME/.gnupg/**", "$MOUSER_HOME/secrets/**", "$MOUSER_HOME/config.toml", "$HOME/Library/Keychains/**"];