crabpot 0.2.1

Native Rust sandbox runtime for AI agents, rewritten from Anthropic's sandbox-runtime
Documentation
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//! What a Windows host reads before a command runs: the sandbox user, the WFP
//! fence and the profile grant, from the three srt-win queries upstream's
//! `getWindowsSandboxUserStatus`, `mapWfpStatus` and
//! `checkWindowsSandboxStatus` make.

use core::ops::RangeInclusive;

use serde::Deserialize;

use crate::StartError;

/// What the install says about the sandbox, read without elevation.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct WindowsStatus {
    /// Whether a command can be launched as the sandbox user.
    pub user: UserState,
    /// The sandbox user's SID, when it has an account.
    pub sandbox_sid: Option<String>,
    /// The SID of the group the sandbox user is in, when it has one. A grant
    /// made for the sandbox is made for this group or for the account above.
    pub sandbox_group_sid: Option<String>,
    /// The SID of the account this process runs as, when the machine names it.
    pub real_user_sid: Option<String>,
    /// The SID of the user who owns the session this process runs in, when the
    /// machine names one.
    pub session_user_sid: Option<String>,
    /// The install's setup version beside this build's.
    pub setup_version: SetupVersion,
    /// Whether the WFP fence leaves loopback open.
    pub loopback: LoopbackMode,
    /// The loopback ports the fence leaves open for the proxy, when it has
    /// filters to read them from.
    pub proxy_ports: Option<RangeInclusive<u16>>,
    /// Whether a profile read grant is in place.
    pub profile: ProfileState,
    /// What the profile read grant keeps unreadable, empty when there is none.
    pub profile_exclusions: ProfileExclusions,
    /// Whether the Secondary Logon service can launch the sandbox user. False
    /// when it is disabled or missing, and true when it could not be read,
    /// which is not proof of a problem.
    pub seclogon_runs: bool,
    /// Whether this process runs as the user who owns its session.
    pub in_desktop_session: bool,
}

/// Whether a command can be launched as the sandbox user.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum UserState {
    /// The user exists, its credential is stored, and the install's setup
    /// version is this build's.
    Provisioned,
    /// Any of those is missing. A setup version mismatch reads as this, since
    /// srt-win refuses a launch for it the way it does for no install.
    NotProvisioned,
}

/// The setup version of the install and of this build.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct SetupVersion {
    /// What the install recorded, or `None` when there is no install.
    pub installed: Option<u32>,
    /// What this build needs.
    pub expected: u32,
}

/// Whether the sandbox user may connect to loopback.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum LoopbackMode {
    /// Every loopback port is open.
    Open,
    /// Only the proxy's ports are open.
    Closed,
}

/// The paths a profile read grant keeps out of a session's grants, as
/// `profile status` records them.
#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize)]
pub struct ProfileExclusions {
    /// Every protected path the grant keeps unreadable, wildcards as written. A
    /// session may not grant one, a path inside one, or a path above one.
    #[serde(default)]
    pub exclusions: Vec<String>,
    /// Directories the grant leaves ungranted without denying them. A session
    /// may not grant one, or a path above one.
    #[serde(default)]
    pub ungranted_only: Vec<String>,
}

/// Whether the profile read grant is in place, as `profile status` reports it.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ProfileState {
    /// No grant.
    None,
    /// A grant a session may rely on.
    Current,
    /// A grant an older list made.
    Outdated,
    /// A grant that did not finish.
    Incomplete,
    /// A grant or forget is running.
    InProgress,
    /// The record could not be read, or says what this build does not know.
    Unknown,
}

impl WindowsStatus {
    /// The account and the group a sandbox command runs as, which is who a grant
    /// made for the sandbox names. A machine with no install names neither, and
    /// a check left with nothing but the well-known groups.
    #[must_use]
    pub fn sandbox_sids(&self) -> Vec<&str> {
        let mut sids = Vec::new();
        sids.extend(self.sandbox_sid.as_deref());
        sids.extend(self.sandbox_group_sid.as_deref());
        sids
    }
}

/// The error a srt-win query that exits with `code` stands for, when it is one
/// of the exits a host acts on: 15 for no usable install, 18 for seclogon.
pub fn refusal_for_exit(code: i32) -> Option<StartError> {
    match code {
        15 => Some(StartError::NotProvisioned),
        18 => Some(StartError::SeclogonDisabled),
        _ => None,
    }
}

/// `user status`, as far as a status reads it.
#[derive(Deserialize)]
struct UserJson {
    user: UserAccount,
    cred_present: bool,
    marker_version: Option<u32>,
    setup_version: u32,
    real_user_sid: Option<String>,
    session_user_sid: Option<String>,
    seclogon: String,
}

#[derive(Deserialize)]
struct UserAccount {
    exists: bool,
    sid: Option<String>,
    group_sid: Option<String>,
}

/// `wfp status`, as far as a status reads it.
#[derive(Deserialize)]
struct WfpJson {
    port_range: Option<[u16; 2]>,
    allow_loopback: bool,
}

