Skip to content

The agent that grows with you

fix(desktop): probe .cmd launchers through an explicit cmd.exe argv, and derive the default home from data-paths

ethernet9976d3cbd0fa
diff --git a/apps/desktop/electron/update-prerequisites.test.ts b/apps/desktop/electron/update-prerequisites.test.ts
index a95daacbca1..c206f7b7760 100644
--- a/apps/desktop/electron/update-prerequisites.test.ts
+++ b/apps/desktop/electron/update-prerequisites.test.ts
@@ -63,3 +63,19 @@ test('earlier PM user-bin launchers are accepted only for the reported source tr
fs.rmSync(base, { recursive: true, force: true })
}
})
+
+// The .cmd rung goes through cmd.exe with a fixed argv; a launcher path that
+// cmd.exe would re-parse (quote, %var%, &, |, redirection) is refused instead of
+// being handed to the shell. Windows-only: POSIX launchers never touch a shell.
+test.skipIf(process.platform !== 'win32')('a .cmd launcher path with cmd metacharacters is refused, not executed', (): void => {
+ const root: string = fs.mkdtempSync(path.join(os.tmpdir(), 'cmd-meta-launcher-'))
+
+ try {
+ const launcher: string = path.join(root, 'a&b', 'hermes.cmd')
+ fs.mkdirSync(path.dirname(launcher))
+ fs.writeFileSync(launcher, `@echo off\r\necho Install directory: ${root}\r\n`)
+ assert.equal(launcherTargetsInstallation(launcher, root), false)
+ } finally {
+ fs.rmSync(root, { recursive: true, force: true })
+ }
+})
diff --git a/apps/desktop/electron/updater-process.ts b/apps/desktop/electron/updater-process.ts
index 432ac26411e..3ce3015b8ee 100644
--- a/apps/desktop/electron/updater-process.ts
+++ b/apps/desktop/electron/updater-process.ts
@@ -20,6 +20,7 @@ export function resolveVenvDir(updateRoot: string): string {
return path.join(updateRoot, 'venv')
}
+import { platformDefaultHermesHome } from './data-paths'
import { hiddenWindowsChildOptions } from './windows-child-options'
/** Exact installation identity; PATH may refer to another checkout. */
@@ -41,9 +42,7 @@ export function resolveInstallationLauncher(
// Earlier PM installers published only to user-bin. Trust that historical
// launcher only after its existing version surface proves exact source identity.
if (stagedFileExists(path.join(updateRoot, 'hermes_cli', '_launchers.py'))) {
- const defaultHome: string = isWindows
- ? path.join(process.env.LOCALAPPDATA ?? path.join(os.homedir(), 'AppData', 'Local'), 'hermes')
- : path.join(os.homedir(), '.hermes')
+ const defaultHome: string = platformDefaultHermesHome(os.homedir(), process.env, isWindows ? 'win32' : 'linux')
const dirs: string[] = isWindows
? [
@@ -76,16 +75,31 @@ export function resolveInstallationLauncher(
return null
}
+// cmd.exe re-parses its command line, so a launcher path carrying any of
+// these would change the command instead of naming a file.
+const CMD_UNSAFE_PATH: RegExp = /["%&|<>^\r\n]/
+
export function launcherTargetsInstallation(launcher: string, root: string): boolean {
try {
- const shell: boolean = process.platform === 'win32' && /\.cmd$/i.test(launcher)
+ // Node refuses to exec a .cmd directly (CVE-2024-27980), and `shell:true`
+ // would hand an interpolated path to cmd.exe wholesale. Invoke cmd.exe
+ // explicitly instead: a fixed argv, the path quoted verbatim and screened
+ // for cmd metacharacters, and nothing else for the shell to interpret.
+ const viaCmd: boolean = process.platform === 'win32' && /\.cmd$/i.test(launcher)
+
+ if (viaCmd && CMD_UNSAFE_PATH.test(launcher)) {
+ return false
+ }
+
+ const command: string = viaCmd ? (process.env.ComSpec ?? 'cmd.exe') : launcher
+ const args: string[] = viaCmd ? ['/d', '/s', '/c', `""${launcher}" --version"`] : ['--version']
- const output: string = execFileSync(shell ? `"${launcher}"` : launcher, ['--version'], {
+ const output: string = execFileSync(command, args, {
cwd: root,
encoding: 'utf8',
timeout: 15000,
windowsHide: true,
- shell,
+ windowsVerbatimArguments: viaCmd,
env: { ...process.env, HERMES_INSTALL_ROOT: root }
})