tinysandbox 0.4.1

Ultra-minimal Linux-like sandbox for AI agents: shell, coreutils, VFS, and a Wasmtime-hosted JS runtime in one crate
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
//! Async filesystem facade exposed to sandbox commands.

use std::collections::BTreeSet;
use std::future::Future;
use std::io;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};

use tokio::io::{AsyncRead, ReadBuf};
use tokio::task;

use super::command::BoxAsyncRead;
use crate::vfs::{
    DirEntry, Errno, FileHandle, FileType, Metadata, OpenMode, Vfs, VfsError, VfsResult,
};

pub(crate) const STREAM_CHUNK_BYTES: usize = 64 * 1024;

/// Filesystem handle scoped to a sandbox command's current directory.
#[derive(Clone)]
pub struct Fs {
    vfs: Arc<dyn Vfs>,
    bin_commands: Arc<BTreeSet<String>>,
    cwd: String,
}

impl Fs {
    pub(crate) fn new(vfs: Arc<dyn Vfs>, bin_commands: Arc<BTreeSet<String>>, cwd: String) -> Self {
        Self {
            vfs,
            bin_commands,
            cwd,
        }
    }

    /// Returns metadata for a path resolved relative to the current directory.
    pub async fn stat(&self, path: &str) -> VfsResult<Metadata> {
        let path = self.resolve(path);
        if let Some(metadata) = self.bin_stat(&path) {
            return metadata;
        }
        self.dispatch(move |vfs| vfs.stat(&path)).await
    }

    /// Reads directory entries for a path resolved relative to the current directory.
    pub async fn readdir(&self, path: &str) -> VfsResult<Vec<DirEntry>> {
        let path = self.resolve(path);
        if path == "/bin" {
            return Ok(self
                .bin_commands
                .iter()
                .map(|name| DirEntry {
                    name: name.clone(),
                    metadata: Metadata {
                        file_type: FileType::File,
                        len: 0,
                    },
                })
                .collect());
        }
        if path.starts_with("/bin/") {
            return Err(VfsError::new(Errno::ENOTDIR));
        }
        self.dispatch(move |vfs| vfs.readdir(&path)).await
    }

    /// Creates a directory.
    pub async fn mkdir(&self, path: &str) -> VfsResult<()> {
        let path = self.resolve(path);
        if is_bin_path(&path) {
            return Err(VfsError::new(Errno::EACCES));
        }
        self.dispatch(move |vfs| vfs.mkdir(&path)).await
    }

    /// Renames a file or directory.
    pub async fn rename(&self, from: &str, to: &str) -> VfsResult<()> {
        let from = self.resolve(from);
        let to = self.resolve(to);
        if is_bin_path(&from) || is_bin_path(&to) {
            return Err(VfsError::new(Errno::EACCES));
        }
        self.dispatch(move |vfs| vfs.rename(&from, &to)).await
    }

    /// Removes a file.
    pub async fn unlink(&self, path: &str) -> VfsResult<()> {
        let path = self.resolve(path);
        if is_bin_path(&path) {
            return Err(VfsError::new(Errno::EACCES));
        }
        self.dispatch(move |vfs| vfs.unlink(&path)).await
    }

    /// Removes an empty directory.
    pub async fn rmdir(&self, path: &str) -> VfsResult<()> {
        let path = self.resolve(path);
        if is_bin_path(&path) {
            return Err(VfsError::new(Errno::EACCES));
        }
        self.dispatch(move |vfs| vfs.rmdir(&path)).await
    }

    /// Reads an entire file into memory.
    pub async fn read_file(&self, path: &str) -> VfsResult<Vec<u8>> {
        let path = self.resolve(path);
        if path == "/bin" {
            return Err(VfsError::new(Errno::EISDIR));
        }
        if let Some(name) = path.strip_prefix("/bin/") {
            return if self.bin_commands.contains(name) {
                Ok(Vec::new())
            } else {
                Err(VfsError::new(Errno::ENOENT))
            };
        }
        self.dispatch(move |vfs| read_file_sync(vfs, &path)).await
    }

    pub(crate) async fn stream_reader(&self, path: &str) -> VfsResult<BoxAsyncRead> {
        let handle = self.open(path, OpenMode::read_only()).await?;
        Ok(self.stream_reader_from_handle(handle))
    }

    /// Writes a whole file, appending when `append` is true.
    pub async fn write_file(&self, path: &str, data: &[u8], append: bool) -> VfsResult<()> {
        let path = self.resolve(path);
        let data = data.to_vec();
        if is_bin_path(&path) {
            return Err(VfsError::new(Errno::EACCES));
        }
        self.dispatch(move |vfs| write_file_sync(vfs, &path, &data, append))
            .await
    }

