Skip to content

A full-featured download manager.

fix(installer): preserve optional artwork in release builds (#2067)

Dr_rOot7a9ca34bc5bb
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index 071437d2..4b5669ce 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -868,7 +868,7 @@ jobs:
EXPECTED_COMMIT: ${{ github.sha }}
EXPECTED_PLATFORM: ${{ matrix.platform }}
# Updated only after reviewing the verifier source itself.
- VERIFIER_SHA256: eeea9ad40938f308f9cb182c494e02b030fbe60e2e466140e825176a36b38b76
+ VERIFIER_SHA256: 66b9274ea3cb775bc00f02cf82dd18730a09e09005843d270ac5e784f7d5aa9a
run: |
const crypto = require('node:crypto')
const fs = require('node:fs')
diff --git a/build/installer-header.bmp b/build/installerHeader.bmp
similarity index 100%
rename from build/installer-header.bmp
rename to build/installerHeader.bmp
diff --git a/build/installer-sidebar.bmp b/build/installerSidebar.bmp
similarity index 100%
rename from build/installer-sidebar.bmp
rename to build/installerSidebar.bmp
diff --git a/electron-builder.json b/electron-builder.json
index 5be02ec7..9eff1e97 100644
--- a/electron-builder.json
+++ b/electron-builder.json
@@ -168,8 +168,6 @@
},
"nsis": {
"include": "build/installer.nsh",
- "installerHeader": "build/installer-header.bmp",
- "installerSidebar": "build/installer-sidebar.bmp",
"artifactName": "${productName}-Setup-${version}.${ext}",
"oneClick": false,
"allowToChangeInstallationDirectory": true,
diff --git a/scripts/release-signing-input.mjs b/scripts/release-signing-input.mjs
index eecb89bd..b0e3a148 100644
--- a/scripts/release-signing-input.mjs
+++ b/scripts/release-signing-input.mjs
@@ -46,6 +46,10 @@ const TRUSTED_INPUT_SHA256 = Object.freeze({
'f9ff86f32c2110b21d71a49a346f4186c4ef2cf1aed3884eca9704cb77fb4ed7',
'signing-build-resources/icon.ico':
'a71d20d0ca732e27b445e3f09a0cd22cb356620929416e790d005dfff9c4e5a7',
+ 'signing-build-resources/installerHeader.bmp':
+ '1fec8201191e832831f93c5dbaca1a1941b17c72d29ede4ddbba0a721b58d3a5',
+ 'signing-build-resources/installerSidebar.bmp':
+ '510391ad03a2df0a83d2daaeacfc4d2a25b85166f701843d44d85b221df14ec0',
'signing-build-resources/torrent.icns':
'1c8e042a7d4391fa241b4411badbe4a7393a8ad8429679838bf43b25ab29425f',
'signing-build-resources/torrent.ico':
@@ -68,6 +72,13 @@ const TRUSTED_INPUT_SHA256 = Object.freeze({
'ec160ddfa929242155e38d3e0cfdd752ca748a16a84c71eefd39d5b0cbfb9e8a',
})
+// Use electron-builder's default resource names so the restricted config can
+// discover each bitmap when present and retain NSIS defaults when absent.
+const OPTIONAL_NSIS_ARTWORK = Object.freeze({
+ 'signing-build-resources/installerHeader.bmp': 'build/installerHeader.bmp',
+ 'signing-build-resources/installerSidebar.bmp': 'build/installerSidebar.bmp',
+})
+
const SOURCE_MAPPINGS = [
['THIRD_PARTY_LICENSES', 'THIRD_PARTY_LICENSES'],
['THIRD_PARTY_NOTICES.md', 'THIRD_PARTY_NOTICES.md'],
@@ -81,6 +92,10 @@ const SOURCE_MAPPINGS = [
],
['build/icon.icns', 'signing-build-resources/icon.icns'],
['build/icon.ico', 'signing-build-resources/icon.ico'],
+ ...Object.entries(OPTIONAL_NSIS_ARTWORK).map(([destination, source]) => [
+ source,
+ destination,
+ ]),
['build/torrent.icns', 'signing-build-resources/torrent.icns'],
['build/torrent.ico', 'signing-build-resources/torrent.ico'],
['build/installer.nsh', 'signing-policy/installer.nsh'],
@@ -225,6 +240,7 @@ function isAllowedSigningDirectory(relativePath) {
}
function isAllowedSigningBuildResource(relativePath) {
+ if (Object.hasOwn(OPTIONAL_NSIS_ARTWORK, relativePath)) return true
return new Set([
'signing-build-resources/256x256.png',
'signing-build-resources/background.tiff',
@@ -527,7 +543,11 @@ export async function createSigningArchive(directory, archive, options) {
async function copySource(sourcePath, destinationPath, output) {
const source = path.join(REPOSITORY_ROOT, sourcePath)
- const info = await lstat(source).catch(() => null)
+ const info = await lstat(source).catch((error) => {
+ if (error.code === 'ENOENT') return null
+ throw error
+ })
+ if (!info && Object.hasOwn(OPTIONAL_NSIS_ARTWORK, destinationPath)) return
if (!info) throw new Error(`missing signing input source: ${sourcePath}`)
if (info.isSymbolicLink()) {
throw new Error(`signing input source must not be a symlink: ${sourcePath}`)
@@ -540,7 +560,11 @@ async function copySource(sourcePath, destinationPath, output) {
async function verifyTrustedInputDigests(root) {
for (const [relativePath, expected] of Object.entries(TRUSTED_INPUT_SHA256)) {
const absolute = path.join(root, relativePath)
- const info = await lstat(absolute).catch(() => null)
+ const info = await lstat(absolute).catch((error) => {
+ if (error.code === 'ENOENT') return null
+ throw error
+ })
+ if (!info && Object.hasOwn(OPTIONAL_NSIS_ARTWORK, relativePath)) continue
if (
!info?.isFile() ||
info.size > SIGNING_ARCHIVE_LIMITS.fileBytes ||
diff --git a/scripts/render-installer-images.mjs b/scripts/render-installer-images.mjs
index 049d7762..12fa0a38 100644
--- a/scripts/render-installer-images.mjs
+++ b/scripts/render-installer-images.mjs
@@ -1,6 +1,6 @@
// Converts the NSIS assisted-installer design sources (build/installer-*.png)
-// into the 24-bit BMP files electron-builder hands to NSIS through
-// nsis.installerHeader / nsis.installerSidebar.
+// into 24-bit BMP files using electron-builder's default resource names.
+// Both local and isolated release builds discover the bitmaps when present.
//
// pnpm run build:installer-images
//
@@ -32,14 +32,14 @@ export const INSTALLER_IMAGES = [
{
name: 'header',
png: 'build/installer-header.png',
- output: 'build/installer-header.bmp',
+ output: 'build/installerHeader.bmp',
width: 150,
height: 57,
},
{
name: 'sidebar',
png: 'build/installer-sidebar.png',
- output: 'build/installer-sidebar.bmp',
+ output: 'build/installerSidebar.bmp',
width: 164,
height: 314,
},
diff --git a/tests/scripts/release-signing-input.test.ts b/tests/scripts/release-signing-input.test.ts
index 3be9aba4..0c65b55c 100644
--- a/tests/scripts/release-signing-input.test.ts
+++ b/tests/scripts/release-signing-input.test.ts
@@ -12,6 +12,7 @@ import {
truncate,
writeFile,
} from 'node:fs/promises'
+import { createRequire } from 'node:module'
import os from 'node:os'
import path from 'node:path'
import { afterEach, describe, expect, it } from 'vitest'
@@ -23,6 +24,7 @@ import {
} from '../../scripts/release-signing-input.mjs'
const ROOT = process.cwd()
+const require = createRequire(import.meta.url)
const COMMIT = 'a'.repeat(40)
const temporaryDirectories: string[] = []
const HASH_PINNED_TEXT_SOURCES = [
@@ -48,6 +50,11 @@ const TRUSTED_FIXTURES = [
],
['build/icon.icns', 'signing-build-resources/icon.icns'],
['build/icon.ico', 'signing-build-resources/icon.ico'],
+ ['build/installerHeader.bmp', 'signing-build-resources/installerHeader.bmp'],
+ [
+ 'build/installerSidebar.bmp',
+ 'signing-build-resources/installerSidebar.bmp',
+ ],
['build/torrent.icns', 'signing-build-resources/torrent.icns'],
['build/torrent.ico', 'signing-build-resources/torrent.ico'],
['build/installer.nsh', 'signing-policy/installer.nsh'],
@@ -158,6 +165,70 @@ describe('isolated release signing input', () => {
})
})
+ it.each([
+ { name: 'both images', header: true, sidebar: true },
+ { name: 'missing header', header: false, sidebar: true },
+ { name: 'missing sidebar', header: true, sidebar: false },
+ { name: 'no artwork', header: false, sidebar: false },
+ ])('resolves optional NSIS artwork after staging: $name', async (artwork) => {
+ const omittedSources = [
+ ...(!artwork.header ? ['build/installerHeader.bmp'] : []),
+ ...(!artwork.sidebar ? ['build/installerSidebar.bmp'] : []),
+ ]
+ const { directory, sourceRoot } = await createGeneratedSigningInput(
+ [],
+ omittedSources
+ )
+ await verify(directory)
+ for (const [projectDir, configPath] of [
+ [sourceRoot, path.join(ROOT, 'electron-builder.json')],
+ [directory, path.join(directory, 'electron-builder.signing.json')],
+ ]) {
+ const config = JSON.parse(await readFile(configPath!, 'utf8'))
+ const { defines, buildResourcesDir } = await resolveNsisArtwork(
+ projectDir!,
+ config
+ )
+ const header = path.join(buildResourcesDir, 'installerHeader.bmp')
+ const sidebar = path.join(buildResourcesDir, 'installerSidebar.bmp')
+ if (artwork.header) {
+ expect(defines.MUI_HEADERIMAGE).toBeNull()
+ expect(defines.MUI_HEADERIMAGE_BITMAP).toBe(header)
+ expect(await readFile(header)).toEqual(
+ await readFile(path.join(ROOT, 'build/installerHeader.bmp'))
+ )
+ } else {
+ expect(defines).not.toHaveProperty('MUI_HEADERIMAGE_BITMAP')
+ }
+ const expectedSidebar = artwork.sidebar
+ ? sidebar
+ : `\${NSISDIR}\\Contrib\\Graphics\\Wizard\\nsis3-metro.bmp`
+ expect(defines.MUI_WELCOMEFINISHPAGE_BITMAP).toBe(expectedSidebar)
+ expect(defines.MUI_UNWELCOMEFINISHPAGE_BITMAP).toBe(expectedSidebar)
+ if (artwork.sidebar) {
+ expect(await readFile(sidebar)).toEqual(
+ await readFile(path.join(ROOT, 'build/installerSidebar.bmp'))
+ )
+ }
+ }
+ })
+
+ it.each(['installerHeader.bmp', 'installerSidebar.bmp'])(
+ 'rejects replaced branded artwork even with a refreshed manifest: %s',
+ async (filename) => {
+ const directory = await createFixture()
+ const bitmap = path.join(directory, 'signing-build-resources', filename)
+ const bytes = await readFile(bitmap)
+ bytes[bytes.length - 1] ^= 1
+ await writeFile(bitmap, bytes)
+ await refreshManifest(directory)
+
+ await expect(verify(directory)).rejects.toThrow(
+ `trusted signing input digest mismatch: signing-build-resources/${filename}`
+ )
+ }
+ )
+
it('creates and verifies a canonical manifest for prefix-colliding paths', async () => {
const schemaFile = 'dist/electron-app/node_modules/js-yaml/lib/schema.js'
const schemaChild =
@@ -166,7 +237,7 @@ describe('isolated release signing input', () => {
'dist/electron-app/node_modules/libsodium/dist/libsodium.js'
const libsodiumWrappersFile =
'dist/electron-app/node_modules/libsodium-wrappers/dist/libsodium-wrappers.js'
- const directory = await createGeneratedSigningInput([
+ const { directory } = await createGeneratedSigningInput([
[schemaChild, 'schema child'],
[schemaFile, 'schema module'],
[libsodiumFile, 'libsodium'],
@@ -435,12 +506,14 @@ async function createFixture(
}
async function createGeneratedSigningInput(
- files: Array<[string, string]>
-): Promise<string> {
+ files: Array<[string, string]>,
+ omittedSources: string[] = []
+): Promise<{ directory: string; sourceRoot: string }> {
const sourceRoot = await realpath(
await temporaryDirectory('motrix-signing-source-')
)
for (const [source] of TRUSTED_FIXTURES) {
+ if (omittedSources.includes(source)) continue
const target = path.join(sourceRoot, source)
await mkdir(path.dirname(target), { recursive: true })
await cp(path.join(ROOT, source), target)
@@ -451,6 +524,7 @@ async function createGeneratedSigningInput(
['THIRD_PARTY_NOTICES.zh-CN.md', 'notices'],
['build/legal/LICENSE.txt', 'legal'],
['dist/builtin-plugins/plugin.json', '{}'],
+ ['dist/electron-app/package.json', '{"name":"motrix","private":true}'],
['extra/aria2.conf', ''],
['extra/tray/icon.png', 'tray'],
['extra/win32/x64/aria2c.exe', 'aria2'],
@@ -496,7 +570,7 @@ async function createGeneratedSigningInput(
`signing input create failed: ${result.stderr || result.stdout}`
)
}
- return directory
+ return { directory, sourceRoot }
}
async function refreshManifest(directory: string) {
@@ -563,3 +637,38 @@ function compareCodeUnits(left: string, right: string): number {
if (left > right) return 1
return 0
}
+
+async function resolveNsisArtwork(
+ projectDir: string,
+ config: {
+ directories: { buildResources: string }
+ nsis: { oneClick?: boolean }
+ }
+) {
+ const buildResourcesDir = path.resolve(
+ projectDir,
+ config.directories.buildResources
+ )
+ // Initialize the entry point first: the platform modules have circular
+ // dependencies. Exercise the pinned builder's real resource resolution.
+ require('app-builder-lib')
+ const { PlatformPackager } = require('app-builder-lib/out/platformPackager')
+ const { NsisTarget } = require('app-builder-lib/out/targets/nsis/NsisTarget')
+ const defines: Record<string, string | null> = {}
+ await NsisTarget.prototype.configureDefines.call(
+ {
+ options: config.nsis,
+ packager: {
+ info: { buildResourcesDir, cancellationToken: { cancelled: false } },
+ projectDir,
+ resourceList: Promise.resolve(await readdir(buildResourcesDir)),
+ appInfo: { sanitizedProductName: 'Motrix' },
+ expandMacro: () => 'Motrix',
+ getResource: PlatformPackager.prototype.getResource,
+ },
+ },
+ config.nsis.oneClick !== false,
+ defines
+ )
+ return { defines, buildResourcesDir }
+}