From d7007a68aba0a9bf627337dbd5d312586baa3367 Mon Sep 17 00:00:00 2001 From: Oskar Eichler <62393985+OskarEichler@users.noreply.github.com> Date: Sat, 10 Oct 2026 05:53:57 +0700 Subject: [PATCH] fix(updater): validate cached update filenames (#10115) (cherry picked from commit 4ca053bb8a84735b4d807239c6cbebe59ebe4365) Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01BuQiaJ7WL1k8vji2pHkSHp --- .changeset/cached-update-filenames.md | 5 ++++ .../src/DownloadedUpdateHelper.ts | 7 ++--- .../src/updater/downloadedUpdateHelperTest.ts | 27 +++++++++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 .changeset/cached-update-filenames.md diff --git a/.changeset/cached-update-filenames.md b/.changeset/cached-update-filenames.md new file mode 100644 index 00000000000..cd46ba872d2 --- /dev/null +++ b/.changeset/cached-update-filenames.md @@ -0,0 +1,5 @@ +--- +"electron-updater": patch +--- + +fix: reject missing and path-valued cached update file names diff --git a/packages/electron-updater/src/DownloadedUpdateHelper.ts b/packages/electron-updater/src/DownloadedUpdateHelper.ts index c032d790892..fdcdc731132 100644 --- a/packages/electron-updater/src/DownloadedUpdateHelper.ts +++ b/packages/electron-updater/src/DownloadedUpdateHelper.ts @@ -122,9 +122,10 @@ export class DownloadedUpdateHelper { return null } - const isCachedInfoFileNameValid = cachedInfo?.fileName !== null + const cachedFileName = cachedInfo?.fileName + const isCachedInfoFileNameValid = typeof cachedFileName === "string" && cachedFileName.length > 0 && path.basename(cachedFileName) === cachedFileName if (!isCachedInfoFileNameValid) { - logger.warn(`Cached update info is corrupted: no fileName, directory for cached update will be cleaned`) + logger.warn(`Cached update info is corrupted: invalid fileName, directory for cached update will be cleaned`) await this.cleanCacheDirForPendingUpdate() return null } @@ -137,7 +138,7 @@ export class DownloadedUpdateHelper { return null } - const updateFile = path.join(this.cacheDirForPendingUpdate, cachedInfo.fileName) + const updateFile = path.join(this.cacheDirForPendingUpdate, cachedFileName) if (!(await pathExists(updateFile))) { logger.info("Cached update file doesn't exist") return null diff --git a/test/src/updater/downloadedUpdateHelperTest.ts b/test/src/updater/downloadedUpdateHelperTest.ts index e5e4d24e7a4..9ddb618f752 100644 --- a/test/src/updater/downloadedUpdateHelperTest.ts +++ b/test/src/updater/downloadedUpdateHelperTest.ts @@ -1,6 +1,7 @@ import { createTempUpdateFile, DownloadedUpdateHelper } from "electron-updater/out/DownloadedUpdateHelper" import { outputFile, outputJson, pathExists } from "fs-extra" import { mkdtemp, rm } from "fs/promises" +import { createHash } from "crypto" import { tmpdir } from "os" import * as path from "path" import { afterEach, beforeEach, describe, expect, test } from "vitest" @@ -61,6 +62,32 @@ describe("DownloadedUpdateHelper.getValidCachedUpdateFile", () => { expect(log.infos.some(m => m.includes("No cached update info"))).toBe(true) }) + test("returns null and cleans cache when update-info.json has no file name", async () => { + const pending = path.join(cacheDir, "pending") + const infoFile = path.join(pending, "update-info.json") + await outputJson(infoFile, { sha512: "sha512abc", isAdminRightsRequired: false }) + + await expect((helper as any).getValidCachedUpdateFile(makeFileInfo("sha512abc"), log)).resolves.toBeNull() + expect(await pathExists(infoFile)).toBe(false) + expect(log.warns.some(m => m.includes("invalid fileName"))).toBe(true) + }) + + test("does not read a cached update path outside the pending directory", async () => { + const pending = path.join(cacheDir, "pending") + const outsideFile = path.join(cacheDir, "outside.exe") + const content = Buffer.from("outside content") + const sha512 = createHash("sha512").update(content).digest("base64") + await outputFile(outsideFile, content) + await outputJson(path.join(pending, "update-info.json"), { + fileName: "../outside.exe", + sha512, + isAdminRightsRequired: false, + }) + + await expect((helper as any).getValidCachedUpdateFile(makeFileInfo(sha512), log)).resolves.toBeNull() + expect(await pathExists(outsideFile)).toBe(true) + }) + test("returns null and cleans cache when sha512 in info does not match expected", async () => { const pending = path.join(cacheDir, "pending") const infoFile = path.join(pending, "update-info.json")