noether-engine 0.7.0

Noether composition engine: Lagrange graph AST, type checker, planner, executor, semantic index, LLM-backed composition agent
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
//! Adversarial sandbox-escape tests for the bwrap isolation layer.
//!
//! These tests exercise `build_bwrap_command` directly with a
//! pre-resolved Python binary. They do NOT go through `NixExecutor`
//! because `nix run nixpkgs#python3` expects to fetch flakes at
//! runtime, which can't work inside a sandbox that deliberately has
//! no network or flake-registry access. Running nix *inside* the
//! sandbox is the wrong design regardless; the proper integration is
//! to resolve the runtime on the host first (via `nix build
//! --print-out-paths`) and run only the resolved binary inside the
//! sandbox. That refactor is tracked as a Phase 1.x follow-up;
//! meanwhile these tests validate the sandbox-shape contract directly.
//!
//! Each test registers a Python attack snippet — reading
//! `/etc/shadow`, resolving DNS, observing the real UID — and
//! asserts the sandbox defeats it. If bwrap or a `/nix/store`-hosted
//! python3 isn't available on the test host, the tests skip with a
//! note; we'd rather have a green suite than a fragile one that
//! pretends to test security but doesn't actually run.

#![cfg(target_os = "linux")]

use noether_core::effects::{Effect, EffectSet};
use noether_engine::executor::isolation::{build_bwrap_command, find_bwrap, IsolationPolicy};
use serde_json::{json, Value};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};

/// Resolve a Python interpreter inside `/nix/store`. That store path
/// is already covered by the default `IsolationPolicy` RO bind, so
/// the sandbox can run it with no extra ceremony. Returns `None`
/// when nix isn't available or the build fails — the caller skips.
fn nix_python3() -> Option<PathBuf> {
    let out = Command::new("nix")
        .args([
            "build",
            "--no-link",
            "--print-out-paths",
            "--quiet",
            "nixpkgs#python3",
        ])
        .output()
        .ok()?;
    if !out.status.success() {
        return None;
    }
    let store_path = String::from_utf8(out.stdout).ok()?;
    let store_path = store_path.trim();
    if store_path.is_empty() {
        return None;
    }
    let python = Path::new(store_path).join("bin").join("python3");
    python.exists().then_some(python)
}

/// Return `Some(bwrap_path, python_path)` when both the sandbox
/// wrapper and a runnable Python are available on the host. A test
/// that gets `None` should early-return rather than fail — the host
/// is just missing the toolchain to exercise the sandbox end-to-end.
fn deps() -> Option<(PathBuf, PathBuf)> {
    let bwrap = find_bwrap()?;
    let python = nix_python3()?;
    Some((bwrap, python))
}

fn skip_if_deps_missing() -> Option<(PathBuf, PathBuf)> {
    match deps() {
        Some(d) => Some(d),
        None => {
            eprintln!(
                "isolation_escape: skipping — missing bwrap or nix-built \
                 python3; both are required to drive real sandbox escape \
                 probes. Unit tests in `executor::isolation` still verify \
                 the argv-construction contract."
            );
            None
        }
    }
}

/// Drive `build_bwrap_command` with the given `policy` and Python
/// attack code on stdin. Returns the parsed `{...}` JSON the stage
/// prints to stdout, or an error-shaped JSON if the subprocess
/// crashed / bwrap refused / stdout was garbage.
fn run_attack(bwrap: &Path, python: &Path, policy: &IsolationPolicy, code: &str) -> Value {
    let inner = vec![
        python.to_string_lossy().into_owned(),
        "-c".into(),
        code.into(),
    ];
    let mut cmd = build_bwrap_command(bwrap, policy, &inner);
    cmd.stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped());
    let child = match cmd.spawn() {
        Ok(c) => c,
        Err(e) => return json!({ "spawn_error": format!("{e}") }),
    };
    let out = match child.wait_with_output() {
        Ok(o) => o,
        Err(e) => return json!({ "wait_error": format!("{e}") }),
    };
    let stdout = String::from_utf8_lossy(&out.stdout);
    let stderr = String::from_utf8_lossy(&out.stderr);
    if !out.status.success() {
        return json!({
            "exit_failure": out.status.code(),
            "stderr": stderr.to_string(),
            "stdout": stdout.to_string(),
        });
    }
    serde_json::from_str(stdout.trim()).unwrap_or_else(
        |_| json!({ "unparseable_stdout": stdout.to_string(), "stderr": stderr.to_string() }),
    )
}

fn assert_ran(result: &Value) {
    // A result with `spawn_error` / `wait_error` / `exit_failure`
    // means the probe never ran, so the attack-specific key being
    // absent would silently pass the actual assertion. Fail loudly
    // when that happens so we don't get vacuous green tests.
    for bad in [
        "spawn_error",
        "wait_error",
        "exit_failure",
        "unparseable_stdout",
    ] {
        assert!(
            result.get(bad).is_none(),
            "sandboxed probe did not run cleanly ({bad}): {result}"
        );
    }
}

