Skip to content

fix(hooks): stop the Windows hook payload consuming its own stdin - #19272

Open
brennanb2025 wants to merge 1 commit into
mainfrom
brennanb2025/pr-windows-hook-execution-policy
Open

brennanb2025 wants to merge 1 commit into
mainfrom
brennanb2025/pr-windows-hook-execution-policy

Conversation

@brennanb2025

@brennanb2025 brennanb2025 commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Fixes STA-6357.

The defect

Orca's Windows hook launcher prepends Set-ExecutionPolicy … Bypass to every encoded PowerShell payload. Under PowerShell 5.1 that cmdlet consumes the hook's stdin, so the hook receives an empty or truncated payload and fails.

Blast radius is wider than the ticket suggests. After #18875 Claude reaches the encoded path only as a fallback, but Copilot, Cursor, Gemini, Droid and Command-Code go through it on every hook event, and the .cmd wrapper falls back to it whenever a path is not cmd-safe.

Why not the fix the ticket proposes

STA-6357 suggests WINDOWS_POWERSHELL_HOOK_SWITCHES = '-NoProfile -NonInteractive'.

That command line's exact shape was chosen by AV measurement (#16003, recorded in the launcher's own comment): on the reporting host every variant containing -WindowStyle Hidden exited 126, while -NoProfile -EncodedCommand succeeded five times out of five. The denial happens at CreateProcess and matches on the flag combination — and -NoProfile -NonInteractive -EncodedCommand does not appear in that measured table. Adding the switch would be an unmeasured change to the shape that STA-6325 was about, shipped blind.

The fix

The execution-policy prefix exists solely so managed .ps1 hooks can run under a Restricted or AllSigned policy. copilot-hook.ps1 is the only .ps1 managed hook — Claude, Codex, Cursor, Gemini, Droid, Grok, Devin, Antigravity and Command-Code are all .cmd, which execution policy does not govern at all.

So the prefix is now conditional on the payload being a .ps1. That removes the stdin-consuming cmdlet for every affected agent while leaving the executable path and the switches byte-identical — everything up to -EncodedCommand is unchanged; only the base64 payload after it differs. #16003 measured the denial at CreateProcess and independent of the payload (an exit 0 payload was denied too), so the flag combination is the entire AV surface: no AV risk, no re-measurement, and no coupling to STA-6325.

The option defaults to false deliberately, and the reasoning is commented: forgetting it on a .ps1 produces a loud policy failure under a locked-down GPO, whereas the reverse mistake silently corrupts every hook's stdin. The quiet failure is the one kept off the default.

The headline test

keeps the launch shape byte-identical whichever payload it carries (AV-measured; re-measure before changing) asserts the command line is identical with and without the bypass, equals the exact documented shape, and that the two payloads genuinely differ — so the identity assertion cannot pass vacuously. A sibling assertion in installer-utils.test.ts pins the same shape identity one level up, across the .cmd and .ps1 callers of wrapWindowsHookCommand.

Its comment records that the denial is at CreateProcess, that folding -NonInteractive into the switches constant is the tempting change (STA-6357 proposes exactly that), and that altering the shape requires a measurement rather than an argument. If someone later makes that change, this test is the prompt to go read STA-6885.

Reviewer's attention, flagged rather than buried

Four existing assertions in the launcher suite were modified. They asserted the bypass unconditionally, which is the old contract; they are now scoped to { executionPolicyBypass: true }, keeping every assertion about what the bypass looks like when present — try/catch wrapping, ordering after the progress silencer, exact text.

This is called out because that file's own comment records that "#16576 round 3 dropped a required launcher invariant by accident and updated the tests to match, so nothing caught it." No invariant was weakened here — the scope was narrowed and new ones added — but it is precisely the shape of mistake that file warns about, and it deserves an eye.

Evidence

Reverting the two production files turns 9 tests red: the headline identity test, both .cmd omission tests, the needsHookExecutionPolicyBypass cases, and four pre-existing tests via the updated expectation helper.

