mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
feat(secrets): repository secrets and variables injected into agent shells and terminals
Add a GitHub Actions-style store for secrets and variables so tests can read
credentials from the environment without .env files in the repo.
- secrets table keyed by kind, repo (NULL for global) and environment;
secrets are encrypted with encryptSecret and write-only, variables are plain
text. Names follow GitHub's rules and reject Manager-reserved names.
- Global (/api/secrets, /api/variables) and repo
(/api/repos/:id/secrets|variables) routes.
- The trusted .ocm/project.json gains environment and env fields that map
names to ${{ secrets.X }} / ${{ vars.X }} expressions. Both are part of the
trust hash; files without them hash as before, so existing trust survives.
- RepoEnvService.resolveForDirectory resolves the repo and approved mapping
for any directory, including worktrees outside the repo folder, with
environment > repo > global precedence. It never throws.
- /api/internal/shell-env replaces /git-credentials/gh-env and feeds the
ocm-gh-env plugin, the sandbox shell plan and TerminalService.create.
- The plugin masks injected secret values as *** in completed tool results.
- The sandbox shell plan is now the only source of the guest environment: the
plugin pins OCM_SANDBOX_FORWARD_ENV and the shim forwards only those names.
This commit is contained in:
1 parent
b431c121a2
commit
e0331b43bf
47 files changed
+2073
-156
No files matched your search
@@ -24,3 +24,4 @@ export * from './project-config'
|
||||
export * from './preview'
|
||||
export * from './change-walkthroughs'
|
||||
export * from './opencode-servers'
|
||||
export * from './secrets'
|
||||
@@ -1,5 +1,7 @@
|
||||
import { z } from 'zod'
|
||||
import { TerminalInfoSchema } from './terminal'
|
||||
import { EnvironmentNameSchema, isReservedEnvName } from './secrets'
|
||||
import { parseEnvExpressionReferences } from '../utils/env-expressions'
|
||||
|
||||
export const ProjectActionIconSchema = z.enum([
|
||||
'play',
|
||||
@@ -30,10 +32,42 @@ export const WorktreeSetupCommandsSchema = z.array(z.string().trim().min(1).max(
|
||||
|
||||
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_]*$/
|
||||
|
||||
export const RepoProjectEnvValueSchema = z
|
||||
.string()
|
||||
.max(4000, 'Environment value must be at most 4000 characters')
|
||||
.superRefine((value, ctx) => {
|
||||
const parsed = parseEnvExpressionReferences(value)
|
||||
if (!parsed.ok) {
|
||||
ctx.addIssue({ code: 'custom', message: parsed.error })
|
||||
}
|
||||
})
|
||||
|
||||
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)) {
|
||||
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] })
|
||||
}
|
||||
}
|
||||
})
|
||||
.refine(
|
||||
(env) => Object.keys(env).length <= REPO_PROJECT_ENV_MAX_ENTRIES,
|
||||
`At most ${REPO_PROJECT_ENV_MAX_ENTRIES} environment entries are allowed`,
|
||||
)
|
||||
|
||||
export const RepoProjectFileSchema = z.looseObject({
|
||||
version: z.literal(1),
|
||||
projectActions: z.array(ProjectActionSchema).optional(),
|
||||
setupWorktree: WorktreeSetupCommandsSchema.optional(),
|
||||
environment: EnvironmentNameSchema.optional(),
|
||||
env: RepoProjectEnvSchema.optional(),
|
||||
})
|
||||
|
||||
export const ProjectConfigResponseSchema = z.object({
|
||||
@@ -66,6 +100,8 @@ export const ProjectConfigResponseSchema = z.object({
|
||||
}),
|
||||
),
|
||||
setup: z.array(z.string()),
|
||||
environment: z.string().optional(),
|
||||
env: z.record(z.string(), z.string()).optional(),
|
||||
})
|
||||
.nullable(),
|
||||
error: z.string().optional(),
|
||||
@@ -128,6 +164,7 @@ 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>
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
import { z } from 'zod'
|
||||
import { BLOCKED_SERVER_ENV_KEYS } from './settings'
|
||||
|
||||
export const SecretKindSchema = z.enum(['secret', 'variable'])
|
||||
|
||||
export type SecretKind = z.infer<typeof SecretKindSchema>
|
||||
|
||||
export const SECRET_VALUE_MAX_BYTES = 48 * 1024
|
||||
|
||||
const RESERVED_ENV_PREFIXES = ['OCM_', 'OPENCODE_', 'GIT_CONFIG_', 'MSB_', 'LD_'] as const
|
||||
|
||||
const RESERVED_ENV_NAMES = ['PATH', 'HOME', 'NODE_OPTIONS'] as const
|
||||
|
||||
const BLOCKED_ENV_NAMES = new Set<string>(BLOCKED_SERVER_ENV_KEYS)
|
||||
|
||||
export function isReservedEnvName(name: string): boolean {
|
||||
const normalized = name.toUpperCase()
|
||||
if (BLOCKED_ENV_NAMES.has(normalized)) return true
|
||||
if ((RESERVED_ENV_NAMES as readonly string[]).includes(normalized)) return true
|
||||
return RESERVED_ENV_PREFIXES.some((prefix) => normalized.startsWith(prefix))
|
||||
}
|
||||
|
||||
export const SecretNameSchema = z
|
||||
.string()
|
||||
.max(255, 'Name must be at most 255 characters')
|
||||
.regex(
|
||||
/^[A-Za-z_][A-Za-z0-9_]*$/,
|
||||
'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_')
|
||||
.refine((name) => !isReservedEnvName(name), 'Name is reserved for Manager or the runtime')
|
||||
.transform((name) => name.toUpperCase())
|
||||
|
||||
export type SecretName = z.infer<typeof SecretNameSchema>
|
||||
|
||||
export const EnvironmentNameSchema = z
|
||||
.string()
|
||||
.regex(
|
||||
/^[A-Za-z0-9_.-]{1,64}$/,
|
||||
'Environment name must be 1-64 characters of letters, numbers, dots, underscores, or hyphens',
|
||||
)
|
||||
.transform((name) => name.toLowerCase())
|
||||
|
||||
export type EnvironmentName = z.infer<typeof EnvironmentNameSchema>
|
||||
|
||||
const textEncoder = new TextEncoder()
|
||||
|
||||
export const SecretValueSchema = z
|
||||
.string()
|
||||
.min(1, 'Value must not be empty')
|
||||
.refine(
|
||||
(value) => textEncoder.encode(value).length <= SECRET_VALUE_MAX_BYTES,
|
||||
`Value must be at most ${SECRET_VALUE_MAX_BYTES} bytes`,
|
||||
)
|
||||
|
||||
export const SetSecretRequestSchema = z.object({
|
||||
value: SecretValueSchema,
|
||||
environment: EnvironmentNameSchema.optional(),
|
||||
})
|
||||
|
||||
export type SetSecretRequest = z.infer<typeof SetSecretRequestSchema>
|
||||
|
||||
export const SecretEntrySchema = z.object({
|
||||
name: z.string(),
|
||||
environment: z.string().nullable(),
|
||||
updatedAt: z.number(),
|
||||
value: z.string().optional(),
|
||||
})
|
||||
|
||||
export type SecretEntry = z.infer<typeof SecretEntrySchema>
|
||||
|
||||
export const SecretListResponseSchema = z.object({
|
||||
items: z.array(SecretEntrySchema),
|
||||
})
|
||||
|
||||
export type SecretListResponse = z.infer<typeof SecretListResponseSchema>
|
||||
@@ -0,0 +1,72 @@
|
||||
export type EnvExpressionContext = 'secrets' | 'vars'
|
||||
|
||||
export interface EnvExpressionReference {
|
||||
context: EnvExpressionContext
|
||||
name: string
|
||||
}
|
||||
|
||||
export type ParseEnvExpressionReferencesResult =
|
||||
| { ok: true; references: EnvExpressionReference[] }
|
||||
| { ok: false; error: string }
|
||||
|
||||
const ENV_EXPRESSION_PATTERN = /\$\{\{([\s\S]*?)\}\}/g
|
||||
|
||||
const ENV_EXPRESSION_REFERENCE_PATTERN = /^\s*(secrets|vars)\.([A-Za-z_][A-Za-z0-9_]*)\s*$/i
|
||||
|
||||
function toReference(match: RegExpExecArray): EnvExpressionReference {
|
||||
return {
|
||||
context: (match[1] ?? '').toLowerCase() as EnvExpressionContext,
|
||||
name: (match[2] ?? '').toUpperCase(),
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses every `${{ ... }}` occurrence in a value, accepting only
|
||||
* `secrets.<NAME>` and `vars.<NAME>` references. The context is
|
||||
* case-insensitive and the name is normalized to upper case.
|
||||
*/
|
||||
export function parseEnvExpressionReferences(value: string): ParseEnvExpressionReferencesResult {
|
||||
const references: EnvExpressionReference[] = []
|
||||
|
||||
for (const expression of value.matchAll(ENV_EXPRESSION_PATTERN)) {
|
||||
const reference = ENV_EXPRESSION_REFERENCE_PATTERN.exec(expression[1] ?? '')
|
||||
if (!reference) {
|
||||
return {
|
||||
ok: false,
|
||||
error: `Unsupported expression "${expression[0]}": only secrets.<NAME> and vars.<NAME> are supported`,
|
||||
}
|
||||
}
|
||||
references.push(toReference(reference))
|
||||
}
|
||||
|
||||
return { ok: true, references }
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
export function interpolateEnvExpressions(
|
||||
value: string,
|
||||
lookup: (reference: EnvExpressionReference) => string | undefined,
|
||||
): { value: string; missing: EnvExpressionReference[] } {
|
||||
const missing: EnvExpressionReference[] = []
|
||||
|
||||
const interpolated = value.replace(ENV_EXPRESSION_PATTERN, (expression, inner: string) => {
|
||||
const match = ENV_EXPRESSION_REFERENCE_PATTERN.exec(inner)
|
||||
if (!match) {
|
||||
return expression
|
||||
}
|
||||
const reference = toReference(match)
|
||||
const resolved = lookup(reference)
|
||||
if (resolved === undefined) {
|
||||
missing.push(reference)
|
||||
return ''
|
||||
}
|
||||
return resolved
|
||||
})
|
||||
|
||||
return { value: interpolated, missing }
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
export * from './env-expressions'
|
||||
export * from './jsonc'
|
||||
export * from './record'
|
||||
export * from './repo'
|
||||
|
||||
Reference in new issue
Block a user