Issue #1597: std::env: the environment is untrusted (ADR-397), and env::vars lists it without crashing or handing out secrets by accident

Open and answer this issue on GitHub.

blocked, safety · opened 2026-11-14 · updated 2026-11-16 · 2 comments

Found while checking #669 (closed 2026-11-14): env::var, env::temp_dir and env::current_exe are built, but there is no way to list the environment. The owner decided on 2026-11-14 that reading environment variables is basic functionality, so the list is built. The owner also decided that the environment is data from outside, treated like every other outside data: it is untrusted, and where it goes (a log, output) it is escaped as untrusted data is, never written raw. That is recorded as ADR-397 (changes ADR-010 D2, as ADR-365 did for standard input and files), commit b9c9b9d.

Decision

ADR-397 (owner, 2026-11-14):

env::vars (supervisor, by the language principles; text goes into the specification with item 1): env::vars(prefix: String) -> [(String, String)] returns every variable whose name starts with prefix, as (name, value) pairs, sorted by name.

The safety rules, and why each one is there:

  1. prefix is required. env::vars("") lists everything, but the caller has to write that.
    • Why: the environment holds secrets (tokens, passwords, AWS_*). A program that needs APP_* should not get the rest. The normal case (configuration under one prefix) then asks for exactly what it uses.
    • It also makes the list deterministic and testable.
  2. The same view as env::var. A variable that is empty or not Unicode is left out of the list, so vars never shows a variable that var reports as null.
    • It must never panic. Rust’s std::env::vars() panics on a non-Unicode name or value. A program that crashes because of what its caller put in the environment is a denial of service. Built on vars_os (or the equivalent) with a filter.
  3. Sorted by name. The operating system’s order is arbitrary. A sorted list makes output reproducible, and a test can compare it.
  4. No set_var / remove_var. This issue only reads.
    • Why: changing the environment while the program runs is not thread-safe. It is unsafe in Rust 2024 because another thread or a C library may read it at the same moment.
    • A child process gets a different environment through process::run(...; env:, env_clear:, env_remove:) (ADR-243 D5).
  5. Ordering as for env::var, trust by ADR-397.
    • Ordering: sync, no touches line, so it orders against every other effect (Part III 13.5).
    • Trust: untrusted (ADR-397 D1); trusted: true as for env::var (D2).
  6. The specification warns in plain words.
    • The values may be secrets: do not print, log or send the list as it is.
    • A program started by a CGI-style host, or with privileges, gets an environment the caller chose.

Done when

To build

  1. Specification: in docs/specification/30-nikaia-tooling.md, std::env (around line 1523; the trust sentence from ADR-397 is already there):
    • Add env::vars(prefix: String) -> [(String, String)] with rules 1-3 and the warning of rule 6, and its trusted: option.
    • “All three are sync” becomes “All four”.
  2. env::var is untrusted (ADR-397 D1, D2), in the provenance the compiler keeps for std’s sources, where standard input and fs reads are classified (ADR-365; crates/nikaia-std/src/tools/trust.nika, the ledger’s trusted: markers in ledger.nika), written in Nikaia.
    • Test, built like tests/compiler/tests/a_file_or_stdin_read_is_untrusted_unless_marked.nika: an assert about env::var("X") is refused with NK1202; the same with trusted: true is checked.
    • Test: trusted: true on env::var appears in the ledger, as in trusted_markers_are_recorded_in_the_ledger.nika.
    • Our own code that asserts about or keys a map with an environment value meets this: each place gets a guard, or trusted: true where the operator chose the value (a test runner’s own setting). The report lists every place and which.
  3. env::vars in std, written in Nikaia (CLAUDE.md, Written in Nikaia only), next to crates/nikaia-std/src/env.rs, untrusted like env::var.
    • If listing the environment cannot be reached from Nikaia without a new Rust primitive, this step stops before the first line: Result: blocked, Found: the missing primitive, Needs: supervisor decision.
    • Test: tests/compiler/tests/env_vars.nika with .stdout, built like env_var.nika. It starts itself as a child with process::run(me, ["child"]; env_clear: true, env: [("NIKAIA_V_B", "2"), ("NIKAIA_V_A", "1"), ("NIKAIA_V_EMPTY", ""), ("OTHER", "x")]).
    • The child prints env::vars("NIKAIA_V_"). Expected rows, in this order: NIKAIA_V_A=1, NIKAIA_V_B=2. The empty variable and OTHER are missing.
    • The child also prints the count of env::vars(""), which is 3: everything except the empty one.
  4. Non-Unicode does not crash (Unix only, if the test runner can mark a test per platform; otherwise a probe in the report).
    • The child gets a variable whose value is the bytes 0xFF 0xFE, if process::run can pass bytes.
    • env::vars("") leaves it out and does not panic.
    • If process::run cannot pass non-Unicode bytes, say so in the report. That is a finding, not a workaround.

