Skip to content

feat: Agent-hidden repository secrets via microsandbox placeholder injection (OM-84) - #505

Open
chriswritescode-dev wants to merge 10 commits into
mainfrom
om-84-agent-hidden-repository-secrets-via
Open

chriswritescode-dev wants to merge 10 commits into
mainfrom
om-84-agent-hidden-repository-secrets-via

Conversation

@chriswritescode-dev

@chriswritescode-dev chriswritescode-dev commented Dec 4, 2026 •

Copy link
Copy Markdown
Owner

User description

Follow-up to OM-83. A repository secret can now be marked Hide from agents with a list of allowed hosts, and no agent process gets its real value. Host agent shells get an empty string. Sandboxed agent shells get a random per-VM placeholder, and microsandbox swaps in the real value at the network boundary only for HTTPS requests to the allowed hosts (https://docs.microsandbox.dev/security/secrets). Repository terminals and project actions still get the real value.

What changed

Storage and API

  • A migration adds agent_hidden and allowed_hosts to secrets.
  • SetSecretRequestSchema accepts agentHidden and allowedHosts:
    • A hidden secret needs at least one host and at most 20.
    • Hosts must be bare hostnames or *.domain wildcards. Schemes, ports, paths, IPs and single-label wildcards such as *.com are rejected.
    • Variables cannot be hidden.
  • A PUT that omits agentHidden keeps the stored flag and hosts. See "For the reviewer".

One resolver for every caller

  • RepoEnvService.resolveForDirectory takes a HiddenSecretPolicy:
    • withhold is the default and gives an empty value.
    • reveal is used by terminals.
    • placeholder is used by the sandbox.
  • The resolver returns the hidden bindings, and hidden values never go into mask lists.

Sandbox (sandbox/command.ts, sandbox/runtime.ts)

  • Each hidden secret becomes the msb secret OCM_SECRET_<NAME>, built from the session's location repository.
  • The placeholder is ocm-secret- plus 32 hex characters of HMAC-SHA256(AUTH_SECRET, VM name + secret name). It looks random, differs per VM, and stays the same across Manager restarts.
  • The real value is never written to the sandbox config:
    • The --secret-conf file holds only a $msb_env reference, and substitution is limited to request headers.
    • The file is written 0600 to a temp directory and deleted after msb create.
    • The value reaches msb create, msb start and msb modify through the child process env.
  • Attestation (sandboxSecretsMismatch) used to reject any network.secrets. It now requires exactly the expected set and rejects any of these differences: an extra, missing or duplicate entry, an inline value, a wrong env source, a wrong placeholder, a host set that differs or allows any host, query or body substitution, passthrough hosts, or require_tls_identity: false.
  • Lifecycle:
    • When the secret set (names, placeholders or hosts) changes, the VM goes through the existing check-and-recreate path.
    • When only a value changes, the new value is pushed live with msb modify --secret. If msb refuses, the VM is recreated.
  • Guest CA trust: after start or create, the msb interception CA at /.msb/tls/ca.pem is imported into the guest user's NSS db with certutil, so Chromium and Playwright trust it. Dockerfile.sandbox adds libnss3-tools.
  • A working directory outside the session's repository reuses the VM placeholder only when the secret's value and host set match. Otherwise it gets an empty value.

UI

  • The Secrets form has a "Hide from agents" checkbox with a hint, and an "Allowed hosts" field that appears once the box is checked.
  • List rows show a "Hidden from agents" badge and the hosts.
  • Editing preloads the flag and hosts. Unchecking the box clears them.
  • useSetSecret sends agentHidden only when it is defined.

Validation

Handback validation e273a6be-b7c1-4326-b82f-2142a1026877 passed on c1b8220b plus four review-fix commits (d6e5ef0b, 8f23959a, f0efe260, 94d38226).

  • The production build ran against OpenCode 2.4.2 and was driven in Playwright on desktop (1280×720) and mobile (iPhone 13). All 10 checks passed in both views:

    • The form controls, and the rejection of a hidden secret without hosts or with bad hosts.
    • Saving with two hosts, the badge, and the preloaded edit form.
    • Unhiding clears the flag.
    • Host agent shells get an empty value, and Bearer ${{secrets.X}} becomes Bearer .
    • Terminals get the real value.
    • An unhidden secret reaches agent shells again.
    • The Variables tab has no hide option.
    • Neither view overflows horizontally.
  • pnpm lint, pnpm typecheck and pnpm test pass (backend 223 files, frontend 4143 tests). The 14 Bun internal-sandbox tests also pass.

  • check_design conforms.

  • The axe-core audit found no new accessibility violations.

  • pr-agent ran 5 review rounds. Its fixes were:

    • Placeholder reuse requires a matching value and host set.
    • *.<tld> wildcards are rejected.
    • A test now restores AUTH_SECRET.
    • The hook no longer un-hides a secret by default.

    The remaining findings were declined as owner decisions: recreating the VM on a refused modify, terminals getting the real value, values passed by host-env reference, and dropping the hidden set when .ocm/project.json is untrusted.

For the reviewer

  • The sandboxed path has not run on a real VM. The validation runner has no /dev/kvm. Placeholder injection in the guest, the --secret-conf create, attestation against the real msb inspect shape, msb modify rotation and the NSS CA import are covered only by the mocked-msb unit tests. The post-merge KVM check from the plan still applies, after the Dockerfile.sandbox image is republished with libnss3-tools. It should also confirm that the msb host process environment does not leak OCM_SECRET_*.
  • Decision needed: what a PUT without the flag does. The plan said saving without agentHidden clears the flag. The API instead keeps the stored flag and hosts when the field is omitted, so a script that only rotates a value cannot un-hide a secret by accident. The UI always sends the flag. Confirm keep-on-omit as the contract, or ask for the plan's behavior.
  • Scope limits:
    • Hidden secrets work in the sandbox only with sandbox enforcement on. Without it, agents get an empty value.
    • Substitution covers HTTPS headers and Basic auth only, not the query string or body.
    • Only secrets in the session's location repository get a placeholder.
  • AUTH_SECRET is required. Placeholders are derived from it. Rotating it changes every placeholder, which recreates each VM that has hidden secrets once.
  • Cosmetic: the "Allowed hosts" label sits 4px under the hint (space-y-1). On mobile it reads as part of the hint. space-y-3 would separate them.

Agent documents


PR Type

Enhancement, Tests


Description

  • Add agent-hidden secret storage and validation

    • Migration adds agent_hidden and allowed_hosts
    • Schemas validate hostnames, wildcards, and limits
  • Resolve hidden secrets per caller policy

    • Withhold for agents, reveal for terminals
    • Placeholder mode for sandboxed shells
  • Inject secrets into microsandbox VMs

    • HMAC placeholders, --secret-conf, header-only substitution
    • Attest secrets, rotate via modify, trust interception CA
  • Add UI controls and comprehensive tests

    • Hide-from-agents checkbox with allowed hosts
    • Resolver, sandbox, route, and form coverage

Diagram Walkthrough

flowchart TD
  A["Secrets form"] -- "agentHidden + allowedHosts" --> B["Secrets API"]
  B -- "persist flags" --> C["secrets table"]
  C -- "values + hidden map" --> D["RepoEnvService"]
  D -- "withhold" --> E["Agent host shells"]
  D -- "reveal" --> F["Local terminals"]
  D -- "placeholder" --> G["Sandbox runtime"]
  G -- "secret conf + env" --> H["microsandbox VM"]
  H -- "HTTPS header substitution" --> I["Allowed hosts"]
Loading

File Walkthrough

Relevant files
Enhancement
10 files
secrets.ts
Add hidden secret and allowed-host validation schemas       
+67/-4   
secrets.ts
Persist hidden flags and expose hidden bindings                   
+62/-9   
repo-env.ts
Resolve hidden secrets differently per caller policy         
+37/-2   
command.ts
Build secret conf, placeholders, and attestation checks   
+188/-2 
runtime.ts
Inject, sync, and attest sandbox VM secrets                           
+177/-32
secrets.ts
Forward hidden options through the secret PUT route           
+10/-1   
terminal.ts
Reveal hidden secret values to local terminals                     
+1/-1     
202612031800-secrets-agent-hidden.ts
Add agent_hidden and allowed_hosts columns
+20/-0   
SecretsManager.tsx
Add hide-from-agents controls and badges                                 
+93/-4   
useSecrets.ts
Build secret requests with hidden fields                                 
+22/-3   
Configuration changes
1 files
index.ts
Register the secrets agent-hidden migration                           
+2/-0     
Dependencies
1 files
Dockerfile.sandbox
Install libnss3-tools for CA trust                   
+1/-0     
Tests
8 files
secrets.test.ts
Test hidden secret persistence and scoped resolution         
+97/-1   
repo-env.test.ts
Test withhold, reveal, and placeholder policies                   
+75/-1   
command.test.ts
Test secret conf, placeholders, and attestation                   
+246/-1 
runtime.test.ts
Test sandbox secret creation, rotation, and recreation     
+402/-4 
secrets.test.ts
Test hidden secret API validation and preservation             
+97/-0   
fake-msb.ts
Emulate microsandbox secret storage and modify                     
+49/-8   
SecretsManager.test.tsx
Test hidden secret form and list rendering                             
+107/-3 
secrets-agent-hidden-migration.test.ts
Test migration defaults and idempotent column adds             
+73/-0   
Additional files
7 files
secrets-scope-migration.test.ts +3/-8     
stub-repo-env-service.ts +1/-1     
internal-sandbox.test.ts +1/-0     
shell-env.test.ts +22/-2   
docker-config.test.ts +4/-0     
terminal.test.ts +26/-1   
useSecrets.test.tsx +74/-0   

Comment on lines +344 to +346
export function buildSandboxModifySecretArgs(name: string, secret: SandboxSecretSpec): string[] {
return ['modify', name, '--secret', `${secret.envVar}@${secret.allowedHosts.join(',')}`]
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unverified modify semantics

The rotation command passes only ENV@hosts. It does not pass the HMAC placeholder or the headers-only substitution setting. If msb's CLI fills in its own placeholder or substitution defaults when the secret is modified, the live config no longer matches sandboxSecretEntryMismatch. Every value rotation would then lead to a VM recreate on the next attestation. In the gap before that, the placeholder the agent holds would not be substituted. The fake msb treats modify as a no-op, so the tests cannot catch this. Check against msb's real modify behavior, or attest right after modify succeeds.

Comment on lines +1254 to +1256
const sessionHiddenSecrets = this.repoEnvService
? (await this.repoEnvService.resolveForDirectory(target.locationDirectory)).hiddenSecrets
: []

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Transient failure recreates VM

resolveForDirectory swallows errors and returns an empty result, so a temporary resolution failure looks the same as "no hidden secrets". The spec then has secrets: [], which changes secretSetKey and forces bootSessionSandbox. Attestation then fails with "network.secrets must be empty", and the VM is removed and recreated, killing the agent's running processes. The next call recreates it again with the secrets restored. Consider separating "resolution failed" from "no secrets", for example by keeping the last known secret set or skipping the reconcile when resolution failed.

@lil-worker-cstech

Copy link
Copy Markdown
Contributor

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⚠️ Risk checks: 10 finding(s) detected
Sensitive paths (5): Dockerfile.sandbox, backend/src/db/migrations/202612031800-secrets-agent-hidden.ts, backend/src/db/migrations/index.ts, backend/test/db/secrets-agent-hidden-migration.test.ts, backend/test/db/secrets-scope-migration.test.ts
Risky shapes (5): Removed JS/TS function in backend/src/services/sandbox/command.ts, Removed JS/TS function in backend/src/services/sandbox/runtime.ts, Removed JS/TS function in backend/src/services/sandbox/runtime.ts, Removed JS/TS function in backend/test/db/secrets-scope-migration.test.ts, Removed JS/TS function in backend/test/services/sandbox/runtime.test.ts
⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
⚠️ Risk level: Medium
✅ Merge recommendation: Needs review
Merge recommendation checks
⚠️ CI on reviewed head: pending
✅ Head is current with its base: up to date
⚠️ Human authors: bot or unlinked author
⚠️ No sensitive paths or risky changes: 10 finding(s)
⚠️ Changed lines within cap: 2045 changed lines exceeds cap 100
🔎 Failure modes

What: Rotating a secret's value may change the VM's stored secret settings. If `msb modify --secret ENV@hosts` resets the placeholder or the substitution scope, the next attestation sees a mismatch and recreates the VM. Until then, the agent's placeholder may not be swapped for the real value.
Where: backend/src/services/sandbox/runtime.ts:syncSandboxSecretValues
Trigger: A hidden secret's value changes while its session VM is running. About 5 seconds later, `bootSessionSandbox` attests the VM's config again.
Detected by: nothing; the fake `modify` command in `fake-msb.ts` does nothing and never changes the inspected config
Covered in this PR: No

What: A temporary failure to resolve the repository environment gives an empty hidden-secret set. The VM's secrets then no longer match, so the VM is removed and recreated, which kills the agent processes running in it.
Where: backend/src/services/sandbox/runtime.ts:SandboxRuntimeService.planShell
Trigger: `resolveForDirectory(target.locationDirectory)` hits a git, filesystem, or database error. It catches the error and returns `emptyResult()`.
Detected by: A warning log that reads "Recreating unverifiable sandbox ... network.secrets must be empty"
Covered in this PR: No

🧪 PR contains tests
🔒 No security concerns identified

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant