Files
orca/src/main/powershell-osc133-bootstrap.test.ts
T
OrcaWinandOrca Worker c3c764fa03 docs(win): record why the PTY OSC 133 bootstrap keeps -EncodedCommand (#17875)
The MDE report lists src/main/daemon/shell-ready.ts as a contributing
"suspicious PowerShell" site and cites VS Code as using -Command. Measured
against a real VS Code fork install, VS Code's -Command payload is a one-liner
that dot-sources a *file*; that shape is execution-policy gated and is blocked
under Restricted and AllSigned, so it would silently drop OSC 133 -- and with
it foreground-process and exit-code tracking -- on the managed fleets MDE runs
on.

Inline -Command does carry the payload intact through node-pty/ConPTY
(powershell.exe 5.1 and pwsh 7.6.5), so the switch is feasible. It is declined
because no PTY site spells -ExecutionPolicy Bypass, AMSI and script-block
logging decode the payload either way, and the swap would put
$ExecutionContext.SessionState.LanguageMode, a Global:prompt override and
[char]27-assembled control sequences in clear text on every terminal's command
line -- higher-signal than the token it removes.

No behaviour change. Adds the rationale at the payload's source of truth,
one-line pointers at the three PTY launch sites, and a ratchet that both
launch builders must deliver the bootstrap byte for byte.

Co-authored-by: Orca Worker <orca-worker@localhost>
2026-09-01 23:21:14 -07:00

78 lines
3.4 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import {
encodePowerShellCommand,
getPowerShellOsc133Bootstrap
} from './powershell-osc133-bootstrap'
import { getShellLaunchConfig } from './daemon/shell-ready'
import { resolveWindowsShellLaunchArgs } from './providers/windows-shell-args'
import { STARTUP_COMMAND_FEATURES } from './shell-startup-launch-intent-fixtures'
describe('PowerShell OSC 133 bootstrap', () => {
it('wraps prompt/readline without bypassing profiles or execution policy', () => {
const script = getPowerShellOsc133Bootstrap()
expect(script).toContain('[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new()')
expect(script).toContain('ORCA_OPENCODE_CONFIG_DIR')
expect(script).toContain('ORCA_MIMOCODE_HOME')
expect(script).not.toContain('ORCA_PI_CODING_AGENT_DIR')
expect(script).not.toContain('ORCA_OMP_CODING_AGENT_DIR')
expect(script).toContain('ORCA_OMP_STATUS_EXTENSION')
expect(script).toContain('function Global:omp')
expect(script).toContain('--extension $env:ORCA_OMP_STATUS_EXTENSION')
expect(script).toContain('ORCA_CODEX_HOME')
expect(script).toContain('ORCA_CODEX_LAUNCH_PREFLIGHT')
expect(script).toContain('function Global:codex')
expect(script).not.toContain('$Global:__OrcaCodexExecutable')
expect(script).toContain('function Global:prompt')
expect(script).toContain('function Global:PSConsoleHostReadLine')
expect(script).toContain('Esc = [char]27')
expect(script).toContain('Bel = [char]7')
expect(script).toContain(')]133;D;$fakeExitCode$(')
expect(script).toContain(')]133;A$(')
expect(script).toContain(')]133;B$(')
expect(script).toContain(')]133;C$(')
expect(script).not.toContain('`e]133')
expect(script).not.toContain('$PROFILE')
expect(script).not.toContain('ExecutionPolicy')
expect(script).not.toContain('NoProfile')
const codexHomeRestore = script.indexOf('if ($env:ORCA_CODEX_HOME)')
expect(codexHomeRestore).toBeGreaterThan(-1)
expect(codexHomeRestore).toBeLessThan(script.indexOf('Test-Path variable:global:'))
expect(codexHomeRestore).toBeLessThan(script.indexOf('LanguageMode -eq "FullLanguage"'))
})
it('encodes commands as UTF-16LE base64 for PowerShell -EncodedCommand', () => {
expect(encodePowerShellCommand('Write-Output ok')).toBe(
Buffer.from('Write-Output ok', 'utf16le').toString('base64')
)
})
// Why pinned: the MDE review (see powershell-osc133-bootstrap.ts) declined a
// switch to -Command. Any future delivery shape must still hand PowerShell this
// payload byte for byte -- comments, quotes, `$` and newlines included.
describe.each([
[
'daemon shell-ready',
() => getShellLaunchConfig('powershell.exe', STARTUP_COMMAND_FEATURES).args ?? []
],
[
'windows shell args',
() => resolveWindowsShellLaunchArgs('pwsh.exe', 'C:\\repo', 'C:\\repo').shellArgs
]
])('%s PowerShell launch', (_name, getArgs) => {
it('delivers the bootstrap unmangled', () => {
const args = getArgs()
const encodedIndex = args.indexOf('-EncodedCommand')
expect(encodedIndex).toBeGreaterThanOrEqual(0)
expect(args).not.toContain('-Command')
expect(args).not.toContain('-ExecutionPolicy')
const delivered = Buffer.from(args[encodedIndex + 1] ?? '', 'base64').toString('utf16le')
expect(delivered.startsWith(getPowerShellOsc133Bootstrap())).toBe(true)
})
})
})