fix(ort): avoid implicit runtime discovery from the workspace (#4570)

## Description

When `ORT_DYLIB_PATH` is unset, native ONNX Runtime discovery
inadvertently searches `.venv` and `venv` directories under the current
working directory, even when neither represents an active environment.
Consequently, a caller launched inside an untrusted directory or
workspace could implicitly load a native binary from that location.

This change eliminates those two implicit roots from fallback discovery.
Workspace environments remain fully discoverable whenever explicitly
activated via `VIRTUAL_ENV` or `CONDA_PREFIX`. Standard user-install
locations and explicit `ORT_DYLIB_PATH` configuration remain unaffected.

Addresses the discovery-root behavior discussed in #4547 (hardening
fallback discovery paths when no runtime is explicitly pinned).

## Type of Change

- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature
- [ ] Breaking change

## Changes Made

- Stop implicitly searching the current workspace's `.venv` and `venv`
for native libraries.
- Cover both rejected implicit roots alongside explicitly activated
virtual environments (`VIRTUAL_ENV` / `CONDA_PREFIX`).
- Run each discovery regression in an isolated subprocess so environment
and directory mutations cannot leak into parallel test runs.

## Testing

- [x] Discovery regressions: **2 failed, 3 passed** on main; **5
passed** after the patch.
- [x] Standalone native executable exercises the public
`headroom_core::transforms::magika_detect` API against the compiled
library.
- [x] Pre-commit and commit-message hooks pass cleanly.
- [x] Pre-push verification (`make ci-precheck`): **1,676 Rust tests
passed, 3 ignored**; native extension rebuilt/imported cleanly; **178
Python tests passed, 5 skipped**; commitlint passed.

```text
Before: discovery_ignores_cwd_dot_venv FAILED
        discovery_ignores_cwd_venv FAILED
After:  5 passed
```

## Real Behavior Proof

- Environment: Windows x86-64, Rust 1.95.0, default `headroom-core`
features, `main` at `a2d4fe72381e186651466ee94e92ce9d8d1396cc`.
Standalone executable links the compiled core library and calls its
public detector without custom discovery overrides.
- Exact command / steps: Build the core library and compile the Rust
probe: `rustc --edition=2021 native_probe.rs --extern
headroom_core=<built libheadroom_core-*.rlib> -L dependency=<cargo
target>/debug/deps -o native_probe.exe`. Run from a scratch directory
containing a dummy
`.venv/Lib/site-packages/onnxruntime/capi/onnxruntime.dll` (`b"not a
native library"`) under a 15-second timeout to exercise unpinned
fallback discovery on `main` vs this patch.
- Observed result: On `main`, the non-empty detector call hangs/times
out at 15 seconds against the invalid stub DLL in cwd (clean cwd and
empty-input controls succeed). With this patch, the call returns
immediately with the expected clean initialization error because the
stub in cwd is ignored. Regressions confirm implicit cwd stubs are
excluded while explicitly activated virtual environments remain valid
candidates.
- Not tested: Valid ONNX inference, malicious native payloads, normal
Python proxy traffic, or native execution on Linux/macOS. No valid
native library was placed in the workspace.

```rust
fn main() {
    for name in ["ORT_DYLIB_PATH", "VIRTUAL_ENV", "CONDA_PREFIX", "USERPROFILE"] {
        std::env::remove_var(name);
    }
    std::env::set_var("HEADROOM_MAGIKA_INIT_TIMEOUT_SECS", "1");
    eprintln!("calling public magika_detect");
    let content = std::env::args().nth(1).unwrap_or_else(|| "hello".into());
    match headroom_core::transforms::magika_detect(&content) {
        Ok(kind) => println!("detected: {kind:?}"),
        Err(error) => println!("detector error: {error}"),
    }
    eprintln!("public call returned");
}
```

The native executable can be driven with:

```python
from pathlib import Path
import subprocess
import tempfile

executable = str(Path("native_probe.exe").resolve())
with tempfile.TemporaryDirectory() as directory:
    root = Path(directory)
    stub = root / ".venv/Lib/site-packages/onnxruntime/capi/onnxruntime.dll"
    stub.parent.mkdir(parents=True)
    stub.write_bytes(b"not a native library")
    try:
        result = subprocess.run(
            [executable, "hello"], cwd=root, capture_output=True,
            text=True, timeout=15,
        )
        print(result.returncode, result.stdout, result.stderr)
    except subprocess.TimeoutExpired:
        print("detector did not return within 15 seconds")
```

```text
Main, stub cwd, nonempty input: timeout at 15 seconds
Main, clean cwd:               normal no-runtime initialization error
Main, stub cwd, empty input:   PlainText, normal exit
Patch, stub cwd, nonempty:     normal no-runtime initialization error
```

The timeout observation reflects this specific Windows loader setup; the
contract of this patch is strictly boundary hardening for discovery
paths rather than a general loader timeout fix.

## Runtime Rollout Safety

- Rollout-managed feature(s): none.
- Minimum rollout channel: stable.
- Stable/default behavior changed: Fallback discovery no longer
implicitly trusts unactivated `.venv` / `venv` directories under the
current working directory.
- Kill switch / disable path: Explicitly configure `ORT_DYLIB_PATH`, or
activate the environment via `VIRTUAL_ENV` / `CONDA_PREFIX`.
- Unsafe override required: no new override flag.
- Qualification impact: Any callers relying on implicit unactivated
workspace venvs will need to activate the environment or set
`ORT_DYLIB_PATH`. No new dependencies, public API, or file-format
changes.
- Rollback path: revert this patch; no migration.

## Review Readiness

- [x] I have performed a self-review
- [x] This PR is ready for human review

## Additional Notes

I implemented and verified this with Codex assistance for drafting and
local tests.

<!-- devin-review-badge-begin -->

---

<a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/4570"
target="_blank"><picture><source media="(prefers-color-scheme: dark)"
srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img
src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4"
alt="Devin Review"></picture></a>
<!-- devin-review-badge-end -->

### Maintainer current-main requalification

Exact head `6e4efe83bc7ced929e4769e371ed024ad213247c`, upstream main
`e4a60ed72f6ef5b28569f8bfbe2163cfda32c948`.

Clean mechanical update from current upstream main. The production delta
remains removal of implicit cwd .venv/venv roots from native ONNX
Runtime discovery. Explicit VIRTUAL_ENV and CONDA_PREFIX roots and other
existing trusted discovery sources are preserved. No contributor
discovery policy was rewritten and no new finding surfaced in the
reviewed scope.

Fresh native Windows/Rust 1.95 compilation ran all five named discovery
cases, including both implicit-cwd rejection tests, both
explicit-environment acceptance tests and their isolated child. The log
confirms the current worktree compiled and all five ran, avoiding the
historical stale-target binary issue. Child processes enumerate a
harmless invalid-library fixture; they never load it. This qualifies
candidate enumeration, not loading an arbitrary library, a fresh Python
extension build, ML inference or every platform. rustfmt and
main-relative whitespace checks pass. Ten shared Gemini streamed-request
fixture cases also pass, with Ruff/format clean; that test-only repair
addresses independently verified main failures.

Prior author discussion and exact-head approval were read, including the
historical 20 ms CCR-expiry test failure outside this changed file. It
is not carried forward as a discovery finding or a reason to withhold
approval. Maintainers handle the new exact-head hosted checks; nothing
merged.


Exact-head hosted checks are being handled by maintainers. No new
finding in the reviewed scope; formal approval awaits exact-head hosted
qualification; nothing merged.

---------

Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
Co-authored-by: JerrettDavis <2610199+JerrettDavis@users.noreply.github.com>
Co-authored-by: JD Davis <mxjerrett@gmail.com>
This commit is contained in:
Yuchuan He
2026-11-05 10:47:01 -06:00
committed by GitHub
co-authored by Copilot JerrettDavis JD Davis
parent c6f5bf9549
commit 69fbd2390d
@@ -189,10 +189,8 @@ fn discover_onnxruntime_libraries() -> Vec<PathBuf> {
}
}
if let Ok(cwd) = std::env::current_dir() {
roots.push(cwd.join(".venv"));
roots.push(cwd.join("venv"));
}
// The cwd may be an untrusted agent workspace. Only discover an environment
// there when the operator explicitly selects it with VIRTUAL_ENV/CONDA_PREFIX.
if let Some(user_profile) = env_path("USERPROFILE") {
roots.extend(versioned_children(
@@ -527,6 +525,81 @@ pub fn map_magika_label(label: &str) -> ContentType {
mod tests {
use super::*;
fn check_discovery_root(directory: &str, active_env: Option<&str>) {
let root = tempfile::tempdir().unwrap();
let environment = root.path().join(directory);
#[cfg(target_os = "windows")]
let library = environment.join("Lib/site-packages/onnxruntime/capi/onnxruntime.dll");
#[cfg(target_os = "macos")]
let library =
environment.join("lib/python3.11/site-packages/onnxruntime/capi/libonnxruntime.dylib");
#[cfg(all(not(target_os = "windows"), not(target_os = "macos")))]
let library =
environment.join("lib/python3.11/site-packages/onnxruntime/capi/libonnxruntime.so");
std::fs::create_dir_all(library.parent().unwrap()).unwrap();
std::fs::write(&library, b"not a native library").unwrap();
// Isolate process cwd and environment from parallel unit tests. The
// child enumerates candidates only; it never loads the stub library.
let mut child = std::process::Command::new(std::env::current_exe().unwrap());
child
.arg("--exact")
.arg("transforms::magika_detector::tests::discovery_root_child")
.arg("--nocapture")
.current_dir(root.path())
.env_remove("VIRTUAL_ENV")
.env_remove("CONDA_PREFIX")
.env("HEADROOM_TEST_ORT_LIBRARY", &library)
.env(
"HEADROOM_TEST_ORT_EXPECTED",
if active_env.is_some() { "yes" } else { "no" },
);
if let Some(variable) = active_env {
child.env(variable, &environment);
}
let output = child.output().unwrap();
assert!(
output.status.success(),
"{}\n{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
}
#[test]
fn discovery_root_child() {
let Some(library) = env_path("HEADROOM_TEST_ORT_LIBRARY") else {
return;
};
let expected = std::env::var("HEADROOM_TEST_ORT_EXPECTED").unwrap() == "yes";
let candidates = discover_onnxruntime_libraries();
assert_eq!(
candidates.contains(&library),
expected,
"candidates: {candidates:?}"
);
}
#[test]
fn discovery_ignores_cwd_dot_venv() {
check_discovery_root(".venv", None);
}
#[test]
fn discovery_ignores_cwd_venv() {
check_discovery_root("venv", None);
}
#[test]
fn discovery_keeps_explicit_virtual_env() {
check_discovery_root(".venv", Some("VIRTUAL_ENV"));
}
#[test]
fn discovery_keeps_explicit_conda_prefix() {
check_discovery_root("venv", Some("CONDA_PREFIX"));
}
#[test]
fn ort_telemetry_stays_off_unless_explicitly_allowed() {
assert!(!ort_telemetry_opted_in(None), "unset must stay off");