aither-sandbox 0.20.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
//! The `CPython` that the pinned `uv` installs into a prepared tool tree.
//!
//! A prepared tree carries the interpreter beside the tools, under
//! `bin/.python/<request>/`, and exposes it as the relative link
//! `bin/python3`. Keeping the tree inside `bin` means the one executable
//! grant every backend already gives the tools directory covers the whole
//! interpreter. The tree is installed once, by the verified `uv` outside any
//! sandbox, and is read-only afterwards; the shared writable state `uv` needs
//! at run time lives elsewhere (`PreparedTools::uv_dir`).

use std::path::{Component, Path, PathBuf};

use serde::{Deserialize, Serialize};

use crate::tool_runtime::{ToolRuntimeError, ToolTarget, digest, run_installed};

/// Name of the command that runs the managed interpreter.
pub const PYTHON_COMMAND: &str = "python3";

/// Directory inside `bin` that holds the installed interpreter trees.
pub const PYTHON_DIRECTORY: &str = ".python";

/// Proxy and certificate settings the installer inherits; everything else,
/// including every `UV_*` setting that could redirect the download, is dropped.
const INSTALLER_NETWORK_ENVIRONMENT: [&str; 10] = [
    "HTTPS_PROXY",
    "https_proxy",
    "HTTP_PROXY",
    "http_proxy",
    "ALL_PROXY",
    "all_proxy",
    "NO_PROXY",
    "no_proxy",
    "SSL_CERT_FILE",
    "SSL_CERT_DIR",
];

/// A manifest pin of the interpreter `uv` installs on one target.
///
/// The version pins what is installed; the digest pins the interpreter
/// executable it yields, which preparation verifies after installation and
/// every reuse verifies again.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PythonPin {
    /// `CPython` version, such as `3.13.18`.
    pub version: String,
    /// Platform the interpreter was built for.
    pub target: ToolTarget,
    /// SHA-256 of the interpreter executable (`bin/python3.<minor>`).
    pub interpreter_sha256: String,
}

/// What a shell script needs to install or check the managed interpreter.
#[derive(Debug)]
pub struct PythonScript {
    /// The `uv python install` request, also the installed directory's name.
    pub(crate) request: String,
    /// Where `bin/python3` points, relative to `bin`.
    pub(crate) link_target: String,
    /// Hex SHA-256 of the interpreter executable.
    pub(crate) interpreter_sha256: String,
    /// Names of the environment variables the installer inherits, space separated.
    pub(crate) network_environment: String,
}

/// The validated interpreter pin of one target.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PythonRuntime {
    version: semver::Version,
    interpreter_sha256: [u8; 32],
}

impl PythonPin {
    pub(crate) fn runtime(&self) -> Result<PythonRuntime, ToolRuntimeError> {
        let mut interpreter_sha256 = [0; 32];
        hex::decode_to_slice(&self.interpreter_sha256, &mut interpreter_sha256)?;
        Ok(PythonRuntime {
            version: self.version.parse()?,
            interpreter_sha256,
        })
    }
}

impl PythonRuntime {
    /// The `CPython` version the pinned `uv` installs.
    #[must_use]
    pub const fn version(&self) -> &semver::Version {
        &self.version
    }

    pub(crate) const fn interpreter_sha256(&self) -> &[u8; 32] {
        &self.interpreter_sha256
    }

    /// The version `python3 --version` reports: `Python 3.13.18`.
    pub(crate) fn reported_version(output: &str) -> Option<semver::Version> {
        let mut words = output.lines().next()?.split_whitespace();
        (words.next()? == "Python")
            .then(|| words.next())
            .flatten()
            .and_then(|version| semver::Version::parse(version).ok())
    }

    /// The `uv python install` request, which also names the installed
    /// directory: `cpython-3.13.18-macos-aarch64-none`.
    ///
    /// Linux requests the glibc build explicitly, so the interpreter digest
    /// does not depend on the libc of the host `uv` happens to run on.
    pub(crate) fn request(&self, target: &ToolTarget) -> Result<String, ToolRuntimeError> {
        let (os, libc) = match target.os() {
            "macos" => ("macos", "none"),
            "linux" => ("linux", "gnu"),
            os => {
                return Err(ToolRuntimeError::UnsupportedTarget {
                    os: os.into(),
                    arch: target.arch().into(),
                });
            }
        };
        Ok(format!(
            "cpython-{}-{os}-{}-{libc}",
            self.version,
            target.arch()
        ))
    }

    /// The interpreter executable's file name: `python3.13`.
    fn interpreter_name(&self) -> String {
        format!("python{}.{}", self.version.major, self.version.minor)
    }

    /// Where `bin/python3` points, relative to `bin`.
    fn link_target(&self, target: &ToolTarget) -> Result<PathBuf, ToolRuntimeError> {
        Ok(Path::new(PYTHON_DIRECTORY)
            .join(self.request(target)?)
            .join("bin")
            .join(self.interpreter_name()))
    }

