Skip to content

Commit b645126

Browse files
committed
fix(codex): encode Windows isolated login commands
1 parent 908872c commit b645126

2 files changed

Lines changed: 25 additions & 7 deletions

File tree

‎apps/web/src/components/settings/CodexSetupSection.logic.test.ts‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,24 @@ describe("isolatedCodexLoginCommand", () => {
1515
);
1616
});
1717

18-
it("uses a child PowerShell so the current terminal keeps its environment", () => {
19-
expect(
20-
isolatedCodexLoginCommand("C:\\Tools & Apps\\codex.exe", "C:\\O'Brien\\home", "windows"),
21-
).toBe(
22-
"powershell -NoProfile -Command '$env:CODEX_HOME=''C:\\O''''Brien\\home''; $env:CODEX_SQLITE_HOME=''C:\\O''''Brien\\home\\sqlite''; & ''C:\\Tools & Apps\\codex.exe'' login --device-auth'",
23-
);
18+
it("encodes a child PowerShell command for both PowerShell and Command Prompt", () => {
19+
for (const [executablePath, homePath, expectedScript] of [
20+
[
21+
"C:\\Tools & Apps\\codex.exe",
22+
"C:\\O'Brien\\home",
23+
"$env:CODEX_HOME='C:\\O''Brien\\home'; $env:CODEX_SQLITE_HOME='C:\\O''Brien\\home\\sqlite'; & 'C:\\Tools & Apps\\codex.exe' login --device-auth",
24+
],
25+
[
26+
"C:\\工具\\🚀\\codex.exe",
27+
"C:\\用户\\Chai $data (100%)!\\home",
28+
"$env:CODEX_HOME='C:\\用户\\Chai $data (100%)!\\home'; $env:CODEX_SQLITE_HOME='C:\\用户\\Chai $data (100%)!\\home\\sqlite'; & 'C:\\工具\\🚀\\codex.exe' login --device-auth",
29+
],
30+
] as const) {
31+
const command = isolatedCodexLoginCommand(executablePath, homePath, "windows");
32+
expect(command).toMatch(/^powershell -NoProfile -EncodedCommand [A-Za-z0-9+/]+=*$/);
33+
const payload = command?.split(" ").at(-1) ?? "";
34+
expect(Buffer.from(payload, "base64").toString("utf16le")).toBe(expectedScript);
35+
}
2436
expect(isolatedCodexLoginCommand("/codex", "/home", "unknown")).toBeNull();
2537
});
2638
});

‎apps/web/src/components/settings/CodexSetupSection.logic.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ export function isolatedCodexLoginCommand(
1414
if (platform === "windows") {
1515
const quote = (value: string) => `'${value.replaceAll("'", "''")}'`;
1616
const script = `$env:CODEX_HOME=${quote(homePath)}; $env:CODEX_SQLITE_HOME=${quote(`${homePath}\\sqlite`)}; & ${quote(executablePath)} login --device-auth`;
17-
return `powershell -NoProfile -Command ${quote(script)}`;
17+
// -EncodedCommand requires UTF-16LE and keeps cmd.exe from interpreting paths.
18+
let utf16le = "";
19+
for (let index = 0; index < script.length; index += 1) {
20+
const codeUnit = script.charCodeAt(index);
21+
utf16le += String.fromCharCode(codeUnit & 0xff, codeUnit >>> 8);
22+
}
23+
return `powershell -NoProfile -EncodedCommand ${btoa(utf16le)}`;
1824
}
1925
const quote = (value: string) => `'${value.replaceAll("'", `'"'"'`)}'`;
2026
return `env CODEX_HOME=${quote(homePath)} CODEX_SQLITE_HOME=${quote(`${homePath}/sqlite`)} ${quote(executablePath)} login --device-auth`;

0 commit comments

Comments
 (0)