lex-runtime 0.11.38

Effect handler runtime + capability policy for Lex.
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
//! Integration tests for `std.fs`. Closes #99.

use lex_ast::canonicalize_program;
use lex_bytecode::{compile_program, vm::Vm, Value};
use lex_runtime::{DefaultHandler, Policy};
use lex_syntax::parse_source;
use std::collections::BTreeSet;
use std::sync::Arc;

fn policy_walk_only(read_root: &std::path::Path) -> Policy {
    let mut p = Policy::pure();
    p.allow_effects = ["fs_walk".to_string()].into_iter().collect::<BTreeSet<_>>();
    p.allow_fs_read = vec![read_root.to_path_buf()];
    p
}

fn policy_walk_and_write(read_root: &std::path::Path, write_root: &std::path::Path) -> Policy {
    let mut p = Policy::pure();
    p.allow_effects = ["fs_walk".to_string(), "fs_write".to_string()]
        .into_iter()
        .collect::<BTreeSet<_>>();
    p.allow_fs_read = vec![read_root.to_path_buf()];
    p.allow_fs_write = vec![write_root.to_path_buf()];
    p
}

fn run(src: &str, fn_name: &str, args: Vec<Value>, policy: Policy) -> Value {
    let prog = parse_source(src).expect("parse");
    let stages = canonicalize_program(&prog);
    if let Err(errs) = lex_types::check_program(&stages) {
        panic!("type errors:\n{errs:#?}");
    }
    let bc = Arc::new(compile_program(&stages));
    let handler = DefaultHandler::new(policy).with_program(Arc::clone(&bc));
    let mut vm = Vm::with_handler(&bc, Box::new(handler));
    vm.call(fn_name, args).unwrap_or_else(|e| panic!("call {fn_name}: {e}"))
}

fn unique_dir(name: &str) -> std::path::PathBuf {
    let dir = std::env::temp_dir().join(format!(
        "lex-fs-{}-{}-{}",
        std::process::id(),
        name,
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos()
    ));
    std::fs::create_dir_all(&dir).unwrap();
    dir
}

const SRC: &str = r#"
import "std.fs" as fs
import "std.list" as list

fn does_exist(path :: Str) -> [fs_walk] Bool { fs.exists(path) }

fn is_a_dir(path :: Str) -> [fs_walk] Bool { fs.is_dir(path) }

fn count_walk(path :: Str) -> [fs_walk] Int {
  match fs.walk(path) {
    Ok(paths) => list.len(paths),
    Err(_)    => 0 - 1,
  }
}

fn list_dir_count(path :: Str) -> [fs_walk] Int {
  match fs.list_dir(path) {
    Ok(paths) => list.len(paths),
    Err(_)    => 0 - 1,
  }
}

fn make_dir(path :: Str) -> [fs_write] Bool {
  match fs.mkdir_p(path) {
    Ok(_)  => true,
    Err(_) => false,
  }
}

fn stat_size(path :: Str) -> [fs_walk] Int {
  match fs.stat(path) {
    Ok(s)  => s.size,
    Err(_) => 0 - 1,
  }
}
"#;

#[test]
fn exists_returns_true_for_a_real_path() {
    let dir = unique_dir("exists_true");
    let v = run(
        SRC,
        "does_exist",
        vec![Value::Str(dir.to_string_lossy().to_string().into())],
        policy_walk_only(&dir),
    );
    assert_eq!(v, Value::Bool(true));
}

#[test]
fn exists_returns_false_for_nonexistent_path() {
    let dir = unique_dir("exists_false");
    let phantom = dir.join("nope");
    let v = run(
        SRC,
        "does_exist",
        vec![Value::Str(phantom.to_string_lossy().to_string().into())],
        policy_walk_only(&dir),
    );
    assert_eq!(v, Value::Bool(false));
}

#[test]
fn is_dir_distinguishes_dir_from_file() {
    let dir = unique_dir("is_dir");
    let file = dir.join("a.txt");
    std::fs::write(&file, "hi").unwrap();

    let v = run(SRC, "is_a_dir", vec![Value::Str(dir.to_string_lossy().to_string().into())], policy_walk_only(&dir));
    assert_eq!(v, Value::Bool(true));

    let v = run(SRC, "is_a_dir", vec![Value::Str(file.to_string_lossy().to_string().into())], policy_walk_only(&dir));
    assert_eq!(v, Value::Bool(false));
}

