Skip to content

Commit ac8c0d2

Browse files
authored
fix(windows): preserve scheduled-task elevation launcher arguments (#1860)
Fix the fresh Windows scheduler install path by keeping Start-Process elevation arguments in one PowerShell statement, with focused regression coverage.
1 parent f5d9f83 commit ac8c0d2

2 files changed

Lines changed: 47 additions & 3 deletions

File tree

‎src/lib/windows-elevation.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -648,12 +648,12 @@ export function runWindowsElevatedScheduledTaskRegistration(
648648
"-EncodedCommand",
649649
encodedCommand,
650650
]))}`,
651-
" -Verb RunAs -WindowStyle Hidden -PassThru -Wait",
651+
" -Verb RunAs -WindowStyle Hidden -PassThru -Wait;",
652652
`if ($null -eq $p) { exit ${OCX_ELEVATED_UAC_CANCELLED} }`,
653-
"$null = $p.Handle",
653+
"$null = $p.Handle;",
654654
`if ($null -eq $p.ExitCode) { exit ${OCX_ELEVATED_PROTOCOL_FAILED} }`,
655655
"exit $p.ExitCode",
656-
].join("; ");
656+
].join("");
657657

658658
return startPowerShellCommand(script).completion.then(result => result.exitCode);
659659
}

‎tests/windows-elevation-spawn.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,50 @@ describe("runWindowsElevated spawn contract", () => {
120120
await expect(runWindowsElevated("schtasks.exe", ["/create"])).resolves.toBe(1);
121121
});
122122

123+
test("keeps scheduled-task elevation arguments in one Start-Process statement", async () => {
124+
let commandScript = "";
125+
setWindowsElevationSpawnForTests(((
126+
_cmd: string,
127+
args: ReadonlyArray<string>,
128+
) => {
129+
commandScript = String(args[args.length - 1] ?? "");
130+
const child = new EventEmitter() as EventEmitter & {
131+
stdout: EventEmitter & { setEncoding?: (enc: string) => void };
132+
stderr: EventEmitter & { setEncoding?: (enc: string) => void };
133+
kill: ReturnType<typeof mock>;
134+
};
135+
child.stdout = new EventEmitter();
136+
child.stderr = new EventEmitter();
137+
child.stdout.setEncoding = () => undefined;
138+
child.stderr.setEncoding = () => undefined;
139+
child.kill = mock(() => true);
140+
queueMicrotask(() => child.emit("close", 0, null));
141+
return child as never;
142+
}) as never);
143+
144+
await expect(runWindowsElevatedScheduledTaskRegistration(
145+
"opencodex-proxy",
146+
"<Task />",
147+
)).resolves.toBe(0);
148+
149+
const startProcessIndex = commandScript.indexOf("Start-Process");
150+
const filePathIndex = commandScript.indexOf(" -FilePath ");
151+
const argumentListIndex = commandScript.indexOf(" -ArgumentList ");
152+
const verbIndex = commandScript.indexOf(" -Verb RunAs ");
153+
const waitIndex = commandScript.indexOf(" -Wait");
154+
const firstTerminator = commandScript.indexOf(";");
155+
156+
expect(startProcessIndex).toBeGreaterThanOrEqual(0);
157+
expect(filePathIndex).toBeGreaterThan(startProcessIndex);
158+
expect(argumentListIndex).toBeGreaterThan(filePathIndex);
159+
expect(verbIndex).toBeGreaterThan(argumentListIndex);
160+
expect(waitIndex).toBeGreaterThan(verbIndex);
161+
expect(firstTerminator).toBeGreaterThan(waitIndex);
162+
expect(commandScript.match(/Start-Process/g)).toHaveLength(1);
163+
expect(commandScript).not.toMatch(/powershell\.exe';\s+-ArgumentList/i);
164+
expect(commandScript).not.toMatch(/-ArgumentList\s+'[^']*';\s+-Verb RunAs/);
165+
});
166+
123167
test("scheduled-task registration embeds immutable XML bytes instead of a file path", async () => {
124168
let commandScript = "";
125169
setWindowsElevationSpawnForTests(((

0 commit comments

Comments
 (0)