    /// Creates a file if needed or updates its metadata when supported.
    pub async fn touch(&self, path: &str) -> VfsResult<()> {
        let path = self.resolve(path);
        if is_bin_path(&path) {
            return Err(VfsError::new(Errno::EACCES));
        }
        self.dispatch(move |vfs| {
            let handle = match vfs.open(&path, OpenMode::write_only().create()) {
                Ok(handle) => handle,
                Err(err) => return Err(err),
            };
            vfs.close(handle)
        })
        .await
    }

    /// Opens a VFS file handle for a path.
    pub async fn open(&self, path: &str, mode: OpenMode) -> VfsResult<FileHandle> {
        let path = self.resolve(path);
        if is_bin_path(&path) {
            return Err(if path == "/bin" {
                VfsError::new(Errno::EISDIR)
            } else if !mode.write && !self.bin_commands.contains(path.trim_start_matches("/bin/")) {
                VfsError::new(Errno::ENOENT)
            } else {
                VfsError::new(Errno::EACCES)
            });
        }
        self.dispatch(move |vfs| vfs.open(&path, mode)).await
    }

    /// Reads from a file handle at `offset`.
    pub async fn read_at(
        &self,
        handle: FileHandle,
        offset: u64,
        mut buf: Vec<u8>,
    ) -> VfsResult<(Vec<u8>, usize)> {
        self.dispatch(move |vfs| {
            let n = vfs.read_at(handle, offset, &mut buf)?;
            Ok((buf, n))
        })
        .await
    }

    /// Writes to a file handle at `offset`.
    pub async fn write_at(
        &self,
        handle: FileHandle,
        offset: u64,
        data: Vec<u8>,
    ) -> VfsResult<usize> {
        self.dispatch(move |vfs| vfs.write_at(handle, offset, &data))
            .await
    }

    /// Changes a file handle's length.
    pub async fn truncate(&self, handle: FileHandle, len: u64) -> VfsResult<()> {
        self.dispatch(move |vfs| vfs.truncate(handle, len)).await
    }

    /// Closes a file handle.
    pub async fn close(&self, handle: FileHandle) -> VfsResult<()> {
        self.dispatch(move |vfs| vfs.close(handle)).await
    }

    pub(crate) fn stream_reader_from_handle(&self, handle: FileHandle) -> BoxAsyncRead {
        Box::pin(FsStreamReader::new(self.clone(), handle))
    }

    async fn dispatch<R, F>(&self, op: F) -> VfsResult<R>
    where
        R: Send + 'static,
        F: FnOnce(&dyn Vfs) -> VfsResult<R> + Send + 'static,
    {
        if self.vfs.is_fast() {
            return op(self.vfs.as_ref());
        }

        let vfs = Arc::clone(&self.vfs);
        task::spawn_blocking(move || op(vfs.as_ref()))
            .await
            .unwrap_or_else(|_| Err(VfsError::new(Errno::EINVAL)))
    }

    fn bin_stat(&self, path: &str) -> Option<VfsResult<Metadata>> {
        if path == "/bin" {
            return Some(Ok(Metadata {
                file_type: FileType::Directory,
                len: 0,
            }));
        }

        let name = path.strip_prefix("/bin/")?;
        Some(if !name.contains('/') && self.bin_commands.contains(name) {
            Ok(Metadata {
                file_type: FileType::File,
                len: 0,
            })
        } else {
            Err(VfsError::new(Errno::ENOENT))
        })
    }

    pub(crate) fn resolve(&self, path: &str) -> String {
        normalize_absolute(if path.starts_with('/') {
            path.to_owned()
        } else if self.cwd == "/" {
            format!("/{path}")
        } else {
            format!("{}/{path}", self.cwd)
        })
    }
}

type ReadAtFuture = Pin<Box<dyn Future<Output = VfsResult<(Vec<u8>, usize)>> + Send>>;

struct FsStreamReader {
    fs: Fs,
    handle: Option<FileHandle>,
    offset: u64,
    pending: Vec<u8>,
    in_flight: Option<ReadAtFuture>,
}

impl FsStreamReader {
    fn new(fs: Fs, handle: FileHandle) -> Self {
        Self {
            fs,
            handle: Some(handle),
            offset: 0,
            pending: Vec::new(),
            in_flight: None,
        }
    }

    fn close_handle(&mut self) {
        let Some(handle) = self.handle.take() else {
            return;
        };
        let fs = self.fs.clone();
        if let Ok(runtime) = tokio::runtime::Handle::try_current() {
            runtime.spawn(async move {
                let _ = fs.close(handle).await;
            });
        }
    }

    fn copy_pending(&mut self, buf: &mut ReadBuf<'_>) -> bool {
        if self.pending.is_empty() || buf.remaining() == 0 {
            return false;
        }
        let n = self.pending.len().min(buf.remaining());
        buf.put_slice(&self.pending[..n]);
        self.pending.drain(..n);
        true
    }
}