Files and limits

Ruled out

Comments

keywan-ghadami, 2026-11-14

Brief queued: issue-1597


agent: builder model: opus max_turns: 200 budget_usd: 30 effort: medium —

Issue #1597

Your work order is issue #1597, inlined below with its comments. There is no brief written around it: plan your own steps from its To build list, build them in order, commit after each finished step, and end when its Done when holds (or at the limits of builder.md, reporting what is left). The facts in your prompt are Core of docs/builder-facts.md; more on a topic: nikaia run scripts/syntax.nika -- KEYWORD, a probe: nikaia run scripts/probe.nika -- SNIPPET.

Measure the issue against docs/issues.md while you work: what it lacked, what was wrong in it. That answer goes into the report’s Issue: field. A finding, a changed plan or a finished item also goes into the issue as a comment (gh api repos/Nikaia-Foundation/Nikaia/issues/1597/comments -F body=@file, ending with the Claude Code footer), rule I13.

Rules (CLAUDE.md; shared checkout)

Report (at most 15 lines, to exactly /home/user/Nikaia/docs/briefs/trust/issue-1597.md.report.md)

Fields: Result, Files, Commit, Body (optional), Fixes, Pushed, Tests, Gate, Deviation, Found, Needs, Changelog, Issue, Owner (a question for the owner), Disk freed, and last Attribution: with your commit trailer lines below it.


Generated by Claude Code

keywan-ghadami, 2026-11-14

Run: issue-1597, Result: blocked

Tokens: input 84, cache read 2330890, cache write 65063, output 17238 Cost: 2.71 USD, time 11 min 14 s, turns 42

Report

