crabpot 0.10.3

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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
//! `windows-install` and `windows-uninstall`: the one-time, elevated setup of
//! the sandbox user, the WFP fence and the grants.
//!
//! Both run this binary again as srt-win (`--srt-win`), which asks for
//! elevation itself and does the work. The install is upstream's: the same
//! user, the same sublayer and the same setup version, so an install by either
//! tool satisfies the other.

use core::fmt;
use std::ffi::OsString;
use std::io;
use std::path::{Path, PathBuf};

/// What `windows-install` passes on to `srt-win install`.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(crate) struct InstallOptions {
    /// `--sublayer-guid`: the WFP sublayer, when it is not the default.
    pub(crate) sublayer_guid: Option<OsString>,
    /// `--proxy-port-range LOW-HIGH`: the loopback ports left open for the proxy.
    pub(crate) proxy_port_range: Option<OsString>,
    /// `--allow-loopback`: every loopback port open, not the proxy's alone.
    pub(crate) allow_loopback: bool,
    /// `--sandbox-user`: the account's name, when it is not the default.
    pub(crate) sandbox_user: Option<OsString>,
    /// `--force`: replace an install that differs.
    pub(crate) force: bool,
    /// `--profile-exclude <path>`: a path inside the profile the read grant
    /// keeps out, on top of the list srt-win owns. Repeatable, and passed to
    /// `profile grant` as `--exclude`.
    pub(crate) profile_exclude: Vec<OsString>,
    /// `--skip-profile-grant`: make no profile read grant at all.
    pub(crate) skip_profile_grant: bool,
}

/// What `windows-uninstall` passes on to `srt-win uninstall` and `srt-win wfp
/// uninstall`.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(crate) struct UninstallOptions {
    /// `--sublayer-guid`: the WFP sublayer, when it is not the default.
    pub(crate) sublayer_guid: Option<OsString>,
    /// `--keep-user`: leave the account, its credential and the setup marker.
    pub(crate) keep_user: bool,
}

/// Why an install or uninstall did not finish.
// Each platform constructs some of these and not the rest.
#[allow(dead_code)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum InstallError {
    /// The command is for Windows.
    NotWindows,
    /// The path of this binary is unknown, and srt-win needs it to run it again.
    CurrentExe(io::ErrorKind),
    /// The sandbox user cannot read this binary, so seclogon would refuse to
    /// launch it as the runner with a misleading message.
    RunnerUnreadable(PathBuf),
    /// `user status` named no account for this process, so no profile read
    /// grant can be decided for it.
    StatusUnreadable,
    /// srt-win could not be started.
    NotRun(io::ErrorKind),
    /// srt-win's `command` exited with `code`, or was ended with none.
    Exited {
        command: &'static str,
        code: Option<i32>,
    },
}

impl fmt::Display for InstallError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::NotWindows => write!(f, "this command is for Windows"),
            Self::CurrentExe(kind) => write!(f, "cannot find this binary: {kind}"),
            Self::RunnerUnreadable(path) => write!(
                f,
                "the sandbox user cannot read {path}, which it runs as the runner; run \
                 windows-install to make the profile read grant, or move crabpot to a folder \
                 every user can read, such as one under Program Files; a binary moved in from \
                 another folder keeps its old access list, and `icacls \"{path}\" /reset` \
                 re-applies the folder's inherited entries",
                path = path.display()
            ),
            Self::NotRun(kind) => write!(f, "cannot run srt-win: {kind}"),
            Self::StatusUnreadable => write!(
                f,
                "cannot read which account and session this install ran as, so no profile \
                 read grant is decided",
            ),
            Self::Exited {
                command: "install",
                code: Some(10),
            } => write!(f, "the elevation prompt was cancelled"),
            Self::Exited {
                command: "install",
                code: Some(12),
            } => write!(f, "the WFP filters could not be installed"),
            Self::Exited {
                command: "install",
                code: Some(13),
            } => write!(
                f,
                "an install with another port range, loopback mode or sandbox user is \
                 there; pass --force to replace it"
            ),
            Self::Exited {
                command: "install",
                code: Some(14),
            } => write!(f, "the sandbox user could not be provisioned"),
            Self::Exited {
                command,
                code: Some(code),
            } => write!(f, "`srt-win {command}` exited {code}"),
            Self::Exited {
                command,
                code: None,
            } => write!(f, "`srt-win {command}` was ended before it exited"),
        }
    }
}

impl core::error::Error for InstallError {}

