Skip to content

Commit 02740b4

Browse files
Test Userclaude
andcommitted
fix(rlm): add SAFETY comment to production set_var in auto_configure_llm
Resolves the P3-1 finding from #2797 (security-sentinel 2026-06-20 scan): `crates/terraphim_rlm/src/rlm.rs:942` called `unsafe { std::env::set_var(...) }` in a non-test auto-configure path with no `// SAFETY:` justification, unlike the established pattern in `terraphim_spawner/src/lib.rs` (pre_exec setrlimit). Rust 2024 made `set_var` unsafe because env vars are process-global and mutating one races concurrent readers. Document the invariant that makes this call sound: `auto_configure_llm` runs once at RLM startup initialisation, before any worker tasks or child processes are spawned, so there are no concurrent readers of `OPENROUTER_API_KEY` at the call site. The key value originates from a trusted source (env var or 1Password), not untrusted input. Comment-only change: zero behavioural change, zero new code. Surgical scope per the issue's P3-1 remediation note ("add SAFETY justification" — moving to a config struct is explicitly the alternative, not required). Verification (this session): - cargo check -p terraphim_rlm -> clean (5.30s) - cargo test -p terraphim_rlm --lib -> 135 passed, 0 failed - cargo fmt -p terraphim_rlm --check on this hunk -> clean (the crate-wide fmt drift reported is pre-existing on origin/main, unrelated to this comment) - cargo clippy -p terraphim_rlm --all-targets -> the 3 errors (2x doc-list-indent, 1x redundant_closure) are PRE-EXISTING on origin/main (verified by stashing this edit and re-running on pristine HEAD); this comment-only change introduces zero new lint findings - ubs crates/terraphim_rlm/src/rlm.rs -> 0 critical, 0 warning The P1-1 (LLM proxy 0.0.0.0:3456) and P1-2 (git2 unsound) findings from #2797 are host-infra / upstream respectively and out of scope for this surgical fix; P1-1 requires human root on bigbox (fleet agents blocked by no_new_privs per the recurring sentinel comments). Refs #2797 Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 55e959a commit 02740b4

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

  • crates/terraphim_rlm/src

crates/terraphim_rlm/src/rlm.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,16 @@ impl TerraphimRlm {
938938
.unwrap_or_else(|_| "meta-llama/llama-3.2-3b-instruct:free".to_string());
939939
role.llm_api_key = Some(key.clone());
940940
role.llm_model = Some(or_model.clone());
941-
// Cache for child processes and build_llm_from_role
941+
// Cache for child processes and build_llm_from_role.
942+
//
943+
// SAFETY: `set_var` is only unsafe in Rust 2024 because env vars are
944+
// process-global and mutating one can race concurrent readers. This
945+
// runs once during `auto_configure_llm` (RLM startup initialisation),
946+
// before any worker tasks or child processes are spawned, so there are
947+
// no concurrent readers of `OPENROUTER_API_KEY` at this point. The
948+
// value is propagated to spawned processes intentionally (see comment
949+
// above) and the key originates from a trusted source (env var or
950+
// 1Password), not untrusted input.
942951
unsafe {
943952
std::env::set_var("OPENROUTER_API_KEY", key);
944953
}

0 commit comments

Comments
 (0)