/// A pure-effect stage must NOT be able to reach DNS.
#[test]
fn network_blocked_when_effect_not_declared() {
    let Some((bwrap, python)) = skip_if_deps_missing() else {
        return;
    };
    let policy = IsolationPolicy::from_effects(&EffectSet::pure());
    let code = r#"
import socket, json
try:
    socket.gethostbyname("example.com")
    print(json.dumps({"blocked": False}))
except OSError as e:
    print(json.dumps({"blocked": True, "errno": e.errno}))
"#;
    let result = run_attack(&bwrap, &python, &policy, code);
    assert_ran(&result);
    assert_eq!(
        result.get("blocked"),
        Some(&json!(true)),
        "pure-effect stage resolved DNS — network namespace not \
         unshared: {result}"
    );
}

/// Counter-test: a stage declaring `Effect::Network` must get DNS
/// resolution. Two probes so the test is decisive regardless of
/// whether the host itself has internet:
///
/// - `localhost` — resolves via `/etc/hosts`, which the sandbox
///   binds when `network=true`. Works offline. If this fails, the
///   `/etc/hosts` bind is broken or NSS is miswired.
/// - `example.com` — resolves via `/etc/resolv.conf` + DNS. Requires
///   real internet. Logs inconclusively when offline but never fails
///   the test on that axis alone.
///
/// A round-1 version of this test hedged its ONLY probe as "maybe
/// offline" — that reviewer-flagged hedge masked the worst case
/// where a connected host still couldn't resolve because NSS had
/// no config. The `localhost` probe closes that hole.
#[test]
fn network_allowed_when_effect_declared() {
    let Some((bwrap, python)) = skip_if_deps_missing() else {
        return;
    };
    let policy = IsolationPolicy::from_effects(&EffectSet::new([Effect::Pure, Effect::Network]));
    let code = r#"
import socket, json

def resolve(host):
    try:
        socket.gethostbyname(host)
        return {"ok": True}
    except OSError as e:
        return {"ok": False, "errno": e.errno, "msg": str(e)}

print(json.dumps({
    "localhost": resolve("localhost"),
    "example_com": resolve("example.com"),
}))
"#;
    let result = run_attack(&bwrap, &python, &policy, code);
    assert_ran(&result);

    // localhost MUST resolve — /etc/hosts is bound and doesn't
    // require network. If this fails, the bind-mount wiring is
    // broken regardless of host connectivity.
    assert_eq!(
        result.get("localhost").and_then(|v| v.get("ok")),
        Some(&json!(true)),
        "localhost failed to resolve inside sandbox — NSS config \
         (/etc/hosts, /etc/nsswitch.conf) not correctly bound: {result}"
    );

    // example.com is best-effort: tolerates an offline test host,
    // but logs so the operator can see what happened.
    if result.get("example_com").and_then(|v| v.get("ok")) != Some(&json!(true)) {
        eprintln!(
            "network_allowed_when_effect_declared: example.com did not \
             resolve — test host may be offline. localhost worked, so \
             the sandbox's NSS wiring is verified. Result: {result}"
        );
    }
}

/// `/etc/shadow` is outside the default `ro_binds`; opening it must
/// fail with not-found. Catches anyone widening the binds to include
/// `/etc` or `/`.
#[test]
fn cannot_read_etc_shadow() {
    let Some((bwrap, python)) = skip_if_deps_missing() else {
        return;
    };
    let policy = IsolationPolicy::from_effects(&EffectSet::pure());
    let code = r#"
import json
try:
    with open("/etc/shadow", "r") as f:
        f.read()
    print(json.dumps({"leaked": True}))
except (FileNotFoundError, PermissionError) as e:
    print(json.dumps({"leaked": False, "error": type(e).__name__}))
"#;
    let result = run_attack(&bwrap, &python, &policy, code);
    assert_ran(&result);
    assert_eq!(
        result.get("leaked"),
        Some(&json!(false)),
        "/etc/shadow readable inside sandbox — bind-mount policy too \
         wide: {result}"
    );
}

/// Host user's SSH keys and cloud credentials must be unreachable.
#[test]
fn cannot_read_host_ssh_keys() {
    let Some((bwrap, python)) = skip_if_deps_missing() else {
        return;
    };
    let policy = IsolationPolicy::from_effects(&EffectSet::pure());
    let code = r#"
import os, json
candidates = [
    os.path.expanduser("~/.ssh/id_rsa"),
    os.path.expanduser("~/.ssh/id_ed25519"),
    os.path.expanduser("~/.aws/credentials"),
]
leaked = []
for p in candidates:
    try:
        with open(p, "rb") as f:
            f.read(1)
        leaked.append(p)
    except (FileNotFoundError, PermissionError, IsADirectoryError, NotADirectoryError):
        pass
print(json.dumps({"leaked_paths": leaked}))
"#;
    let result = run_attack(&bwrap, &python, &policy, code);
    assert_ran(&result);
    let leaked = result
        .get("leaked_paths")
        .and_then(|v| v.as_array())
        .cloned()
        .unwrap_or_default();
    assert!(
        leaked.is_empty(),
        "sandbox leaked host credentials: {leaked:?}; result: {result}"
    );
}