Result: blocked Files: crates/nikaia-std/std.contracts docs/specification/30-nikaia-tooling.md tests/compiler/tests/an_environment_variable_is_untrusted_unless_marked.nika tests/compiler/tests/an_environment_variable_is_untrusted_unless_marked.stdout Commit: env::var is an untrusted source unless marked trusted: true (ADR-397) Body: std’s ledger gives env::var provenance = "untrusted" and the option trusted: bool = false (ADR-397 D1, D2), so an assert about it is NK1202, a map keyed by it gets the keyed hash, and trusted: true is checked as any claim, named by --trust and recorded in the ledger. No Rust and no .nika changed: the checker, prover and ledger already read provenance from std’s ledger. Our own code: no place asserts about or keys a map with an environment value (lower-std left lowered/ unchanged; scripts/drive.nika, scripts/gate.nika check as before; no use in tests/accepted or packages), so no guard and no marker was added. The specification’s std::env documents env::vars(prefix) (rules 1-4, 6, trusted:), “All four”. Part: #1597 Pushed: local 194d42b5365eb06193207d1751548cdd7d45bda1 Tests: new an_environment_variable_is_untrusted_unless_marked passes on the new binary and FAILED on the before binary (no NK1202, trusted: was NK1109, no –trust line); env_var (stdout byte-identical), a_lent_match_binding_is_the_pattern_of_a_text_method_as_it_is, a_file_or_stdin_read_is_untrusted_unless_marked, trusted_markers_are_recorded_in_the_ledger pass –both-settings; lower-std clean with no lowered change; pre-push green Gate: green Deviation: items 3-4 (env::vars in std, its env_vars test, the non-Unicode test) not built: blocked on a missing primitive, below; the trust test is one new program covering the assert, guard, trusted: true, –trust and the ledger record, instead of also extending trusted_markers_are_recorded_in_the_ledger Found: (1) no Nikaia path lists the environment: std has no primitive for it (env.rs has only var, temp_dir; ADR-302 foreign calls cannot walk C’s environ, a global, not a function), so env::vars needs a new Rust primitive in crates/nikaia-std/src/env.rs (built on std::env::vars_os, filtering empty and non-Unicode, sorted) plus its ledger entry; probe: use std::env\nfn main() throws { println(env::vars("").len()) } gives NK1171 env has no function called vars. (2) process::run’s env: is Vec[(String, String)], so a test cannot hand a child a non-Unicode value (item 4). (3) env::current_exe is in the specification (30-nikaia-tooling.md std::env) and #669 is called closed, but it is neither in env.rs nor in std.contracts. (4) ADR-399 makes cli::args untrusted, but std.contracts still marks cli::args and cli::Args::* provenance = "trusted". Needs: supervisor decision: a Rust primitive for listing the environment in crates/nikaia-std/src/env.rs (the one function item 3 needs), or another way to reach it from Nikaia Changelog: env::var is untrusted (ADR-397): an assert about an environment value is NK1202 unless the call says trusted: true, maps keyed by it get the keyed hash, and the marker is recorded in the ledger; the specification documents env::vars(prefix). Issue: wrong I7: the provenance of std’s sources is the provenance column of crates/nikaia-std/std.contracts, not trust.nika or ledger.nika (they read it), so item 2 was a ledger edit; wrong I9: item 3 orders env::vars “written in Nikaia” though no primitive to list the environment exists, which the issue foresaw only as a stop Owner: none Disk freed: none; target/nikaia-project-tests 7.3G (over the 2G limit before this run), / 8.7G free, no new cache entry over 100 MB Attribution: Co-Authored-By: Claude Opus 5.5 noreply@anthropic.com Claude-Session: https://claude.ai/code/session_01UVxJDv4ha75BSeuJhmqFAL

Debrief

  1. Most time: finding where std’s sources get their trust. The issue pointed at trust.nika and ledger.nika, but the classification is the provenance column in crates/nikaia-std/std.contracts. It took about 8 turns of grep. The lower-std run (4 minutes) and a broad grep -r over tests/ that timed out cost wall time too.
  2. Looked up: how a source is marked untrusted (provenance plus the ; trusted: bool = false option in std.contracts); that prover_calls.nika reads it from the ledger, so no code change was needed; whether anything can list the environment from Nikaia (env.rs, ADR-302 foreign calls); and what process::run’s env: takes.
  3. Checks run:
    • Useful: the build; the before/after probes; the new test on both binaries; the four touched tests under --both-settings; check of scripts/drive.nika and scripts/gate.nika; pre-push.
    • Mostly wasted: lower-std. Only the ledger changed and lowered/ came out unchanged, though it did confirm our own code was unaffected. Also the background grep -r tests/.
  4. One change: the brief should say up front: “std’s sources are classified in std.contracts (provenance, trusted: option); a new source is a ledger edit, not a .nika change”. It should also say whether a new Rust primitive in env.rs is pre-approved for env::vars.
  5. What the issue lacked:
    • wrong I7: it names trust.nika and ledger.nika as where provenance lives; it is std.contracts.
    • wrong I9: item 3 orders env::vars “in Nikaia”, but no primitive to list the environment exists. The blocker was foreseeable and could have been decided before the run.
    • broke I5: no probe showing env::vars missing (NK1171) or env::var currently trusted.
    • rules-gap: it states env::current_exe is built (#669), which is false.
  6. Traps for the next builder:
    • Fact: Core A std source’s trust is the provenance line plus the trusted: bool = false option in crates/nikaia-std/std.contracts; the checker and prover read it from there, so making a source untrusted needs no .nika change.
    • Fact: Core std.contracts is compiled into the binary (include_str!), so a ledger edit needs only cargo build; lower-std changes nothing unless a tool file uses that source.
    • Fact: Core process::run’s env: is Vec[(String, String)], so no test can hand a child a non-Unicode variable.

Generated by Claude Code

Back to Open Issues