bashkit 0.14.3

Awesomely fast virtual sandbox with bash and file system
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
use bashkit::{
    Error, FileSystem, FileSystemExt, FileType, InMemoryFs, NamespaceAccess, NamespaceFs,
    ReadOnlyFs,
};
use std::io::ErrorKind;
use std::path::Path;
use std::sync::Arc;
use std::time::SystemTime;

async fn source_with(path: &str, content: &[u8]) -> Arc<InMemoryFs> {
    let fs = Arc::new(InMemoryFs::new());
    let parent = Path::new(path).parent().unwrap();
    fs.mkdir(parent, true).await.unwrap();
    fs.write_file(Path::new(path), content).await.unwrap();
    fs
}

#[tokio::test]
async fn namespace_mounts_arbitrary_filesystems_with_longest_prefix_precedence() {
    let parent = source_with("/parent.txt", b"parent").await;
    parent.mkdir(Path::new("/nested"), false).await.unwrap();
    parent
        .write_file(Path::new("/nested/hidden.txt"), b"hidden")
        .await
        .unwrap();
    let nested = source_with("/visible.txt", b"nested").await;

    let namespace = NamespaceFs::builder()
        .mount_readwrite("/workspace", parent.clone())
        .unwrap()
        .mount_readwrite("/workspace/nested", nested.clone())
        .unwrap()
        .build();

    assert_eq!(
        namespace
            .read_file(Path::new("/workspace/parent.txt"))
            .await
            .unwrap(),
        b"parent"
    );
    assert_eq!(
        namespace
            .read_file(Path::new("/workspace/nested/visible.txt"))
            .await
            .unwrap(),
        b"nested"
    );
    assert!(
        !namespace
            .exists(Path::new("/workspace/nested/hidden.txt"))
            .await
            .unwrap()
    );
}

#[tokio::test]
async fn namespace_lists_synthetic_ancestors_and_mount_points_consistently() {
    let source = source_with("/project/file.txt", b"hello").await;
    let namespace = NamespaceFs::builder()
        .mount_from(
            "/sandbox/work/src",
            source,
            "/project",
            NamespaceAccess::ReadOnly,
        )
        .unwrap()
        .build();

    for path in ["/", "/sandbox", "/sandbox/work", "/sandbox/work/src"] {
        let metadata = namespace.stat(Path::new(path)).await.unwrap();
        assert_eq!(metadata.file_type, FileType::Directory, "{path}");
        assert!(namespace.exists(Path::new(path)).await.unwrap(), "{path}");
    }

    let root = namespace.read_dir(Path::new("/")).await.unwrap();
    assert_eq!(
        root.iter()
            .map(|entry| entry.name.as_str())
            .collect::<Vec<_>>(),
        ["sandbox"]
    );

    let work = namespace
        .read_dir(Path::new("/sandbox/work"))
        .await
        .unwrap();
    let src = work.iter().find(|entry| entry.name == "src").unwrap();
    assert_eq!(
        src.metadata.file_type,
        namespace
            .stat(Path::new("/sandbox/work/src"))
            .await
            .unwrap()
            .file_type
    );
    assert_eq!(src.metadata.size, 0);

    let mounted = namespace
        .read_dir(Path::new("/sandbox/work/src"))
        .await
        .unwrap();
    let file = mounted
        .iter()
        .find(|entry| entry.name == "file.txt")
        .unwrap();
    assert_eq!(file.metadata.size, 5);
    assert_eq!(
        file.metadata.size,
        namespace
            .stat(Path::new("/sandbox/work/src/file.txt"))
            .await
            .unwrap()
            .size
    );
}

#[tokio::test]
async fn namespace_rebases_source_root_and_keeps_nested_writable_override() {
    let source = source_with("/repo/src/lib.rs", b"readonly").await;
    source
        .mkdir(Path::new("/repo/src/generated"), false)
        .await
        .unwrap();
    source
        .write_file(Path::new("/repo/private.txt"), b"private")
        .await
        .unwrap();
    let generated = Arc::new(InMemoryFs::new());

    let namespace = NamespaceFs::builder()
        .mount_readonly_from("/src", source.clone(), "/repo/src")
        .unwrap()
        .mount_readwrite("/src/generated", generated.clone())
        .unwrap()
        .build();

    assert_eq!(
        namespace.read_file(Path::new("/src/lib.rs")).await.unwrap(),
        b"readonly"
    );
    assert!(!namespace.exists(Path::new("/private.txt")).await.unwrap());
    assert!(
        namespace
            .write_file(Path::new("/src/lib.rs"), b"blocked")
            .await
            .is_err()
    );
    namespace
        .write_file(Path::new("/src/generated/output.rs"), b"generated")
        .await
        .unwrap();
    assert_eq!(
        generated.read_file(Path::new("/output.rs")).await.unwrap(),
        b"generated"
    );
}