/// What an install or an uninstall needs from the machine.
pub(crate) trait Host {
    /// Whether the sandbox user can read this binary, which it runs as the
    /// runner: [`InstallError::RunnerUnreadable`] when it cannot.
    fn check_runner(&self) -> Result<(), InstallError>;
    /// The account this install ran as, and the owner of the session it ran in,
    /// which decide whether a profile read grant is made for it.
    fn grant_ids(&self) -> Result<GrantIds, InstallError>;
    /// The profile directory a grant here would open, `None` when the machine
    /// does not name one.
    fn profile_dir(&self) -> Option<PathBuf>;
    /// Runs this binary as `srt-win <args>`, to the end, and returns its exit
    /// code.
    fn run_srt_win(&mut self, args: &[OsString]) -> Result<Option<i32>, InstallError>;
}

/// The arguments of `srt-win install` for `options`.
pub(crate) fn install_args(options: &InstallOptions) -> Vec<OsString> {
    let mut args = vec![OsString::from("install")];
    if let Some(guid) = &options.sublayer_guid {
        args.extend([OsString::from("--sublayer-guid"), guid.clone()]);
    }
    if let Some(range) = &options.proxy_port_range {
        args.extend([OsString::from("--proxy-port-range"), range.clone()]);
    }
    if options.allow_loopback {
        args.push("--allow-loopback".into());
    }
    if let Some(user) = &options.sandbox_user {
        args.extend([OsString::from("--sandbox-user"), user.clone()]);
    }
    if options.force {
        args.push("--force".into());
    }
    args
}

/// The arguments of `srt-win profile grant` for `options`: the exclusions
/// `--profile-exclude` names, on top of the list srt-win owns.
fn grant_args(options: &InstallOptions) -> Vec<OsString> {
    let mut args = vec![OsString::from("profile"), OsString::from("grant")];
    for path in &options.profile_exclude {
        args.extend([OsString::from("--exclude"), path.clone()]);
    }
    args
}

/// LocalSystem, LocalService, NetworkService: accounts no one runs sessions as.
const WINDOWS_SERVICE_ACCOUNT_SIDS: [&str; 3] = ["S-1-5-18", "S-1-5-19", "S-1-5-20"];

/// Who an install is asked to grant for: the account that ran it, and the owner
/// of the session it ran in. What `user status` reports as `real_user_sid` and
/// `session_user_sid`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct GrantIds {
    pub(crate) real_user_sid: String,
    pub(crate) session_user_sid: Option<String>,
}

/// What every skip reason but the flag's tells the person at the keyboard to do
/// instead.
const GRANT_HOW: &str = "run the install, or `crabpot --srt-win profile grant`, as the user \
                         whose sessions are to read their profile";

/// Why an install leaves the profile read grant unapplied: asked to skip, a
/// service account, or not running as the owner of the interactive session,
/// whose profile it would open instead. Upstream's four reasons, in upstream's
/// order.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum GrantSkip {
    /// `--skip-profile-grant` was given.
    Flag,
    /// The install ran as LocalSystem, LocalService or NetworkService.
    ServiceAccount { real_user_sid: String },
    /// The owner of the session the install ran in could not be read.
    SessionOwnerUnknown { real_user_sid: String },
    /// The install ran in a session that belongs to another account.
    SessionOwnerDiffers {
        real_user_sid: String,
        session_user_sid: String,
    },
}

impl fmt::Display for GrantSkip {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Flag => write!(f, "--skip-profile-grant was set"),
            Self::ServiceAccount { real_user_sid } => write!(
                f,
                "the install ran as the service account {real_user_sid}, which runs no \
                 sessions; {GRANT_HOW}"
            ),
            Self::SessionOwnerUnknown { real_user_sid } => write!(
                f,
                "the owner of the session this install ran in could not be determined, so \
                 it is not known whose profile {real_user_sid} is; {GRANT_HOW}"
            ),
            Self::SessionOwnerDiffers {
                real_user_sid,
                session_user_sid,
            } => write!(
                f,
                "the install ran as {real_user_sid} in a session that belongs to \
                 {session_user_sid}; granting would open the installing account's profile, \
                 not the session owner's; {GRANT_HOW}"
            ),
        }
    }
}

