|
| 1 | +/** |
| 2 | + * Regression coverage for the Windows console-popup fix (#1278). |
| 3 | + * |
| 4 | + * The desktop proxy parent runs without a console. Every console-subsystem |
| 5 | + * child it spawns without CREATE_NO_WINDOW (`windowsHide`) gets a fresh |
| 6 | + * visible console window — observed at startup, on config writes, and on |
| 7 | + * shutdown. The proxy-internal identity and process lookups must therefore |
| 8 | + * spawn PowerShell hidden, from the trusted System32 directory (never PATH), |
| 9 | + * and under a bounded timeout so a hung child cannot wedge those paths. |
| 10 | + */ |
| 11 | +import { afterEach, describe, expect, test } from "bun:test"; |
| 12 | + |
| 13 | +import { readProcessStartMsBatch } from "../src/codex/app-server-processes"; |
| 14 | +import { |
| 15 | + resolveEffectiveUserIdentity, |
| 16 | + windowsIdentityPowerShellCommandForTests, |
| 17 | + windowsIdentityPowerShellSpawnOptionsForTests, |
| 18 | +} from "../src/codex/user-identity"; |
| 19 | +import { setTrustedWindowsElevationExecutablesForTests } from "../src/lib/windows-elevation"; |
| 20 | + |
| 21 | +const TRUSTED_POWERSHELL = "C:\\trusted-system32\\WindowsPowerShell\\v1.0\\powershell.exe"; |
| 22 | + |
| 23 | +afterEach(() => { |
| 24 | + setTrustedWindowsElevationExecutablesForTests(null); |
| 25 | +}); |
| 26 | + |
| 27 | +describe("Windows identity lookup popup fix (#1278)", () => { |
| 28 | + test("builds a hidden non-interactive command from the trusted PowerShell path", () => { |
| 29 | + setTrustedWindowsElevationExecutablesForTests({ powershell: TRUSTED_POWERSHELL }); |
| 30 | + const command = windowsIdentityPowerShellCommandForTests( |
| 31 | + "[System.Security.Principal.WindowsIdentity]::GetCurrent().User.Value", |
| 32 | + ); |
| 33 | + expect(command[0]).toBe(TRUSTED_POWERSHELL); |
| 34 | + expect(command).toContain("-NoProfile"); |
| 35 | + expect(command).toContain("-NonInteractive"); |
| 36 | + const windowStyle = command.indexOf("-WindowStyle"); |
| 37 | + expect(windowStyle).toBeGreaterThan(0); |
| 38 | + expect(command[windowStyle + 1]).toBe("Hidden"); |
| 39 | + expect(command[command.length - 2]).toBe("-Command"); |
| 40 | + expect(command[command.length - 1]) |
| 41 | + .toBe("[System.Security.Principal.WindowsIdentity]::GetCurrent().User.Value"); |
| 42 | + }); |
| 43 | + |
| 44 | + test("spawn options are hidden and bounded", () => { |
| 45 | + const options = windowsIdentityPowerShellSpawnOptionsForTests(); |
| 46 | + expect(options.windowsHide).toBe(true); |
| 47 | + expect(options.stdin).toBe("ignore"); |
| 48 | + // Assert the exact budget: the identity lookup contract is an 8-second |
| 49 | + // bound, and a looser assertion would let a silent re-tune through. |
| 50 | + expect(options.timeout).toBe(8_000); |
| 51 | + }); |
| 52 | + |
| 53 | + test("the hidden trusted lookup resolves the real token on Windows", () => { |
| 54 | + if (process.platform !== "win32") return; |
| 55 | + const identity = resolveEffectiveUserIdentity(); |
| 56 | + expect(identity.platform).toBe("win32"); |
| 57 | + if (identity.platform === "win32") { |
| 58 | + expect(identity.sid).toMatch(/^S-1-(?:\d+-)+\d+$/i); |
| 59 | + } |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +describe("Windows process-lookup popup fix (#1278)", () => { |
| 64 | + test("batch start-time lookup is trusted, bounded, and never throws cross-platform", () => { |
| 65 | + // On POSIX hosts the trusted System32 resolver throws inside the win32 |
| 66 | + // branch's catch — the batch must degrade to nulls, exactly like a |
| 67 | + // missing/failed PowerShell, instead of propagating. |
| 68 | + const batch = readProcessStartMsBatch([process.pid], "win32"); |
| 69 | + const startedAtMs = batch.get(process.pid) ?? null; |
| 70 | + if (process.platform === "win32") { |
| 71 | + expect(startedAtMs).not.toBeNull(); |
| 72 | + expect(Number.isFinite(startedAtMs)).toBe(true); |
| 73 | + expect(startedAtMs!).toBeGreaterThan(0); |
| 74 | + } else { |
| 75 | + expect(startedAtMs).toBeNull(); |
| 76 | + } |
| 77 | + }); |
| 78 | +}); |
0 commit comments