All hook subsystems (agent-hooks, claude, copilot, cursor, gemini, droid, codex, grok, devin, antigravity, command-code): 324 files, 3,143 passed, 0 failed. Typecheck, oxfmt, oxlint, max-lines, ts-nocheck, reliability manifest and the changed-code gate all clean.

The Windows gap, stated not buried

This cannot prove that removing the prefix actually stops PS 5.1 consuming stdin, that hook JSON parses cleanly afterwards, or that the payload arrives non-empty. Those need a Windows host with PowerShell 5.1 — the reporter measured them; this change did not.

What makes it safe to land without that proof is the asymmetry: the change cannot alter the launch shape, and there is a test that fails if anyone makes it.

Related

On Windows PowerShell 5.1, Set-ExecutionPolicy takes a host confirmation path
even with -Force -ErrorAction SilentlyContinue. With stdin redirected — how
every agent delivers hook JSON — that prompt consumes the payload and echoes it
to stdout, so the agent rejects the hook output as malformed JSON and the script
behind it reads empty stdin.

Emit the bypass only for a payload that runs a .ps1. Copilot ships the only
managed .ps1 hook and still needs it under a locked-down policy; every other
hook is a .cmd, which execution policy does not govern.

The launch shape is unchanged by construction, and a test pins that: the shape
was chosen by AV measurement at CreateProcess, so it may only change alongside a
new measurement.
@coderabbitai

coderabbitai Bot commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Team

Run ID: 40f23ac5-2915-4e72-ae07-d4535a50c04b

📥 Commits

Reviewing files that changed from the base of the PR and between 9c8f4c3 and b22379b.

📒 Files selected for processing (4)
  • src/main/agent-hooks/installer-utils.test.ts
  • src/main/agent-hooks/installer-utils.ts
  • src/main/agent-hooks/windows-powershell-hook-launcher.test.ts
  • src/main/agent-hooks/windows-powershell-hook-launcher.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 2 remain after this review.


📝 Walkthrough

Walkthrough

The PowerShell hook launcher now accepts an optional execution-policy bypass setting. The setting defaults to disabled and enables the bypass snippet only when requested. The installer derives the setting from the managed script path, enabling it for .ps1 files and disabling it for .cmd files. Tests cover casing, decoded payloads, command-line shape, and installer integration.

Merge Risk: ⚪ Minimal · up to b2237

Windows .cmd hooks no longer include the PowerShell execution-policy command that could consume hook stdin, while .ps1 hooks retain it. The changed behavior is covered for both payload types and is ready to merge.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 6 functions across 4 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly describes the primary defect addressed by the change: preventing Windows hook payloads from consuming their own stdin.
Description check ✅ Passed The description is detailed and covers the change, rationale, linked issue, testing evidence, limitations, compatibility considerations, and related work. It does not reproduce every template heading …
  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes

Reviewed the fix that makes the Windows hook launcher's execution-policy bypass conditional on the payload being a .ps1, so .cmd hooks stop triggering the PowerShell 5.1 stdin-consuming Set-ExecutionPolicy cmdlet.

  • needsHookExecutionPolicyBypass + WindowsPowerShellHookOptions — the bypass becomes an explicit executionPolicyBypass option on encodeWindowsPowerShellHookCommand/wrapWindowsPowerShellEncodedCommand, defaulting to false.
  • wrapWindowsHookCommand (installer-utils.ts) — keys the bypass off the script extension, so only Copilot's .ps1 keeps it and every .cmd hook drops it.
  • Test coverage — a headline identity test pins the AV-measured launch shape byte-identical across payloads (with an anti-vacuity guard that the payloads differ), plus new .cmd-omission, .ps1-retention, and helper tests; four pre-existing assertions narrowed to the explicit { executionPolicyBypass: true } path.

