-
Notifications
You must be signed in to change notification settings - Fork 53
fix(desktop): seal macOS release bundles #97
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| import { execFile as execFileCallback } from "node:child_process"; | ||
| import { mkdtemp, rm } from "node:fs/promises"; | ||
| import os from "node:os"; | ||
| import path from "node:path"; | ||
| import { fileURLToPath } from "node:url"; | ||
| import { promisify } from "node:util"; | ||
|
|
||
| const execFile = promisify(execFileCallback); | ||
|
|
||
| export const EXPECTED_IDENTITY = "Developer ID Application: OpenClaw Foundation (FWJYW4S8P8)"; | ||
| export const EXPECTED_IDENTITY_QUALIFIER = "OpenClaw Foundation (FWJYW4S8P8)"; | ||
| export const NOTARY_RETRY_DELAY_MS = 10 * 60 * 1000; | ||
|
|
||
| function commandFailure(error) { | ||
| return [error?.message, error?.stdout, error?.stderr].filter(Boolean).join("\n"); | ||
| } | ||
|
|
||
| export function isConnectTimeout(error) { | ||
| return /connectTimeout/i.test(commandFailure(error)); | ||
| } | ||
|
|
||
| export async function submitNotarizationWithRetry(submit, delay) { | ||
| try { | ||
| return await submit(); | ||
| } catch (error) { | ||
| if (!isConnectTimeout(error)) { | ||
| throw error; | ||
| } | ||
| console.warn("notarytool hit connectTimeout; waiting 10 minutes before one retry"); | ||
| await delay(NOTARY_RETRY_DELAY_MS); | ||
| return submit(); | ||
| } | ||
| } | ||
|
|
||
| export function assertAcceptedNotarization(rawResult) { | ||
| let result; | ||
| try { | ||
| result = JSON.parse(rawResult); | ||
| } catch { | ||
| throw new Error("notarytool did not return valid JSON"); | ||
| } | ||
| if (result.status !== "Accepted") { | ||
| throw new Error(`notarytool returned ${result.status || "no status"}, expected Accepted`); | ||
| } | ||
| if (!/^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i.test(result.id || "")) { | ||
| throw new Error("notarytool returned an invalid submission id"); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| async function run(command, args, options = {}) { | ||
| return execFile(command, args, { | ||
| encoding: "utf8", | ||
| maxBuffer: 10 * 1024 * 1024, | ||
| ...options, | ||
| }); | ||
| } | ||
|
|
||
| async function verifyApp(script, appPath, requireNotarized = false) { | ||
| const args = requireNotarized ? ["--require-notarized", appPath] : [appPath]; | ||
| await run("bash", [script, ...args]); | ||
| } | ||
|
|
||
| export async function notarizeApp(appPath, options = {}) { | ||
| const profile = options.profile || process.env.NOTARYTOOL_KEYCHAIN_PROFILE; | ||
| if (!profile) { | ||
| throw new Error("official macOS packaging requires NOTARYTOOL_KEYCHAIN_PROFILE"); | ||
| } | ||
|
|
||
| const identity = options.identity || process.env.CSC_NAME; | ||
| if (identity !== EXPECTED_IDENTITY_QUALIFIER) { | ||
| throw new Error(`official macOS packaging requires ${EXPECTED_IDENTITY_QUALIFIER}`); | ||
| } | ||
|
|
||
| const script = path.join(path.dirname(fileURLToPath(import.meta.url)), "verify-macos-app.sh"); | ||
| const workDirectory = await mkdtemp(path.join(os.tmpdir(), "clickclack-notary.")); | ||
| const submission = path.join(workDirectory, "ClickClack.zip"); | ||
| const delay = | ||
| options.delay || | ||
| ((milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds))); | ||
|
|
||
| try { | ||
| await verifyApp(script, appPath); | ||
| await run("ditto", ["-c", "-k", "--sequesterRsrc", "--keepParent", appPath, submission]); | ||
|
|
||
| const submit = async () => { | ||
| const { stdout } = await run("xcrun", [ | ||
| "notarytool", | ||
| "submit", | ||
| submission, | ||
| "--keychain-profile", | ||
| profile, | ||
| "--no-s3-acceleration", | ||
| "--wait", | ||
| "--output-format", | ||
| "json", | ||
| ]); | ||
| return stdout; | ||
| }; | ||
| const result = await submitNotarizationWithRetry(submit, delay); | ||
| assertAcceptedNotarization(result); | ||
|
|
||
| await run("xcrun", ["stapler", "staple", appPath]); | ||
| await verifyApp(script, appPath, true); | ||
| } finally { | ||
| await rm(workDirectory, { recursive: true, force: true }); | ||
| } | ||
| } | ||
|
|
||
| export default async function afterSign(context) { | ||
| if ( | ||
| context.electronPlatformName !== "darwin" || | ||
| process.env.CLICKCLACK_OFFICIAL_MACOS_RELEASE !== "1" | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| const appName = `${context.packager.appInfo.productFilename}.app`; | ||
| await notarizeApp(path.join(context.appOutDir, appName)); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import assert from "node:assert/strict"; | ||
| import { readFile } from "node:fs/promises"; | ||
| import path from "node:path"; | ||
| import test from "node:test"; | ||
| import { fileURLToPath } from "node:url"; | ||
|
|
||
| import { | ||
| EXPECTED_IDENTITY, | ||
| EXPECTED_IDENTITY_QUALIFIER, | ||
| NOTARY_RETRY_DELAY_MS, | ||
| assertAcceptedNotarization, | ||
| submitNotarizationWithRetry, | ||
| } from "./after-sign.mjs"; | ||
|
|
||
| const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); | ||
| const repo = path.resolve(root, "../.."); | ||
| const accepted = JSON.stringify({ | ||
| id: "12345678-1234-1234-1234-123456789abc", | ||
| status: "Accepted", | ||
| }); | ||
|
|
||
| test("notarization retries one connectTimeout after exactly ten minutes", async () => { | ||
| let attempts = 0; | ||
| const delays = []; | ||
| const result = await submitNotarizationWithRetry( | ||
| async () => { | ||
| attempts += 1; | ||
| if (attempts === 1) { | ||
| const error = new Error("notary transport failed"); | ||
| error.stderr = "connectTimeout"; | ||
| throw error; | ||
| } | ||
| return accepted; | ||
| }, | ||
| async (milliseconds) => delays.push(milliseconds), | ||
| ); | ||
| assert.equal(result, accepted); | ||
| assert.equal(attempts, 2); | ||
| assert.deepEqual(delays, [NOTARY_RETRY_DELAY_MS]); | ||
| }); | ||
|
|
||
| test("notarization does not retry other failures", async () => { | ||
| let attempts = 0; | ||
| await assert.rejects( | ||
| submitNotarizationWithRetry( | ||
| async () => { | ||
| attempts += 1; | ||
| throw new Error("invalid credentials"); | ||
| }, | ||
| async () => assert.fail("unexpected delay"), | ||
| ), | ||
| /invalid credentials/, | ||
| ); | ||
| assert.equal(attempts, 1); | ||
| }); | ||
|
|
||
| test("notarization accepts only Apple's accepted response shape", () => { | ||
| assert.equal(assertAcceptedNotarization(accepted).status, "Accepted"); | ||
| assert.throws(() => assertAcceptedNotarization("{}"), /no status/); | ||
| assert.throws( | ||
| () => assertAcceptedNotarization('{"id":"not-a-uuid","status":"Accepted"}'), | ||
| /invalid submission id/, | ||
| ); | ||
| }); | ||
|
|
||
| test("macOS release policy is fail-closed and Foundation-scoped", async () => { | ||
| const [config, packageScript, verifier, workflow] = await Promise.all([ | ||
| readFile(path.join(root, "electron-builder.yml"), "utf8"), | ||
| readFile(path.join(root, "scripts", "package-macos-release.sh"), "utf8"), | ||
| readFile(path.join(root, "scripts", "verify-macos-app.sh"), "utf8"), | ||
| readFile(path.join(repo, ".github", "workflows", "release.yml"), "utf8"), | ||
| ]); | ||
|
|
||
| assert.match(config, /afterSign: scripts\/after-sign\.mjs/); | ||
| assert.match(config, /hardenedRuntime: true/); | ||
| assert.match(config, /strictVerify: true/); | ||
| assert.match(config, /notarize: false/); | ||
| assert.match(verifier, new RegExp(EXPECTED_IDENTITY.replace(/[()]/g, "\\$&"))); | ||
| assert.match(packageScript, new RegExp(EXPECTED_IDENTITY_QUALIFIER.replace(/[()]/g, "\\$&"))); | ||
| assert.match(packageScript, /NOTARYTOOL_KEYCHAIN_PROFILE/); | ||
| assert.match(packageScript, /tag -v/); | ||
| assert.match(verifier, /codesign --verify --strict --deep/); | ||
| assert.match(verifier, /chat\.clickclack\.desktop/); | ||
| assert.match(verifier, /FWJYW4S8P8/); | ||
| assert.match(workflow, /Verify signed macOS release assets/); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| #!/usr/bin/env bash | ||
| set -euo pipefail | ||
|
|
||
| root=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd) | ||
| repo=$(cd "$root/../.." && pwd) | ||
| tag=${1:-} | ||
| identity_qualifier='OpenClaw Foundation (FWJYW4S8P8)' | ||
|
|
||
| if [[ ! "$tag" =~ ^v((0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?)$ ]]; then | ||
| echo "usage: $0 vMAJOR.MINOR.PATCH" >&2 | ||
| exit 2 | ||
| fi | ||
| if [[ "$(uname -s)" != Darwin ]]; then | ||
| echo "official macOS packaging must run on macOS" >&2 | ||
| exit 1 | ||
| fi | ||
| if [[ -z "${NOTARYTOOL_KEYCHAIN_PROFILE:-}" ]]; then | ||
| echo "official macOS packaging requires NOTARYTOOL_KEYCHAIN_PROFILE" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| head_commit=$(git -C "$repo" rev-parse HEAD) | ||
| tag_commit=$(git -C "$repo" rev-parse "refs/tags/$tag^{commit}" 2>/dev/null) || { | ||
| echo "release tag does not exist locally: $tag" >&2 | ||
| exit 1 | ||
| } | ||
| if [[ "$head_commit" != "$tag_commit" ]]; then | ||
| echo "HEAD does not match release tag $tag" >&2 | ||
| exit 1 | ||
| fi | ||
| if [[ -n "$(git -C "$repo" status --porcelain --untracked-files=normal)" ]]; then | ||
| echo "release checkout is not clean" >&2 | ||
| exit 1 | ||
| fi | ||
| git -C "$repo" tag -v "$tag" >/dev/null 2>&1 || { | ||
| echo "release tag is not signed by a trusted git signing key: $tag" >&2 | ||
| exit 1 | ||
| } | ||
|
|
||
| version=${tag#v} | ||
| cd "$root" | ||
| pnpm build | ||
| rm -rf release | ||
| CLICKCLACK_OFFICIAL_MACOS_RELEASE=1 \ | ||
| CSC_IDENTITY_AUTO_DISCOVERY=true \ | ||
| CSC_NAME="$identity_qualifier" \ | ||
| pnpm exec electron-builder \ | ||
| --mac dmg zip --x64 --arm64 \ | ||
| --config.mac.identity="$identity_qualifier" \ | ||
| --config.extraMetadata.version="$version" \ | ||
| --publish never | ||
|
|
||
| "$root/scripts/verify-macos-release.sh" "$tag" "$root/release" | ||
| node "$root/scripts/release-artifacts.mjs" mac "$version" "$root/release" | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the maintainer keyring contains a public key belonging to any contributor or other unapproved signer,
git tag -vsucceeds for that key's cryptographically valid signature even if the key is not trusted for releases; its help only describes-vas “verify tags,” not authorize the signer. An attacker able to publish such a tag could therefore have arbitrary code signed with the Foundation Developer ID certificate. Check the verified signer's fingerprint against an explicit release-key allowlist before packaging.Useful? React with 👍 / 👎.