From 27af65a0e0804cf717fc55de4488af7c78697629 Mon Sep 17 00:00:00 2001 From: Andre Brait Date: Thu, 19 Nov 2026 02:47:58 +0000 Subject: [PATCH] fix(windows): find the host's scheduled task without administrator rights --- CHANGELOG.md | 4 ++++ lib/upgrade-process.ts | 17 ++++++++++++----- lib/upgrade-process.windows-service.test.mjs | 15 +++++++++++++++ 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ab3cefe9..d4908cd3f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ All notable changes to **omp-web** (`@kahme247/ompweb`) are documented in this f ## [Unreleased] +### Fixed + +- **In-app updates work again on a Windows install without administrator rights.** The update looked for the service supervising the host daemon among its parent processes and skipped Task Scheduler's `svchost.exe` by its executable path, which Windows does not show to a non-administrator. It then took the daemon started by the `omp-web-host` scheduled task for the `Schedule` service, could not control that service and refused the update with `not_controllable` (#537). Service hosts are now recognized by process name, so the scheduled task is found. + ## [v0.7.3] - 2026-11-18 ### Fixed diff --git a/lib/upgrade-process.ts b/lib/upgrade-process.ts index 43f5ec82b..a8bc2f2a7 100644 --- a/lib/upgrade-process.ts +++ b/lib/upgrade-process.ts @@ -93,7 +93,7 @@ function assertTrustedFile(path: string, what: string): void { // ---------------------------------------------------------------- processes -interface ProcessInfo { pid: number; ppid: number; state: string; exe: string; args: string[]; cwd: string | null; created: string | null } +interface ProcessInfo { pid: number; ppid: number; state: string; exe: string; args: string[]; cwd: string | null; created: string | null; name?: string } /** CommandLineToArgvW's rules: whitespace separates arguments, quotes group, backslashes escape only before a quote. */ function splitWindowsCommandLine(line: string): string[] { @@ -192,8 +192,8 @@ async function listProcesses(): Promise { return rows; } if (process.platform === "win32") { - const out = await powershell(`$rows = @(Get-CimInstance Win32_Process | ForEach-Object { [pscustomobject]@{ pid = [int]$_.ProcessId; ppid = [int]$_.ParentProcessId; exe = [string]$_.ExecutablePath; cmd = [string]$_.CommandLine; created = $(if ($_.CreationDate) { [string]$_.CreationDate.ToUniversalTime().Ticks } else { '' }) } })\nConvertTo-Json -Compress -InputObject $rows`, 120000); - return parseJson<{ pid: number; ppid: number; exe: string; cmd: string; created: string }[]>(out, "process list").map((row) => ({ pid: row.pid, ppid: row.ppid, state: "R", exe: row.exe, args: splitWindowsCommandLine(row.cmd), cwd: null, created: row.created || null })); + const out = await powershell(`$rows = @(Get-CimInstance Win32_Process | ForEach-Object { [pscustomobject]@{ pid = [int]$_.ProcessId; ppid = [int]$_.ParentProcessId; name = [string]$_.Name; exe = [string]$_.ExecutablePath; cmd = [string]$_.CommandLine; created = $(if ($_.CreationDate) { [string]$_.CreationDate.ToUniversalTime().Ticks } else { '' }) } })\nConvertTo-Json -Compress -InputObject $rows`, 120000); + return parseJson<{ pid: number; ppid: number; name: string; exe: string; cmd: string; created: string }[]>(out, "process list").map((row) => ({ pid: row.pid, ppid: row.ppid, state: "R", name: row.name, exe: row.exe, args: splitWindowsCommandLine(row.cmd), cwd: null, created: row.created || null })); } const env = { ...process.env, LC_ALL: "C" }; const [commands, names] = await Promise.all([ @@ -511,14 +511,21 @@ async function windowsService(name: string): Promise { const out = await powershell(`$s = @(Get-CimInstance Win32_Service | Where-Object { $_.Name -eq ${psString(name)} } | ForEach-Object { ${SERVICE_FIELDS} })\nif ($s.Count -ne 1) { throw 'Service not found' }\nConvertTo-Json -Compress -InputObject $s[0]`); return parseJson(out, "service"); } +/** + * The ancestors whose services may supervise this process. A scheduled task's tree hangs under the Schedule service's + * svchost (shared by many services), which is never one. Matched by name too: a non-administrator reads no + * ExecutablePath for a SYSTEM process. + */ +export function supervisorCandidatePids(chain: Pick[]): number[] { + return chain.filter((row) => !/^svchost\.exe$/i.test(row.name ?? "") && !/(^|[\\/])svchost\.exe$/i.test(row.exe)).map((row) => row.pid); +} async function inspectWindowsSupervisor(me: ProcessInfo, rows: ProcessInfo[], installDir: string): Promise { const byPid = new Map(rows.map((row) => [row.pid, row])); const chain = ancestry(byPid, me); const normal = (value: string) => value.toLowerCase().replaceAll("\\", "/"); const scripts = [...new Set(chain.slice(1).flatMap((row) => row.args.slice(1).filter((arg) => /\.(?:ps1|cmd|bat|vbs)$/i.test(arg))))]; const found = parseJson<{ services: WindowsService[]; tasks: WindowsTask[] }>(await powershell( - // A scheduled task's tree hangs under the Schedule service's svchost (shared by many services): never a candidate supervisor. - `$ids = @(${chain.filter((row) => !/(^|[\\/])svchost\.exe$/i.test(row.exe)).map((row) => row.pid).join(",")})\n$scripts = ${psStrings(scripts.map(normal))}\n` + + `$ids = @(${supervisorCandidatePids(chain).join(",")})\n$scripts = ${psStrings(scripts.map(normal))}\n` + `$services = @(Get-CimInstance Win32_Service | Where-Object { $_.ProcessId -and ($ids -contains [int]$_.ProcessId) } | ForEach-Object { ${SERVICE_FIELDS} })\n` + `$tasks = @()\nif ($scripts.Count -gt 0) { $tasks = @(Get-ScheduledTask | ForEach-Object { $t = $_; $text = ((@($t.Actions | ForEach-Object { $_.Execute + ' ' + $_.Arguments })) -join ' ').ToLowerInvariant().Replace('\\', '/'); $hit = $false; foreach ($s in $scripts) { if ($text.Contains($s)) { $hit = $true } }; if ($hit) { ${TASK_FIELDS} } }) }\n` + `ConvertTo-Json -Compress -Depth 6 -InputObject ([pscustomobject]@{ services = $services; tasks = $tasks })`), "supervisor query"); diff --git a/lib/upgrade-process.windows-service.test.mjs b/lib/upgrade-process.windows-service.test.mjs index 748d081b8..9c781831c 100644 --- a/lib/upgrade-process.windows-service.test.mjs +++ b/lib/upgrade-process.windows-service.test.mjs @@ -259,6 +259,21 @@ test("a helper launched under a service survives the service being stopped, with const INSTALL = "C:/Users/x/AppData/Roaming/npm/node_modules/@kahme247/ompweb"; +test("the Schedule service's svchost is never a supervisor candidate, even with no readable executable path", async () => { + const { supervisorCandidatePids } = await import("./upgrade-process.ts"); + // A non-administrator's Win32_Process rows: SYSTEM processes have a name but an empty ExecutablePath (issue #537). + const chain = [ + { pid: 10, name: "node.exe", exe: "E:/Nodev/node.exe" }, + { pid: 11, name: "cmd.exe", exe: "C:\\Windows\\System32\\cmd.exe" }, + { pid: 12, name: "powershell.exe", exe: "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe" }, + { pid: 3336, name: "svchost.exe", exe: "" }, + { pid: 13, name: "SVCHOST.EXE", exe: "" }, + { pid: 14, exe: "C:\\Windows\\System32\\svchost.exe" }, + { pid: 700, name: "services.exe", exe: "" }, + ]; + assert.deepEqual(supervisorCandidatePids(chain), [10, 11, 12, 700]); +}); + test("a tray exe inside the package is selected without a pid in web-service.json", async () => { const { selectInstallLockingProcesses } = await import("./upgrade-process.ts"); const rows = [