Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(updater): add custom isUpdateSupported hook for validating update downloads #8692

Merged
merged 6 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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/smart-starfishes-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"electron-updater": minor
---

feat: add support for custom `isUpdateSupported` hook for validating `UpdateInfo`, with fallback to previous `minimumSystemVersion` logic
53 changes: 41 additions & 12 deletions packages/electron-updater/src/AppUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,17 @@ import { createTempUpdateFile, DownloadedUpdateHelper } from "./DownloadedUpdate
import { ElectronAppAdapter } from "./ElectronAppAdapter"
import { ElectronHttpExecutor, getNetSession, LoginCallback } from "./electronHttpExecutor"
import { GenericProvider } from "./providers/GenericProvider"
import { DOWNLOAD_PROGRESS, Logger, Provider, ResolvedUpdateFileInfo, UPDATE_DOWNLOADED, UpdateCheckResult, UpdateDownloadedEvent, UpdaterSignal } from "./main"
import {
DOWNLOAD_PROGRESS,
Logger,
Provider,
ResolvedUpdateFileInfo,
UPDATE_DOWNLOADED,
UpdateCheckResult,
UpdateDownloadedEvent,
UpdaterSignal,
VerifyUpdateSupport,
} from "./main"
import { createClient, isUrlProbablySupportMultiRangeRequests } from "./providerFactory"
import { ProviderPlatform } from "./providers/Provider"
import type { TypedEmitter } from "tiny-typed-emitter"
Expand Down Expand Up @@ -203,6 +213,18 @@ export abstract class AppUpdater extends (EventEmitter as new () => TypedEmitter
this.configOnDisk = new Lazy<any>(() => this.loadUpdateConfig())
}

protected _isUpdateSupported: VerifyUpdateSupport = (updateInfo: UpdateInfo): boolean | Promise<boolean> => this.checkIfUpdateSupported(updateInfo)

get isUpdateSupported(): VerifyUpdateSupport {
return this._isUpdateSupported
}

set isUpdateSupported(value: VerifyUpdateSupport) {
if (value) {
this._isUpdateSupported = value
}
}

private clientPromise: Promise<Provider<any>> | null = null

protected readonly stagingUserIdPromise = new Lazy<string>(() => this.getOrCreateStagingUserId())
Expand Down Expand Up @@ -395,17 +417,8 @@ export abstract class AppUpdater extends (EventEmitter as new () => TypedEmitter
return false
}

const minimumSystemVersion = updateInfo?.minimumSystemVersion
const currentOSVersion = release()
if (minimumSystemVersion) {
try {
if (isVersionLessThan(currentOSVersion, minimumSystemVersion)) {
this._logger.info(`Current OS version ${currentOSVersion} is less than the minimum OS version required ${minimumSystemVersion} for version ${currentOSVersion}`)
return false
}
} catch (e: any) {
this._logger.warn(`Failed to compare current OS version(${currentOSVersion}) with minimum OS version(${minimumSystemVersion}): ${(e.message || e).toString()}`)
}
if (!(await Promise.resolve(this.isUpdateSupported(updateInfo)))) {
return false
}

const isStagingMatch = await this.isStagingMatch(updateInfo)
Expand All @@ -424,6 +437,22 @@ export abstract class AppUpdater extends (EventEmitter as new () => TypedEmitter
return this.allowDowngrade && isLatestVersionOlder
}

private checkIfUpdateSupported(updateInfo: UpdateInfo) {
const minimumSystemVersion = updateInfo?.minimumSystemVersion
const currentOSVersion = release()
if (minimumSystemVersion) {
try {
if (isVersionLessThan(currentOSVersion, minimumSystemVersion)) {
this._logger.info(`Current OS version ${currentOSVersion} is less than the minimum OS version required ${minimumSystemVersion} for version ${currentOSVersion}`)
return false
}
} catch (e: any) {
this._logger.warn(`Failed to compare current OS version(${currentOSVersion}) with minimum OS version(${minimumSystemVersion}): ${(e.message || e).toString()}`)
}
}
return true
}

protected async getUpdateInfoAndProvider(): Promise<UpdateInfoAndProvider> {
await this.app.whenReady()

Expand Down
8 changes: 4 additions & 4 deletions packages/electron-updater/src/NsisUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { DownloadUpdateOptions } from "./AppUpdater"
import { BaseUpdater, InstallOptions } from "./BaseUpdater"
import { DifferentialDownloaderOptions } from "./differentialDownloader/DifferentialDownloader"
import { FileWithEmbeddedBlockMapDifferentialDownloader } from "./differentialDownloader/FileWithEmbeddedBlockMapDifferentialDownloader"
import { DOWNLOAD_PROGRESS, verifyUpdateCodeSignature } from "./main"
import { DOWNLOAD_PROGRESS, VerifyUpdateCodeSignature } from "./main"
import { findFile, Provider } from "./providers/Provider"
import { unlink } from "fs-extra"
import { verifySignature } from "./windowsExecutableCodeSignatureVerifier"
Expand All @@ -22,18 +22,18 @@ export class NsisUpdater extends BaseUpdater {
super(options, app)
}

protected _verifyUpdateCodeSignature: verifyUpdateCodeSignature = (publisherNames: Array<string>, unescapedTempUpdateFile: string) =>
protected _verifyUpdateCodeSignature: VerifyUpdateCodeSignature = (publisherNames: Array<string>, unescapedTempUpdateFile: string) =>
verifySignature(publisherNames, unescapedTempUpdateFile, this._logger)

/**
* The verifyUpdateCodeSignature. You can pass [win-verify-signature](https://github.com/beyondkmp/win-verify-trust) or another custom verify function: ` (publisherName: string[], path: string) => Promise<string | null>`.
* The default verify function uses [windowsExecutableCodeSignatureVerifier](https://github.com/electron-userland/electron-builder/blob/master/packages/electron-updater/src/windowsExecutableCodeSignatureVerifier.ts)
*/
get verifyUpdateCodeSignature(): verifyUpdateCodeSignature {
get verifyUpdateCodeSignature(): VerifyUpdateCodeSignature {
return this._verifyUpdateCodeSignature
}

set verifyUpdateCodeSignature(value: verifyUpdateCodeSignature) {
set verifyUpdateCodeSignature(value: VerifyUpdateCodeSignature) {
if (value) {
this._verifyUpdateCodeSignature = value
}
Expand Down
9 changes: 6 additions & 3 deletions packages/electron-updater/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,10 @@ export interface Logger {
debug?(message: string): void
}

// return null if verify signature succeed
// return error message if verify signature failed
/**
* return null if verify signature succeed
* return error message if verify signature failed
*/
export type VerifyUpdateCodeSignature = (publisherName: string[], path: string) => Promise<string | null>

export type verifyUpdateCodeSignature = (publisherName: string[], path: string) => Promise<string | null>
export type VerifyUpdateSupport = (updateInfo: UpdateInfo) => boolean | Promise<boolean>
Loading