#[test]
fn walk_returns_recursive_path_count() {
    let dir = unique_dir("walk");
    std::fs::create_dir(dir.join("sub")).unwrap();
    std::fs::write(dir.join("a.txt"), "a").unwrap();
    std::fs::write(dir.join("sub/b.txt"), "b").unwrap();
    // walk yields: dir, sub, a.txt, sub/b.txt → 4 entries.
    let v = run(
        SRC,
        "count_walk",
        vec![Value::Str(dir.to_string_lossy().to_string().into())],
        policy_walk_only(&dir),
    );
    assert_eq!(v, Value::Int(4));
}

#[test]
fn list_dir_returns_immediate_children_only() {
    let dir = unique_dir("list_dir");
    std::fs::create_dir(dir.join("sub")).unwrap();
    std::fs::write(dir.join("a.txt"), "a").unwrap();
    std::fs::write(dir.join("sub/b.txt"), "b").unwrap();
    // list_dir yields only the direct children: sub, a.txt → 2.
    let v = run(
        SRC,
        "list_dir_count",
        vec![Value::Str(dir.to_string_lossy().to_string().into())],
        policy_walk_only(&dir),
    );
    assert_eq!(v, Value::Int(2));
}

#[test]
fn mkdir_p_creates_nested() {
    let root = unique_dir("mkdir_root");
    let nested = root.join("a/b/c");
    let v = run(
        SRC,
        "make_dir",
        vec![Value::Str(nested.to_string_lossy().to_string().into())],
        policy_walk_and_write(&root, &root),
    );
    assert_eq!(v, Value::Bool(true));
    assert!(nested.exists());
}

#[test]
fn mkdir_p_outside_write_root_returns_err() {
    let allowed = unique_dir("mkdir_allowed");
    let outside = unique_dir("mkdir_outside").join("new_subdir");
    let v = run(
        SRC,
        "make_dir",
        vec![Value::Str(outside.to_string_lossy().to_string().into())],
        policy_walk_and_write(&allowed, &allowed),
    );
    assert_eq!(v, Value::Bool(false));
}

#[test]
fn stat_returns_size_of_file() {
    let dir = unique_dir("stat");
    let file = dir.join("payload.txt");
    std::fs::write(&file, b"abcdef").unwrap();
    let v = run(
        SRC,
        "stat_size",
        vec![Value::Str(file.to_string_lossy().to_string().into())],
        policy_walk_only(&dir),
    );
    assert_eq!(v, Value::Int(6));
}

// ── content read/write (#882) ────────────────────────────────────────────────
//
// `fs.read_to_string` / `fs.write` exist so that "this touches the filesystem"
// is answerable from an effect row. The behaviour they must have is exactly the
// behaviour `io.read` / `io.write` already had — same allowlists, same refusals
// — with `[fs_read]` / `[fs_write]` in the row instead of `[io]`. These tests
// pin both halves: that the ops work, and that the scope still bites.

fn policy_read_only(read_root: &std::path::Path) -> Policy {
    let mut p = Policy::pure();
    p.allow_effects = ["fs_read".to_string()].into_iter().collect::<BTreeSet<_>>();
    p.allow_fs_read = vec![read_root.to_path_buf()];
    p
}

fn policy_write_only(write_root: &std::path::Path) -> Policy {
    let mut p = Policy::pure();
    p.allow_effects = ["fs_write".to_string()].into_iter().collect::<BTreeSet<_>>();
    p.allow_fs_write = vec![write_root.to_path_buf()];
    p
}

const READ_SRC: &str = r#"
import "std.fs" as fs

fn read_it(p :: Str) -> [fs_read] Str {
  match fs.read_to_string(p) {
    Err(m) => m,
    Ok(t) => t,
  }
}
"#;

const WRITE_SRC: &str = r#"
import "std.fs" as fs

fn write_it(p :: Str, body :: Str) -> [fs_write] Str {
  match fs.write(p, body) {
    Err(m) => m,
    Ok(_) => "WROTE",
  }
}
"#;

const APPEND_SRC: &str = r#"
import "std.fs" as fs

fn append_it(p :: Str, body :: Str) -> [fs_write] Str {
  match fs.append(p, body) {
    Err(m) => m,
    Ok(_) => "APPENDED",
  }
}
"#;

