mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
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:
1 parent
a0d1d91f18
commit
3e51280894
36 files changed
+1010
-259
No files matched your search
@@ -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>
|
||||
|
||||
@@ -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_')
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { ENV_NAME_PATTERN } from '../schemas/secrets'
|
||||
|
||||
export type EnvExpressionContext = 'secrets' | 'vars'
|
||||
|
||||
export interface EnvExpressionReference {
|
||||
@@ -11,7 +13,7 @@ export type ParseEnvExpressionReferencesResult =
|
||||
|
||||
const ENV_EXPRESSION_PATTERN = /\$\{\{([\s\S]*?)\}\}/g
|
||||
|
||||
const ENV_EXPRESSION_REFERENCE_PATTERN = /^\s*(secrets|vars)\.([A-Za-z_][A-Za-z0-9_]*)\s*$/i
|
||||
const ENV_EXPRESSION_REFERENCE_PATTERN = /^\s*(secrets|vars)\.(\S+)\s*$/i
|
||||
|
||||
function toReference(match: RegExpExecArray): EnvExpressionReference {
|
||||
return {
|
||||
@@ -20,6 +22,12 @@ function toReference(match: RegExpExecArray): EnvExpressionReference {
|
||||
}
|
||||
}
|
||||
|
||||
function matchReference(inner: string): EnvExpressionReference | null {
|
||||
const match = ENV_EXPRESSION_REFERENCE_PATTERN.exec(inner)
|
||||
if (!match || !ENV_NAME_PATTERN.test(match[2] ?? '')) return null
|
||||
return toReference(match)
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses every `${{ ... }}` occurrence in a value, accepting only
|
||||
* `secrets.<NAME>` and `vars.<NAME>` references. The context is
|
||||
@@ -29,14 +37,14 @@ export function parseEnvExpressionReferences(value: string): ParseEnvExpressionR
|
||||
const references: EnvExpressionReference[] = []
|
||||
|
||||
for (const expression of value.matchAll(ENV_EXPRESSION_PATTERN)) {
|
||||
const reference = ENV_EXPRESSION_REFERENCE_PATTERN.exec(expression[1] ?? '')
|
||||
const reference = matchReference(expression[1] ?? '')
|
||||
if (!reference) {
|
||||
return {
|
||||
ok: false,
|
||||
error: `Unsupported expression "${expression[0]}": only secrets.<NAME> and vars.<NAME> are supported`,
|
||||
}
|
||||
}
|
||||
references.push(toReference(reference))
|
||||
references.push(reference)
|
||||
}
|
||||
|
||||
return { ok: true, references }
|
||||
@@ -45,28 +53,21 @@ export function parseEnvExpressionReferences(value: string): ParseEnvExpressionR
|
||||
/**
|
||||
* Replaces every `${{ secrets.<NAME> }}` / `${{ vars.<NAME> }}` occurrence with
|
||||
* the value returned by `lookup`. A reference that resolves to `undefined`
|
||||
* becomes an empty string and is reported in `missing`, matching GitHub Actions.
|
||||
* Unsupported expressions are left untouched.
|
||||
* becomes an empty string, matching GitHub Actions. Unsupported expressions and
|
||||
* expressions with an invalid name are left untouched.
|
||||
*/
|
||||
export function interpolateEnvExpressions(
|
||||
value: string,
|
||||
lookup: (reference: EnvExpressionReference) => string | undefined,
|
||||
): { value: string; missing: EnvExpressionReference[] } {
|
||||
const missing: EnvExpressionReference[] = []
|
||||
|
||||
): { value: string } {
|
||||
const interpolated = value.replace(ENV_EXPRESSION_PATTERN, (expression, inner: string) => {
|
||||
const match = ENV_EXPRESSION_REFERENCE_PATTERN.exec(inner)
|
||||
if (!match) {
|
||||
const reference = matchReference(inner)
|
||||
if (!reference) {
|
||||
return expression
|
||||
}
|
||||
const reference = toReference(match)
|
||||
const resolved = lookup(reference)
|
||||
if (resolved === undefined) {
|
||||
missing.push(reference)
|
||||
return ''
|
||||
}
|
||||
return resolved
|
||||
return resolved === undefined ? '' : resolved
|
||||
})
|
||||
|
||||
return { value: interpolated, missing }
|
||||
return { value: interpolated }
|
||||
}
|
||||
Reference in new issue
Block a user