aither-sandbox 0.31.0

Terminal-first command execution with heel sandboxing
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
//! The spawn layer: start a process under a sandbox.
//!
//! A [`Sandbox`] is one per home environment. It holds the live [`Grants`],
//! the environment, the network lists and their [`DomainApprover`], and a
//! [`Backend`] saying where processes run. [`Sandbox::spawn`] starts a
//! [`SpawnSpec`] on that backend and answers a [`SandboxedChild`] whose
//! [`kill`](SandboxedChild::kill) ends the whole process tree.
//!
//! What the sandbox enforces per backend:
//!
//! - [`Backend::Host`]: heel's sandbox, with the grants, the environment, the
//!   write and read denylists, the process priority and the network lists
//!   with their approver, whose answer is enforced per connection.
//! - [`Backend::Container`]: the process runs in the container, which is its
//!   boundary. A [derived](Sandbox::derived) sandbox, a child's, is
//!   additionally confined inside the container to its own writable grants.
//!   The environment reaches it as exports of the script.
//! - [`Backend::Ssh`]: the process runs on the server under the server's own
//!   `heel`, with the tools the embedder prepared there. Grants and the
//!   host-side environment never reach another machine.

mod backend;
mod child;
mod container;
pub mod container_ipc;
mod domain;
mod env;
mod grants;
pub mod host;
mod ipc;
mod launch;
mod route;
mod ssh;
pub mod stdio;
#[cfg(all(test, unix))]
mod tests;

use std::path::{Path, PathBuf};
use std::sync::Arc;

use askama::Template;
use executor_core::{AnyExecutor, Executor};

pub use backend::Backend;
pub use child::{ChildExit, Descendants, SandboxedChild};
pub use domain::{
    AnyDomainApprover, ApproveAllDomains, DenyAllDomains, DomainApprover, DomainRequest,
};
pub use env::{EnvSource, LaunchInfo};
pub use grants::{GitDirAccess, GrantSnapshot, Grants};
#[cfg(all(unix, feature = "jobserver"))]
pub use host::resolved;
pub use host::{LocalSpawn, TreeLifetime};
#[cfg(test)]
pub use ipc::fixture as ipc_fixture;
pub use ipc::{AnyIpcProvider, IpcProvider};
#[cfg(not(windows))]
pub use launch::shell_arguments;
pub use launch::{ContainerLaunch, HostLaunch, RemoteLaunch};
pub use route::{HostRoute, HostRouteError};

use crate::network_policy::CompiledNetworkPolicy;
use crate::process_tree::ProcessScanner;

/// Why a process could not be spawned.
#[derive(Debug, thiserror::Error)]
pub enum SpawnError {
    /// The sandbox, or the backend it runs on, could not be set up: a grant
    /// cannot be resolved, the heel binary is unusable, the server has no
    /// `heel`, a path has no counterpart in the container.
    #[error("sandbox setup failed: {0}")]
    Setup(String),
    /// The process could not be started.
    #[error("failed to start the process: {0}")]
    Start(String),
    /// The spec asks for something the backend cannot honour, and the
    /// process is not started without it.
    #[error("the {backend} backend cannot honour {what}")]
    Unsupported {
        /// The backend that refused.
        backend: &'static str,
        /// What the spec asked for.
        what: &'static str,
    },
}

/// Whether a standard stream of a spawned process is piped to its handle.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum Pipe {
    /// Connected to the null device: no input, or output dropped. A stdin of
    /// the null device is the same on every backend, so a program that picks
    /// its input by what its stdin is — `rg` searching the directory rather
    /// than a pipe — behaves alike on all of them.
    #[default]
    Null,
    /// Connected to the [`SandboxedChild`]'s channel for the stream.
    Piped,
}

/// The standard streams of a spawned process. The default pipes `stdout` and
/// `stderr` and gives `stdin` nothing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Stdio {
    /// The process's standard input.
    pub stdin: Pipe,
    /// The process's standard output.
    pub stdout: Pipe,
    /// The process's standard error.
    pub stderr: Pipe,
}

impl Default for Stdio {
    fn default() -> Self {
        Self {
            stdin: Pipe::Null,
            stdout: Pipe::Piped,
            stderr: Pipe::Piped,
        }
    }
}