/// Why no profile read grant is made for `options` run as `ids`, if anything.
/// SIDs are compared without case, as upstream compares them.
fn grant_skip_reason(options: &InstallOptions, ids: &GrantIds) -> Option<GrantSkip> {
    if options.skip_profile_grant {
        return Some(GrantSkip::Flag);
    }
    if WINDOWS_SERVICE_ACCOUNT_SIDS.contains(&ids.real_user_sid.as_str()) {
        return Some(GrantSkip::ServiceAccount {
            real_user_sid: ids.real_user_sid.clone(),
        });
    }
    let Some(session_user_sid) = &ids.session_user_sid else {
        return Some(GrantSkip::SessionOwnerUnknown {
            real_user_sid: ids.real_user_sid.clone(),
        });
    };
    if !session_user_sid.eq_ignore_ascii_case(&ids.real_user_sid) {
        return Some(GrantSkip::SessionOwnerDiffers {
            real_user_sid: ids.real_user_sid.clone(),
            session_user_sid: session_user_sid.clone(),
        });
    }
    None
}

/// What the profile read grant is about to do, in one line, for the person at
/// the keyboard. `profile_dir` is the profile it opens, `None` when the machine
/// does not name one.
fn grant_notice(profile_dir: Option<&Path>) -> String {
    let dir = match profile_dir {
        Some(dir) => dir.display().to_string(),
        None => "your profile".to_owned(),
    };
    format!(
        "Making {dir} readable to sandboxed commands on this machine, except the excluded \
         credential stores (SSH and GPG keys, cloud and browser sign-ins, password managers, \
         registry hives). Readable files include package-manager configuration that can hold \
         feed tokens: .npmrc, pip.ini, NuGet.Config, Maven settings.xml, gradle.properties, \
         .gitconfig. Skip with --skip-profile-grant; undo with `crabpot --srt-win profile \
         forget`."
    )
}

/// Installs the sandbox, makes the profile read grant unless one of the skip
/// reasons holds, and then checks that the sandbox user can read this binary,
/// which is what it runs as the runner. The check comes last because the grant
/// is what makes a runner under the profile readable. After a grant the install
/// was asked to skip, an unreadable runner is a notice and not a failure: the
/// install did what it was asked to do, and the repair it names is not the
/// command that was just run.
pub(crate) fn install(host: &mut impl Host, options: &InstallOptions) -> Result<(), InstallError> {
    run(host, "install", &install_args(options))?;
    let ids = host.grant_ids()?;
    let Some(skip) = grant_skip_reason(options, &ids) else {
        eprintln!("crabpot: {}", grant_notice(host.profile_dir().as_deref()));
        run(host, "profile grant", &grant_args(options))?;
        return host.check_runner();
    };
    eprintln!("crabpot: no profile read grant: {skip}");
    match host.check_runner() {
        Ok(()) => Ok(()),
        Err(InstallError::RunnerUnreadable(path)) => {
            eprintln!("crabpot: {}", unreadable_without_grant(&path));
            Ok(())
        }
        Err(error) => Err(error),
    }
}

/// What an install that made no grant says about a runner the sandbox user
/// cannot read.
fn unreadable_without_grant(path: &Path) -> String {
    format!(
        "the sandbox user cannot read {path}, which it runs as the runner; run `crabpot --srt-win \
         profile grant` as the user whose sessions read their profile, or move crabpot to a \
         folder every user can read, such as one under Program Files; a binary moved in from \
         another folder keeps its old access list, and `icacls \"{path}\" /reset` re-applies \
         the folder's inherited entries",
        path = path.display()
    )
}

/// Removes the sandbox: `srt-win uninstall`, then `wfp uninstall` last. The
/// second does not run when the first fails, since the filters are what fence
/// a sandbox user that is still there.
pub(crate) fn uninstall(
    host: &mut impl Host,
    options: &UninstallOptions,
) -> Result<(), InstallError> {
    let guid = |args: &mut Vec<OsString>| {
        if let Some(guid) = &options.sublayer_guid {
            args.extend([OsString::from("--sublayer-guid"), guid.clone()]);
        }
    };
    let mut user = vec![OsString::from("uninstall")];
    guid(&mut user);
    if options.keep_user {
        user.push("--keep-user".into());
    }
    let mut fence = vec![OsString::from("wfp"), OsString::from("uninstall")];
    guid(&mut fence);
    run(host, "uninstall", &user)?;
    run(host, "wfp uninstall", &fence)
}

/// Runs `srt-win <args>` and turns any exit but 0 into an error naming
/// `command`.
fn run(host: &mut impl Host, command: &'static str, args: &[OsString]) -> Result<(), InstallError> {
    match host.run_srt_win(args)? {
        Some(0) => Ok(()),
        code => Err(InstallError::Exited { command, code }),
    }
}

