Skip to content

Commit 675045f

Browse files
fix(browser): read Windows Chrome version from build dir in doctor (#87937)
* fix(browser): read Windows Chrome version metadata Read PE ProductVersion before a conservative unambiguous install-layout fallback, without interpolating configured paths into PowerShell code.\n\nCo-authored-by: Mukunda Rao Katta <[email protected]> * style(browser): clarify Windows version probe * fix(browser): use trusted Windows PowerShell path --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent 67732dd commit 675045f

2 files changed

Lines changed: 124 additions & 1 deletion

File tree

‎extensions/browser/src/browser/chrome.executables.ts‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const CHROME_VERSION_RE = /\b(\d+)(?:\.\d+){1,3}\b/g;
2424
const PLAYWRIGHT_BROWSERS_PATH_ENV = "PLAYWRIGHT_BROWSERS_PATH";
2525
const BROWSER_VERSION_TIMEOUT_MS = 6000;
2626
const MAC_PLISTBUDDY_TIMEOUT_MS = 800;
27+
const WINDOWS_FILE_METADATA_TIMEOUT_MS = 4000;
2728

2829
const CHROMIUM_BUNDLE_IDS = new Set([
2930
"com.google.Chrome",
@@ -743,7 +744,7 @@ export function resolveGoogleChromeExecutableForPlatform(
743744
return null;
744745
}
745746

746-
/** Read a browser executable version string using its command-line flag. */
747+
/** Read a browser executable version from platform metadata or a command-line probe. */
747748
export function readBrowserVersion(executablePath: string): string | null {
748749
if (process.platform === "darwin") {
749750
const bundleVersion = readMacBundleBrowserVersion(executablePath);
@@ -752,6 +753,12 @@ export function readBrowserVersion(executablePath: string): string | null {
752753
}
753754
}
754755

756+
if (process.platform === "win32") {
757+
// Windows GUI browsers do not report `--version` to inherited stdout.
758+
// Read PE metadata first, then use the install layout only as a safe fallback.
759+
return readWindowsBrowserVersion(executablePath);
760+
}
761+
755762
const output = execText(executablePath, ["--version"], BROWSER_VERSION_TIMEOUT_MS);
756763
if (!output) {
757764
return null;
@@ -772,6 +779,50 @@ function readMacBundleBrowserVersion(executablePath: string): string | null {
772779
);
773780
}
774781

782+
const WINDOWS_VERSION_DIR_RE = /^\d+(?:\.\d+){1,3}$/;
783+
784+
function readWindowsBrowserVersion(executablePath: string): string | null {
785+
// Read the inspected executable's authoritative PE metadata. Pass the path as
786+
// data so a configured path cannot become part of the PowerShell program.
787+
const configuredSystemRoot = normalizeOptionalString(process.env.SystemRoot);
788+
const systemRoot =
789+
configuredSystemRoot && path.win32.isAbsolute(configuredSystemRoot)
790+
? configuredSystemRoot
791+
: "C:\\Windows";
792+
const powershellPath = path.win32.join(
793+
systemRoot,
794+
"System32",
795+
"WindowsPowerShell",
796+
"v1.0",
797+
"powershell.exe",
798+
);
799+
const metadataVersion = execText(
800+
powershellPath,
801+
[
802+
"-NoProfile",
803+
"-NonInteractive",
804+
"-Command",
805+
"[System.Diagnostics.FileVersionInfo]::GetVersionInfo($args[0]).ProductVersion",
806+
executablePath,
807+
],
808+
WINDOWS_FILE_METADATA_TIMEOUT_MS,
809+
);
810+
if (metadataVersion) {
811+
return metadataVersion.replace(/\s+/g, " ").trim();
812+
}
813+
814+
// Standard Chromium installers also keep a versioned child directory. Only
815+
// trust that layout when it is unambiguous; updates may leave two builds.
816+
try {
817+
const versionDirs = fs
818+
.readdirSync(path.win32.dirname(executablePath), { withFileTypes: true })
819+
.filter((entry) => entry.isDirectory() && WINDOWS_VERSION_DIR_RE.test(entry.name));
820+
return versionDirs.length === 1 ? (versionDirs[0]?.name ?? null) : null;
821+
} catch {
822+
return null;
823+
}
824+
}
825+
775826
function resolveMacAppBundlePath(executablePath: string): string | null {
776827
const parts = path.normalize(executablePath).split(path.sep);
777828
const appIndex = parts.findIndex((part) => part.endsWith(".app"));

‎extensions/browser/src/browser/chrome.version.test.ts‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
// Browser tests cover chrome.version plugin behavior.
2+
import fs from "node:fs";
3+
import os from "node:os";
4+
import path from "node:path";
25
import { afterEach, describe, expect, it, vi } from "vitest";
36

47
const execFileSyncMock = vi.hoisted(() => vi.fn());
@@ -84,4 +87,73 @@ describe("readBrowserVersion", () => {
8487
expect.objectContaining({ timeout: 6000 }),
8588
);
8689
});
90+
91+
describe("on Windows", () => {
92+
function makeWindowsChromeDir(versionDirs: string[]): string {
93+
const root = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-chrome-win-"));
94+
const appDir = path.join(root, "Application");
95+
fs.mkdirSync(appDir, { recursive: true });
96+
for (const name of versionDirs) {
97+
fs.mkdirSync(path.join(appDir, name));
98+
}
99+
fs.writeFileSync(path.join(appDir, "chrome.exe"), "");
100+
return appDir;
101+
}
102+
103+
it("reads PE product metadata without interpolating the executable path", () => {
104+
stubPlatform("win32");
105+
const exePath = "C:\\Users\\Example\\Browser's Path\\chrome.exe";
106+
execFileSyncMock.mockReturnValue("148.0.7778.179\r\n");
107+
108+
expect(readBrowserVersion(exePath)).toBe("148.0.7778.179");
109+
expect(execFileSyncMock).toHaveBeenCalledTimes(1);
110+
const powershellPath = path.win32.join(
111+
process.env.SystemRoot ?? "C:\\Windows",
112+
"System32",
113+
"WindowsPowerShell",
114+
"v1.0",
115+
"powershell.exe",
116+
);
117+
expect(path.win32.isAbsolute(powershellPath)).toBe(true);
118+
expect(execFileSyncMock).toHaveBeenCalledWith(
119+
powershellPath,
120+
[
121+
"-NoProfile",
122+
"-NonInteractive",
123+
"-Command",
124+
"[System.Diagnostics.FileVersionInfo]::GetVersionInfo($args[0]).ProductVersion",
125+
exePath,
126+
],
127+
expect.objectContaining({ timeout: 4000 }),
128+
);
129+
});
130+
131+
it("falls back to one unambiguous version directory", () => {
132+
stubPlatform("win32");
133+
const appDir = makeWindowsChromeDir(["148.0.7778.179"]);
134+
fs.writeFileSync(path.join(appDir, "149.0.0.0"), "not a directory");
135+
execFileSyncMock.mockImplementation(() => {
136+
throw new Error("PowerShell unavailable");
137+
});
138+
try {
139+
expect(readBrowserVersion(path.join(appDir, "chrome.exe"))).toBe("148.0.7778.179");
140+
} finally {
141+
fs.rmSync(path.dirname(appDir), { recursive: true, force: true });
142+
}
143+
});
144+
145+
it.each([
146+
{ label: "no version directory", versionDirs: [] },
147+
{ label: "multiple version directories", versionDirs: ["147.0.0.0", "148.0.0.0"] },
148+
])("returns null with $label after metadata lookup fails", ({ versionDirs }) => {
149+
stubPlatform("win32");
150+
const appDir = makeWindowsChromeDir(versionDirs);
151+
execFileSyncMock.mockReturnValue("");
152+
try {
153+
expect(readBrowserVersion(path.join(appDir, "chrome.exe"))).toBeNull();
154+
} finally {
155+
fs.rmSync(path.dirname(appDir), { recursive: true, force: true });
156+
}
157+
});
158+
});
87159
});

0 commit comments

Comments
 (0)