feat(hooks): gate orca.yaml execution on trust
Audited and rebased PR 1138. Squashed to remove the original untrusted commit stack; hook execution now requires explicit UI trust or CLI --run-hooks opt-in.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
/* eslint-disable max-lines -- Why: runtime behavior is stateful and cross-cutting, so these tests stay in one file to preserve the end-to-end invariants around handles, waits, and graph sync. */
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import type { WorktreeMeta } from '../../shared/types'
|
||||
import { addWorktree, listWorktrees } from '../git/worktree'
|
||||
import { addWorktree, listWorktrees, removeWorktree } from '../git/worktree'
|
||||
import { createSetupRunnerScript, getEffectiveHooks, runHook } from '../hooks'
|
||||
import { OrchestrationDb } from './orchestration/db'
|
||||
import { OrcaRuntimeService } from './orca-runtime'
|
||||
@@ -9,6 +9,7 @@ import { OrcaRuntimeService } from './orca-runtime'
|
||||
const {
|
||||
MOCK_GIT_WORKTREES,
|
||||
addWorktreeMock,
|
||||
removeWorktreeMock,
|
||||
computeWorktreePathMock,
|
||||
ensurePathWithinWorkspaceMock,
|
||||
invalidateAuthorizedRootsCacheMock
|
||||
@@ -23,6 +24,7 @@ const {
|
||||
}
|
||||
],
|
||||
addWorktreeMock: vi.fn(),
|
||||
removeWorktreeMock: vi.fn(),
|
||||
computeWorktreePathMock: vi.fn(),
|
||||
ensurePathWithinWorkspaceMock: vi.fn(),
|
||||
invalidateAuthorizedRootsCacheMock: vi.fn()
|
||||
@@ -30,7 +32,8 @@ const {
|
||||
|
||||
vi.mock('../git/worktree', () => ({
|
||||
listWorktrees: vi.fn().mockResolvedValue(MOCK_GIT_WORKTREES),
|
||||
addWorktree: addWorktreeMock
|
||||
addWorktree: addWorktreeMock,
|
||||
removeWorktree: removeWorktreeMock
|
||||
}))
|
||||
|
||||
vi.mock('../hooks', () => ({
|
||||
@@ -70,6 +73,7 @@ vi.mock('../git/repo', async (importOriginal) => {
|
||||
afterEach(() => {
|
||||
vi.mocked(listWorktrees).mockResolvedValue(MOCK_GIT_WORKTREES)
|
||||
vi.mocked(addWorktree).mockReset()
|
||||
vi.mocked(removeWorktree).mockReset()
|
||||
vi.mocked(createSetupRunnerScript).mockReset()
|
||||
vi.mocked(getEffectiveHooks).mockReset()
|
||||
vi.mocked(runHook).mockReset()
|
||||
@@ -899,7 +903,7 @@ describe('OrcaRuntimeService', () => {
|
||||
await expect(runtime.searchRepoRefs('id:repo-1', 'main', -5)).rejects.toThrow('invalid_limit')
|
||||
})
|
||||
|
||||
it('returns a setup launch payload for CLI-created worktrees when orca.yaml defines setup', async () => {
|
||||
it('returns a setup launch payload for CLI-created worktrees when hooks are explicitly enabled', async () => {
|
||||
const runtime = new OrcaRuntimeService(store)
|
||||
const activateWorktree = vi.fn()
|
||||
runtime.setNotifier({
|
||||
@@ -940,7 +944,8 @@ describe('OrcaRuntimeService', () => {
|
||||
|
||||
const result = await runtime.createManagedWorktree({
|
||||
repoSelector: 'id:repo-1',
|
||||
name: 'runtime-hook-test'
|
||||
name: 'runtime-hook-test',
|
||||
runHooks: true
|
||||
})
|
||||
|
||||
expect(createSetupRunnerScript).toHaveBeenCalledWith(
|
||||
@@ -973,6 +978,90 @@ describe('OrcaRuntimeService', () => {
|
||||
expect(activateWorktree).toHaveBeenCalledWith('repo-1', expect.any(String), result.setup)
|
||||
})
|
||||
|
||||
it('skips setup hooks for CLI-created worktrees by default', async () => {
|
||||
const runtime = new OrcaRuntimeService(store)
|
||||
const activateWorktree = vi.fn()
|
||||
runtime.setNotifier({
|
||||
worktreesChanged: vi.fn(),
|
||||
reposChanged: vi.fn(),
|
||||
activateWorktree,
|
||||
createTerminal: vi.fn(),
|
||||
splitTerminal: vi.fn(),
|
||||
renameTerminal: vi.fn(),
|
||||
focusTerminal: vi.fn(),
|
||||
closeTerminal: vi.fn()
|
||||
})
|
||||
runtime.attachWindow(1)
|
||||
|
||||
computeWorktreePathMock.mockReturnValue('/tmp/workspaces/runtime-hook-skip')
|
||||
ensurePathWithinWorkspaceMock.mockReturnValue('/tmp/workspaces/runtime-hook-skip')
|
||||
vi.mocked(getEffectiveHooks).mockReturnValue({
|
||||
scripts: {
|
||||
setup: 'pnpm worktree:setup'
|
||||
}
|
||||
})
|
||||
vi.mocked(listWorktrees).mockResolvedValueOnce([
|
||||
{
|
||||
path: '/tmp/workspaces/runtime-hook-skip',
|
||||
head: 'def',
|
||||
branch: 'runtime-hook-skip',
|
||||
isBare: false,
|
||||
isMainWorktree: false
|
||||
}
|
||||
])
|
||||
|
||||
const result = await runtime.createManagedWorktree({
|
||||
repoSelector: 'id:repo-1',
|
||||
name: 'runtime-hook-skip'
|
||||
})
|
||||
|
||||
expect(createSetupRunnerScript).not.toHaveBeenCalled()
|
||||
expect(runHook).not.toHaveBeenCalled()
|
||||
expect(result).toEqual({
|
||||
worktree: expect.objectContaining({
|
||||
repoId: 'repo-1',
|
||||
path: '/tmp/workspaces/runtime-hook-skip',
|
||||
branch: 'runtime-hook-skip'
|
||||
})
|
||||
})
|
||||
expect(activateWorktree).toHaveBeenCalledWith('repo-1', expect.any(String), undefined)
|
||||
})
|
||||
|
||||
it('skips archive hooks for CLI worktree removal by default', async () => {
|
||||
const runtime = new OrcaRuntimeService(store)
|
||||
vi.mocked(getEffectiveHooks).mockReturnValue({
|
||||
scripts: {
|
||||
archive: 'pnpm worktree:archive'
|
||||
}
|
||||
})
|
||||
vi.mocked(removeWorktree).mockResolvedValue(undefined)
|
||||
|
||||
await runtime.removeManagedWorktree(TEST_WORKTREE_ID)
|
||||
|
||||
expect(runHook).not.toHaveBeenCalled()
|
||||
expect(removeWorktree).toHaveBeenCalledWith(TEST_REPO_PATH, TEST_WORKTREE_PATH, false)
|
||||
})
|
||||
|
||||
it('runs archive hooks for CLI worktree removal when hooks are explicitly enabled', async () => {
|
||||
const runtime = new OrcaRuntimeService(store)
|
||||
vi.mocked(getEffectiveHooks).mockReturnValue({
|
||||
scripts: {
|
||||
archive: 'pnpm worktree:archive'
|
||||
}
|
||||
})
|
||||
vi.mocked(runHook).mockResolvedValue({ success: true, output: '' })
|
||||
vi.mocked(removeWorktree).mockResolvedValue(undefined)
|
||||
|
||||
await runtime.removeManagedWorktree(TEST_WORKTREE_ID, false, true)
|
||||
|
||||
expect(runHook).toHaveBeenCalledWith(
|
||||
'archive',
|
||||
TEST_WORKTREE_PATH,
|
||||
expect.objectContaining({ id: TEST_REPO_ID, path: TEST_REPO_PATH })
|
||||
)
|
||||
expect(removeWorktree).toHaveBeenCalledWith(TEST_REPO_PATH, TEST_WORKTREE_PATH, false)
|
||||
})
|
||||
|
||||
it('invalidates the filesystem-auth cache after CLI worktree creation', async () => {
|
||||
// Reproduces: CLI-created worktrees fail with "Access denied: unknown
|
||||
// repository or worktree path" because the filesystem-auth cache was
|
||||
|
||||
@@ -949,6 +949,7 @@ export class OrcaRuntimeService {
|
||||
baseBranch?: string
|
||||
linkedIssue?: number | null
|
||||
comment?: string
|
||||
runHooks?: boolean
|
||||
}): Promise<CreateWorktreeResult> {
|
||||
if (!this.store) {
|
||||
throw new Error('runtime_unavailable')
|
||||
@@ -1035,7 +1036,7 @@ export class OrcaRuntimeService {
|
||||
|
||||
let setup: CreateWorktreeResult['setup']
|
||||
const hooks = getEffectiveHooks(repo)
|
||||
if (hooks?.scripts.setup) {
|
||||
if (hooks?.scripts.setup && args.runHooks === true) {
|
||||
if (this.authoritativeWindowId !== null) {
|
||||
try {
|
||||
// Why: CLI-created worktrees must use the same runner-script path as the
|
||||
@@ -1056,6 +1057,9 @@ export class OrcaRuntimeService {
|
||||
}
|
||||
})
|
||||
}
|
||||
} else if (hooks?.scripts.setup) {
|
||||
// Runtime RPC calls have no renderer trust prompt, so hooks require explicit CLI opt-in.
|
||||
console.info(`[hooks] setup hook skipped for ${worktreePath}; pass --run-hooks to run it`)
|
||||
}
|
||||
|
||||
this.notifier?.worktreesChanged(repo.id)
|
||||
@@ -1101,7 +1105,11 @@ export class OrcaRuntimeService {
|
||||
return mergeWorktree(worktree.repoId, worktree.git, meta)
|
||||
}
|
||||
|
||||
async removeManagedWorktree(worktreeSelector: string, force = false): Promise<void> {
|
||||
async removeManagedWorktree(
|
||||
worktreeSelector: string,
|
||||
force = false,
|
||||
runHooks = false
|
||||
): Promise<void> {
|
||||
if (!this.store) {
|
||||
throw new Error('runtime_unavailable')
|
||||
}
|
||||
@@ -1115,11 +1123,14 @@ export class OrcaRuntimeService {
|
||||
}
|
||||
|
||||
const hooks = getEffectiveHooks(repo)
|
||||
if (hooks?.scripts.archive) {
|
||||
if (hooks?.scripts.archive && runHooks) {
|
||||
const result = await runHook('archive', worktree.path, repo)
|
||||
if (!result.success) {
|
||||
console.error(`[hooks] archive hook failed for ${worktree.path}:`, result.output)
|
||||
}
|
||||
} else if (hooks?.scripts.archive) {
|
||||
// Runtime RPC calls have no renderer trust prompt, so hooks require explicit CLI opt-in.
|
||||
console.info(`[hooks] archive hook skipped for ${worktree.path}; pass --run-hooks to run it`)
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
@@ -34,7 +34,8 @@ const WorktreeCreate = z.object({
|
||||
.pipe(z.string().min(1, 'Missing worktree name')),
|
||||
baseBranch: OptionalString,
|
||||
linkedIssue: TriStateLinkedIssue,
|
||||
comment: OptionalString
|
||||
comment: OptionalString,
|
||||
runHooks: OptionalBoolean
|
||||
})
|
||||
|
||||
const WorktreeSet = WorktreeSelector.extend({
|
||||
@@ -44,7 +45,8 @@ const WorktreeSet = WorktreeSelector.extend({
|
||||
})
|
||||
|
||||
const WorktreeRemove = WorktreeSelector.extend({
|
||||
force: OptionalBoolean
|
||||
force: OptionalBoolean,
|
||||
runHooks: OptionalBoolean
|
||||
})
|
||||
|
||||
export const WORKTREE_METHODS: RpcMethod[] = [
|
||||
@@ -74,7 +76,8 @@ export const WORKTREE_METHODS: RpcMethod[] = [
|
||||
name: params.name,
|
||||
baseBranch: params.baseBranch,
|
||||
linkedIssue: params.linkedIssue,
|
||||
comment: params.comment
|
||||
comment: params.comment,
|
||||
runHooks: params.runHooks === true
|
||||
})
|
||||
}),
|
||||
defineMethod({
|
||||
@@ -92,7 +95,11 @@ export const WORKTREE_METHODS: RpcMethod[] = [
|
||||
name: 'worktree.rm',
|
||||
params: WorktreeRemove,
|
||||
handler: async (params, { runtime }) => {
|
||||
await runtime.removeManagedWorktree(params.worktree, params.force === true)
|
||||
await runtime.removeManagedWorktree(
|
||||
params.worktree,
|
||||
params.force === true,
|
||||
params.runHooks === true
|
||||
)
|
||||
return { removed: true }
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user