fix(secrets): forward only planned variables into the sandbox and cache repo env resolution

The sandbox shim forwards only the plan's pinned names, so turning sandbox Git credentials off withholds tokens. Sandboxed shells resolve repo env once, terminals on linked servers get no local env, mapping keys cannot override git credentials, masks are kept per directory, and directory resolution is cached. A legacy gh-env alias keeps adopted OpenCode processes working for one release.
This commit is contained in:
Chris Scott committed 2026-10-23 23:47:44 -04:00
1 parent a0d1d91f18
commit 3e51280894
36 files changed
+1010 -259

No files matched your search

+17 -4
View File
@@ -1,6 +1,6 @@
import { z } from 'zod'
import { TerminalInfoSchema } from './terminal'
import { EnvironmentNameSchema, isReservedEnvName } from './secrets'
import { ENV_NAME_PATTERN, EnvironmentNameSchema, isReservedEnvName } from './secrets'
import { parseEnvExpressionReferences } from '../utils/env-expressions'
export const ProjectActionIconSchema = z.enum([
@@ -34,7 +34,15 @@ export const ProjectItemSourceSchema = z.enum(['personal', 'repo'])
export const REPO_PROJECT_ENV_MAX_ENTRIES = 100
export const REPO_PROJECT_ENV_NAME_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*$/
const REPO_PROJECT_ENV_RESERVED_NAMES = ['GH_TOKEN'] as const
const REPO_PROJECT_ENV_RESERVED_PREFIXES = ['GITHUB_', 'GIT_'] as const
function isRepoProjectEnvGitReserved(name: string): boolean {
const normalized = name.toUpperCase()
if ((REPO_PROJECT_ENV_RESERVED_NAMES as readonly string[]).includes(normalized)) return true
return REPO_PROJECT_ENV_RESERVED_PREFIXES.some((prefix) => normalized.startsWith(prefix))
}
export const RepoProjectEnvValueSchema = z
.string()
@@ -50,10 +58,16 @@ export const RepoProjectEnvSchema = z
.record(z.string(), RepoProjectEnvValueSchema)
.superRefine((env, ctx) => {
for (const key of Object.keys(env)) {
if (!REPO_PROJECT_ENV_NAME_PATTERN.test(key)) {
if (!ENV_NAME_PATTERN.test(key)) {
ctx.addIssue({ code: 'custom', message: `${key} is not a valid environment variable name`, path: [key] })
} else if (isReservedEnvName(key)) {
ctx.addIssue({ code: 'custom', message: `${key} is reserved for Manager or the runtime`, path: [key] })
} else if (isRepoProjectEnvGitReserved(key)) {
ctx.addIssue({
code: 'custom',
message: `${key} is reserved for git authentication; map the value in a local env file or use a secret instead`,
path: [key],
})
}
}
})
@@ -164,7 +178,6 @@ export type ProjectAction = z.infer<typeof ProjectActionSchema>
export type WorktreeSetupCommands = z.infer<typeof WorktreeSetupCommandsSchema>
export type ProjectItemSource = z.infer<typeof ProjectItemSourceSchema>
export type RepoProjectFile = z.infer<typeof RepoProjectFileSchema>
export type RepoProjectEnv = z.infer<typeof RepoProjectEnvSchema>
export type ProjectConfigResponse = z.infer<typeof ProjectConfigResponseSchema>
export type UpdateProjectActionsRequest = z.infer<typeof UpdateProjectActionsRequestSchema>
export type UpdateWorktreeSetupRequest = z.infer<typeof UpdateWorktreeSetupRequestSchema>
+10 -1
View File
@@ -7,8 +7,17 @@ export type SecretKind = z.infer<typeof SecretKindSchema>
export const SECRET_VALUE_MAX_BYTES = 48 * 1024
export const ENV_NAME_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*$/
const RESERVED_ENV_PREFIXES = ['OCM_', 'OPENCODE_', 'GIT_CONFIG_', 'MSB_', 'LD_'] as const
/**
* Prefixes the sandbox owns: a plan-provided or forwarded variable using one of
* them could replace the sandbox control variables, so they never cross into a
* microVM from a plan.
*/
export const SANDBOX_CONTROL_ENV_PREFIXES = ['OCM_', 'MSB_'] as const satisfies readonly (typeof RESERVED_ENV_PREFIXES)[number][]
const RESERVED_ENV_NAMES = ['PATH', 'HOME', 'NODE_OPTIONS'] as const
const BLOCKED_ENV_NAMES = new Set<string>(BLOCKED_SERVER_ENV_KEYS)
@@ -24,7 +33,7 @@ export const SecretNameSchema = z
.string()
.max(255, 'Name must be at most 255 characters')
.regex(
/^[A-Za-z_][A-Za-z0-9_]*$/,
ENV_NAME_PATTERN,
'Name must start with a letter or underscore and contain only letters, numbers, and underscores',
)
.refine((name) => !name.toUpperCase().startsWith('GITHUB_'), 'Name must not start with GITHUB_')