    /// The values the install and image-verification scripts need.
    pub(crate) fn script(&self, target: &ToolTarget) -> Result<PythonScript, ToolRuntimeError> {
        let request = self.request(target)?;
        let link_target = format!(
            "{PYTHON_DIRECTORY}/{request}/bin/{}",
            self.interpreter_name()
        );
        Ok(PythonScript {
            request,
            link_target,
            interpreter_sha256: hex::encode(self.interpreter_sha256),
            network_environment: INSTALLER_NETWORK_ENVIRONMENT.join(" "),
        })
    }

    /// Installs the interpreter with `installer`, the verified bytes of the
    /// `uv` pinned for the build machine, into `<stage>/bin`.
    ///
    /// The installer runs from a scratch directory that is removed again, with
    /// a cleared environment, no configuration file and no cache. Afterwards
    /// the whole tree is checked, its interpreter digest verified, and the
    /// tree made read-only.
    pub(crate) fn stage(
        &self,
        target: &ToolTarget,
        stage: &Path,
        installer: &[u8],
    ) -> Result<(), ToolRuntimeError> {
        let request = self.request(target)?;
        let scratch = stage.join(".python-install");
        std::fs::create_dir(&scratch)?;
        let uv = scratch.join("uv");
        std::fs::write(&uv, installer)?;
        set_mode(&uv, 0o500)?;
        let install_dir = scratch.join("python");
        std::fs::create_dir(&install_dir)?;
        let output = run_installed(
            std::process::Command::new(&uv)
                .args(["python", "install", "--no-bin", "--no-config", "--no-cache"])
                .arg(&request)
                .env_clear()
                .envs(
                    INSTALLER_NETWORK_ENVIRONMENT
                        .iter()
                        .filter_map(|name| Some((*name, std::env::var_os(name)?))),
                )
                .env("UV_PYTHON_INSTALL_DIR", &install_dir),
        )?;
        if !output.status.success() {
            return Err(ToolRuntimeError::PythonInstall {
                status: output.status.code(),
                stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
            });
        }
        let unpacked = install_dir.join(&request);
        if !std::fs::symlink_metadata(&unpacked)?.file_type().is_dir() {
            return Err(ToolRuntimeError::InvalidPythonTree(unpacked));
        }
        let bin = stage.join("bin");
        let python = bin.join(PYTHON_DIRECTORY);
        std::fs::create_dir(&python)?;
        let tree = python.join(&request);
        std::fs::rename(&unpacked, &tree)?;
        std::fs::remove_dir_all(&scratch)?;
        verify_tree(&tree)?;
        link_python(&self.link_target(target)?, &bin.join(PYTHON_COMMAND))?;
        lock_tree(&python)?;
        // A locked tree cannot be deleted, so a rejected one is opened again.
        self.verify(target, &bin).inspect_err(|_| {
            let _ = unlock_tree(&python);
        })
    }

    /// Checks that `<bin>/python3` is the expected relative link and that the
    /// interpreter it reaches is a read-only executable with the pinned digest.
    pub(crate) fn verify(&self, target: &ToolTarget, bin: &Path) -> Result<(), ToolRuntimeError> {
        let link = bin.join(PYTHON_COMMAND);
        let expected = self.link_target(target)?;
        let actual = std::fs::read_link(&link).map_err(|error| link_error(&link, error))?;
        if actual != expected {
            return Err(ToolRuntimeError::InvalidCache(link));
        }
        let interpreter = bin.join(&expected);
        let metadata = std::fs::symlink_metadata(&interpreter)
            .map_err(|error| entry_error(&interpreter, error))?;
        if !metadata.file_type().is_file() {
            return Err(ToolRuntimeError::InvalidCache(interpreter));
        }
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let mode = metadata.permissions().mode();
            if mode & 0o222 != 0 || mode & 0o111 == 0 {
                return Err(ToolRuntimeError::InvalidCache(interpreter));
            }
        }
        if digest(&std::fs::read(&interpreter)?) != self.interpreter_sha256 {
            return Err(ToolRuntimeError::PythonIntegrity);
        }
        Ok(())
    }
}

fn entry_error(path: &Path, error: std::io::Error) -> ToolRuntimeError {
    match error.kind() {
        std::io::ErrorKind::NotFound | std::io::ErrorKind::NotADirectory => {
            ToolRuntimeError::InvalidCache(path.to_path_buf())
        }
        _ => ToolRuntimeError::Io(error),
    }
}

fn link_error(path: &Path, error: std::io::Error) -> ToolRuntimeError {
    match error.kind() {
        std::io::ErrorKind::InvalidInput => ToolRuntimeError::InvalidCache(path.to_path_buf()),
        _ => entry_error(path, error),
    }
}