#[derive(Template)]
#[template(path = "null_stdin.sh", escape = "none")]
struct NullStdinTemplate<'a> {
    script: &'a str,
}

/// `script` for a backend whose processes always start on a stdin pipe —
/// a container exec's attached stream, an SSH session's channel — reading
/// `stdin`: a script whose stdin is [`Pipe::Null`] reads the null device, as
/// a process on the host does.
///
/// # Errors
/// Fails when the redirect cannot render.
pub fn script_reading(stdin: Pipe, script: String) -> Result<String, SpawnError> {
    match stdin {
        Pipe::Piped => Ok(script),
        Pipe::Null => NullStdinTemplate { script: &script }
            .render()
            .map_err(|error| SpawnError::Setup(error.to_string())),
    }
}

/// What a spawned process runs.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SpawnCommand {
    /// A program with its arguments.
    Program {
        /// The program to run.
        program: String,
        /// Its arguments.
        args: Vec<String>,
    },
    /// A script for the backend's managed shell, which only the container
    /// and SSH backends have: a host sandbox names no shell, so spawning a
    /// script on it fails with [`SpawnError::Unsupported`]. A caller that
    /// wants a script on the host spawns its shell as a program, or runs it
    /// through a [`Shell`](crate::Shell).
    ///
    /// A script is one level of quoting for a server's login shell, which
    /// parses the command line first; a program that runs the shell would
    /// need two.
    Script(String),
}

/// An access a process is granted beyond the sandbox's own grants, named
/// where the backend that honours it sees the path.
#[derive(Debug, Clone)]
pub enum SpawnGrant {
    /// A resolved path on the host; only the host backend honours it.
    Host(heel::Grant),
    /// A path inside the container; only the container backend honours it.
    /// It widens only a [derived](Sandbox::derived) sandbox's confinement:
    /// any other process already has the whole container.
    Container {
        /// The path, as the container spells it.
        path: crate::ContainerPath,
        /// What the process may do with it.
        access: heel::Access,
    },
}

/// A process to start under a [`Sandbox`].
pub struct SpawnSpec {
    pub(crate) command: SpawnCommand,
    pub(crate) env: Vec<(String, String)>,
    pub(crate) cwd: Option<PathBuf>,
    pub(crate) stdio: Stdio,
    /// Grants this one process needs beyond the sandbox's (a command's
    /// jobserver fifo).
    pub(crate) grants: Vec<SpawnGrant>,
    /// The commands the process may call back into the host with.
    pub(crate) ipc: Option<heel::IpcRouter>,
    /// Whether the backend ends the processes the top-level one leaves.
    pub(crate) lifetime: TreeLifetime,
    /// What the launch is, for the sandbox's [`EnvSource`] to read.
    pub(crate) info: LaunchInfo,
}

impl std::fmt::Debug for SpawnSpec {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SpawnSpec")
            .field("command", &self.command)
            .field("cwd", &self.cwd)
            .field("stdio", &self.stdio)
            .finish_non_exhaustive()
    }
}

impl SpawnSpec {
    /// A process running `program` with no arguments, in the sandbox's
    /// working directory, with the default [`Stdio`].
    #[must_use]
    pub fn new(program: impl Into<String>) -> Self {
        Self::of(SpawnCommand::Program {
            program: program.into(),
            args: Vec::new(),
        })
    }

    /// A process running `source` in the managed shell the backend was given
    /// the programs of, with the default [`Stdio`]. Only the container and
    /// SSH backends have a managed shell to run it in: a spawn on the host
    /// names a program.
    #[must_use]
    pub fn script(source: impl Into<String>) -> Self {
        Self::of(SpawnCommand::Script(source.into()))
    }

    fn of(command: SpawnCommand) -> Self {
        Self {
            command,
            env: Vec::new(),
            cwd: None,
            stdio: Stdio::default(),
            grants: Vec::new(),
            ipc: None,
            lifetime: TreeLifetime::Background,
            info: LaunchInfo::default(),
        }
    }

    /// Says what this launch is, for the sandbox's [`EnvSource`] to read:
    /// the job, the call and the mode it runs in.
    #[must_use]
    pub fn launch_info(mut self, info: LaunchInfo) -> Self {
        self.info = info;
        self
    }

    /// Appends arguments.
    ///
    /// # Panics
    /// Panics on a script, which takes none.
    #[must_use]
    pub fn args(mut self, more: impl IntoIterator<Item = impl Into<String>>) -> Self {
        let SpawnCommand::Program { args, .. } = &mut self.command else {
            panic!("a script takes no arguments");
        };
        args.extend(more.into_iter().map(Into::into));
        self
    }

    /// Adds environment for this process alone; it overrides the sandbox's
    /// environment and its [`EnvSource`].
    #[must_use]
    pub fn env(mut self, env: impl IntoIterator<Item = (String, String)>) -> Self {
        self.env.extend(env);
        self
    }

    /// Grants this process alone beyond the sandbox's. A grant is named
    /// where the backend that honours it sees the path: a backend refuses
    /// the [`SpawnGrant`] it cannot honour, and a server honours none.
    #[must_use]
    pub fn grants(mut self, grants: impl IntoIterator<Item = SpawnGrant>) -> Self {
        self.grants.extend(grants);
        self
    }

    /// Lets the process call the commands of `router` back on the host. The
    /// host serves it in process; a container is reached over a listener at
    /// its [host route](crate::ContainerShellRuntime::host_route); a server
    /// has no way back, so its backend refuses it.
    #[must_use]
    pub fn ipc(mut self, router: Option<heel::IpcRouter>) -> Self {
        self.ipc = router;
        self
    }

    /// Sets how long the process's descendants may outlive it. The default
    /// is [`TreeLifetime::Background`]: the child lasts until its whole tree
    /// has exited, and its piped stdin is watched
    /// ([`SandboxedChild::take_stdin_blocked`]); a container child's watch
    /// runs, and costs a probe in the container, even when stdin is not
    /// piped. SSH keeps the default for either value, as it cannot see the
    /// tree.
    #[must_use]
    pub const fn lifetime(mut self, lifetime: TreeLifetime) -> Self {
        self.lifetime = lifetime;
        self
    }

    /// Starts the process in `cwd`, a path on the host the sandbox runs on
    /// (for a container, one under a [mount](crate::ContainerShellRuntime::with_workspace_mount)).
    #[must_use]
    pub fn cwd(mut self, cwd: impl Into<PathBuf>) -> Self {
        self.cwd = Some(cwd.into());
        self
    }

    /// Sets the standard streams.
    #[must_use]
    pub const fn stdio(mut self, stdio: Stdio) -> Self {
        self.stdio = stdio;
        self
    }
}

#[derive(Clone)]
struct SandboxInner {
    backend: Backend,
    executor: Arc<AnyExecutor>,
    working_dir: PathBuf,
    grants: Grants,
    fixed_grants: Vec<heel::Grant>,
    environment: Vec<(String, String)>,
    env_source: Option<Arc<dyn EnvSource>>,
    /// Variables naming directories inside a subagent's own space, by their
    /// host paths: the layer between the source and a spec's own variables.
    space_environment: Vec<(String, PathBuf)>,
    write_denylist: Option<heel::WriteDenylist>,
    network: Option<CompiledNetworkPolicy>,
    approver: Option<AnyDomainApprover>,
    audit: Option<heel::NetworkAuditLog>,
    priority: heel::ProcessPriority,
    agent: Option<Arc<str>>,
    scanner: ProcessScanner,
    unconfined: bool,
    ssh_client: crate::ssh::SshClient,
}

/// One home environment's sandbox: grants, environment, network policy and
/// the [`Backend`] processes run on. Cheap to clone; clones share the live
/// grants.
#[derive(Clone)]
pub struct Sandbox {
    inner: Arc<SandboxInner>,
}

impl std::fmt::Debug for Sandbox {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Sandbox")
            .field("working_dir", &self.inner.working_dir)
            .field("agent", &self.inner.agent)
            .finish_non_exhaustive()
    }
}

