feat(stdlib): add explicitly granted env API

This commit is contained in:
Marqdo Agent committed 2026-11-21 08:40:54 +08:00
1 parent ae69391bc0
commit 9922bfae98
17 files changed
+322 -15

No files matched your search

+7 -6
View File
@@ -58,7 +58,7 @@ byte-identical).
cargo fmt --all -- --check PASS
cargo clippy --workspace --all-targets -- -D warnings PASS
cargo test --workspace --locked PASS
474 passed, 0 failed, 2 ignored; includes 7/7 Linux security tests
476 passed, 0 failed, 2 ignored; includes 7/7 Linux security tests
```
The documentation and test migration is local work; re-run this block after it
@@ -203,11 +203,12 @@ This is still a **local VM lane**, not a self-hosted GitHub Actions security URL
`删除`/`重命名` over `resource.request` (ADR-0041); `data/json`/`data/csv`
provide recursive JSON and quoted single-row CSV product parsers; `path` and a
`regex` subset are pure `.mq.md`; `datetime` is pure UTC ISO-8601 and
`clock.现在` reads `clock.now` in project mode (ADR-0042). Append/seek/watch,
recursive walk, streaming JSON/CSV, a JSON depth bound, regex groups/
alternation and `env`/`process` remain open.
`clock.现在` reads `clock.now` in project mode (ADR-0042); `env.读取` reads
only exact `--allow-env NAME` grants in project mode (ADR-0043). Append/seek/
watch, recursive walk, streaming JSON/CSV, a JSON depth bound, regex groups/
alternation and `process` remain open.
- Bundled standard library (CM-A10, ADR-0028): `stdlib/` ships
`format`/`text`/`io`/`fs`/`data`/`path`/`regex`/`datetime`/`clock`; explicit `std:` imports resolve only inside the bundle;
`format`/`text`/`io`/`fs`/`data`/`path`/`regex`/`datetime`/`clock`/`env`; explicit `std:` imports resolve only inside the bundle;
a SHA-256 digest over `*.mq.md` is verified before loading; Analysis records
`project.stdlib`; a frozen-binary test asserts the CLI hash is unchanged after
running a `std:io` project and that tampering is rejected. `打印/print` is
@@ -233,7 +234,7 @@ This is still a **local VM lane**, not a self-hosted GitHub Actions security URL
status/index links and SHA-pinned evidence; `crates/cli/tests/architecture_boundaries.rs`
pins the workspace graph, `marqdo-runtime -> marqdo-host`, the bundled stdlib
digest and the stdio/file/TCP endpoints; `crates/cli/tests/stdlib_product.rs`
runs every public stdlib module (`clock/console/csv/datetime/demo/format/fs/http/io/json/path/regex/stream/table/text/utf8`)
runs every public stdlib module (`clock/console/csv/datetime/demo/env/format/fs/http/io/json/path/regex/stream/table/text/utf8`)
through `std:` imports over `stdlib/tests/*.mq.md`. `tools/pack-stdlib.py`
now sorts `.mq.md` by the same relative path key as the CLI. Source/test baseline
`0d40de7`; later docs-only commits keep code/test bytes identical. Evidence:
+48
View File
@@ -0,0 +1,48 @@
# ADR-0043: Explicitly granted `env.get` resource operation
Status: **Accepted**
Date: 2026-11-21
Relates to: ADR-0041, ADR-0042
Specs: `spec/security.md`, `spec/evaluation.md`
Evidence: `docs/evidence/2026-11-21-env-product.md`
## Context
A product `std:env` module needs to read process environment variables. The
environment is an ambient OS side channel that can contain secrets and
credentials. Reading it through a wildcard path grant or by default would
silently widen the capability surface; `env.list` would expose every variable
without naming it.
## Decision
Add one operation to `resource.request`:
```text
resource.request("env.get", [name]) -> {ok:true, value:Text}
| {ok:false, code:"env.not_found"}
| {ok:false, code:"env.not_unicode"}
```
* Arguments: exactly one `Text` variable name. Non-Text or wrong arity is a
fatal type/arity error.
* Grants: a new `Capabilities::env: Vec<String>` with **exact name matching**
(no glob). `--allow-env NAME` adds one grant; un-granted names return
`permission.denied` before the OS is read.
* The check happens before `std::env::var`. Project mode only; `pure` and
`strict` refuse. No default grant.
* Missing variables and non-UTF-8 variables are recoverable
`env.not_found`/`env.not_unicode`, not fatal errors.
* There is no `env.list`, `env.set`, process spawning or `std::env::set_var`
in this decision. A future `env.list` would need a separate ADR because it
changes the disclosure model.
* `stdlib/env.mq.md` is a thin `.mq.md` facade: `读取` returns the Result and
`存在` maps `env.not_found` to `{ok:true, value:false}`.
## Consequences
* The environment is readable only by exact, operator-supplied names in
project mode.
* The CLI gains `--allow-env NAME`; strict mode rejects it with exit 2.
* `std:process` remains unsupported in this decision; process execution needs
its own capability and OS-sandbox story.
+4 -2
View File
@@ -18,7 +18,7 @@ platform".
| Dimension | Current state | Source |
|---|---|---|
| Implemented | Rust workspace `0.1.0`: Markdown/frontend/IR, tree-walk runtime, `Memory`, capability host, Tool/Analysis/Trace, Plugin ABI, Foreign kernels, bundled stdlib, CLI composition root | `Cargo.toml`, `crates/`, `stdlib/` |
| Tested locally | `cargo test --workspace --locked`: **474 passed, 0 failed, 2 ignored** on Linux x86_64 at the baseline | `docs/STATUS.md` §3 |
| Tested locally | `cargo test --workspace --locked`: **476 passed, 0 failed, 2 ignored** on Linux x86_64 at the baseline | `docs/STATUS.md` §3 |
| Bundled stdlib released | In-repo bundle `marqdo-stdlib@0.1.0`, SHA-256 digest verified before loading; it is **not** an external registry package | `stdlib/stdlib.json`, `stdlib/README.md` |
| Multi-platform verified | Linux local gate is the only executed lane here; Windows/macOS native lanes are **UNVERIFIED**, and the GitHub security lane remains a **VM-only** gate | `docs/STATUS.md` §1, §4 |
@@ -94,6 +94,7 @@ review RFC, not a new crate per provider.
| `std:regex` | `regex.匹配/搜索` with `^ $ . * + ? [...]` escapes | implemented subset; no groups/alternation | `stdlib_product` |
| `std:datetime` | `datetime.格式化(毫秒)` / `datetime.解析(文本)`; UTC ISO-8601, pre-1970 supported | implemented (pure) | `stdlib_product` |
| `std:clock` | `clock.现在()` -> `resource.request("clock.now", [])` | implemented (project mode; pure/strict denied) | `resource_streams`, `stdlib_product` |
| `std:env` | `env.读取/存在` -> `resource.request("env.get", [name])`; exact `--allow-env` grant | implemented (project mode; no `env.list`) | `resource_streams`, `stdlib_product` |
| file metadata / directory / rename / remove / mkdir | not part of the current Resource contract | **not implemented**; needs a separate Resource Operation ADR | — |
| TCP | `resource.open("tcp", ip:port, ["read","write"])`; exact IP grant, 5 s timeout | implemented; no DNS/TLS/server socket/UDP | `tcp_endpoint`, `http_over_tcp` |
| HTTP/1.1 | `stdlib/net/http.mq.md`: status/header/chunked-byte parse, request build | implemented over an already-authorized TCP stream; not a DNS/TLS client | `stdlib_http`, `http_over_tcp`, `stdlib_product` |
@@ -125,7 +126,7 @@ that a phase is complete.
| P1 | `crates/cli/src/commands.rs` and `crates/marqdo-host/src/host.rs` still mix multiple responsibilities | open refactor, behavior-preserving |
| P2 | HTTP parsing is Unicode-codepoint oriented; a general byte body is not the only path | open; convert before a general network release |
| P2 | No DNS, TLS, listener or UDP provider | open |
| P2 | `std:path`, a regex subset, `std:datetime` and `std:clock` are implemented; no `std:env`, `std:process`, secure random or compression | open |
| P2 | `std:path`, a regex subset, `std:datetime`, `std:clock` and `std:env` are implemented; no `std:process`, secure random or compression | open |
| P2 | Memory limits: 16 MiB per region, 64 MiB per run | current budget, not a product limit |
| P3 | No concurrency tasks, atomics or cancellable long-running scheduling | open; separate ADR |
| P3 | No native optimizer/JIT or columnar numeric backend | open; benchmark-first |
@@ -157,5 +158,6 @@ that a phase is complete.
- [Stable-release implementation plan](../STABLE_RELEASE_IMPLEMENTATION.md) — next-phase plan; not implemented behavior.
- [ADR-0041: non-streaming resource operations](../adr/0041-resource-operation-fs.md) — accepted `resource.request` `fs.*` contract.
- [ADR-0042: `clock.now` resource operation](../adr/0042-clock-resource-operation.md) — accepted project-mode Unix-millis clock.
- [ADR-0043: `env.get` resource operation](../adr/0043-env-resource-operation.md) — accepted exact-name environment grants.
- [Compute-machine RFC-0001](../MARQDO_COMPUTE_MACHINE_RFC_0001.md) and [core/stdlib RFC-0002](../MARQDO_COMPUTE_CORE_STDLIB_MIGRATION_RFC_0002.md) — design input.
- [Architecture audit and refactor RFC](Marqdo_CLI_Latest_Architecture_Audit_and_Refactor_RFC.md) — review input for this migration.
+1
View File
@@ -47,6 +47,7 @@ The implemented first slices of these RFCs are recorded in `CURRENT.md` and
| Stdio stdin/stderr plus stdlib stream/console APIs | [0040](../adr/0040-stdio-stdin-stderr-and-stdlib-streams.md) | Accepted (stdin read, separate stdout/stderr, product stdlib) |
| Non-streaming `resource.request` `fs.*` operations | [0041](../adr/0041-resource-operation-fs.md) | Accepted (project mode, grant-checked, recoverable fs Result) |
| `clock.now` resource operation | [0042](../adr/0042-clock-resource-operation.md) | Accepted (project mode, Unix millis) |
| `env.get` resource operation | [0043](../adr/0043-env-resource-operation.md) | Accepted (exact-name project grant) |
| Table physical storage (T1/T2/T3), strict stdin/stderr/file/network resources | — | **Not decided**; do not implement by implication |
## Evidence and status
+43
View File
@@ -0,0 +1,43 @@
# Evidence: `std:env` and the `env.get` grant
Date: 2026-11-21 (Asia/Shanghai)
Source baseline: `ae69391bc0e51e0b87f3ba15a3679111c40a1231`
Repository: `marqdo-cli`
Decision record: [ADR-0043](../adr/0043-env-resource-operation.md)
RFC item: P2 environment product API
## Implemented
`resource.request("env.get", [name])` is a project-mode operation:
- exactly one Text name; wrong arity/type is fatal;
- `Capabilities::env` is an exact-name list; `--allow-env NAME` adds one
grant; un-granted names return `permission.denied` before `std::env::var`;
- missing names are recoverable `env.not_found`; non-UTF-8 values are
`env.not_unicode`;
- no `env.list`, `env.set` or process execution.
`stdlib/env.mq.md` is a thin `.mq.md` facade: `读取` returns the Result and
`存在` maps `env.not_found` to `{ok:true, value:false}`.
## Tests
`stdlib/tests/env.mq.md` runs through `std:env` with
`--allow-env MARQDO_TEST_ENV --allow-env MARQDO_MISSING_ENV`; the harness sets
`MARQDO_TEST_ENV=env-value`. `crates/marqdo-host/tests/resource_streams.rs`
pins exact grants, pure-mode refusal, `env.not_found` and arity.
```text
cargo fmt --all -- --check PASS
cargo clippy --workspace --all-targets -- -D warnings PASS
cargo test --workspace 476 passed, 0 failed, 2 ignored
cargo test -p marqdo-host --test resource_streams 22 passed
cargo test -p marqdo-cli --test stdlib_product 13 passed
python3 tools/pack-stdlib.py PASS
```
## Not covered
No environment enumeration, mutation, expansion, per-call delegation or
process execution. `std:process` remains open and needs its own ADR and OS
sandbox story.