Skip to content

A full-featured download manager.

fix(macos): sign safari helpers before the main executable (#2273)

Dr_rOotbe7b89b8b429
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 4f4b93e3..ded1cde0 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -459,6 +459,7 @@ jobs:
- name: Test and build Safari desktop helpers
if: matrix.platform == 'darwin'
run: |
+ pnpm exec vitest run tests/scripts/safari-desktop-package.test.ts
swift test --package-path packages/safari-bootstrap
pnpm run build:safari-bootstrap -- --arch ${{ matrix.arch }} --test
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index bec4731c..bf2d3812 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -258,6 +258,7 @@ jobs:
- name: Test and build Safari desktop helpers
if: matrix.platform == 'darwin'
run: |
+ pnpm exec vitest run tests/scripts/safari-desktop-package.test.ts
swift test --package-path packages/safari-bootstrap
pnpm run build:safari-bootstrap -- --arch ${{ matrix.arch }} --test
@@ -897,7 +898,7 @@ jobs:
EXPECTED_COMMIT: ${{ github.sha }}
EXPECTED_PLATFORM: ${{ matrix.platform }}
# Updated only after reviewing the verifier source itself.
- VERIFIER_SHA256: ac73a236208b8c5d0ca156f558d39812b86d00031f69ac886a50b4b88a5b0fa7
+ VERIFIER_SHA256: dc838f0ec84ef65118d60a47a0aae362cdd95588dd1e0cef705836ec1629cbd1
run: |
const crypto = require('node:crypto')
const fs = require('node:fs')
diff --git a/scripts/release-signing-input.mjs b/scripts/release-signing-input.mjs
index f0478677..90d3f72d 100644
--- a/scripts/release-signing-input.mjs
+++ b/scripts/release-signing-input.mjs
@@ -39,7 +39,7 @@ const TRUSTED_INPUT_SHA256 = Object.freeze({
'signing-build-resources/entitlements.safari.plist':
'9d8cee581ad890260639ec94839844cbd12abf3396131604b32820bf28328c4d',
'scripts/sign-macos.mjs':
- 'e5109fd4ef90aaaac74bc91b1b9470f163d61cff32ec241ce5b216318849375d',
+ '98bda5c77dce2a3e5d9c9a2f8211f4d5594d060bf9d2839326db50dc41f8c58b',
'electron-builder.signing.json':
'da0d66babef56e80e725fd62b7b7b659571809d01e0f4383f28f6623a21962aa',
'signing-build-resources/256x256.png':
diff --git a/scripts/sign-macos.mjs b/scripts/sign-macos.mjs
index e2568bcc..04b326aa 100644
--- a/scripts/sign-macos.mjs
+++ b/scripts/sign-macos.mjs
@@ -27,6 +27,34 @@ export function safariSignOptions(app, file, entitlements, fallback) {
}
}
+export function signSafariHelpers(options, entitlements) {
+ if (typeof options.identity !== 'string' || !options.identity.trim()) {
+ throw new Error('Safari helper signing requires the selected identity')
+ }
+ for (const relative of Object.keys(HELPERS)) {
+ const file = path.join(options.app, relative)
+ const helper = safariSignOptions(options.app, file, entitlements)
+ execFileSync(
+ '/usr/bin/codesign',
+ [
+ '--sign',
+ options.identity,
+ '--force',
+ ...(options.keychain ? ['--keychain', options.keychain] : []),
+ '--timestamp',
+ '--options',
+ 'runtime',
+ `-r${helper.requirements}`,
+ ...helper.additionalArguments,
+ '--entitlements',
+ helper.entitlements,
+ file,
+ ],
+ { stdio: 'pipe' }
+ )
+ }
+}
+
// Invoked only by electron-builder after identity selection. Resolve dependencies
// from its isolated, lockfile-pinned runtime, never from the unsigned app payload.
export default async function sign(options, packager) {
@@ -41,6 +69,10 @@ export default async function sign(options, packager) {
process.env.ELECTRON_BUILDER_CLI ? 'signing-build-resources' : 'build',
'entitlements.safari.plist'
)
+ // Signing Contents/MacOS/Motrix also seals its enclosing bundle. osx-sign
+ // visits it before the same-depth registrar, whose x64 binary is unsigned.
+ // Sign both helpers first; the normal pass still signs and verifies everything.
+ signSafariHelpers(options, entitlements)
await signElectron({
...options,
optionsForFile: (file) =>
diff --git a/tests/scripts/safari-desktop-package.test.ts b/tests/scripts/safari-desktop-package.test.ts
index 62341019..cd65ea44 100644
--- a/tests/scripts/safari-desktop-package.test.ts
+++ b/tests/scripts/safari-desktop-package.test.ts
@@ -1,10 +1,97 @@
-import { readFileSync } from 'node:fs'
+import { execFileSync, spawnSync } from 'node:child_process'
+import {
+ mkdirSync,
+ mkdtempSync,
+ readFileSync,
+ rmSync,
+ writeFileSync,
+} from 'node:fs'
+import os from 'node:os'
import path from 'node:path'
import { describe, expect, it } from 'vitest'
// @ts-expect-error -- release signing hook is JavaScript
-import { safariSignOptions } from '../../scripts/sign-macos.mjs'
+import {
+ safariSignOptions,
+ signSafariHelpers,
+} from '../../scripts/sign-macos.mjs'
describe('Safari desktop distribution', () => {
+ it('rejects helper signing without a selected identity', () => {
+ expect(() =>
+ signSafariHelpers({ app: '/missing.app' }, 'safari.plist')
+ ).toThrow('requires the selected identity')
+ })
+
+ it.runIf(process.platform === 'darwin')(
+ 'signs unsigned x64 helpers before the main executable seals the app',
+ () => {
+ const root = mkdtempSync(path.join(os.tmpdir(), 'motrix-sign-order-'))
+ try {
+ const app = path.join(root, 'Motrix.app')
+ const macos = path.join(app, 'Contents/MacOS')
+ const launch = path.join(app, 'Contents/Library/LaunchServices')
+ mkdirSync(macos, { recursive: true })
+ mkdirSync(launch, { recursive: true })
+ writeFileSync(
+ path.join(app, 'Contents/Info.plist'),
+ `<?xml version="1.0" encoding="UTF-8"?>
+<plist version="1.0"><dict>
+<key>CFBundleIdentifier</key><string>app.motrix.signing-test</string>
+<key>CFBundleExecutable</key><string>Motrix</string>
+<key>CFBundleName</key><string>Motrix</string>
+<key>CFBundlePackageType</key><string>APPL</string>
+<key>CFBundleVersion</key><string>1</string>
+</dict></plist>`
+ )
+ const source = path.join(root, 'main.c')
+ writeFileSync(source, 'int main(void) { return 0; }\n')
+ for (const file of [
+ path.join(macos, 'Motrix'),
+ path.join(macos, 'MotrixSafariRegistrar'),
+ path.join(launch, 'MotrixSafariBootstrap'),
+ ]) {
+ execFileSync('/usr/bin/xcrun', [
+ 'clang',
+ '-target',
+ 'x86_64-apple-macos13.0',
+ '-Wl,-no_adhoc_codesign',
+ source,
+ '-o',
+ file,
+ ])
+ }
+ const mainArguments = [
+ '--force',
+ '--sign',
+ '-',
+ '--timestamp=none',
+ path.join(macos, 'Motrix'),
+ ]
+ const unsigned = spawnSync('/usr/bin/codesign', mainArguments, {
+ encoding: 'utf8',
+ })
+ expect(unsigned.status).not.toBe(0)
+ expect(unsigned.stderr).toContain('code object is not signed at all')
+ signSafariHelpers(
+ { app, identity: '-' },
+ path.resolve('build/entitlements.safari.plist')
+ )
+ execFileSync('/usr/bin/codesign', mainArguments)
+ // Ad-hoc fixtures prove the ordering without accessing certificates.
+ // Deep verification of the Apple designated requirements stays in the
+ // protected release path, using the selected Developer ID identity.
+ for (const file of [
+ path.join(macos, 'MotrixSafariRegistrar'),
+ path.join(launch, 'MotrixSafariBootstrap'),
+ ]) {
+ execFileSync('/usr/bin/codesign', ['--verify', '--strict', file])
+ }
+ } finally {
+ rmSync(root, { recursive: true, force: true })
+ }
+ }
+ )
+
it('gives only the exact helper paths their dedicated entitlements and identifiers', () => {
const app = '/Applications/Motrix.app'
const fallback = () => ({