#[tokio::test]
async fn tm_esc_031_namespace_readonly_mount_denies_every_mutation_without_bypass() {
    let source = source_with("/file.txt", b"original").await;
    let namespace = NamespaceFs::builder()
        .mount_readonly("/ro", source.clone())
        .unwrap()
        .mount_readwrite("/rw", Arc::new(InMemoryFs::new()))
        .unwrap()
        .build();

    let operations = [
        namespace
            .write_file(Path::new("/ro/file.txt"), b"changed")
            .await,
        namespace
            .append_file(Path::new("/ro/file.txt"), b"changed")
            .await,
        namespace.remove(Path::new("/ro/file.txt"), false).await,
        namespace.chmod(Path::new("/ro/file.txt"), 0o777).await,
        namespace
            .rename(Path::new("/ro/file.txt"), Path::new("/rw/file.txt"))
            .await,
        namespace.mkdir(Path::new("/ro/new"), false).await,
        namespace
            .copy(Path::new("/rw/missing"), Path::new("/ro/new"))
            .await,
        namespace
            .symlink(Path::new("/target"), Path::new("/ro/link"))
            .await,
        namespace
            .set_modified_time(Path::new("/ro/file.txt"), SystemTime::now())
            .await,
        namespace.mkfifo(Path::new("/ro/fifo"), 0o644).await,
    ];
    for result in operations {
        let Error::Io(error) = result.unwrap_err() else {
            panic!("expected I/O permission error");
        };
        assert_eq!(error.kind(), ErrorKind::PermissionDenied);
    }
    assert_eq!(
        source.read_file(Path::new("/file.txt")).await.unwrap(),
        b"original"
    );
}

#[tokio::test]
async fn tm_esc_031_namespace_normalizes_without_source_or_nested_mount_escape() {
    let source = source_with("/allowed/file.txt", b"allowed").await;
    source
        .write_file(Path::new("/secret.txt"), b"secret")
        .await
        .unwrap();
    let nested = source_with("/nested.txt", b"nested").await;
    let namespace = NamespaceFs::builder()
        .mount_readwrite_from("/visible", source, "/allowed")
        .unwrap()
        .mount_readwrite("/visible/nested", nested)
        .unwrap()
        .build();

    assert_eq!(
        namespace
            .read_file(Path::new("/visible/./file.txt"))
            .await
            .unwrap(),
        b"allowed"
    );
    assert!(
        !namespace
            .exists(Path::new("/visible/../secret.txt"))
            .await
            .unwrap()
    );
    assert!(
        !namespace
            .exists(Path::new("/visible/nested/../nested.txt"))
            .await
            .unwrap()
    );
    assert_eq!(
        namespace
            .read_file(Path::new("/visible/other/../nested/nested.txt"))
            .await
            .unwrap(),
        b"nested"
    );
}

#[tokio::test]
async fn namespace_cross_mount_copy_is_supported_but_rename_is_typed_non_atomic_error() {
    let source = source_with("/file.txt", b"copy me").await;
    let destination = Arc::new(InMemoryFs::new());
    let namespace = NamespaceFs::builder()
        .mount_readonly("/input", source.clone())
        .unwrap()
        .mount_readwrite("/output", destination.clone())
        .unwrap()
        .build();

    namespace
        .copy(Path::new("/input/file.txt"), Path::new("/output/file.txt"))
        .await
        .unwrap();
    assert_eq!(
        destination.read_file(Path::new("/file.txt")).await.unwrap(),
        b"copy me"
    );

    let Error::Io(error) = namespace
        .rename(Path::new("/output/file.txt"), Path::new("/input/moved.txt"))
        .await
        .unwrap_err()
    else {
        panic!("expected typed cross-device error");
    };
    assert_eq!(error.kind(), ErrorKind::PermissionDenied);

    let other = Arc::new(InMemoryFs::new());
    let namespace = NamespaceFs::builder()
        .mount_readwrite("/one", destination)
        .unwrap()
        .mount_readwrite("/two", other)
        .unwrap()
        .build();
    let Error::Io(error) = namespace
        .rename(Path::new("/one/file.txt"), Path::new("/two/file.txt"))
        .await
        .unwrap_err()
    else {
        panic!("expected typed cross-device error");
    };
    assert_eq!(error.kind(), ErrorKind::CrossesDevices);
}

#[tokio::test]
async fn namespace_uses_readonly_wrapper_semantics() {
    let source: Arc<dyn FileSystem> = Arc::new(InMemoryFs::new());
    let readonly: Arc<dyn FileSystem> = Arc::new(ReadOnlyFs::new(source));
    let namespace = NamespaceFs::builder()
        .mount("/wrapped", readonly, NamespaceAccess::ReadWrite)
        .unwrap()
        .build();

    let Error::Io(error) = namespace
        .write_file(Path::new("/wrapped/file.txt"), b"blocked")
        .await
        .unwrap_err()
    else {
        panic!("expected I/O error");
    };
    assert_eq!(error.kind(), ErrorKind::PermissionDenied);
}

#[tokio::test]
async fn tm_esc_031_namespace_rejects_invalid_paths_and_protects_namespace_nodes() {
    let source = source_with("/nested/file.txt", b"data").await;
    assert!(
        NamespaceFs::builder()
            .mount_readwrite("relative", source.clone())
            .is_err()
    );
    assert!(
        NamespaceFs::builder()
            .mount_readwrite_from("/valid", source.clone(), "relative")
            .is_err()
    );

    let namespace = NamespaceFs::builder()
        .mount_readwrite("/parent", source.clone())
        .unwrap()
        .mount_readwrite("/parent/nested/mount", Arc::new(InMemoryFs::new()))
        .unwrap()
        .build();

    for path in ["/parent", "/parent/nested", "/parent/nested/mount"] {
        let Error::Io(error) = namespace.remove(Path::new(path), true).await.unwrap_err() else {
            panic!("expected namespace-node protection");
        };
        assert_eq!(error.kind(), ErrorKind::PermissionDenied, "{path}");
    }
    assert!(source.exists(Path::new("/nested/file.txt")).await.unwrap());
}

#[tokio::test]
async fn namespace_cross_mount_copy_preserves_symlinks() {
    let source = Arc::new(InMemoryFs::new());
    source
        .symlink(Path::new("/target"), Path::new("/link"))
        .await
        .unwrap();
    let destination = Arc::new(InMemoryFs::new());
    let namespace = NamespaceFs::builder()
        .mount_readonly("/source", source)
        .unwrap()
        .mount_readwrite("/destination", destination.clone())
        .unwrap()
        .build();

    namespace
        .copy(Path::new("/source/link"), Path::new("/destination/link"))
        .await
        .unwrap();
    assert_eq!(
        destination.read_link(Path::new("/link")).await.unwrap(),
        Path::new("/target")
    );
}

#[tokio::test]
async fn namespace_same_mount_copy_and_rename_delegate_atomically() {
    let source = source_with("/original.txt", b"data").await;
    let namespace = NamespaceFs::builder()
        .mount_readwrite("/work", source.clone())
        .unwrap()
        .build();

    namespace
        .copy(
            Path::new("/work/original.txt"),
            Path::new("/work/copied.txt"),
        )
        .await
        .unwrap();
    namespace
        .rename(
            Path::new("/work/copied.txt"),
            Path::new("/work/renamed.txt"),
        )
        .await
        .unwrap();

    assert!(!source.exists(Path::new("/copied.txt")).await.unwrap());
    assert_eq!(
        source.read_file(Path::new("/renamed.txt")).await.unwrap(),
        b"data"
    );
}

#[tokio::test]
async fn namespace_cross_mount_directory_copy_is_typed_unsupported_error() {
    let source = Arc::new(InMemoryFs::new());
    source.mkdir(Path::new("/directory"), false).await.unwrap();
    let namespace = NamespaceFs::builder()
        .mount_readonly("/source", source)
        .unwrap()
        .mount_readwrite("/destination", Arc::new(InMemoryFs::new()))
        .unwrap()
        .build();

    let Error::Io(error) = namespace
        .copy(
            Path::new("/source/directory"),
            Path::new("/destination/directory"),
        )
        .await
        .unwrap_err()
    else {
        panic!("expected typed unsupported error");
    };
    assert_eq!(error.kind(), ErrorKind::Unsupported);
}