Skip to content

Commit 8dcd382

Browse files
authored
Merge pull request #419 from adolago/fix/openclaw-4702-issue-344
[OpenClaw anomalyco#4702] Harden Windows binary detection and command execution
2 parents 05d426c + 30ee377 commit 8dcd382

4 files changed

Lines changed: 58 additions & 13 deletions

File tree

‎packages/zee/Swabble/src/agents/skills/config.ts‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,21 @@ export function isBundledSkillAllowed(entry: SkillEntry, allowlist?: string[]):
7575
export function hasBinary(bin: string): boolean {
7676
const pathEnv = process.env.PATH ?? "";
7777
const parts = pathEnv.split(path.delimiter).filter(Boolean);
78+
const winPathExt = process.env.PATHEXT;
79+
const winExtensions =
80+
winPathExt !== undefined
81+
? winPathExt.split(";").filter(Boolean)
82+
: [".EXE", ".CMD", ".BAT", ".COM"];
83+
const extensions = process.platform === "win32" ? ["", ...winExtensions] : [""];
7884
for (const part of parts) {
79-
const candidate = path.join(part, bin);
80-
try {
81-
fs.accessSync(candidate, fs.constants.X_OK);
82-
return true;
83-
} catch {
84-
// keep scanning
85+
for (const ext of extensions) {
86+
const candidate = path.join(part, bin + ext);
87+
try {
88+
fs.accessSync(candidate, fs.constants.X_OK);
89+
return true;
90+
} catch {
91+
// keep scanning
92+
}
8593
}
8694
}
8795
return false;

‎packages/zee/Swabble/src/hooks/config.ts‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,24 @@ export function resolveRuntimePlatform(): string {
5454
export function hasBinary(bin: string): boolean {
5555
const pathEnv = process.env.PATH ?? "";
5656
const parts = pathEnv.split(path.delimiter).filter(Boolean);
57+
const extensions =
58+
process.platform === "win32"
59+
? [
60+
"",
61+
...(process.env.PATHEXT ?? ".EXE;.CMD;.BAT;.COM")
62+
.split(";")
63+
.filter(Boolean),
64+
]
65+
: [""];
5766
for (const part of parts) {
58-
const candidate = path.join(part, bin);
59-
try {
60-
fs.accessSync(candidate, fs.constants.X_OK);
61-
return true;
62-
} catch {
63-
// keep scanning
67+
for (const ext of extensions) {
68+
const candidate = path.join(part, bin + ext);
69+
try {
70+
fs.accessSync(candidate, fs.constants.X_OK);
71+
return true;
72+
} catch {
73+
// keep scanning
74+
}
6475
}
6576
}
6677
return false;

‎packages/zee/Swabble/src/process/exec.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,24 @@ describe("runCommandWithTimeout", () => {
4242
}
4343
}
4444
});
45+
46+
it("coerces env values to strings and omits undefined values", async () => {
47+
const result = await runCommandWithTimeout(
48+
[
49+
process.execPath,
50+
"-e",
51+
'process.stdout.write((process.env.ZEE_NUM ?? "") + "|" + String(Object.prototype.hasOwnProperty.call(process.env, "ZEE_UNDEF")))',
52+
],
53+
{
54+
timeoutMs: 5_000,
55+
env: {
56+
ZEE_NUM: 42 as unknown as string,
57+
ZEE_UNDEF: undefined,
58+
} as unknown as NodeJS.ProcessEnv,
59+
},
60+
);
61+
62+
expect(result.code).toBe(0);
63+
expect(result.stdout).toBe("42|false");
64+
});
4565
});

‎packages/zee/Swabble/src/process/exec.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,12 @@ export async function runCommandWithTimeout(
7373
return false;
7474
})();
7575

76-
const resolvedEnv = env ? { ...process.env, ...env } : { ...process.env };
76+
const mergedEnv = env ? { ...process.env, ...env } : { ...process.env };
77+
const resolvedEnv = Object.fromEntries(
78+
Object.entries(mergedEnv)
79+
.filter(([, value]) => value !== undefined)
80+
.map(([key, value]) => [key, String(value)]),
81+
);
7782
if (shouldSuppressNpmFund) {
7883
if (resolvedEnv.NPM_CONFIG_FUND == null) resolvedEnv.NPM_CONFIG_FUND = "false";
7984
if (resolvedEnv.npm_config_fund == null) resolvedEnv.npm_config_fund = "false";
@@ -85,6 +90,7 @@ export async function runCommandWithTimeout(
8590
cwd,
8691
env: resolvedEnv,
8792
windowsVerbatimArguments,
93+
shell: process.platform === "win32",
8894
});
8995
// Spawn with inherited stdin (TTY) so tools like `pi` stay interactive when needed.
9096
return await new Promise((resolve, reject) => {

0 commit comments

Comments
 (0)