impl Sandbox {
    /// A sandbox on `backend` whose processes start in `working_dir`, with no
    /// grants, no environment of its own and no network lists: every domain
    /// is open to the approver, which denies it until
    /// [`with_domain_approver`](Self::with_domain_approver) gives one.
    ///
    /// `executor` runs the tasks that watch spawned processes and heel's
    /// proxy.
    #[must_use]
    pub fn new(
        backend: Backend,
        executor: impl Executor + 'static,
        working_dir: impl Into<PathBuf>,
    ) -> Self {
        Self {
            inner: Arc::new(SandboxInner {
                backend,
                executor: Arc::new(AnyExecutor::new(executor)),
                working_dir: working_dir.into(),
                grants: Grants::new(),
                fixed_grants: Vec::new(),
                environment: Vec::new(),
                env_source: None,
                space_environment: Vec::new(),
                write_denylist: None,
                network: None,
                approver: None,
                audit: None,
                priority: heel::ProcessPriority::default(),
                agent: None,
                scanner: ProcessScanner::default(),
                unconfined: false,
                ssh_client: crate::ssh::SshClient::default(),
            }),
        }
    }

    fn edit(mut self, edit: impl FnOnce(&mut SandboxInner)) -> Self {
        edit(Arc::make_mut(&mut self.inner));
        self
    }

    /// Uses `grants` as the sandbox's live grants; the sandbox sees every
    /// change made through any clone of the handle.
    #[must_use]
    pub fn with_grants(self, grants: Grants) -> Self {
        self.edit(|inner| inner.grants = grants)
    }

    /// Heel grants every spawn on the host carries ahead of the live
    /// [`Grants`]: the directories the embedder's tools and toolchains live
    /// in, and what its `PATH` lists.
    #[must_use]
    pub fn with_fixed_grants(self, grants: Vec<heel::Grant>) -> Self {
        self.edit(|inner| inner.fixed_grants = grants)
    }

    /// The environment every spawn on the host starts with: the session's
    /// `HOME`, temporary and cache directories, `PATH` and toolchain
    /// variables. It never reaches a container or an SSH server, whose
    /// filesystems differ.
    #[must_use]
    pub fn with_environment(self, environment: Vec<(String, String)>) -> Self {
        self.edit(|inner| inner.environment = environment)
    }

    /// Environment resolved at each spawn, on the host and in a container;
    /// never on an SSH server, so host-side values such as secrets never
    /// reach another machine.
    #[must_use]
    pub fn with_env_source(self, source: impl EnvSource + 'static) -> Self {
        self.edit(|inner| inner.env_source = Some(Arc::new(source)))
    }

    /// The variables of a subagent's space: each names a directory inside
    /// it, by its host path. They override the environment and its source
    /// and yield to a spec's own variables; a container sees each directory
    /// through its workspace mount, and an SSH server none.
    pub(crate) fn with_space_environment(self, environment: Vec<(String, PathBuf)>) -> Self {
        self.edit(|inner| inner.space_environment = environment)
    }

    /// The paths a spawn on the host must not write, however they are
    /// granted. Not supported on Windows, whose backend refuses a denylist
    /// rather than run weaker, so it is not applied there.
    #[must_use]
    pub fn with_write_denylist(self, denylist: heel::WriteDenylist) -> Self {
        self.edit(|inner| inner.write_denylist = Some(denylist))
    }

    /// The network lists: a host they allow or deny is decided without the
    /// approver.
    #[must_use]
    pub fn with_network_policy(self, policy: CompiledNetworkPolicy) -> Self {
        self.edit(|inner| inner.network = Some(policy))
    }

    /// Decides the connections the network lists leave open.
    #[must_use]
    pub fn with_domain_approver(self, approver: impl DomainApprover + 'static) -> Self {
        self.edit(|inner| inner.approver = Some(AnyDomainApprover::new(approver)))
    }

    /// Records every network decision of a spawn on the host in `log`.
    #[must_use]
    pub fn with_audit_log(self, log: heel::NetworkAuditLog) -> Self {
        self.edit(|inner| inner.audit = Some(log))
    }

    /// Lowers the scheduling priority of what the sandbox spawns on the host,
    /// inherited by everything it starts. A container's image needs its own
    /// `heel` for a non-normal priority; an SSH server's priority is its own.
    #[must_use]
    pub fn with_priority(self, priority: heel::ProcessPriority) -> Self {
        self.edit(|inner| inner.priority = priority)
    }