fn link_python(target: &Path, link: &Path) -> Result<(), std::io::Error> {
    #[cfg(unix)]
    {
        std::os::unix::fs::symlink(target, link)
    }
    #[cfg(not(unix))]
    {
        let _ = (target, link);
        Err(std::io::ErrorKind::Unsupported.into())
    }
}

fn set_mode(path: &Path, mode: u32) -> Result<(), std::io::Error> {
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(path, std::fs::Permissions::from_mode(mode))
    }
    #[cfg(not(unix))]
    {
        let _ = (path, mode);
        Err(std::io::ErrorKind::Unsupported.into())
    }
}

/// Checks that `root` holds only directories, regular files and relative
/// symlinks that stay inside `root`.
pub fn verify_tree(root: &Path) -> Result<(), ToolRuntimeError> {
    let mut directories = vec![root.to_path_buf()];
    while let Some(directory) = directories.pop() {
        for entry in std::fs::read_dir(&directory)? {
            let entry = entry?;
            let path = entry.path();
            let kind = entry.file_type()?;
            if kind.is_dir() {
                directories.push(path);
            } else if kind.is_symlink() {
                let target = std::fs::read_link(&path)?;
                let parent = path.parent().unwrap_or(root);
                if !stays_inside(root, parent, &target) {
                    return Err(ToolRuntimeError::InvalidPythonTree(path));
                }
            } else if !kind.is_file() {
                return Err(ToolRuntimeError::InvalidPythonTree(path));
            }
        }
    }
    Ok(())
}

/// Whether the relative link `target`, placed in `parent`, resolves to a path
/// under `root`, judged lexically.
fn stays_inside(root: &Path, parent: &Path, target: &Path) -> bool {
    let Ok(relative) = parent.strip_prefix(root) else {
        return false;
    };
    let mut depth = relative.components().count();
    for component in target.components() {
        match component {
            Component::Normal(_) => depth += 1,
            Component::CurDir => {}
            Component::ParentDir => {
                let Some(shallower) = depth.checked_sub(1) else {
                    return false;
                };
                depth = shallower;
            }
            Component::RootDir | Component::Prefix(_) => return false,
        }
    }
    true
}

/// Removes every write bit under `root`, links excluded.
fn lock_tree(root: &Path) -> Result<(), ToolRuntimeError> {
    change_modes(root, |mode| mode & !0o222)
}

/// Restores the owner's write bit under `root` so the tree can be deleted.
pub fn unlock_tree(root: &Path) -> Result<(), ToolRuntimeError> {
    change_modes(root, |mode| mode | 0o200)
}

#[cfg(unix)]
fn change_modes(root: &Path, change: fn(u32) -> u32) -> Result<(), ToolRuntimeError> {
    use std::os::unix::fs::PermissionsExt;
    let mut pending = vec![root.to_path_buf()];
    while let Some(path) = pending.pop() {
        let metadata = match std::fs::symlink_metadata(&path) {
            Ok(metadata) => metadata,
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => continue,
            Err(error) => return Err(error.into()),
        };
        if metadata.file_type().is_symlink() {
            continue;
        }
        let mode = metadata.permissions().mode();
        if metadata.is_dir() {
            // Directories open up first so their entries can be listed and changed.
            std::fs::set_permissions(&path, std::fs::Permissions::from_mode(mode | 0o700))?;
            for entry in std::fs::read_dir(&path)? {
                pending.push(entry?.path());
            }
        }
        // Changing a child needs only search permission on its parent.
        std::fs::set_permissions(&path, std::fs::Permissions::from_mode(change(mode)))?;
    }
    Ok(())
}

#[cfg(not(unix))]
fn change_modes(_root: &Path, _change: fn(u32) -> u32) -> Result<(), ToolRuntimeError> {
    Err(std::io::Error::from(std::io::ErrorKind::Unsupported).into())
}

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

    #[test]
    fn python_banner_is_parsed() {
        let version = |text| semver::Version::parse(text).unwrap();
        assert_eq!(
            PythonRuntime::reported_version("Python 3.13.18\n"),
            Some(version("3.13.18"))
        );
        for rejected in [
            "",
            "Python\n",
            "python 3.13.18\n",
            "uv 0.13.7\n",
            "Python 3.13\n",
        ] {
            assert_eq!(
                PythonRuntime::reported_version(rejected),
                None,
                "{rejected:?}"
            );
        }
    }

    #[test]
    fn links_must_stay_inside_the_tree() {
        let root = Path::new("/tree");
        let inside =
            |parent: &str, target: &str| stays_inside(root, &root.join(parent), Path::new(target));
        assert!(inside("bin", "python3.13"));
        assert!(inside("share/terminfo/3", "../x/xterm"));
        assert!(inside("bin", "../lib/libpython3.so"));
        assert!(!inside("bin", "../../outside"));
        assert!(!inside("", "../outside"));
        assert!(!inside("bin", "/usr/bin/python3"));
    }
}