/// `profile status`, as far as a status reads it.
#[derive(Deserialize)]
struct ProfileJson {
    state: String,
    #[serde(flatten)]
    exclusions: ProfileExclusions,
}

/// Reads the status through `run`, which runs one srt-win query with the
/// arguments after it and returns what it printed. `sublayer` is the WFP
/// sublayer the filters were installed under, when it is not the default.
pub(crate) fn read(
    sublayer: Option<&str>,
    run: impl Fn(&'static str, &[&str]) -> Result<String, StartError>,
) -> Result<WindowsStatus, StartError> {
    let user: UserJson = parse("user status", &[], &run)?;
    let wfp_args: Vec<&str> = sublayer
        .into_iter()
        .flat_map(|guid| ["--sublayer-guid", guid])
        .collect();
    let wfp: WfpJson = parse("wfp status", &wfp_args, &run)?;
    let profile: ProfileJson = parse("profile status", &[], &run)?;
    let provisioned =
        user.user.exists && user.cred_present && user.marker_version == Some(user.setup_version);
    // A session without an owner is session 0, which is refused by name before
    // the status is read.
    let in_desktop_session =
        user.session_user_sid.is_some() && user.session_user_sid == user.real_user_sid;
    Ok(WindowsStatus {
        user: if provisioned {
            UserState::Provisioned
        } else {
            UserState::NotProvisioned
        },
        sandbox_sid: user.user.sid,
        sandbox_group_sid: user.user.group_sid,
        real_user_sid: user.real_user_sid,
        session_user_sid: user.session_user_sid,
        setup_version: SetupVersion {
            installed: user.marker_version,
            expected: user.setup_version,
        },
        loopback: if wfp.allow_loopback {
            LoopbackMode::Open
        } else {
            LoopbackMode::Closed
        },
        proxy_ports: wfp.port_range.map(|[low, high]| low..=high),
        profile: match profile.state.as_str() {
            "none" => ProfileState::None,
            "current" => ProfileState::Current,
            "outdated" => ProfileState::Outdated,
            "incomplete" => ProfileState::Incomplete,
            "in_progress" => ProfileState::InProgress,
            _ => ProfileState::Unknown,
        },
        profile_exclusions: profile.exclusions,
        seclogon_runs: !matches!(user.seclogon.as_str(), "disabled" | "missing"),
        in_desktop_session,
    })
}

/// Runs `command` through `run` and reads what it printed as `T`.
fn parse<T: for<'de> Deserialize<'de>>(
    command: &'static str,
    args: &[&str],
    run: &impl Fn(&'static str, &[&str]) -> Result<String, StartError>,
) -> Result<T, StartError> {
    serde_json::from_str(&run(command, args)?).map_err(|_| StartError::StatusFailed {
        command,
        code: None,
    })
}

#[cfg(test)]
mod tests {
    use core::cell::RefCell;

    use super::*;

    const PROVISIONED: &str = r#"{
        "user": {"name": "crabpot-sandbox", "exists": true, "sid": "S-1-5-21-1",
                 "group_exists": true, "group_sid": "S-1-5-21-9",
                 "in_builtin_users": true,
                 "in_sandbox_group": true, "hidden_from_logon": true},
        "cred_present": true, "marker_version": 3, "setup_version": 3,
        "real_user_sid": "S-1-5-21-2", "session_user_sid": "S-1-5-21-2",
        "profile_grant": {"state": "present"}, "seclogon": "ok"
    }"#;
    const WFP: &str =
        r#"{"state":"installed","filters":4,"port_range":[3128,3130],"allow_loopback":false}"#;
    const PROFILE: &str = r#"{"state":"current","record_version":1}"#;
    /// An account with no group beside it, which is what a machine whose sandbox
    /// group is missing reads as.
    const NO_GROUP: &str = r#"{
        "user": {"name": "crabpot-sandbox", "exists": true, "sid": "S-1-5-21-1",
                 "group_exists": false, "group_sid": null,
                 "in_builtin_users": true,
                 "in_sandbox_group": false, "hidden_from_logon": true},
        "cred_present": true, "marker_version": 3, "setup_version": 3,
        "real_user_sid": "S-1-5-21-2", "session_user_sid": "S-1-5-21-2",
        "profile_grant": {"state": "present"}, "seclogon": "ok"
    }"#;

    /// A srt-win whose three queries print what it is given.
    fn fake(user: &str, wfp: &str) -> impl Fn(&'static str, &[&str]) -> Result<String, StartError> {
        let (user, wfp) = (user.to_owned(), wfp.to_owned());
        move |command, _| match command {
            "user status" => Ok(user.clone()),
            "wfp status" => Ok(wfp.clone()),
            "profile status" => Ok(PROFILE.to_owned()),
            other => panic!("unexpected query {other}"),
        }
    }

    #[test]
    fn user_status_json_maps_to_the_state() {
        let status = read(None, fake(PROVISIONED, WFP)).unwrap();

        assert_eq!(status.user, UserState::Provisioned);
        assert_eq!(status.sandbox_sid.as_deref(), Some("S-1-5-21-1"));
        assert_eq!(status.sandbox_group_sid.as_deref(), Some("S-1-5-21-9"));
        assert_eq!(status.sandbox_sids(), ["S-1-5-21-1", "S-1-5-21-9"]);
        assert_eq!(status.real_user_sid.as_deref(), Some("S-1-5-21-2"));
        assert_eq!(status.session_user_sid.as_deref(), Some("S-1-5-21-2"));
        assert_eq!(
            status.setup_version,
            SetupVersion {
                installed: Some(3),
                expected: 3
            }
        );
        assert_eq!(status.loopback, LoopbackMode::Closed);
        assert_eq!(status.profile, ProfileState::Current);
        assert!(status.seclogon_runs);
        assert!(status.in_desktop_session);
    }

    /// A grant made for the sandbox is made for its account or for the group it
    /// is in, and a machine missing one of the two is asked about the other.
    #[test]
    fn the_sandbox_sids_are_who_a_grant_for_the_sandbox_names() {
        let both = read(None, fake(PROVISIONED, WFP)).unwrap();
        assert_eq!(both.sandbox_sids(), ["S-1-5-21-1", "S-1-5-21-9"]);

        let no_group = read(None, fake(NO_GROUP, WFP)).unwrap();
        assert_eq!(no_group.sandbox_sids(), ["S-1-5-21-1"]);
    }

    #[test]
    fn setup_version_mismatch_is_reported_not_provisioned() {
        let older = PROVISIONED.replace(r#""marker_version": 3"#, r#""marker_version": 2"#);

        let status = read(None, fake(&older, WFP)).unwrap();

        assert_eq!(status.user, UserState::NotProvisioned);
        assert_eq!(
            status.setup_version,
            SetupVersion {
                installed: Some(2),
                expected: 3
            }
        );
    }

    #[test]
    fn no_install_is_not_provisioned() {
        let none = r#"{"user": {"exists": false}, "cred_present": false,
            "marker_version": null, "setup_version": 3, "real_user_sid": "S-1-5-21-2",
            "session_user_sid": "S-1-5-21-2", "profile_grant": {"state": "none"},
            "seclogon": "disabled"}"#;
        let absent = r#"{"state":"absent","filters":0,"allow_loopback":true}"#;

        let status = read(None, fake(none, absent)).unwrap();

        assert_eq!(status.user, UserState::NotProvisioned);
        assert_eq!(status.setup_version.installed, None);
        assert_eq!(status.proxy_ports, None);
        assert_eq!(status.loopback, LoopbackMode::Open);
        assert!(!status.seclogon_runs);
    }

    #[test]
    fn the_profile_exclusions_come_from_profile_status() {
        let profile = r#"{"state":"current","exclusions":["C:\\Users\\me\\.ssh"],
            "ungranted_only":["C:\\Users\\me\\AppData\\Local\\Temp"]}"#;

        let status = read(None, |command, _| match command {
            "user status" => Ok(PROVISIONED.to_owned()),
            "wfp status" => Ok(WFP.to_owned()),
            _ => Ok(profile.to_owned()),
        })
        .unwrap();

        assert_eq!(
            status.profile_exclusions,
            ProfileExclusions {
                exclusions: vec![r"C:\Users\me\.ssh".to_owned()],
                ungranted_only: vec![r"C:\Users\me\AppData\Local\Temp".to_owned()],
            }
        );
    }

    #[test]
    fn exit_codes_15_and_18_map_to_their_errors() {
        assert_eq!(refusal_for_exit(15), Some(StartError::NotProvisioned));
        assert_eq!(refusal_for_exit(18), Some(StartError::SeclogonDisabled));
        assert_eq!(refusal_for_exit(0), None);
        assert_eq!(refusal_for_exit(1), None);
    }

    #[test]
    fn the_port_range_comes_from_wfp_status() {
        let status = read(None, fake(PROVISIONED, WFP)).unwrap();

        assert_eq!(status.proxy_ports, Some(3128..=3130));
    }

    #[test]
    fn a_user_in_another_users_session_is_not_in_the_desktop_session() {
        let runas = PROVISIONED.replace(
            r#""session_user_sid": "S-1-5-21-2""#,
            r#""session_user_sid": "S-1-5-21-9""#,
        );

        let status = read(None, fake(&runas, WFP)).unwrap();

        assert!(!status.in_desktop_session);
    }

    #[test]
    fn the_sublayer_reaches_wfp_status_alone() {
        let asked = RefCell::new(Vec::new());
        let guid = "0f4b1c2d-0000-4000-8000-000000000001";

        read(Some(guid), |command, args| {
            asked.borrow_mut().push((command, args.join(" ")));
            fake(PROVISIONED, WFP)(command, args)
        })
        .unwrap();

        assert_eq!(
            asked.into_inner(),
            [
                ("user status", String::new()),
                ("wfp status", format!("--sublayer-guid {guid}")),
                ("profile status", String::new()),
            ]
        );
    }

    #[test]
    fn output_that_is_not_json_is_a_failed_query() {
        let error = read(None, fake("not json", WFP)).unwrap_err();

        assert_eq!(
            error,
            StartError::StatusFailed {
                command: "user status",
                code: None
            }
        );
    }
}