Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cached-update-filenames.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"electron-updater": patch
---

fix: reject missing and path-valued cached update file names
7 changes: 4 additions & 3 deletions packages/electron-updater/src/DownloadedUpdateHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down
27 changes: 27 additions & 0 deletions test/src/updater/downloadedUpdateHelperTest.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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")
Expand Down
Loading