From b6ac7b9c929423167bdbf03208eb2692b1c7e481 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Thu, 25 Jun 2026 22:43:25 -0700 Subject: [PATCH] Harden Windows hook command resolution (#6409) * Keep spaced Windows Codex hook paths fast * Harden Windows hook command resolution --------- Co-authored-by: Neil --- src/main/agent-hooks/installer-utils.test.ts | 17 ++++++-- src/main/agent-hooks/installer-utils.ts | 12 +++++- src/main/antigravity/hook-service.test.ts | 2 + src/main/antigravity/hook-service.ts | 5 ++- src/main/claude/hook-service.test.ts | 14 +++--- src/main/codex/hook-service.test.ts | 45 +++++++++++++++++--- src/main/codex/hook-service.ts | 7 +-- src/main/command-code/hook-service.test.ts | 11 +++-- src/main/cursor/hook-service.test.ts | 14 +++--- src/main/droid/hook-service.test.ts | 11 +++-- src/main/gemini/hook-service.test.ts | 11 ++--- src/main/grok/hook-service.test.ts | 14 +++--- 12 files changed, 103 insertions(+), 60 deletions(-) diff --git a/src/main/agent-hooks/installer-utils.test.ts b/src/main/agent-hooks/installer-utils.test.ts index 51993cd1e..28d5462be 100644 --- a/src/main/agent-hooks/installer-utils.test.ts +++ b/src/main/agent-hooks/installer-utils.test.ts @@ -340,6 +340,9 @@ describe('wrapPosixHookCommand', () => { }) describe('wrapWindowsHookCommand', () => { + const qualifiedPowerShellCommand = + /^[A-Za-z]:\/[^"]*\/System32\/WindowsPowerShell\/v1\.0\/powershell\.exe -NoProfile -ExecutionPolicy Bypass -EncodedCommand \S+$/ + function decodeWindowsHookCommand(command: string): string { const encodedCommand = command.match(/ -EncodedCommand (\S+)$/)?.[1] expect(encodedCommand).toBeTruthy() @@ -348,7 +351,8 @@ describe('wrapWindowsHookCommand', () => { it('invokes the .cmd through an encoded PowerShell command', () => { const command = wrapWindowsHookCommand('C:\\Users\\alice\\.orca\\agent-hooks\\codex-hook.cmd') - expect(command).toMatch(/^powershell -NoProfile -ExecutionPolicy Bypass -EncodedCommand \S+$/) + expect(command).toMatch(qualifiedPowerShellCommand) + expect(command).not.toMatch(/^powershell\b/i) expect(decodeWindowsHookCommand(command)).toBe( "& 'C:\\Users\\alice\\.orca\\agent-hooks\\codex-hook.cmd'; exit $LASTEXITCODE" ) @@ -359,7 +363,7 @@ describe('wrapWindowsHookCommand', () => { // the whole path inside the encoded command so shells do not split it. it('preserves spaces in the script path (user profile with space case)', () => { const cmd = wrapWindowsHookCommand('C:\\Users\\Jorge Silva\\.orca\\agent-hooks\\codex-hook.cmd') - expect(cmd).toMatch(/^powershell -NoProfile -ExecutionPolicy Bypass -EncodedCommand \S+$/) + expect(cmd).toMatch(qualifiedPowerShellCommand) expect(decodeWindowsHookCommand(cmd)).toBe( "& 'C:\\Users\\Jorge Silva\\.orca\\agent-hooks\\codex-hook.cmd'; exit $LASTEXITCODE" ) @@ -395,7 +399,7 @@ describe('buildWindowsAgentHookPostCommand', () => { it('posts hook stdin through bounded curl without spawning PowerShell', () => { const command = buildWindowsAgentHookPostCommand('codex') - expect(command).toContain('curl.exe -sS -X POST') + expect(command).toContain('"%SystemRoot%\\System32\\curl.exe" -sS -X POST') expect(command).toContain('--connect-timeout 0.5 --max-time 1.5') expect(command).toContain('-H "Content-Type: application/x-www-form-urlencoded"') expect(command).toContain('-H "X-Orca-Agent-Hook-Token: %ORCA_AGENT_HOOK_TOKEN%"') @@ -405,6 +409,13 @@ describe('buildWindowsAgentHookPostCommand', () => { expect(command).not.toContain('powershell') expect(command).not.toContain('Invoke-WebRequest') }) + + it('does not resolve curl from the current directory or PATH', () => { + const command = buildWindowsAgentHookPostCommand('gemini') + + expect(command).toMatch(/^"%SystemRoot%\\System32\\curl\.exe"/) + expect(command).not.toMatch(/^curl\.exe\b/) + }) }) describe('buildWindowsAgentHookCurlPostCommand', () => { diff --git a/src/main/agent-hooks/installer-utils.ts b/src/main/agent-hooks/installer-utils.ts index 33ef8bfd1..699a2f21e 100644 --- a/src/main/agent-hooks/installer-utils.ts +++ b/src/main/agent-hooks/installer-utils.ts @@ -147,20 +147,28 @@ function quotePowerShellString(value: string): string { return `'${value.replaceAll("'", "''")}'` } +function getWindowsPowerShellExecutablePath(): string { + const systemRoot = process.env.SystemRoot || 'C:\\Windows' + // Why: PATH lookup lets a worktree-local powershell.exe hijack hook payloads. + // Forward slashes keep this absolute path shell-friendly for cmd.exe and Git Bash. + return `${systemRoot.replaceAll('\\', '/')}/System32/WindowsPowerShell/v1.0/powershell.exe` +} + export function wrapWindowsHookCommand(scriptPath: string): string { // Why: most Windows agents run hooks through Git Bash or another shell that // mangles a raw backslash path. Codex has its own cmd.exe-safe fast path; the // shared wrapper keeps the encoded launcher for every other agent. const command = `& ${quotePowerShellString(scriptPath)}; exit $LASTEXITCODE` const encodedCommand = Buffer.from(command, 'utf16le').toString('base64') - return `powershell -NoProfile -ExecutionPolicy Bypass -EncodedCommand ${encodedCommand}` + return `${getWindowsPowerShellExecutablePath()} -NoProfile -ExecutionPolicy Bypass -EncodedCommand ${encodedCommand}` } export function buildWindowsAgentHookPostCommand(source: AgentHookSource): string { // Why: Codex runs these hooks inline on every turn. PowerShell startup alone // makes trusted Windows hooks visibly slow, so mirror the POSIX curl path. + // Qualify curl so a repo-local curl.exe cannot hijack hook payloads. return [ - `curl.exe -sS -X POST "http://127.0.0.1:%ORCA_AGENT_HOOK_PORT%/hook/${source}" ^`, + `"%SystemRoot%\\System32\\curl.exe" -sS -X POST "http://127.0.0.1:%ORCA_AGENT_HOOK_PORT%/hook/${source}" ^`, ' --connect-timeout 0.5 --max-time 1.5 ^', ' -H "Content-Type: application/x-www-form-urlencoded" ^', ' -H "X-Orca-Agent-Hook-Token: %ORCA_AGENT_HOOK_TOKEN%" ^', diff --git a/src/main/antigravity/hook-service.test.ts b/src/main/antigravity/hook-service.test.ts index da4bbc7ab..2969e6f3b 100644 --- a/src/main/antigravity/hook-service.test.ts +++ b/src/main/antigravity/hook-service.test.ts @@ -87,6 +87,7 @@ describe('AntigravityHookService', () => { ) expect(script).toContain('/hook/antigravity') if (process.platform === 'win32') { + expect(script).toContain('%SystemRoot%\\System32\\WindowsPowerShell\\v1.0\\powershell.exe') expect(script).toContain('hook_event_name=$env:ORCA_ANTIGRAVITY_EVENT') expect(script).toContain('[string]::IsNullOrWhiteSpace($inputData)) { @{} }') expect(script).not.toContain('[string]::IsNullOrWhiteSpace($inputData)) { exit 0 }') @@ -174,6 +175,7 @@ describe('AntigravityHookService', () => { 'utf8' ) expect(script).toContain('/hook/antigravity') + expect(script).toContain('%SystemRoot%\\System32\\WindowsPowerShell\\v1.0\\powershell.exe') expect(script).toContain('hook_event_name=$env:ORCA_ANTIGRAVITY_EVENT') expect(script).toContain('[string]::IsNullOrWhiteSpace($inputData)) { @{} }') expect(script).not.toContain('[string]::IsNullOrWhiteSpace($inputData)) { exit 0 }') diff --git a/src/main/antigravity/hook-service.ts b/src/main/antigravity/hook-service.ts index 343c4d215..c17a95145 100644 --- a/src/main/antigravity/hook-service.ts +++ b/src/main/antigravity/hook-service.ts @@ -166,8 +166,9 @@ function getWindowsWrapperScript(eventName: string): string { function buildWindowsAntigravityHookPostCommand(): string { // Why: Antigravity hooks are best-effort status updates; do not let a stalled - // local listener hold the agent process open. - return `powershell -NoProfile -ExecutionPolicy Bypass -Command "$utf8=[System.Text.UTF8Encoding]::new($false); [Console]::InputEncoding=$utf8; [Console]::OutputEncoding=$utf8; $inputData=[Console]::In.ReadToEnd(); try { $payload=if ([string]::IsNullOrWhiteSpace($inputData)) { @{} } else { $inputData | ConvertFrom-Json }; $body=@{ paneKey=$env:ORCA_PANE_KEY; launchToken=$env:ORCA_AGENT_LAUNCH_TOKEN; tabId=$env:ORCA_TAB_ID; worktreeId=$env:ORCA_WORKTREE_ID; env=$env:ORCA_AGENT_HOOK_ENV; version=$env:ORCA_AGENT_HOOK_VERSION; hook_event_name=$env:ORCA_ANTIGRAVITY_EVENT; payload=$payload } | ConvertTo-Json -Depth 100 -Compress; $bodyBytes=$utf8.GetBytes($body); Invoke-WebRequest -UseBasicParsing -Method Post -Uri ('http://127.0.0.1:' + $env:ORCA_AGENT_HOOK_PORT + '/hook/antigravity') -ContentType 'application/json; charset=utf-8' -Headers @{ 'X-Orca-Agent-Hook-Token'=$env:ORCA_AGENT_HOOK_TOKEN } -Body $bodyBytes -TimeoutSec 2 | Out-Null } catch {}"` + // local listener hold the agent process open. Qualify PowerShell so a + // worktree-local powershell.exe cannot hijack hook payloads. + return `"%SystemRoot%\\System32\\WindowsPowerShell\\v1.0\\powershell.exe" -NoProfile -ExecutionPolicy Bypass -Command "$utf8=[System.Text.UTF8Encoding]::new($false); [Console]::InputEncoding=$utf8; [Console]::OutputEncoding=$utf8; $inputData=[Console]::In.ReadToEnd(); try { $payload=if ([string]::IsNullOrWhiteSpace($inputData)) { @{} } else { $inputData | ConvertFrom-Json }; $body=@{ paneKey=$env:ORCA_PANE_KEY; launchToken=$env:ORCA_AGENT_LAUNCH_TOKEN; tabId=$env:ORCA_TAB_ID; worktreeId=$env:ORCA_WORKTREE_ID; env=$env:ORCA_AGENT_HOOK_ENV; version=$env:ORCA_AGENT_HOOK_VERSION; hook_event_name=$env:ORCA_ANTIGRAVITY_EVENT; payload=$payload } | ConvertTo-Json -Depth 100 -Compress; $bodyBytes=$utf8.GetBytes($body); Invoke-WebRequest -UseBasicParsing -Method Post -Uri ('http://127.0.0.1:' + $env:ORCA_AGENT_HOOK_PORT + '/hook/antigravity') -ContentType 'application/json; charset=utf-8' -Headers @{ 'X-Orca-Agent-Hook-Token'=$env:ORCA_AGENT_HOOK_TOKEN } -Body $bodyBytes -TimeoutSec 2 | Out-Null } catch {}"` } function isRecord(value: unknown): value is Record { diff --git a/src/main/claude/hook-service.test.ts b/src/main/claude/hook-service.test.ts index e67bff55e..2bba3e240 100644 --- a/src/main/claude/hook-service.test.ts +++ b/src/main/claude/hook-service.test.ts @@ -21,6 +21,8 @@ import { OPENCLAUDE_HOOK_SETTINGS } from './hook-settings' const CLAUDE_SCRIPT_FILE_NAME = process.platform === 'win32' ? 'claude-hook.cmd' : 'claude-hook.sh' const OPENCLAUDE_SCRIPT_FILE_NAME = process.platform === 'win32' ? 'openclaude-hook.cmd' : 'openclaude-hook.sh' +const WINDOWS_POWERSHELL_LAUNCHER = + /^[A-Za-z]:\/[^"]*\/System32\/WindowsPowerShell\/v1\.0\/powershell\.exe -NoProfile -ExecutionPolicy Bypass -EncodedCommand \S+$/ type FakeFs = { files: Map @@ -163,7 +165,7 @@ describe('ClaudeHookService.install', () => { expect( legacyCommands.some((command: string) => process.platform === 'win32' - ? command.startsWith('powershell -NoProfile -ExecutionPolicy Bypass -EncodedCommand ') + ? WINDOWS_POWERSHELL_LAUNCHER.test(command) : command.includes(CLAUDE_SCRIPT_FILE_NAME) ) ).toBe(true) @@ -174,7 +176,7 @@ describe('ClaudeHookService.install', () => { ).toBe(false) expect(legacy.hooks.StopFailure[0].hooks[0].command).toMatch( process.platform === 'win32' - ? /^powershell -NoProfile -ExecutionPolicy Bypass -EncodedCommand \S+$/ + ? WINDOWS_POWERSHELL_LAUNCHER : new RegExp(CLAUDE_SCRIPT_FILE_NAME) ) expect( @@ -205,9 +207,7 @@ describe('ClaudeHookService.install', () => { for (const eventName of ['UserPromptSubmit', 'Stop', 'StopFailure']) { const command = settings.hooks[eventName]?.[0]?.hooks?.[0]?.command - expect(command).toMatch( - /^powershell -NoProfile -ExecutionPolicy Bypass -EncodedCommand \S+$/ - ) + expect(command).toMatch(WINDOWS_POWERSHELL_LAUNCHER) } } finally { vi.unstubAllEnvs() @@ -352,9 +352,7 @@ describe('OpenClaudeHookService-compatible install', () => { for (const event of ['UserPromptSubmit', 'Stop', 'StopFailure']) { const command = parsed.hooks[event][0].hooks[0].command as string if (process.platform === 'win32') { - expect(command).toMatch( - /^powershell -NoProfile -ExecutionPolicy Bypass -EncodedCommand \S+$/ - ) + expect(command).toMatch(WINDOWS_POWERSHELL_LAUNCHER) } else { expect(command).toContain(OPENCLAUDE_SCRIPT_FILE_NAME) expect(command).toMatch(/^if \[ -x /) diff --git a/src/main/codex/hook-service.test.ts b/src/main/codex/hook-service.test.ts index c0f6c1aac..bea05fee3 100644 --- a/src/main/codex/hook-service.test.ts +++ b/src/main/codex/hook-service.test.ts @@ -40,6 +40,9 @@ vi.mock('os', async (importOriginal) => { import { CodexHookService } from './hook-service' +const WINDOWS_POWERSHELL_LAUNCHER = + /^[A-Za-z]:\/[^"]*\/System32\/WindowsPowerShell\/v1\.0\/powershell\.exe -NoProfile -ExecutionPolicy Bypass -EncodedCommand \S+$/ + let tmpHome: string let userDataDir: string @@ -183,10 +186,10 @@ describe('CodexHookService', () => { // Why: #6078 — a Windows user profile path like `C:\Users\Jane Doe` used to // be written verbatim as the hook command, so Codex split it at the space and - // the hook exited with code 1. The managed command uses an encoded launcher - // so the path never appears raw on the cmd.exe command line. + // the hook exited with code 1. Keep spaced paths on the encoded launcher so + // `cmd.exe /C` never sees the raw script path. it.skipIf(process.platform !== 'win32')( - 'wraps the managed hook command in an encoded launcher when the profile path contains a space (#6078)', + 'wraps the managed hook command when the profile path contains a space (#6078)', () => { const spaceHome = join(tmpdir(), 'orca home with spaces') mkdirSync(spaceHome, { recursive: true }) @@ -205,9 +208,7 @@ describe('CodexHookService', () => { for (const eventName of localManagedCodexEvents()) { const command = hooksConfig.hooks[eventName]?.[0]?.hooks?.[0]?.command - expect(command).toMatch( - /^powershell -NoProfile -ExecutionPolicy Bypass -EncodedCommand \S+$/ - ) + expect(command).toMatch(WINDOWS_POWERSHELL_LAUNCHER) } } finally { rmSync(spaceHome, { recursive: true, force: true }) @@ -215,6 +216,36 @@ describe('CodexHookService', () => { } ) + // Why: cmd.exe expands `%` and treats `^` as an escape even inside otherwise + // plausible paths. Keep those rare cases on the encoded launcher from #6078. + it.skipIf(process.platform !== 'win32')( + 'keeps the encoded launcher when the profile path contains cmd metacharacters', + () => { + const metacharHome = join(tmpdir(), 'orca %ORCA_TEST% ^ home') + mkdirSync(metacharHome, { recursive: true }) + homedirMock.mockReturnValue(metacharHome) + try { + const systemCodexHome = join(metacharHome, '.codex') + mkdirSync(systemCodexHome, { recursive: true }) + + const status = new CodexHookService().install() + expect(status.state).toBe('installed') + + const managedCodexHome = join(userDataDir, 'codex-runtime-home', 'home') + const hooksConfig = JSON.parse( + readFileSync(join(managedCodexHome, 'hooks.json'), 'utf-8') + ) as { hooks: Record } + + for (const eventName of localManagedCodexEvents()) { + const command = hooksConfig.hooks[eventName]?.[0]?.hooks?.[0]?.command + expect(command).toMatch(WINDOWS_POWERSHELL_LAUNCHER) + } + } finally { + rmSync(metacharHome, { recursive: true, force: true }) + } + } + ) + // Why: the common case — a profile path with no spaces or cmd metacharacters // — must launch the .cmd directly with no PowerShell, restoring the pre-#6078 // speed that Codex 0.140's synchronous "Running hook" rows expose. @@ -237,7 +268,7 @@ describe('CodexHookService', () => { expect(command).not.toMatch(/powershell/i) expect(command).toMatch(/\\agent-hooks\\codex-hook\.cmd$/) } else { - expect(command).toMatch(/^powershell -NoProfile/) + expect(command).toMatch(WINDOWS_POWERSHELL_LAUNCHER) } } ) diff --git a/src/main/codex/hook-service.ts b/src/main/codex/hook-service.ts index b090bc527..c8ab51dfd 100644 --- a/src/main/codex/hook-service.ts +++ b/src/main/codex/hook-service.ts @@ -122,11 +122,8 @@ function getManagedScriptPath(): string { // Why: a Windows script path is cmd-safe when it holds only characters cmd.exe // passes through untouched (drive letter, backslash, dot, dash, underscore). -// Codex runs hooks as `cmd.exe /C ` and forwards our string verbatim -// when it has no spaces/quotes, so a bare path then launches with zero shell -// startup. Spaces or cmd metacharacters (`% ^ & ! ( )` etc.) force the encoded -// PowerShell launcher (#6078), which hides the path in base64. `~` is allowed -// because 8.3 short paths (e.g. `RUNNER~1`) are cmd-safe and common. +// Spaces or cmd metacharacters force the encoded launcher; `cmd.exe /C` splits +// bare `.cmd` paths at spaces before the script can run. const WINDOWS_CMD_SAFE_PATH = /^[A-Za-z0-9_.:\\~-]+$/ function getManagedCommand(scriptPath: string): string { diff --git a/src/main/command-code/hook-service.test.ts b/src/main/command-code/hook-service.test.ts index 7dd2abcb9..c6b88b27d 100644 --- a/src/main/command-code/hook-service.test.ts +++ b/src/main/command-code/hook-service.test.ts @@ -20,6 +20,9 @@ vi.mock('os', async () => { import { CommandCodeHookService } from './hook-service' +const WINDOWS_POWERSHELL_LAUNCHER = + /^[A-Za-z]:\/[^"]*\/System32\/WindowsPowerShell\/v1\.0\/powershell\.exe -NoProfile -ExecutionPolicy Bypass -EncodedCommand \S+$/ + describe('CommandCodeHookService', () => { let homeDir: string @@ -49,9 +52,7 @@ describe('CommandCodeHookService', () => { expect(config.hooks.PostToolUse[0].matcher).toBe('.*') expect(config.hooks.Stop[0].matcher).toBeUndefined() expect(config.hooks.PreToolUse[0].hooks[0].command).toMatch( - process.platform === 'win32' - ? /^powershell -NoProfile -ExecutionPolicy Bypass -EncodedCommand \S+$/ - : /command-code-hook/ + process.platform === 'win32' ? WINDOWS_POWERSHELL_LAUNCHER : /command-code-hook/ ) if (process.platform !== 'win32') { expect(config.hooks.PreToolUse[0].hooks[0].command).toContain(join(homeDir, '.orca')) @@ -79,9 +80,7 @@ describe('CommandCodeHookService', () => { ) as { hooks: Record } const command = config.hooks.PreToolUse[0].hooks[0].command - expect(command).toMatch( - /^powershell -NoProfile -ExecutionPolicy Bypass -EncodedCommand \S+$/ - ) + expect(command).toMatch(WINDOWS_POWERSHELL_LAUNCHER) } finally { rmSync(spaceHome, { recursive: true, force: true }) } diff --git a/src/main/cursor/hook-service.test.ts b/src/main/cursor/hook-service.test.ts index 4f310d595..ba04ba2be 100644 --- a/src/main/cursor/hook-service.test.ts +++ b/src/main/cursor/hook-service.test.ts @@ -29,6 +29,8 @@ const CURSOR_EVENTS = [ ] const CURSOR_SCRIPT_FILE_NAME = process.platform === 'win32' ? 'cursor-hook.cmd' : 'cursor-hook.sh' +const WINDOWS_POWERSHELL_LAUNCHER = + /^[A-Za-z]:\/[^"]*\/System32\/WindowsPowerShell\/v1\.0\/powershell\.exe -NoProfile -ExecutionPolicy Bypass -EncodedCommand \S+$/ describe('CursorHookService', () => { let homeDir: string @@ -59,9 +61,7 @@ describe('CursorHookService', () => { for (const eventName of CURSOR_EVENTS) { const definition = config.hooks[eventName]?.[0] expect(definition?.command).toMatch( - process.platform === 'win32' - ? /^powershell -NoProfile -ExecutionPolicy Bypass -EncodedCommand \S+$/ - : /cursor-hook/ + process.platform === 'win32' ? WINDOWS_POWERSHELL_LAUNCHER : /cursor-hook/ ) if (process.platform !== 'win32') { expect(definition?.command).toContain(join(homeDir, '.orca')) @@ -75,7 +75,7 @@ describe('CursorHookService', () => { ) expect(script).toContain('/hook/cursor') if (process.platform === 'win32') { - expect(script).toContain('powershell -NoProfile') + expect(script).toContain('%SystemRoot%\\System32\\curl.exe') } else { expect(script).toContain('payload=$(cat)') } @@ -100,9 +100,7 @@ describe('CursorHookService', () => { for (const eventName of ['beforeSubmitPrompt', 'stop']) { const command = config.hooks[eventName]?.[0]?.command - expect(command).toMatch( - /^powershell -NoProfile -ExecutionPolicy Bypass -EncodedCommand \S+$/ - ) + expect(command).toMatch(WINDOWS_POWERSHELL_LAUNCHER) } } finally { rmSync(spaceHome, { recursive: true, force: true }) @@ -144,7 +142,7 @@ describe('CursorHookService', () => { expect( promptCommands.filter((command) => process.platform === 'win32' - ? command?.startsWith('powershell -NoProfile -ExecutionPolicy Bypass -EncodedCommand ') + ? command !== undefined && WINDOWS_POWERSHELL_LAUNCHER.test(command) : command?.includes(CURSOR_SCRIPT_FILE_NAME) ) ).toHaveLength(1) diff --git a/src/main/droid/hook-service.test.ts b/src/main/droid/hook-service.test.ts index 65ac15383..f5943f918 100644 --- a/src/main/droid/hook-service.test.ts +++ b/src/main/droid/hook-service.test.ts @@ -24,6 +24,9 @@ vi.mock('os', async () => { import { DroidHookService } from './hook-service' +const WINDOWS_POWERSHELL_LAUNCHER = + /^[A-Za-z]:\/[^"]*\/System32\/WindowsPowerShell\/v1\.0\/powershell\.exe -NoProfile -ExecutionPolicy Bypass -EncodedCommand \S+$/ + describe('DroidHookService', () => { let homeDir: string let userDataDir: string @@ -71,9 +74,7 @@ describe('DroidHookService', () => { expect(config.hooks.PermissionRequest[0].matcher).toBe('*') expect(config.hooks.UserPromptSubmit[0].matcher).toBeUndefined() expect(config.hooks.PreToolUse[0].hooks[0].command).toMatch( - process.platform === 'win32' - ? /^powershell -NoProfile -ExecutionPolicy Bypass -EncodedCommand \S+$/ - : /droid-hook/ + process.platform === 'win32' ? WINDOWS_POWERSHELL_LAUNCHER : /droid-hook/ ) if (process.platform !== 'win32') { expect(config.hooks.PreToolUse[0].hooks[0].command).toContain(join(homeDir, '.orca')) @@ -100,9 +101,7 @@ describe('DroidHookService', () => { for (const eventName of ['SessionStart', 'UserPromptSubmit', 'Stop']) { const command = config.hooks[eventName]?.[0]?.hooks?.[0]?.command - expect(command).toMatch( - /^powershell -NoProfile -ExecutionPolicy Bypass -EncodedCommand \S+$/ - ) + expect(command).toMatch(WINDOWS_POWERSHELL_LAUNCHER) } } finally { rmSync(spaceHome, { recursive: true, force: true }) diff --git a/src/main/gemini/hook-service.test.ts b/src/main/gemini/hook-service.test.ts index 587762de9..de859838b 100644 --- a/src/main/gemini/hook-service.test.ts +++ b/src/main/gemini/hook-service.test.ts @@ -25,6 +25,9 @@ vi.mock('os', async (importOriginal) => { import { GeminiHookService } from './hook-service' +const WINDOWS_POWERSHELL_LAUNCHER = + /^[A-Za-z]:\/[^"]*\/System32\/WindowsPowerShell\/v1\.0\/powershell\.exe -NoProfile -ExecutionPolicy Bypass -EncodedCommand \S+$/ + describe('GeminiHookService', () => { let homeDir: string let userDataDir: string @@ -102,7 +105,7 @@ describe('GeminiHookService', () => { expect(config.hooks.BeforeAgent[0].hooks[0].command).toBe('echo user-before-agent') const managedCommandPattern = process.platform === 'win32' - ? /^powershell -NoProfile -ExecutionPolicy Bypass -EncodedCommand \S+$/ + ? WINDOWS_POWERSHELL_LAUNCHER : new RegExp(managedHookPath.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')) expect(config.hooks.BeforeAgent[1].hooks[0].command).toMatch(managedCommandPattern) expect(config.hooks.AfterAgent[0].hooks[0].command).toMatch(managedCommandPattern) @@ -129,9 +132,7 @@ describe('GeminiHookService', () => { for (const eventName of ['BeforeAgent', 'AfterAgent', 'AfterTool']) { const command = config.hooks[eventName]?.[0]?.hooks?.[0]?.command - expect(command).toMatch( - /^powershell -NoProfile -ExecutionPolicy Bypass -EncodedCommand \S+$/ - ) + expect(command).toMatch(WINDOWS_POWERSHELL_LAUNCHER) } } finally { rmSync(spaceHome, { recursive: true, force: true }) @@ -183,7 +184,7 @@ describe('GeminiHookService', () => { expect(preToolCommands).toEqual(['echo user-authored']) expect(config.hooks.BeforeTool[0].hooks[0].command).toMatch( process.platform === 'win32' - ? /^powershell -NoProfile -ExecutionPolicy Bypass -EncodedCommand \S+$/ + ? WINDOWS_POWERSHELL_LAUNCHER : new RegExp(managedHookFileName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')) ) }) diff --git a/src/main/grok/hook-service.test.ts b/src/main/grok/hook-service.test.ts index 52d46fbcf..5a3bbd021 100644 --- a/src/main/grok/hook-service.test.ts +++ b/src/main/grok/hook-service.test.ts @@ -18,6 +18,8 @@ vi.mock('os', async () => { import { GrokHookService } from './hook-service' const GROK_SCRIPT_FILE_NAME = process.platform === 'win32' ? 'grok-hook.cmd' : 'grok-hook.sh' +const WINDOWS_POWERSHELL_LAUNCHER = + /^[A-Za-z]:\/[^"]*\/System32\/WindowsPowerShell\/v1\.0\/powershell\.exe -NoProfile -ExecutionPolicy Bypass -EncodedCommand \S+$/ describe('GrokHookService', () => { let homeDir: string @@ -60,9 +62,7 @@ describe('GrokHookService', () => { expect(config.hooks.PostToolUseFailure[0].matcher).toBe('*') expect(config.hooks.Notification[0].matcher).toBeUndefined() expect(config.hooks.PreToolUse[0].hooks[0].command).toMatch( - process.platform === 'win32' - ? /^powershell -NoProfile -ExecutionPolicy Bypass -EncodedCommand \S+$/ - : /grok-hook/ + process.platform === 'win32' ? WINDOWS_POWERSHELL_LAUNCHER : /grok-hook/ ) if (process.platform !== 'win32') { expect(config.hooks.PreToolUse[0].hooks[0].command).toContain(join(homeDir, '.orca')) @@ -74,7 +74,7 @@ describe('GrokHookService', () => { ) expect(script).toContain('/hook/grok') if (process.platform === 'win32') { - expect(script).toContain('powershell -NoProfile') + expect(script).toContain('%SystemRoot%\\System32\\curl.exe') } else { expect(script).toContain('payload=$(cat)') } @@ -99,9 +99,7 @@ describe('GrokHookService', () => { for (const eventName of ['SessionStart', 'UserPromptSubmit', 'Stop']) { const command = config.hooks[eventName]?.[0]?.hooks?.[0]?.command - expect(command).toMatch( - /^powershell -NoProfile -ExecutionPolicy Bypass -EncodedCommand \S+$/ - ) + expect(command).toMatch(WINDOWS_POWERSHELL_LAUNCHER) } } finally { rmSync(spaceHome, { recursive: true, force: true }) @@ -137,7 +135,7 @@ describe('GrokHookService', () => { expect( commands.some((command) => process.platform === 'win32' - ? command.startsWith('powershell -NoProfile -ExecutionPolicy Bypass -EncodedCommand ') + ? WINDOWS_POWERSHELL_LAUNCHER.test(command) : command.includes(GROK_SCRIPT_FILE_NAME) ) ).toBe(true)