Skip to content

Commit

Permalink
feat: Provide a custom verify function interface in NsisUpdater for n…
Browse files Browse the repository at this point in the history
…ative verification of nsis signatures (#7337)
  • Loading branch information
beyondkmp committed Jan 6, 2023
1 parent 7566c98 commit 9c0c422
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/slow-avocados-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"electron-updater": minor
---

feat: Provide a custom verify function interface to enable nsis signature verification alternatives instead of powershell
43 changes: 43 additions & 0 deletions docs/configuration/win.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,49 @@ exports.default = async function(configuration) {
}
```

#### How do use a custom verify function to enable nsis signature verification alternatives instead of powershell?

Use the `verifyUpdateCodeSignature` interface:

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

Pass a custom verify function to the nsis updater. For example, if you want to use a native verify function, you can use [win-verify-signature](https://github.com/beyondkmp/win-verify-trust).


```js
import { NsisUpdater } from "electron-updater"
import { verifySignatureByPublishName } from "win-verify-signature"
// Or MacUpdater, AppImageUpdater

export default class AppUpdater {
constructor() {
const options = {
requestHeaders: {
// Any request headers to include here
},
provider: 'generic',
url: 'https://example.com/auto-updates'
}

const autoUpdater = new NsisUpdater(options)
autoUpdater.verifyUpdateCodeSignature = (publisherName: string[], path: string) => {
const result = verifySignatureByPublishName(path, publisherName);
if(result.signed) return Promise.resolve(null);
return Promise.resolve(result.message);
}
autoUpdater.addAuthHeader(`Bearer ${token}`)
autoUpdater.checkForUpdatesAndNotify()
}
}
```


#### How do create Parallels Windows 10 Virtual Machine?

!!! warning "Disable "Share Mac user folders with Windows""
Expand Down
21 changes: 19 additions & 2 deletions packages/electron-updater/src/NsisUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { BaseUpdater, InstallOptions } from "./BaseUpdater"
import { DifferentialDownloaderOptions } from "./differentialDownloader/DifferentialDownloader"
import { FileWithEmbeddedBlockMapDifferentialDownloader } from "./differentialDownloader/FileWithEmbeddedBlockMapDifferentialDownloader"
import { GenericDifferentialDownloader } from "./differentialDownloader/GenericDifferentialDownloader"
import { DOWNLOAD_PROGRESS, ResolvedUpdateFileInfo } from "./main"
import { DOWNLOAD_PROGRESS, ResolvedUpdateFileInfo, verifyUpdateCodeSignature } from "./main"
import { blockmapFiles } from "./util"
import { findFile, Provider } from "./providers/Provider"
import { unlink } from "fs-extra"
Expand All @@ -25,6 +25,23 @@ export class NsisUpdater extends BaseUpdater {
super(options, app)
}

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 {
return this._verifyUpdateCodeSignature
}

set verifyUpdateCodeSignature(value: verifyUpdateCodeSignature) {
if (value) {
this._verifyUpdateCodeSignature = value
}
}

/*** @private */
protected doDownloadUpdate(downloadUpdateOptions: DownloadUpdateOptions): Promise<Array<string>> {
const provider = downloadUpdateOptions.updateInfoAndProvider.provider
Expand Down Expand Up @@ -101,7 +118,7 @@ export class NsisUpdater extends BaseUpdater {
}
throw e
}
return await verifySignature(Array.isArray(publisherName) ? publisherName : [publisherName], tempUpdateFile, this._logger)
return await this._verifyUpdateCodeSignature(Array.isArray(publisherName) ? publisherName : [publisherName], tempUpdateFile)
}

protected doInstall(options: InstallOptions): boolean {
Expand Down
5 changes: 5 additions & 0 deletions packages/electron-updater/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,8 @@ export interface Logger {

debug?(message: string): void
}

// return null if verify signature succeed
// return error message if verify signature failed

export type verifyUpdateCodeSignature = (publisherName: string[], path: string) => Promise<string | null>

0 comments on commit 9c0c422

Please sign in to comment.