/// UID/GID must be mapped to nobody (65534). Without `--uid`/`--gid`
/// the host user's real UID is observable via `os.getuid()`.
#[test]
fn uid_mapped_to_nobody_inside_sandbox() {
    let Some((bwrap, python)) = skip_if_deps_missing() else {
        return;
    };
    let policy = IsolationPolicy::from_effects(&EffectSet::pure());
    let code = r#"
import os, json
print(json.dumps({"uid": os.getuid(), "gid": os.getgid()}))
"#;
    let result = run_attack(&bwrap, &python, &policy, code);
    assert_ran(&result);
    assert_eq!(
        result.get("uid"),
        Some(&json!(65534)),
        "sandbox did not apply --uid 65534: {result}"
    );
    assert_eq!(
        result.get("gid"),
        Some(&json!(65534)),
        "sandbox did not apply --gid 65534: {result}"
    );
}

/// A stage inside the sandbox must not be able to regain
/// privileges. The combination `--unshare-user + --uid 65534 +
/// --cap-drop ALL` is supposed to prevent `setuid(0)` and
/// `chroot("/")` from succeeding. Future refactors that accidentally
/// drop any one of those would pass the existing UID-observation
/// test (which only asserts the CURRENT uid is 65534) but fail
/// here — so this locks the capability-drop contract separately.
#[test]
fn cannot_escalate_to_root() {
    let Some((bwrap, python)) = skip_if_deps_missing() else {
        return;
    };
    let policy = IsolationPolicy::from_effects(&EffectSet::pure());
    let code = r#"
import os, json
attempts = {}
try:
    os.setuid(0)
    attempts["setuid_0"] = True
except (PermissionError, OSError) as e:
    attempts["setuid_0"] = False
    attempts["setuid_error"] = type(e).__name__
try:
    os.setgid(0)
    attempts["setgid_0"] = True
except (PermissionError, OSError) as e:
    attempts["setgid_0"] = False
try:
    os.chroot("/")
    attempts["chroot"] = True
except (PermissionError, OSError) as e:
    attempts["chroot"] = False
print(json.dumps(attempts))
"#;
    let result = run_attack(&bwrap, &python, &policy, code);
    assert_ran(&result);
    assert_eq!(
        result.get("setuid_0"),
        Some(&json!(false)),
        "setuid(0) succeeded inside sandbox — capability drop misconfigured: {result}"
    );
    assert_eq!(
        result.get("setgid_0"),
        Some(&json!(false)),
        "setgid(0) succeeded inside sandbox — capability drop misconfigured: {result}"
    );
    assert_eq!(
        result.get("chroot"),
        Some(&json!(false)),
        "chroot succeeded inside sandbox — CAP_SYS_CHROOT not dropped: {result}"
    );
}

/// Regression guard for the `--setenv` env-wiring bug discovered
/// during review round 1. Setting `cmd.env("HOME", "/work")` on the
/// outer `Command` was silently stripped by bwrap's `--clearenv`
/// before the stage process ran; the fix was to emit `--setenv
/// HOME /work` on the bwrap argv instead. A future refactor that
/// reintroduces `cmd.env()` would pass the argv-shape unit tests
/// (they don't inspect Command.get_envs) but would cause HOME to
/// leak the host user's real home path here.
///
/// The sandbox-side HOME must always be `/work`, never the host
/// user's `$HOME`.
#[test]
fn home_env_is_sandbox_consistent() {
    let Some((bwrap, python)) = skip_if_deps_missing() else {
        return;
    };
    let policy = IsolationPolicy::from_effects(&EffectSet::pure());
    let code = r#"
import os, json
print(json.dumps({
    "home": os.environ.get("HOME"),
    "user": os.environ.get("USER"),
}))
"#;
    let result = run_attack(&bwrap, &python, &policy, code);
    assert_ran(&result);
    assert_eq!(
        result.get("home"),
        Some(&json!("/work")),
        "HOME inside sandbox leaked host value — `--setenv` wiring broken: {result}"
    );
    assert_eq!(
        result.get("user"),
        Some(&json!("nobody")),
        "USER inside sandbox leaked host value: {result}"
    );
}

/// `/work` is a sandbox-private tmpfs. Must start empty and be
/// writable — regression guard against both state leakage and
/// privilege misconfiguration.
#[test]
fn work_dir_is_private_tmpfs_and_empty() {
    let Some((bwrap, python)) = skip_if_deps_missing() else {
        return;
    };
    let policy = IsolationPolicy::from_effects(&EffectSet::pure());
    let code = r#"
import os, json
entries = sorted(os.listdir("/work"))
with open("/work/scratch.txt", "w") as f:
    f.write("ok")
with open("/work/scratch.txt") as f:
    content = f.read()
print(json.dumps({"initial_entries": entries, "roundtrip": content}))
"#;
    let result = run_attack(&bwrap, &python, &policy, code);
    assert_ran(&result);
    assert_eq!(
        result.get("initial_entries"),
        Some(&json!([])),
        "/work was not empty at stage entry — state leak or stale tmpdir: {result}"
    );
    assert_eq!(
        result.get("roundtrip"),
        Some(&json!("ok")),
        "/work not writable inside sandbox: {result}"
    );
}