import { Arch, CopyFileTransformer, exists, FileTransformer, InvalidConfigurationError, log, walk } from "builder-util" import { createRequire } from "node:module" import { Nullish } from "builder-util-runtime" import { isCI } from "ci-info" import { createHash } from "crypto" import { readdir } from "fs/promises" import { Lazy } from "lazy-val" import * as path from "path" import { readAsarHeader } from "./asar/asar.js" import { forceCodeSigningError, VERIFICATION_FAILED_ERROR_CODE } from "./codeSign/forceCodeSigning.js" import { AuthenticodeSignerInfo, authenticodeSignatureProblem, isAuthenticodeFileType, readAuthenticodeSigner } from "./codeSign/win/authenticodeSignature.js" import { createSignManager } from "./codeSign/win/signManager.js" import { combineSignResults, isSignResultSigned, SignFileResult, SigningResult } from "./codeSign/signResult.js" import { signWindows, WindowsSignOptions } from "./codeSign/win/windowsCodeSign.js" import { FileCodeSigningInfo } from "./codeSign/win/signtoolBaseSignManager.js" import { AfterPackContext } from "./configuration.js" import { DIR_TARGET, Platform, Target } from "./core.js" import { isWindowsSigningDisabled, RequestedExecutionLevel, resolveWindowsSigningConfiguration, shouldSignWindowsFile, WindowsConfiguration } from "./options/winOptions.js" import { Packager } from "./packager.js" import { chooseNotNull, DoPackOptions, PlatformPackager } from "./platformPackager.js" import MsixTarget from "./targets/win/MsixTarget.js" import AppXTarget from "./targets/win/AppxTarget.js" import MsiTarget from "./targets/win/MsiTarget.js" import MsiWrappedTarget from "./targets/win/MsiWrappedTarget.js" import { NsisTarget } from "./targets/win/nsis/NsisTarget.js" import { AppPackageHelper, CopyElevateHelper } from "./targets/win/nsis/nsisUtil.js" import { WebInstallerTarget } from "./targets/win/nsis/WebInstallerTarget.js" import { createCommonTarget } from "./targets/targetFactory.js" import { BuildCacheManager, digest } from "./util/cacheManager.js" import { isBuildCacheEnabled } from "./util/flags.js" import { editWindowsResources, ResourceEditOptions } from "./util/win/resEdit.js" import { time } from "./util/timer.js" import { getWindowsExecVm, getWindowsVm, VmManager } from "./vm/vm.js" const _require = createRequire(import.meta.url) const SIGNING_DISABLED_WITH_FORCE_CODE_SIGNING = "Signing is disabled (`sign: false`) but `forceCodeSigning` is enabled. Remove one of these options." const SIGN_IN_PLACE_HINT = "A custom win.sign.sign hook must sign the file at configuration.path in place." export class WinPackager extends PlatformPackager { _iconPath = new Lazy(() => this.getOrConvertIcon("ico")) readonly vm = new Lazy(() => (process.platform === "win32" ? Promise.resolve(new VmManager()) : getWindowsVm(this.debugLogger, this.config.toolsets?.wine))) // Like `vm`, but for running Windows executables: falls back to wine rather than PwshVmManager (which runs files natively) readonly execVm = new Lazy(() => getWindowsExecVm(this.debugLogger, this.config.toolsets?.wine, this.buildResourcesDir)) readonly signingManager = new Lazy(async () => { const manager = createSignManager(this) await manager.initialize() return manager }) // Sequences the sign requests; per-file failures are logged and swallowed in the queue itself (hence `| void`) // so it keeps flowing to the next file — each caller still observes its own failure via the promise `signIf` returns. private signingQueue: Promise = Promise.resolve() get isForceCodeSigningVerification(): boolean { return this.platformSpecificBuildOptions.verifyUpdateCodeSignature !== false } constructor(info: Packager) { super(info, Platform.WINDOWS) } get defaultTarget(): Array { return ["nsis"] } createTargets(targets: Array, mapper: (name: string, factory: (outDir: string) => Target) => void): void { let copyElevateHelper: CopyElevateHelper | null const getCopyElevateHelper = () => { if (copyElevateHelper == null) { copyElevateHelper = new CopyElevateHelper() } return copyElevateHelper } let helper: AppPackageHelper | null const getHelper = () => { if (helper == null) { helper = new AppPackageHelper(getCopyElevateHelper()) } return helper } for (const name of targets) { if (name === DIR_TARGET) { continue } if (name === "nsis" || name === "portable") { mapper(name, outDir => new NsisTarget(this, outDir, name, getHelper())) } else if (name === "nsis-web") { // package file format differs from nsis target mapper(name, outDir => new WebInstallerTarget(this, path.join(outDir, name), name, new AppPackageHelper(getCopyElevateHelper()))) } else { const targetClass: typeof NsisTarget | typeof AppXTarget | typeof MsixTarget | typeof MsiTarget | typeof MsiWrappedTarget | null = (() => { switch (name) { case "squirrel": try { return _require("electron-builder-squirrel-windows").default } catch (e: any) { throw new InvalidConfigurationError(`Module electron-builder-squirrel-windows must be installed in addition to build Squirrel.Windows: ${e.stack || e}`) } case "appx": return AppXTarget case "msi": return MsiTarget case "msiwrapped": return MsiWrappedTarget case "msix": return MsixTarget default: return null } })() mapper(name, outDir => (targetClass === null ? createCommonTarget(name, outDir, this) : new (targetClass as any)(this, outDir, name))) } } } getIconPath() { return this._iconPath.value } doGetCscPassword(): string | Nullish { const signing = resolveWindowsSigningConfiguration(this.platformSpecificBuildOptions) const certPassword = signing?.type === "signtool" ? signing.certificatePassword : null return chooseNotNull(chooseNotNull(certPassword, process.env.WIN_CSC_KEY_PASSWORD), super.doGetCscPassword()) } async signIf(file: string): Promise { const logFields = { file: log.filePath(file) } if (!this.shouldSignFile(file, true)) { if (this.forceCodeSigning) { // an explicit signExts exclusion is honored under forceCodeSigning, but not silently log.warn({ ...logFields, reason: "excluded by signExts" }, "file is not signed although forceCodeSigning is enabled") } else { log.info(logFields, "file signing skipped via signExts configuration") } return "skipped:filtered" } if (isWindowsSigningDisabled(this.platformSpecificBuildOptions)) { // also reached by targets of a prepackaged app, which never runs signApp's check if (this.forceCodeSigning) { throw new InvalidConfigurationError(SIGNING_DISABLED_WITH_FORCE_CODE_SIGNING) } log.info(logFields, "file signing disabled (`sign: false` or `sign: null`)") return "skipped:disabled" } const promise = this.signingQueue.then(() => this._sign(file)) this.signingQueue = promise.catch(e => { log.warn({ file: log.filePath(file), error: e.message }, "signing failed for file, queue will continue to next file") }) return promise } private async _sign(file: string): Promise { const signOptions: WindowsSignOptions = { path: file, options: this.platformSpecificBuildOptions, } const result = await signWindows(signOptions, this) if (!isSignResultSigned(result) && this.forceCodeSigning) { throw new InvalidConfigurationError( `App is not signed and "forceCodeSigning" is set to true, please ensure that code signing configuration is correct, please see https://electron.build/docs/features/code-signing` ) } if (this.forceCodeSigning) { await this.verifySignedFile(file, "the signed file", SIGN_IN_PLACE_HINT) } return result } /** * Reads the Authenticode signature of a file back under forceCodeSigning, so that a signer which reported success * without signing the file (a custom `win.sign.sign` hook that signs another path, a tool that exits 0) fails the * build. Only Authenticode file types are read. Any status but HashMismatch passes, since the build host may not trust * the signing chain that the users' machines trust. A signature that cannot be read fails on Windows - where it is read * with the API electron-updater verifies updates with - and only warns elsewhere. */ private async verifySignedFile(file: string, subject: string, hint: string): Promise { if (!isAuthenticodeFileType(file)) { log.debug({ file: log.filePath(file) }, "signature not read back: not an Authenticode file type") return } const signer = await this.readAuthenticodeSignature(file) const problem = authenticodeSignatureProblem(signer) if (problem == null) { return } if (signer.type === "unreadable" && process.platform !== "win32") { log.warn({ file: log.filePath(file), reason: signer.reason }, "cannot read the Authenticode signature back on this host, so forceCodeSigning does not verify it") return } throw forceCodeSigningError(`forceCodeSigning is enabled, but ${subject} ${log.filePath(file)} ${problem}. ${hint}`, VERIFICATION_FAILED_ERROR_CODE) } /** @internal reads the Authenticode signature of a file back for {@link verifySignedFile}; tests replace it */ protected readAuthenticodeSignature(file: string): Promise { return readAuthenticodeSigner(file) } protected async doPack(packOptions: DoPackOptions): Promise { if (this.packagerOptions.prepackaged != null && this.forceCodeSigning) { await this.verifyPrepackagedExecutable(packOptions.appOutDir) } await super.doPack(packOptions) } /** * electron-builder does not sign a prepackaged app (doPack returns early for it, so signApp never runs), so under * forceCodeSigning its executable must already carry a signature: the targets would otherwise wrap an unsigned app in * a signed installer. */ private async verifyPrepackagedExecutable(appOutDir: string): Promise { if (isWindowsSigningDisabled(this.platformSpecificBuildOptions)) { throw new InvalidConfigurationError(SIGNING_DISABLED_WITH_FORCE_CODE_SIGNING) } const exe = path.join(appOutDir, `${this.appInfo.productFilename}.exe`) if (!this.shouldSignFile(exe, true)) { log.warn({ file: log.filePath(exe), reason: "excluded by signExts" }, "the prepackaged app's executable is not verified although forceCodeSigning is enabled") return } if (!(await exists(exe))) { throw forceCodeSigningError(`forceCodeSigning is enabled, but the prepackaged app has no ${log.filePath(exe)} whose signature could be verified.`) } await this.verifySignedFile(exe, "the executable of the prepackaged app", "electron-builder does not sign a prepackaged app: sign it before packaging it.") } async signAndEditResources( file: string, arch: Arch, outDir: string, internalName?: string | null, requestedExecutionLevel?: RequestedExecutionLevel | null ): Promise { const appInfo = this.appInfo const files: Array = [] const versionStrings: Record = { FileDescription: appInfo.productName, ProductName: appInfo.productName, LegalCopyright: appInfo.copyright, } if (internalName != null) { versionStrings.InternalName = internalName versionStrings.OriginalFilename = "" } if (appInfo.companyName != null) { versionStrings.CompanyName = appInfo.companyName } if (this.platformSpecificBuildOptions.legalTrademarks != null) { versionStrings.LegalTrademarks = this.platformSpecificBuildOptions.legalTrademarks } const iconPath = await this.getIconPath() if (iconPath != null) { files.push(iconPath) } const opts: ResourceEditOptions = { file, versionStrings, fileVersion: appInfo.shortVersion || appInfo.buildVersion, productVersion: appInfo.shortVersionWindows || appInfo.getVersionInWeirdWindowsForm(), requestedExecutionLevel, iconPath, } const config = this.config const cscInfoForCacheDigest = !isBuildCacheEnabled() || isCI || config.electronDist != null ? null : await (await this.signingManager.value).cscInfo.value let buildCacheManager: BuildCacheManager | null = null // resources editing doesn't change executable for the same input and executed quickly - no need to complicate if (cscInfoForCacheDigest != null) { const cscFile = (cscInfoForCacheDigest as FileCodeSigningInfo).file if (cscFile != null) { files.push(cscFile) } const timer = time("executable cache") const hash = createHash("sha512") hash.update(config.electronVersion || "no electronVersion") hash.update(JSON.stringify(this.platformSpecificBuildOptions)) hash.update(JSON.stringify(opts)) const signingConfig = resolveWindowsSigningConfiguration(this.platformSpecificBuildOptions) const certSha1 = signingConfig?.type === "signtool" || signingConfig?.type === "hsm" ? signingConfig.certificateSha1 : null const subjectName = signingConfig?.type === "signtool" || signingConfig?.type === "hsm" ? signingConfig.certificateSubjectName : null hash.update(certSha1 || "no certificateSha1") hash.update(subjectName || "no subjectName") const asar = path.resolve(this.getResourcesDir(outDir), "app.asar") if (await exists(asar)) { hash.update((await readAsarHeader(asar)).header) } else { hash.update("no asar") } buildCacheManager = new BuildCacheManager(outDir, file, arch) if (await buildCacheManager.copyIfValid(await digest(hash, files))) { timer.end() // the cache is only used when signing is configured (cscInfo != null), so the restored executable was signed when it was cached if (this.forceCodeSigning && this.shouldSignFile(file, true)) { await this.verifySignedFile( file, "the executable restored from the build cache", "Clear the build cache (or set ELECTRON_BUILDER_DISABLE_BUILD_CACHE=true) and build again." ) } return "signed" } timer.end() } const timer = time("resource-edit&sign") await editWindowsResources(opts) const signResult = await this.signIf(file) timer.end() if (buildCacheManager != null) { await buildCacheManager.save() } return signResult } private shouldSignFile(file: string, fallbackValue = false): boolean { return shouldSignWindowsFile(file, this.platformSpecificBuildOptions.signExts, fallbackValue) } protected createTransformerForExtraFiles(packContext: AfterPackContext): FileTransformer | null { if (isWindowsSigningDisabled(this.platformSpecificBuildOptions)) { return null } return file => { if (this.shouldSignFile(file)) { const parentDir = path.dirname(file) if (parentDir !== packContext.appOutDir) { return new CopyFileTransformer(file => this.signIf(file).then(isSignResultSigned)) } } return null } } protected async signApp(packContext: AfterPackContext, isAsar: boolean): Promise { const exeFileName = `${this.appInfo.productFilename}.exe` const signingDisabled = isWindowsSigningDisabled(this.platformSpecificBuildOptions) if (signingDisabled && this.forceCodeSigning) { throw new InvalidConfigurationError(SIGNING_DISABLED_WITH_FORCE_CODE_SIGNING) } const results: Array = [] const files = await readdir(packContext.appOutDir) for (const file of files) { if (file === exeFileName) { results.push( await this.signAndEditResources( path.join(packContext.appOutDir, exeFileName), packContext.arch, packContext.outDir, path.basename(exeFileName, ".exe"), this.platformSpecificBuildOptions.requestedExecutionLevel ) ) } else if (this.shouldSignFile(file)) { results.push(await this.signIf(path.join(packContext.appOutDir, file))) } } // the combined result gates the `afterSign` hook — a signed result wins (see PlatformPackager.doSignAfterPack) if (!isAsar || signingDisabled) { return combineSignResults(results) } const filesToSign = await Promise.all([ this.walkSignableFiles(packContext.appOutDir, "resources", "app.asar.unpacked"), // Note: The `swiftshader` directory is absent in modern electron versions. `swiftshader/` held Chromium's legacy SwiftShader GL fallback (libEGL.dll / libGLESv2.dll), removed in Chromium 102 (Electron 19+) in favor of SwANGLE (ANGLE + SwiftShader Vulkan). This is kept here only for backwards compat with older Electron; `walk` no-ops on a missing dir (readdir ENOENT is swallowed), so this is harmless when the directory is absent. this.walkSignableFiles(packContext.appOutDir, "swiftshader"), ]) for (const file of filesToSign.flat(1)) { results.push(await this.signIf(file)) } return combineSignResults(results) } private walkSignableFiles(baseDir: string, ...subpath: string[]): Promise { return walk(path.join(baseDir, ...subpath), (file, stat) => stat.isDirectory() || this.shouldSignFile(file)) } }