fix(secrets): separate global from assistant-repo scope and require an exact repo root

Global secrets used COALESCE(repo_id, 0), the same key as the Assistant repo (id 0), so same-named values overwrote each other. A new migration rebuilds idx_secrets_scope with a -1 sentinel shared with the upsert target, so databases that already applied the secrets migration upgrade cleanly. Repo env resolution now requires an exact registered git root, so an unregistered nested repository no longer receives its ancestor's secrets.
This commit is contained in:
Chris Scott committed 2026-10-24 10:37:45 -04:00
1 parent 3e51280894
commit e3557f8eec
9 files changed
+153 -5

No files matched your search

@@ -0,0 +1,24 @@
import type { Database } from 'bun:sqlite'
import type { Migration } from '../migration-runner'
import { GLOBAL_SECRETS_SCOPE_ID } from '../secrets-scope'
const LEGACY_GLOBAL_SCOPE_ID = 0
function rebuildScopeIndex(db: Database, globalScopeId: number): void {
db.run('DROP INDEX IF EXISTS idx_secrets_scope')
db.run(
`CREATE UNIQUE INDEX idx_secrets_scope ON secrets(kind, COALESCE(repo_id, ${globalScopeId}), environment, name)`,
)
}
const migration: Migration = {
id: '202610181410-secrets-global-scope-index',
up(db) {
rebuildScopeIndex(db, GLOBAL_SECRETS_SCOPE_ID)
},
down(db) {
rebuildScopeIndex(db, LEGACY_GLOBAL_SCOPE_ID)
},
}
export default migration
+2
View File
@@ -38,6 +38,7 @@ import migration202610181300 from './202610181300-session-server-scope'
import migration202610181310 from './202610181310-multi-run-targets'
import migration202610181320 from './202610181320-remote-schedule-targets'
import migration202610181400 from './202610181400-secrets'
import migration202610181410 from './202610181410-secrets-global-scope-index'
export const allMigrations: Migration[] = [
migration001,
@@ -79,4 +80,5 @@ export const allMigrations: Migration[] = [
migration202610181310,
migration202610181320,
migration202610181400,
migration202610181410,
]
+6
View File
@@ -94,6 +94,12 @@ export function setRepoSandboxGitCredentials(db: Database, repoId: number, allow
setRepoSetting(db, repoId, REPO_SANDBOX_GIT_CREDENTIALS_SETTING_KEY, allowed === null ? null : String(allowed))
}
export function getRepoByExactDirectory(db: Database, directory: string): Repo | null {
const resolvedDirectory = canonicalPathSync(path.resolve(directory))
return listRepos(db).find((repo) => canonicalPathSync(path.resolve(repo.fullPath)) === resolvedDirectory) ?? null
}
export function getRepoByDirectory(db: Database, directory: string): Repo | null {
const resolvedDirectory = canonicalPathSync(path.resolve(directory))
const repos = listRepos(db)
+7
View File
@@ -0,0 +1,7 @@
/**
* Repo scope stored in place of a NULL `repo_id` for global secrets, so the unique scope index
* and its conflict target treat every global entry as one scope while repository scopes keep
* their own. Real repo ids are `0` (the Assistant repository) or positive, so a negative
* sentinel can never collide with one.
*/
export const GLOBAL_SECRETS_SCOPE_ID = -1
+2 -2
View File
@@ -3,7 +3,7 @@ import path from 'node:path'
import type { Database } from 'bun:sqlite'
import type { Repo } from '@opencode-manager/shared/types'
import { interpolateEnvExpressions, type EnvExpressionReference } from '@opencode-manager/shared/utils'
import { getRepoByDirectory } from '../db/queries'
import { getRepoByExactDirectory } from '../db/queries'
import { canonicalPathSync } from '../utils/fs-safe'
import { logger } from '../utils/logger'
import { repoEnvCacheEpoch } from '../utils/repo-env-cache'
@@ -133,7 +133,7 @@ export class RepoEnvService {
const topLevel = await findGitRepoRoot(cwd)
const repo = topLevel
? getRepoByDirectory(this.db, topLevel) ?? (await resolveRepoForDirectory(this.db, topLevel))
? getRepoByExactDirectory(this.db, topLevel) ?? (await resolveRepoForDirectory(this.db, topLevel))
: null
const projectRepo = repo ? await this.projectConfigService.resolveProjectRepo(repo) : null
+2 -1
View File
@@ -1,5 +1,6 @@
import type { Database } from 'bun:sqlite'
import type { SecretEntry, SecretKind } from '@opencode-manager/shared/schemas'
import { GLOBAL_SECRETS_SCOPE_ID } from '../db/secrets-scope'
import { decryptSecret, encryptSecret } from '../utils/crypto'
import { logger } from '../utils/logger'
@@ -46,7 +47,7 @@ export class SecretsService {
.prepare(
`INSERT INTO secrets (kind, repo_id, environment, name, value, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(kind, COALESCE(repo_id, 0), environment, name) DO UPDATE SET value = excluded.value, updated_at = excluded.updated_at`,
ON CONFLICT(kind, COALESCE(repo_id, ${GLOBAL_SECRETS_SCOPE_ID}), environment, name) DO UPDATE SET value = excluded.value, updated_at = excluded.updated_at`,
)
.run(kind, repoId, normalizedEnvironment, name, storedValue, now, now)
}