impl Drop for FsStreamReader {
    fn drop(&mut self) {
        self.close_handle();
    }
}

impl AsyncRead for FsStreamReader {
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut ReadBuf<'_>,
    ) -> Poll<io::Result<()>> {
        let this = self.get_mut();
        if this.copy_pending(buf) {
            return Poll::Ready(Ok(()));
        }
        if buf.remaining() == 0 {
            return Poll::Ready(Ok(()));
        }

        loop {
            if this.in_flight.is_none() {
                let Some(handle) = this.handle else {
                    return Poll::Ready(Ok(()));
                };
                let fs = this.fs.clone();
                let offset = this.offset;
                this.in_flight = Some(Box::pin(async move {
                    fs.read_at(handle, offset, vec![0; STREAM_CHUNK_BYTES])
                        .await
                }));
            }

            let result = {
                let future = this.in_flight.as_mut().expect("future was just installed");
                match future.as_mut().poll(cx) {
                    Poll::Pending => return Poll::Pending,
                    Poll::Ready(result) => result,
                }
            };
            this.in_flight = None;

            let (mut bytes, n) = match result {
                Ok(result) => result,
                Err(err) => {
                    this.close_handle();
                    return Poll::Ready(Err(io::Error::other(err)));
                }
            };
            if n == 0 {
                this.close_handle();
                return Poll::Ready(Ok(()));
            }
            bytes.truncate(n);
            this.offset = this.offset.saturating_add(n as u64);
            this.pending = bytes;
            if this.copy_pending(buf) {
                return Poll::Ready(Ok(()));
            }
        }
    }
}

pub(crate) fn errno_message(errno: Errno) -> &'static str {
    match errno {
        Errno::EBADF => "Bad file descriptor",
        Errno::EBUSY => "Device or resource busy",
        Errno::EACCES => "Permission denied",
        Errno::EEXIST => "File exists",
        Errno::EINVAL => "Invalid argument",
        Errno::EISDIR => "Is a directory",
        Errno::ENOENT => "No such file or directory",
        Errno::ENOSPC => "No space left on device",
        Errno::ENOTDIR => "Not a directory",
        Errno::ENOTEMPTY => "Directory not empty",
    }
}

pub(crate) fn join_path(dir: &str, name: &str) -> String {
    if dir == "/" {
        format!("/{name}")
    } else {
        format!("{dir}/{name}")
    }
}

fn read_file_sync(vfs: &dyn Vfs, path: &str) -> VfsResult<Vec<u8>> {
    let handle = vfs.open(path, OpenMode::read_only())?;
    let result = read_all_from_handle(vfs, handle);
    let close = vfs.close(handle);
    match (result, close) {
        (Ok(data), Ok(())) => Ok(data),
        (Err(err), _) | (_, Err(err)) => Err(err),
    }
}

fn read_all_from_handle(vfs: &dyn Vfs, handle: FileHandle) -> VfsResult<Vec<u8>> {
    let mut out = Vec::new();
    let mut offset = 0_u64;
    let mut buf = [0_u8; 8192];
    loop {
        let read = vfs.read_at(handle, offset, &mut buf)?;
        if read == 0 {
            return Ok(out);
        }
        out.extend_from_slice(&buf[..read]);
        offset += u64::try_from(read).map_err(|_| VfsError::new(Errno::EINVAL))?;
    }
}

fn write_file_sync(vfs: &dyn Vfs, path: &str, data: &[u8], append: bool) -> VfsResult<()> {
    let mode = if append {
        OpenMode::write_only().create().append()
    } else {
        OpenMode::write_only().create().truncate()
    };
    let handle = vfs.open(path, mode)?;
    let result = write_all_to_handle(vfs, handle, data);
    let close = vfs.close(handle);
    result.and(close)
}

fn write_all_to_handle(vfs: &dyn Vfs, handle: FileHandle, data: &[u8]) -> VfsResult<()> {
    let mut written = 0;
    while written < data.len() {
        let n = vfs.write_at(
            handle,
            u64::try_from(written).map_err(|_| VfsError::new(Errno::EINVAL))?,
            &data[written..],
        )?;
        if n == 0 {
            return Err(VfsError::new(Errno::ENOSPC));
        }
        written += n;
    }
    Ok(())
}

fn is_bin_path(path: &str) -> bool {
    path == "/bin" || path.starts_with("/bin/")
}

pub(crate) fn normalize_absolute(path: String) -> String {
    let mut parts = Vec::new();
    for part in path.split('/') {
        match part {
            "" | "." => {}
            ".." => {
                parts.pop();
            }
            part => parts.push(part),
        }
    }
    if parts.is_empty() {
        "/".to_owned()
    } else {
        format!("/{}", parts.join("/"))
    }
}