/// The machine this process runs on.
pub(crate) struct Machine;

#[cfg(windows)]
impl Host for Machine {
    fn check_runner(&self) -> Result<(), InstallError> {
        let exe = current_exe()?;
        // The status names the account and the group a grant for the sandbox was
        // made for. A machine that cannot read it names neither, and the check is
        // left with the well-known groups alone.
        let status = super::windows_status().ok();
        if super::runner::Runner::of(exe.clone(), status.as_ref()).readable() {
            Ok(())
        } else {
            Err(InstallError::RunnerUnreadable(exe))
        }
    }

    fn grant_ids(&self) -> Result<GrantIds, InstallError> {
        let status = super::windows_status().map_err(|_| InstallError::StatusUnreadable)?;
        let Some(real_user_sid) = status.real_user_sid else {
            return Err(InstallError::StatusUnreadable);
        };
        Ok(GrantIds {
            real_user_sid,
            session_user_sid: status.session_user_sid,
        })
    }

    fn profile_dir(&self) -> Option<PathBuf> {
        std::env::var_os("USERPROFILE").map(PathBuf::from)
    }

    fn run_srt_win(&mut self, args: &[OsString]) -> Result<Option<i32>, InstallError> {
        let status = std::process::Command::new(current_exe()?)
            .arg(crabpot_windows::SRT_WIN_DISPATCH_ARG1)
            .args(args)
            .status()
            .map_err(|e| InstallError::NotRun(e.kind()))?;
        Ok(status.code())
    }
}

#[cfg(not(windows))]
impl Host for Machine {
    fn check_runner(&self) -> Result<(), InstallError> {
        Err(InstallError::NotWindows)
    }

    fn grant_ids(&self) -> Result<GrantIds, InstallError> {
        Err(InstallError::NotWindows)
    }

    fn profile_dir(&self) -> Option<PathBuf> {
        None
    }

    fn run_srt_win(&mut self, args: &[OsString]) -> Result<Option<i32>, InstallError> {
        let _ = args;
        Err(InstallError::NotWindows)
    }
}

/// This binary's path with links resolved, which is the form the profile's is
/// compared in.
#[cfg(windows)]
fn current_exe() -> Result<PathBuf, InstallError> {
    let exe = std::env::current_exe().map_err(|e| InstallError::CurrentExe(e.kind()))?;
    Ok(std::fs::canonicalize(&exe).unwrap_or(exe))
}

#[cfg(test)]
mod tests {
    use super::*;

    /// A machine that records what srt-win is asked to do and answers with
    /// `exit`.
    struct Fake {
        readable: bool,
        ids: GrantIds,
        profile_dir: Option<PathBuf>,
        exit: Vec<Option<i32>>,
        ran: Vec<Vec<String>>,
    }

    impl Fake {
        fn new() -> Self {
            Self {
                readable: true,
                // An install that ran as the owner of its own session: the case
                // that grants.
                ids: GrantIds {
                    real_user_sid: "S-1-5-21-1-2-3-1001".to_owned(),
                    session_user_sid: Some("S-1-5-21-1-2-3-1001".to_owned()),
                },
                profile_dir: Some(r"C:\Users\me".into()),
                exit: Vec::new(),
                ran: Vec::new(),
            }
        }
    }

    impl Host for Fake {
        fn check_runner(&self) -> Result<(), InstallError> {
            if self.readable {
                Ok(())
            } else {
                Err(InstallError::RunnerUnreadable("crabpot.exe".into()))
            }
        }

        fn grant_ids(&self) -> Result<GrantIds, InstallError> {
            Ok(self.ids.clone())
        }

        fn profile_dir(&self) -> Option<PathBuf> {
            self.profile_dir.clone()
        }

        fn run_srt_win(&mut self, args: &[OsString]) -> Result<Option<i32>, InstallError> {
            self.ran.push(
                args.iter()
                    .map(|arg| arg.to_string_lossy().into_owned())
                    .collect(),
            );
            Ok(if self.exit.is_empty() {
                Some(0)
            } else {
                self.exit.remove(0)
            })
        }
    }

    fn strs(args: &[OsString]) -> Vec<&str> {
        args.iter().map(|arg| arg.to_str().unwrap()).collect()
    }