I traced every caller of the two launcher entry points to confirm the .ps1/.cmd split is complete and correct: Copilot (copilot-managed-hook-definitions.ts) is the only .ps1 and keeps the bypass; Claude's encoded fallback (hook-settings.ts), runtime-home-hook-command.ts, and the .cmd agents all run batch files and correctly drop it. No .ps1 path loses the bypass and no .cmd path keeps it. The default-false asymmetry (loud GPO failure vs. silent stdin corruption) and the shape-invariance safety argument are sound, and the two touched test files pass locally.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes

Reviewed the fix that makes the Windows hook launcher's execution-policy bypass conditional on the payload being a .ps1, so .cmd hooks stop triggering the PowerShell 5.1 stdin-consuming Set-ExecutionPolicy cmdlet.

  • needsHookExecutionPolicyBypass + WindowsPowerShellHookOptions — the bypass becomes an explicit executionPolicyBypass option on encodeWindowsPowerShellHookCommand/wrapWindowsPowerShellEncodedCommand, defaulting to false.
  • wrapWindowsHookCommand (installer-utils.ts) — keys the bypass off the script extension, so only Copilot's .ps1 keeps it and every .cmd hook drops it.
  • Test coverage — a headline identity test pins the AV-measured launch shape byte-identical across payloads (with an anti-vacuity guard that the payloads differ), plus new .cmd-omission, .ps1-retention, and helper tests; four pre-existing assertions narrowed to the explicit { executionPolicyBypass: true } path.

I traced every caller of the two launcher entry points to confirm the .ps1/.cmd split is complete and correct: Copilot (copilot-managed-hook-definitions.ts) is the only .ps1 and keeps the bypass; Claude's encoded fallback (hook-settings.ts), runtime-home-hook-command.ts, and the .cmd agents all run batch files and correctly drop it. No .ps1 path loses the bypass and no .cmd path keeps it. The default-false asymmetry (loud GPO failure vs. silent stdin corruption) and the shape-invariance safety argument are sound, and the two touched test files pass locally.

Pullfrog  | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

@brennanb2025

Copy link
Copy Markdown
Contributor Author

Status: reviewed CLEAN · one body claim corrected · Windows validation not run

Independent review — CLEAN

A wholly fresh reviewer found no defect. Two non-blocking observations, both acted on.

The load-bearing claim was verified three independent ways. This entire fix rests on copilot-hook.ps1 being the only .ps1 managed hook — if a second existed anywhere, gating the execution-policy prefix on .ps1 would silently break it under a restrictive GPO. The reviewer confirmed it via a per-agent script-name resolver table across all twelve agents; via two pre-existing tests that independently encode the same enumeration, one of which walks the installed hooks directory and pins exactly one -hook.ps1; and via a repo-wide sweep across src/, mobile/, relay/, config/ and native/.

It also checked the surface where a second .ps1 would most plausibly hide: the remote/SSH installers have no Windows branch at all — remote is .sh through wrapPosixHookCommand, untouched by this change.

Launch-shape identity was verified by execution, not by trusting the test. The reviewer bundled the two production modules and called them directly, confirming the shape is identical across both option values and across both hook kinds through wrapWindowsHookCommand. That is the right level of scepticism for a claim whose failure mode is an antivirus denial nobody can reproduce here.

The four modified assertions were each checked for weakening. All were narrowed in scope, not weakened. The subtlest is :95 (silencer ordered before the bypass), which is only meaningful when a bypass is present — so scoping it was required rather than convenient, and silencer-first remains pinned for the no-bypass case by exact equality at :140. The two strongest pre-existing invariants at :34 and :45 were untouched and still pass. This mattered because that file's own comment records #16576 round 3, where an invariant was dropped and the tests updated to match.

A claim in this PR's description was wrong, and is corrected

The body said the change leaves "the command line byte-identical." Measured, the full command line is 513 versus 209 characters — identical in the flag portion, different in the base64 payload. As written the claim was checkable and false.

It now says the executable path and switches are byte-identical, that everything up to -EncodedCommand is unchanged, and — the part that was previously implicit — why the payload is outside the AV surface: #16003 measured the denial at CreateProcess and independent of the payload, since an exit 0 payload was denied too. So the flag combination is the entire surface. That strengthens the argument rather than hedging it.