#[test]
fn fs_read_to_string_reads_inside_the_allowlist() {
    let dir = unique_dir("read-ok");
    let file = dir.join("in.txt");
    std::fs::write(&file, "hello-from-fs").unwrap();

    let got = run(
        READ_SRC,
        "read_it",
        vec![Value::Str(file.to_string_lossy().into_owned().into())],
        policy_read_only(&dir),
    );
    assert_eq!(got, Value::Str("hello-from-fs".into()));
}

#[test]
fn fs_read_to_string_refuses_outside_the_allowlist() {
    let dir = unique_dir("read-deny");
    let outside = unique_dir("read-deny-other").join("secret.txt");
    std::fs::write(&outside, "should-not-be-readable").unwrap();

    // The handler refuses before touching the file, so this surfaces as a
    // call error rather than an Err value — the same shape io.read produced.
    let prog = parse_source(READ_SRC).expect("parse");
    let stages = canonicalize_program(&prog);
    lex_types::check_program(&stages).expect("type errors");
    let bc = Arc::new(compile_program(&stages));
    let handler = DefaultHandler::new(policy_read_only(&dir)).with_program(Arc::clone(&bc));
    let mut vm = Vm::with_handler(&bc, Box::new(handler));
    let err = vm
        .call(
            "read_it",
            vec![Value::Str(outside.to_string_lossy().into_owned().into())],
        )
        .expect_err("read outside --allow-fs-read must be refused");
    assert!(
        format!("{err}").contains("outside --allow-fs-read"),
        "unexpected error: {err}"
    );
}

#[test]
fn fs_write_writes_inside_the_allowlist() {
    let dir = unique_dir("write-ok");
    let file = dir.join("out.txt");

    let got = run(
        WRITE_SRC,
        "write_it",
        vec![
            Value::Str(file.to_string_lossy().into_owned().into()),
            Value::Str("written-by-fs".into()),
        ],
        policy_write_only(&dir),
    );
    assert_eq!(got, Value::Str("WROTE".into()));
    assert_eq!(std::fs::read_to_string(&file).unwrap(), "written-by-fs");
}

#[test]
fn fs_write_refuses_outside_the_allowlist() {
    let dir = unique_dir("write-deny");
    let outside = unique_dir("write-deny-other").join("nope.txt");

    let prog = parse_source(WRITE_SRC).expect("parse");
    let stages = canonicalize_program(&prog);
    lex_types::check_program(&stages).expect("type errors");
    let bc = Arc::new(compile_program(&stages));
    let handler = DefaultHandler::new(policy_write_only(&dir)).with_program(Arc::clone(&bc));
    let mut vm = Vm::with_handler(&bc, Box::new(handler));
    let err = vm
        .call(
            "write_it",
            vec![
                Value::Str(outside.to_string_lossy().into_owned().into()),
                Value::Str("nope".into()),
            ],
        )
        .expect_err("write outside --allow-fs-write must be refused");
    assert!(
        format!("{err}").contains("outside --allow-fs-write"),
        "unexpected error: {err}"
    );
    assert!(!outside.exists(), "refused write must not create the file");
}

#[test]
fn granting_io_does_not_reach_fs_content_ops() {
    // The point of the whole change: [io] must no longer be a way to read
    // files through the `fs` module. A program declaring [fs_read] is not
    // satisfied by a policy granting only `io`.
    let dir = unique_dir("io-not-fs");
    let file = dir.join("in.txt");
    std::fs::write(&file, "x").unwrap();

    let mut p = Policy::pure();
    p.allow_effects = ["io".to_string()].into_iter().collect::<BTreeSet<_>>();
    p.allow_fs_read = vec![dir.clone()];

    let prog = parse_source(READ_SRC).expect("parse");
    let stages = canonicalize_program(&prog);
    lex_types::check_program(&stages).expect("type errors");
    let bc = Arc::new(compile_program(&stages));
    let handler = DefaultHandler::new(p).with_program(Arc::clone(&bc));
    let mut vm = Vm::with_handler(&bc, Box::new(handler));
    let err = vm
        .call(
            "read_it",
            vec![Value::Str(file.to_string_lossy().into_owned().into())],
        )
        .expect_err("[io] must not satisfy an [fs_read] program");
    assert!(
        format!("{err}").contains("fs_read"),
        "unexpected error: {err}"
    );
}

// ── fs.append (#899) ─────────────────────────────────────────────────────────
//
// Without it, adding a line to a file means read-all, concatenate, write-all,
// so an append-only log costs O(n) bytes per entry and O(n^2) over its life.
// Measured on a running hash-chained ledger: 1.8 TB written in a week to store
// 87 MB.

#[test]
fn fs_append_adds_without_truncating() {
    let dir = unique_dir("append-ok");
    let file = dir.join("log.txt");
    std::fs::write(&file, "first\n").unwrap();

    let out = run(
        APPEND_SRC,
        "append_it",
        vec![
            Value::Str(file.to_string_lossy().into_owned().into()),
            Value::Str("second\n".into()),
        ],
        policy_write_only(&dir),
    );
    assert_eq!(out, Value::Str("APPENDED".into()));
    assert_eq!(
        std::fs::read_to_string(&file).unwrap(),
        "first\nsecond\n",
        "append must preserve what was already there — truncating is `write`"
    );
}

// A log's first line should not be a special case: appending to a path that
// does not exist yet creates it, the way `write` would.
#[test]
fn fs_append_creates_a_missing_file() {
    let dir = unique_dir("append-create");
    let file = dir.join("fresh.txt");

    let out = run(
        APPEND_SRC,
        "append_it",
        vec![
            Value::Str(file.to_string_lossy().into_owned().into()),
            Value::Str("line one\n".into()),
        ],
        policy_write_only(&dir),
    );
    assert_eq!(out, Value::Str("APPENDED".into()));
    assert_eq!(std::fs::read_to_string(&file).unwrap(), "line one\n");
}

// Repeated appends accumulate in order. This is the property the whole feature
// exists for: growing a log without rewriting it.
#[test]
fn fs_append_accumulates_in_order() {
    let dir = unique_dir("append-order");
    let file = dir.join("chain.txt");

    for n in ["a\n", "b\n", "c\n"] {
        run(
            APPEND_SRC,
            "append_it",
            vec![
                Value::Str(file.to_string_lossy().into_owned().into()),
                Value::Str(n.into()),
            ],
            policy_write_only(&dir),
        );
    }
    assert_eq!(std::fs::read_to_string(&file).unwrap(), "a\nb\nc\n");
}

// The scope must bite exactly as it does for `write`. Appending is a write:
// nothing about only adding to the end makes it need less authority.
#[test]
fn fs_append_refuses_outside_the_write_allowlist() {
    let allowed = unique_dir("append-scope-ok");
    let other = unique_dir("append-scope-outside");
    let file = other.join("elsewhere.txt");

    let err = std::panic::catch_unwind(|| {
        run(
            APPEND_SRC,
            "append_it",
            vec![
                Value::Str(file.to_string_lossy().into_owned().into()),
                Value::Str("nope\n".into()),
            ],
            policy_write_only(&allowed),
        )
    })
    .expect_err("append outside --allow-fs-write must refuse");
    let msg = err
        .downcast_ref::<String>()
        .cloned()
        .unwrap_or_else(|| "non-string panic".into());
    assert!(
        msg.contains("allow-fs-write") || msg.contains("fs_write"),
        "unexpected error: {msg}"
    );
    assert!(
        !file.exists(),
        "a refused append must not have created the file"
    );
}

// It must NOT imply read. An appender that cannot read what it writes to is a
// genuinely smaller authority for a log-only component, and that reduction is
// only real if `fs_write` alone suffices to append.
#[test]
fn fs_append_needs_no_read_permission() {
    let dir = unique_dir("append-no-read");
    let file = dir.join("writeonly.txt");
    std::fs::write(&file, "existing\n").unwrap();

    let mut p = Policy::pure();
    p.allow_effects = ["fs_write".to_string()].into_iter().collect::<BTreeSet<_>>();
    p.allow_fs_write = vec![dir.clone()];
    // deliberately no allow_fs_read at all

    let out = run(
        APPEND_SRC,
        "append_it",
        vec![
            Value::Str(file.to_string_lossy().into_owned().into()),
            Value::Str("added\n".into()),
        ],
        p,
    );
    assert_eq!(out, Value::Str("APPENDED".into()));
    assert_eq!(
        std::fs::read_to_string(&file).unwrap(),
        "existing\nadded\n"
    );
}