    #[test]
    fn install_args_pass_through_upstreams_flags() {
        assert_eq!(strs(&install_args(&InstallOptions::default())), ["install"]);

        let all = InstallOptions {
            sublayer_guid: Some("{0F4B1C2D-0000-4000-8000-000000000001}".into()),
            proxy_port_range: Some("61000-61009".into()),
            allow_loopback: true,
            sandbox_user: Some("ci-sandbox".into()),
            force: true,
            // The two grant flags belong to `profile grant`, not to `install`.
            profile_exclude: Vec::new(),
            skip_profile_grant: false,
        };
        assert_eq!(
            strs(&install_args(&all)),
            [
                "install",
                "--sublayer-guid",
                "{0F4B1C2D-0000-4000-8000-000000000001}",
                "--proxy-port-range",
                "61000-61009",
                "--allow-loopback",
                "--sandbox-user",
                "ci-sandbox",
                "--force",
            ]
        );
    }

    #[test]
    fn install_makes_the_profile_grant_after_the_install() {
        let mut host = Fake::new();

        install(&mut host, &InstallOptions::default()).unwrap();

        assert_eq!(host.ran, [vec!["install"], vec!["profile", "grant"]]);
    }

    #[test]
    fn skip_profile_grant_makes_no_grant() {
        let mut host = Fake::new();
        let options = InstallOptions {
            skip_profile_grant: true,
            ..InstallOptions::default()
        };

        install(&mut host, &options).unwrap();

        assert_eq!(host.ran, [vec!["install"]]);
    }

    /// A service account runs no sessions, so a grant it made would open the
    /// profile of an account no one signs in as.
    #[test]
    fn a_service_account_install_makes_no_grant() {
        let mut host = Fake::new();
        host.ids.real_user_sid = "S-1-5-19".to_owned();

        install(&mut host, &InstallOptions::default()).unwrap();

        assert_eq!(host.ran, [vec!["install"]]);
    }

    #[test]
    fn an_install_whose_session_owner_is_unknown_makes_no_grant() {
        let mut host = Fake::new();
        host.ids.session_user_sid = None;

        install(&mut host, &InstallOptions::default()).unwrap();

        assert_eq!(host.ran, [vec!["install"]]);
    }

    /// Granting here would open the installing account's profile, which is not
    /// the one the sessions in that session read.
    #[test]
    fn an_install_in_another_accounts_session_makes_no_grant() {
        let mut host = Fake::new();
        host.ids.session_user_sid = Some("S-1-5-21-1-2-3-1002".to_owned());

        install(&mut host, &InstallOptions::default()).unwrap();

        assert_eq!(host.ran, [vec!["install"]]);
    }

    #[test]
    fn the_two_sids_are_compared_without_case() {
        let mut host = Fake::new();
        host.ids.session_user_sid = Some("s-1-5-21-1-2-3-1001".to_owned());

        install(&mut host, &InstallOptions::default()).unwrap();

        assert_eq!(host.ran, [vec!["install"], vec!["profile", "grant"]]);
    }

    /// Every reason names the account it turned on and what to run instead.
    #[test]
    fn each_skip_reason_names_the_account_and_the_repair() {
        let ids = GrantIds {
            real_user_sid: "S-1-5-19".to_owned(),
            session_user_sid: Some("S-1-5-21-1-2-3-1001".to_owned()),
        };
        let reasons = [
            grant_skip_reason(
                &InstallOptions {
                    skip_profile_grant: true,
                    ..InstallOptions::default()
                },
                &ids,
            )
            .unwrap(),
            grant_skip_reason(&InstallOptions::default(), &ids).unwrap(),
            grant_skip_reason(
                &InstallOptions::default(),
                &GrantIds {
                    real_user_sid: "S-1-5-21-1-2-3-1001".to_owned(),
                    session_user_sid: None,
                },
            )
            .unwrap(),
            grant_skip_reason(
                &InstallOptions::default(),
                &GrantIds {
                    real_user_sid: "S-1-5-21-1-2-3-1001".to_owned(),
                    session_user_sid: Some("S-1-5-21-1-2-3-1002".to_owned()),
                },
            )
            .unwrap(),
        ];
        let said: Vec<String> = reasons.iter().map(|reason| reason.to_string()).collect();

        assert_eq!(said[0], "--skip-profile-grant was set");
        assert!(said[1].starts_with("the install ran as the service account S-1-5-19"));
        assert!(said[2].starts_with("the owner of the session this install ran in could not be"));
        assert!(said[2].contains("whose profile S-1-5-21-1-2-3-1001 is"));
        assert!(said[3].starts_with("the install ran as S-1-5-21-1-2-3-1001 in a session"));
        assert!(said[3].contains("belongs to S-1-5-21-1-2-3-1002"));
        for reason in &said[1..] {
            assert!(reason.ends_with("as the user whose sessions are to read their profile"));
        }
    }