A second overstatement is also corrected: the sibling assertion pins shape identity one level up at wrapWindowsHookCommand, not end-to-end through the installer.

On the headline test, precisely

The reviewer checked each leg does independent work. Leg 3 (the two payloads genuinely differ) is the non-vacuity guard and goes red on ablation; leg 2 (the exact documented shape) goes red on a switches-constant mutation — the change STA-6357 itself proposes. They fail against different mutations, which is why the pair is worth having.

Worth stating plainly: legs 1 and 2 overlap with two pre-existing tests that already assert toBe('-NoProfile'). The genuinely new coverage is leg 3 plus the with/without-bypass pairing. The redundancy is a feature — a switches change now goes red three times instead of once — but this test is the third tripwire, not the first.

Manual validation: not run

No Windows host executed this change. The mechanism — that Set-ExecutionPolicy consumes stdin under PowerShell 5.1, and that removing it restores the hook payload — is the reporter's measurement, not one reproduced here.

What makes this safe to land without that proof is the asymmetry stated in the body: the change cannot alter the launch shape, and a test fails if anyone makes it. That is genuinely different from a fix whose safety depends on the unverified behaviour.

The measurement-gated half — adding -NonInteractive for Copilot's .ps1, which would alter the shape — is deliberately deferred to STA-6885, with the required steps and a fallback if the AV measurement comes back denied.

@nwparker

Copy link
Copy Markdown
Contributor

I’m checking the Antigravity path in this proposal against current code. Follow-up draft #21666 adds real Windows process coverage to the existing Windows CI lane, with no launcher change yet so the first run establishes the baseline.

The test installs Antigravity's managed scripts in an isolated directory, pipes a multi-KB Unicode JSON payload through the same command builder, and requires byte-exact delivery to a loopback listener plus exactly {"decision":"ask"} on stdout. It exercises both ordinary paths and a path containing spaces, which forces the encoded PowerShell fallback. This is managed-hook verification, not a live Windows agy generation or sidebar-status claim. Windows execution results are pending.

Two rebase/review constraints:

  • Current wrapWindowsHookCommand delegates to buildWindowsHookPowerShellCommand. Its missing-script path now answers the permission event before the shared environment guard and only then drains stdin. A takeover must preserve that newer behavior rather than restoring the older inline command in this diff.
  • An unchanged switch prefix supports the specific previously measured launch-shape comparison. It does not establish the PR body's broader “no AV risk” claim: docs/reference/windows-edr-posture.md records behavioral scoring of the process chain and encoded policy-bypass activity. I will keep antivirus behavior explicitly unverified.

The proposal's distinction between .cmd and .ps1 is useful; adoption still needs actual payload evidence and preservation of the Copilot .ps1 policy behavior.

@nwparker

Copy link
Copy Markdown
Contributor

Windows baseline verification is complete in #21666, with the existing launcher unchanged.

ELI5: the hook delivered the entire message and returned the permission response correctly on the Windows CI host, even when a space in its path forced it through PowerShell.

Windows job and logs:

  • windows-hook-payload-delivery.test.ts: all four tests passed.
  • Antigravity ordinary profile path: passed, 488 ms.
  • Antigravity profile path containing spaces: passed, 780 ms.
  • Both new cases required the multi-KB Unicode JSON to reach the loopback listener byte-for-byte, exactly {"decision":"ask"} on stdout, empty stderr, and exit 0.
  • Full Windows boundary step: 477 passed / 22 skipped. Neither new Antigravity case was skipped. The package job also completed successfully.

This does not reproduce the reported stdin loss on this Windows host. It verifies installed managed scripts and the generated launcher with synthetic hook input; it does not run the Windows agy binary, exercise the reporter's machine/GPO/AV setup, or prove live sidebar transitions. The proposed production change remains unadopted pending evidence of the failing host condition and a corresponding before/after check. Copilot's .ps1 behavior is outside these new Antigravity cases.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants