feat(cli): add --env flag to sandbox create/exec and fix env var passthrough#1730
feat(cli): add --env flag to sandbox create/exec and fix env var passthrough#1730russellb wants to merge 2 commits into
Conversation
Add `--env KEY=VALUE` (repeatable) to `openshell sandbox create` and `openshell sandbox exec`, wiring user-specified environment variables into the existing `SandboxSpec.environment` and `ExecSandboxRequest.environment` protobuf fields which were previously only accessible via the SDK/gRPC API. Fix a bug where user-specified environment variables set via `SandboxSpec.environment` were silently dropped inside sandbox SSH sessions. The SSH handler's `apply_child_env()` calls `env_clear()` to strip supervisor-internal secrets before spawning user shells, but this also wiped container-level env vars including user-specified ones. This was introduced in 5fd4885 (2026-02-26, "feat(sandbox): VS Code Remote-SSH support with platform detection fix") which added `env_clear()` to fix a VS Code platform detection timeout caused by leaked supervisor env vars. The fix introduces an `OPENSHELL_USER_ENVIRONMENT` sidecar env var: compute drivers (Docker, Podman, Kubernetes, VM) serialize the user-specified environment as JSON into this variable at container creation time, the sandbox supervisor deserializes it at startup, and the SSH handler re-injects the values into child processes after `env_clear()`. User-specified vars are applied at lowest priority so proxy, TLS, and provider credentials still take precedence. Signed-off-by: Russell Bryant <russell.bryant@gmail.com>
| .env("PATH", &path) | ||
| .env("TERM", term); | ||
|
|
||
| for (key, value) in user_environment { |
There was a problem hiding this comment.
Do we want the user to be able to override the environment variables in the preceding block?
|
Label |
/ok to test fada35e |
| /// Set by compute drivers from `SandboxSpec.environment`. The sandbox | ||
| /// supervisor deserializes this at startup and injects the variables into | ||
| /// SSH child processes (which use `env_clear()` for security isolation). | ||
| pub const USER_ENVIRONMENT: &str = "OPENSHELL_USER_ENVIRONMENT"; |
There was a problem hiding this comment.
Agent analysis: In docker/podman/vm the trailing environment.extend(user_env) runs after inserting the sidecar, so a user var literally named OPENSHELL_USER_ENVIRONMENT clobbers the JSON sidecar → supervisor deserialization fails → all user env silently dropped. Edge case, but cheap to defend (reserve/reject the key, or
insert
the sidecar
PR Review StatusValidation: this PR is project-valid as a concentrated CLI/sandbox feature and bug fix for environment injection and passthrough. No exact duplicate was found. Review findings:
Docs: missing for direct UX changes. The new Tests/E2E: Next state: |
|
/ok to test fada35e |
CI Follow-UpBranch Checks failed on both Rust jobs at Branch E2E is still running, Helm Lint passed, and the PR remains in |
E2E ResultBranch E2E completed with one failing job: This does not look directly caused by the env-var changes from the current evidence, but the required E2E status is red. The PR remains in |
- VM driver: restructure build_guest_environment to apply user env before required driver vars, preventing user-specified values from overriding OPENSHELL_ENDPOINT, PATH, and other critical vars. Matches the pattern already used by Docker/Podman/Kubernetes. - All drivers: insert OPENSHELL_USER_ENVIRONMENT sidecar after environment.extend(user_env) so a user-supplied key of the same name cannot clobber the JSON payload the supervisor parses. - Podman driver: fix template/spec env precedence — template env is now applied first with spec overwriting, matching the other drivers (spec is user-specified and should win over image defaults). - CLI: add parse_env_pairs() that rejects OPENSHELL_* prefixed keys with a clear error message. Both sandbox create and exec call sites updated to use it. - SSH handler: filter out OPENSHELL_* keys from user_environment before injecting into child processes. - Docs: add "Set Environment Variables" section to manage-sandboxes.mdx documenting --env for both create and exec, per-command override semantics, and the OPENSHELL_* reservation. - Tests: add sandbox_create_env_rejects_reserved_prefix test. - Fix rustfmt formatting in lib.rs, container.rs, driver.rs.
Fixes pushed in f190fa3Blocking: VM driver override-protectionRestructured Blocking: OPENSHELL_USER_ENVIRONMENT clobberableThree-layer defense:
Needs resolution:
|
|
/ok to test f190fa3 |
PR Review StatusValidation: this PR remains project-valid as a concentrated CLI/sandbox feature and bug fix for environment injection and passthrough. Review findings:
Docs: updated under Checks/E2E: Process blocker: GitHub currently reports Next state: |
Summary
Add
--env KEY=VALUE(repeatable) toopenshell sandbox createandopenshell sandbox exec, and fix a bug where user-specified environment variables were silently dropped inside sandbox SSH sessions.Related Issue
N/A — discovered during investigation of env var injection support for sandboxes.
Changes
CLI (new feature):
--env KEY=VALUErepeatable flag tosandbox createandsandbox execsubcommandsparse_key_value_pairs()helper for consistent KEY=VALUE parsingSandboxSpec.environmentandExecSandboxRequest.environmentprotobuf fields (which were previously only accessible via the SDK/gRPC API)Sandbox env passthrough (bug fix):
apply_child_env()callsenv_clear()to strip supervisor secrets before spawning user shells, but this also wiped user-specified env vars set on the container by compute drivers. Introduced in 5fd4885 (2026-02-26, "feat(sandbox): VS Code Remote-SSH support with platform detection fix").OPENSHELL_USER_ENVIRONMENTsidecar env var: compute drivers serializeSandboxSpec.environmentas JSON, the supervisor deserializes it at startup, and the SSH handler re-injects the values afterenv_clear()Testing
cargo test -p openshell-cli— 18 tests pass (including 2 new: env passthrough + invalid format rejection)cargo testacross all modified crates — 1,252 tests passcargo clippyclean across all modified crates--env FOO=BARappears in sandboxenvoutputChecklist