From d6e5ef0b1fcb839bef140d2c12dac8eb67501c87 Mon Sep 17 00:00:00 2001 From: Forge Sandbox Date: Fri, 4 Dec 2026 01:49:18 +0000 Subject: [PATCH] Fix review findings validating OM-84 --- backend/src/services/repo-env.ts | 7 ++-- backend/src/services/sandbox/runtime.ts | 14 ++++++- backend/test/services/repo-env.test.ts | 2 +- backend/test/services/sandbox/runtime.test.ts | 38 ++++++++++++++++++- 4 files changed, 53 insertions(+), 8 deletions(-) diff --git a/backend/src/services/repo-env.ts b/backend/src/services/repo-env.ts index 2305bf87..f9368a19 100644 --- a/backend/src/services/repo-env.ts +++ b/backend/src/services/repo-env.ts @@ -24,7 +24,7 @@ export interface HiddenSecretBinding { export type HiddenSecretPolicy = | { kind: 'withhold' } | { kind: 'reveal' } - | { kind: 'placeholder'; placeholderFor: (name: string) => string } + | { kind: 'placeholder'; placeholderFor: (name: string, binding: HiddenSecretBinding) => string } export interface ResolvedRepoEnv { env: Record @@ -100,14 +100,15 @@ export class RepoEnvService { const hiddenSecrets = new Map() const hiddenValue = (name: string, value: string, allowedHosts: string[]): string => { + const binding: HiddenSecretBinding = { name, value, allowedHosts } if (!hiddenSecrets.has(name)) { - hiddenSecrets.set(name, { name, value, allowedHosts }) + hiddenSecrets.set(name, binding) } if (policy.kind === 'reveal') { return value } if (policy.kind === 'placeholder') { - return policy.placeholderFor(name) + return policy.placeholderFor(name, binding) } return '' } diff --git a/backend/src/services/sandbox/runtime.ts b/backend/src/services/sandbox/runtime.ts index 6f582f16..a831fffd 100644 --- a/backend/src/services/sandbox/runtime.ts +++ b/backend/src/services/sandbox/runtime.ts @@ -1281,10 +1281,20 @@ export class SandboxRuntimeService { } let mask: string[] = [] if (this.repoEnvService) { - const placeholders = new Map(secrets.map((secret) => [secret.name, secret.placeholder])) + const secretsByName = new Map(secrets.map((secret) => [secret.name, secret])) const repoEnv = await this.repoEnvService.resolveForDirectory(directory, { kind: 'placeholder', - placeholderFor: (secretName) => placeholders.get(secretName) ?? '', + placeholderFor: (secretName, binding) => { + const secret = secretsByName.get(secretName) + if (!secret || secret.value !== binding.value) return '' + const specHosts = new Set(secret.allowedHosts) + const bindingHosts = new Set(binding.allowedHosts) + if (specHosts.size !== bindingHosts.size) return '' + for (const host of specHosts) { + if (!bindingHosts.has(host)) return '' + } + return secret.placeholder + }, }) Object.assign(env, repoEnv.env) mask = repoEnv.maskValues diff --git a/backend/test/services/repo-env.test.ts b/backend/test/services/repo-env.test.ts index ee725814..80044aeb 100644 --- a/backend/test/services/repo-env.test.ts +++ b/backend/test/services/repo-env.test.ts @@ -333,7 +333,7 @@ describe('RepoEnvService', () => { expect(result.maskValues).toEqual(['plain-value']) expect(result.hiddenSecrets).toEqual([hiddenBinding]) expect(placeholderFor).toHaveBeenCalledTimes(2) - expect(placeholderFor).toHaveBeenCalledWith('HID') + expect(placeholderFor).toHaveBeenCalledWith('HID', hiddenBinding) }) it('keeps a hidden secret with an empty host list hidden', async () => { diff --git a/backend/test/services/sandbox/runtime.test.ts b/backend/test/services/sandbox/runtime.test.ts index d20dabc1..b88e14d4 100644 --- a/backend/test/services/sandbox/runtime.test.ts +++ b/backend/test/services/sandbox/runtime.test.ts @@ -353,7 +353,7 @@ function hiddenRepoEnv(secrets: TestHiddenSecret[]) { policy.kind === 'reveal' ? secret.value : policy.kind === 'placeholder' - ? policy.placeholderFor(secret.name) + ? policy.placeholderFor(secret.name, secret) : '' } return { env, maskValues: [], hiddenSecrets } @@ -1004,7 +1004,7 @@ describe('SandboxRuntimeService', () => { policy.kind === 'reveal' ? binding.value : policy.kind === 'placeholder' - ? policy.placeholderFor(binding.name) + ? policy.placeholderFor(binding.name, binding) : '' } return { env, maskValues: [], hiddenSecrets: bindings.map((binding) => ({ ...binding })) } @@ -1025,6 +1025,40 @@ describe('SandboxRuntimeService', () => { ) }) + it('withholds a working directory hidden secret whose value differs from the session secret', async () => { + enableEnforcement() + installFakeMsb(new Map()) + const nested = path.join(repoADir, 'nested') + mkdirSync(nested, { recursive: true }) + const sessionSecret: TestHiddenSecret = { name: 'TOKEN', value: 'hunter2', allowedHosts: ['api.example.com'] } + const workingSecret: TestHiddenSecret = { name: 'TOKEN', value: 'other-value', allowedHosts: ['api.example.com'] } + const repoEnv = { + resolveForDirectory: vi.fn(async (dir: string, policy: HiddenSecretPolicy = { kind: 'withhold' }) => { + const bindings = dir === repoADir ? [sessionSecret] : [workingSecret] + const env: Record = {} + for (const binding of bindings) { + env[binding.name] = + policy.kind === 'reveal' + ? binding.value + : policy.kind === 'placeholder' + ? policy.placeholderFor(binding.name, binding) + : '' + } + return { env, maskValues: [], hiddenSecrets: bindings.map((binding) => ({ ...binding })) } + }), + } satisfies Pick + const withSecrets = new SandboxRuntimeService(db, repoEnv) + + const plan = await withSecrets.planShell(nested, shellTarget(repoADir)) + + expect(plan).toEqual({ + mode: 'sandbox', + name: VM_NAME, + workdir: nested, + env: { TOKEN: '' }, + }) + }) + it('leaves a hidden secret with no allowed hosts out of the VM and withholds it', async () => { enableEnforcement() installFakeMsb(new Map())