    /// Labels the agent this sandbox serves; the label is the `agent` of
    /// every [`DomainRequest`].
    #[must_use]
    pub fn with_agent(self, agent: impl Into<String>) -> Self {
        let agent: Arc<str> = Arc::from(agent.into());
        self.edit(|inner| inner.agent = Some(agent))
    }

    /// A child's sandbox: the same backend, environment and network policy,
    /// grants [derived](Grants::derived) from this sandbox's with `own`, and
    /// no agent label until [`with_agent`](Self::with_agent) gives one. It
    /// cannot widen this sandbox: its grants never reach it, and a container
    /// confines its processes to its own writable grants. The variables of
    /// this sandbox's space are not the child's, which has a space of its
    /// own.
    #[must_use]
    pub fn derived(&self, own: GrantSnapshot) -> Self {
        let grants = self.inner.grants.derived(own);
        self.clone().edit(|inner| {
            inner.grants = grants;
            inner.agent = None;
            inner.space_environment = Vec::new();
        })
    }

    /// The sandbox's live grants.
    #[must_use]
    pub fn grants(&self) -> &Grants {
        &self.inner.grants
    }

    /// The directory processes start in unless their spec says otherwise.
    #[must_use]
    pub fn working_dir(&self) -> &Path {
        &self.inner.working_dir
    }

    /// The same sandbox starting its processes in `working_dir`.
    #[must_use]
    pub fn with_working_dir(self, working_dir: impl Into<PathBuf>) -> Self {
        self.edit(|inner| inner.working_dir = working_dir.into())
    }

    /// The same sandbox on `backend`: the grants, environment and network
    /// policy are kept.
    #[must_use]
    pub fn with_backend(self, backend: Backend) -> Self {
        self.edit(|inner| inner.backend = backend)
    }

    /// The `heel` binary of a sandbox on the host.
    pub(crate) fn host_heel(&self) -> Option<&heel::HeelBinary> {
        match &self.inner.backend {
            Backend::Host { heel } => Some(heel),
            Backend::Container { .. } | Backend::Ssh { .. } => None,
        }
    }

    /// The executor the sandbox's tasks run on.
    pub(crate) fn executor(&self) -> Arc<AnyExecutor> {
        Arc::clone(&self.inner.executor)
    }

    /// Where the sandbox's processes run.
    pub(crate) fn backend(&self) -> &Backend {
        &self.inner.backend
    }

    /// The scheduling priority of the sandbox's processes.
    pub(crate) fn priority(&self) -> heel::ProcessPriority {
        self.inner.priority
    }

    /// The same sandbox without confinement: a host spawn takes host access,
    /// ignoring the grants and denylists and the network lists.
    pub(crate) fn unconfined(&self) -> Self {
        self.clone().edit(|inner| inner.unconfined = true)
    }

    /// Reads process tables through `scanner`, shared with the embedder's
    /// job registry.
    pub(crate) fn with_scanner(self, scanner: ProcessScanner) -> Self {
        self.edit(|inner| inner.scanner = scanner)
    }

    /// Reaches SSH servers through `client` instead of the default one.
    pub(crate) fn with_ssh_client(self, client: crate::ssh::SshClient) -> Self {
        self.edit(|inner| inner.ssh_client = client)
    }

    /// Starts `spec` on the sandbox's backend, under its grants and network
    /// policy as they are now: a change made to the live grants applies to
    /// the next spawn, never to a process already running.
    ///
    /// # Errors
    /// Returns an error when the sandbox cannot be set up for the spawn or
    /// the process cannot be started.
    pub async fn spawn(&self, spec: SpawnSpec) -> Result<SandboxedChild, SpawnError> {
        match &self.inner.backend {
            Backend::Host { .. } => self.spawn_host(spec).await,
            Backend::Container { runtime, launch } => self.spawn_container(runtime, launch, spec),
            Backend::Ssh {
                server,
                heel,
                launch,
            } => self.spawn_ssh(server, heel.as_deref(), launch, spec).await,
        }
    }
}