From 2cf9ecedd590f57cd8a816d26dbe19417ddfed81 Mon Sep 17 00:00:00 2001 From: cflmy Date: Fri, 20 Nov 2026 13:59:12 +0800 Subject: [PATCH] test(cli): prove check never spawns a Foreign kernel A document with an executable foreign.call is statically checked without a marker side effect, then run to show the same provider does execute. This guards the 'check/inspect cannot execute untrusted code' contract. --- CHANGELOG.md | 2 + crates/cli/tests/integration_cli.rs | 63 +++++++++++++++++++++++++++++ docs/STATUS.md | 4 +- docs/security-gate.md | 5 +++ 4 files changed, 72 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dfb7c8c..ae72d92 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,8 @@ currently `0.1.0`; no release tag has been cut. Dates use ISO 8601. `PATH`, and `interpreter_version` is verified before execution. - Release documentation (`COMPATIBILITY.md`, `docs/releasing.md`) and the Linux security-gate evidence report (`docs/security-gate.md`). +- `check_never_spawns_a_foreign_kernel`: proves static `check` does not spawn a + declared Foreign provider, while `run` on the same document does. ### Changed diff --git a/crates/cli/tests/integration_cli.rs b/crates/cli/tests/integration_cli.rs index 1f31206..2212c2b 100644 --- a/crates/cli/tests/integration_cli.rs +++ b/crates/cli/tests/integration_cli.rs @@ -885,3 +885,66 @@ fn plugin_abi_major_mismatch_is_rejected_before_execution() { "plugin export must not run after an ABI mismatch" ); } + +/// `check` is static: a document that would run a Foreign kernel must not spawn +/// it or perform its side effect. The same document is then executed to prove +/// the marker would appear if check were executing code. +#[cfg(unix)] +#[test] +fn check_never_spawns_a_foreign_kernel() { + use std::os::unix::fs::PermissionsExt; + + let dir = TempDir::new("check-no-exec"); + let marker = dir.path().join("spawned.marker"); + let provider = dir.path().join("mocklang"); + fs::write( + &provider, + format!( + "#!/bin/sh\n: > '{}'\ncat >/dev/null\nprintf '%s' '{{\"type\":\"int\",\"decimal\":\"1\"}}'\n", + marker.display() + ), + ) + .expect("write mock provider"); + let mut permissions = fs::metadata(&provider) + .expect("provider metadata") + .permissions(); + permissions.set_mode(0o700); + fs::set_permissions(&provider, permissions).expect("chmod provider"); + let digest = hex::encode(sha2::Sha256::digest( + fs::read(&provider).expect("read provider"), + )); + dir.write( + "kernel.json", + &format!( + "{{\"provider_id\":\"mocklang\",\"languages\":[\"mocklang\"],\"wire_version\":1,\"entrypoint\":\"{}\",\"digest\":\"{digest}\"}}", + provider.display() + ), + ); + dir.write( + "doc.mq.md", + "## 代码\n\n```mocklang\nignored\n```\n\n## main\n\n**值 = foreign.call(ref(\"#代码\"), {})**\n\n> 打印(值)\n", + ); + + let checked = cli(dir.path(), &["check", "doc.mq.md"]); + assert_exit(&checked, 0, "check must accept the static document"); + assert!( + !marker.exists(), + "check spawned the Foreign kernel and produced a side effect" + ); + + let ran = cli( + dir.path(), + &[ + "run", + "doc.mq.md", + "--kernel", + "kernel.json", + "--allow-foreign", + ], + ); + assert_exit(&ran, 0, "run must execute the Foreign kernel"); + assert!( + marker.exists(), + "run did not execute the provider; the no-execution assertion is vacuous" + ); +} diff --git a/docs/STATUS.md b/docs/STATUS.md index df7fa1b..993b061 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -38,7 +38,7 @@ cargo fmt --all -- --check PASS cargo clippy --workspace --all-targets -- -D warnings PASS cargo test --workspace PASS cargo test -p marqdo-cli --test conformance_ast PASS (4 tests) -cargo test -p marqdo-cli --test integration_cli PASS (22 tests) +cargo test -p marqdo-cli --test integration_cli PASS (23 tests) cargo test -p marqdo-cli --test plugin_worker PASS (4 tests) cargo test -p marqdo-foreign --test provider PASS (8 tests) ``` @@ -62,7 +62,7 @@ runs. | 1 | macOS native plugin fixtures | **Fixed locally, macOS CI UNVERIFIED** | host-target manifest + dylib flags, foreign-target/tamper/ABI tests PASS | | 2 | Status/docs reconcile | **Done** | this file, `COMPATIBILITY.md`, `CHANGELOG.md`, `docs/releasing.md`, `docs/security-gate.md` | | 3 | Package lock v3 reproducibility | **Implemented; targeted audit pending** | ADR-0016, `cargo test -p marqdo-cli --test package_env` PASS | -| 4 | Analysis/Trace/Tool protocol | **Implemented; consumers PASS locally** | schemas + `tests/consumers/*.py`, `analysis_trace`/`tool_protocol` PASS | +| 4 | Analysis/Trace/Tool protocol | **Implemented; consumers PASS locally** | schemas + `tests/consumers/*.py`, `analysis_trace`/`tool_protocol` PASS; `check_never_spawns_a_foreign_kernel` PASS | | 5 | Foreign pinned environment | **Done (ADR-0020)** | provider tests PASS; env binds interpreter; version drift rejected | | 6 | ABI session/broker hardening | **Partial** | `fs.read` bridge + budgets + handle tests PASS; Windows ABI unsupported; broader services still `capability.unavailable` | | 7 | Linux strict probe | **Done as fail-closed evidence** | `docs/security-gate.md`; userns/bwrap EPERM, cgroup ro, sudo blocked | diff --git a/docs/security-gate.md b/docs/security-gate.md index 3fea40d..b2e8c5a 100644 --- a/docs/security-gate.md +++ b/docs/security-gate.md @@ -28,6 +28,11 @@ bwrap --unshare-user --unshare-net --ro-bind / / true sudo -n true -> sudo: The "no new privileges" flag is set, which prevents sudo from running as root (exit 1) +capsh --print + -> Current: = (no effective capabilities) + -> Bounding set = cap_sys_admin, ... (not effective; no-new-privs=1) +unshare --mount true / --pid --fork true / --net true + -> Operation not permitted (exit 1 each) ``` The workspace root is writable, but the host root filesystem and cgroup2 mount