mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
Fix review findings validating OM-84
This commit is contained in:
1 parent
c1b8220bc3
commit
d6e5ef0b1f
4 files changed
+53
-8
No files matched your search
@@ -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<string, string>
|
||||
@@ -100,14 +100,15 @@ export class RepoEnvService {
|
||||
const hiddenSecrets = new Map<string, HiddenSecretBinding>()
|
||||
|
||||
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 ''
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 () => {
|
||||
|
||||
@@ -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<string, string> = {}
|
||||
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<RepoEnvService, 'resolveForDirectory'>
|
||||
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())
|
||||
|
||||
Reference in new issue
Block a user