    /// The notice is what the grant is about to do, in the words upstream puts
    /// in front of the person at the keyboard.
    #[test]
    fn the_notice_names_the_profile_and_the_way_back_out() {
        let notice = grant_notice(Some(Path::new(r"C:\Users\me")));

        assert!(notice.starts_with(r"Making C:\Users\me readable to sandboxed commands"));
        assert!(notice.contains("SSH and GPG keys, cloud and browser sign-ins"));
        assert!(notice.contains(".npmrc, pip.ini, NuGet.Config"));
        assert!(notice.contains("Skip with --skip-profile-grant"));
        assert!(notice.contains("undo with `crabpot --srt-win profile forget`"));
        assert!(
            grant_notice(None).starts_with("Making your profile readable to sandboxed commands"),
            "a machine that names no profile still says what is opened"
        );
    }

    #[test]
    fn profile_exclude_passes_through_as_exclude() {
        let mut host = Fake::new();
        let options = InstallOptions {
            profile_exclude: vec![r"C:\Users\me\.aws".into(), ".ssh".into()],
            ..InstallOptions::default()
        };

        install(&mut host, &options).unwrap();

        assert_eq!(
            host.ran,
            [
                vec!["install"],
                vec![
                    "profile",
                    "grant",
                    "--exclude",
                    r"C:\Users\me\.aws",
                    "--exclude",
                    ".ssh"
                ],
            ]
        );
    }

    /// A crabpot under the profile with the grant asked off: the install did what
    /// it was asked to do, so this is a notice and not a failure.
    #[test]
    fn a_skipped_grant_notices_an_unreadable_runner_and_exits_clean() {
        let mut host = Fake::new();
        host.readable = false;
        let options = InstallOptions {
            skip_profile_grant: true,
            ..InstallOptions::default()
        };

        install(&mut host, &options).unwrap();

        assert_eq!(host.ran, [vec!["install"]]);
    }

    /// An install run with `--skip-profile-grant` skips the grant again, so it is
    /// not the repair to name for the runner it left unreadable.
    #[test]
    fn the_repair_after_a_skipped_grant_is_not_the_install_just_run() {
        let said = unreadable_without_grant(Path::new(r"C:\Users\me\.cargo\bin\crabpot.exe"));

        assert!(said.contains(r"C:\Users\me\.cargo\bin\crabpot.exe"));
        assert!(said.contains("profile grant"));
        assert!(
            !said.contains("windows-install"),
            "the named repair loops: {said}"
        );
    }

    #[test]
    fn an_unreadable_runner_is_refused_after_the_grant_that_could_open_it() {
        let mut host = Fake::new();
        host.readable = false;

        let error = install(&mut host, &InstallOptions::default()).unwrap_err();

        assert_eq!(error, InstallError::RunnerUnreadable("crabpot.exe".into()));
        assert_eq!(
            host.ran,
            [vec!["install"], vec!["profile", "grant"]],
            "the install and the grant both ran first: the grant is what makes a runner under the profile readable"
        );
    }

    #[test]
    fn the_notice_after_a_skipped_grant_names_the_reset_for_a_moved_binary() {
        let said = unreadable_without_grant(Path::new("crabpot.exe"));

        assert!(said.contains(r#"icacls "crabpot.exe" /reset"#), "{said}");
    }

    #[test]
    fn an_unreadable_runner_names_its_reset() {
        let said = InstallError::RunnerUnreadable("crabpot.exe".into()).to_string();

        assert!(said.contains(r#"icacls "crabpot.exe" /reset"#), "{said}");
    }

    #[test]
    fn uninstall_runs_wfp_uninstall_last() {
        let mut host = Fake::new();
        let options = UninstallOptions {
            sublayer_guid: Some("{0F4B1C2D-0000-4000-8000-000000000001}".into()),
            keep_user: true,
        };

        uninstall(&mut host, &options).unwrap();

        let guid = "{0F4B1C2D-0000-4000-8000-000000000001}";
        assert_eq!(
            host.ran,
            [
                vec!["uninstall", "--sublayer-guid", guid, "--keep-user"],
                vec!["wfp", "uninstall", "--sublayer-guid", guid],
            ]
        );
    }
}