-
-
Notifications
You must be signed in to change notification settings - Fork 29
fix: require signed and notarized mac releases #62
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
Open
felippe-alves
wants to merge
1
commit into
OpenSource03:master
Choose a base branch
from
felippe-alves:fix/macos-notarized-release
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -1,26 +1,111 @@ | ||
| // macOS notarization hook for electron-builder | ||
| // Requires: pnpm add -D @electron/notarize | ||
| // Requires: APPLE_ID, APPLE_APP_SPECIFIC_PASSWORD, APPLE_TEAM_ID env vars | ||
| // macOS notarization hook for electron-builder. | ||
| // Release builds must provide: | ||
| // - Developer ID signing credentials: CSC_LINK + CSC_KEY_PASSWORD (or CSC_NAME from a prepared keychain) | ||
| // - Notarization credentials: either Apple ID credentials or App Store Connect API key credentials | ||
|
|
||
| const fs = require("fs"); | ||
| const os = require("os"); | ||
| const path = require("path"); | ||
|
|
||
| function isReleaseBuild() { | ||
| return process.env.HARNSS_REQUIRE_NOTARIZATION === "true" || process.env.GITHUB_REF?.startsWith("refs/tags/v"); | ||
| } | ||
|
|
||
| function hasSigningCredentials() { | ||
| return Boolean(process.env.CSC_LINK || process.env.CSC_NAME); | ||
| } | ||
|
|
||
| function hasEveryEnv(names) { | ||
| return names.every((name) => Boolean(process.env[name])); | ||
| } | ||
|
|
||
| function hasSomeEnv(names) { | ||
| return names.some((name) => Boolean(process.env[name])); | ||
| } | ||
|
|
||
| function createApiKeyFile(apiKey, apiKeyId) { | ||
| if (fs.existsSync(apiKey)) return { apiKeyPath: apiKey, cleanup: undefined }; | ||
|
|
||
| const keyPath = path.join(os.tmpdir(), `AuthKey_${apiKeyId}.p8`); | ||
| const normalizedApiKey = apiKey.includes("BEGIN PRIVATE KEY") | ||
| ? apiKey.replace(/\\n/g, "\n") | ||
| : Buffer.from(apiKey, "base64").toString("utf8"); | ||
|
|
||
| if (!normalizedApiKey.includes("BEGIN PRIVATE KEY")) { | ||
| throw new Error("APPLE_API_KEY must be a .p8 file path, raw .p8 contents, or base64-encoded .p8 contents"); | ||
| } | ||
|
|
||
| fs.writeFileSync(keyPath, normalizedApiKey, { mode: 0o600 }); | ||
| return { apiKeyPath: keyPath, cleanup: () => fs.rmSync(keyPath, { force: true }) }; | ||
| } | ||
|
|
||
| function resolveNotarizationOptions() { | ||
| const apiKeyEnv = ["APPLE_API_KEY", "APPLE_API_KEY_ID", "APPLE_API_ISSUER"]; | ||
| if (hasEveryEnv(apiKeyEnv)) { | ||
| const { apiKeyPath, cleanup } = createApiKeyFile(process.env.APPLE_API_KEY, process.env.APPLE_API_KEY_ID); | ||
| return { | ||
| options: { | ||
| appleApiKey: apiKeyPath, | ||
| appleApiKeyId: process.env.APPLE_API_KEY_ID, | ||
| appleApiIssuer: process.env.APPLE_API_ISSUER, | ||
| }, | ||
| cleanup, | ||
| }; | ||
| } | ||
|
|
||
| const appleIdEnv = ["APPLE_ID", "APPLE_APP_SPECIFIC_PASSWORD", "APPLE_TEAM_ID"]; | ||
| if (hasEveryEnv(appleIdEnv)) { | ||
| return { | ||
| options: { | ||
| appleId: process.env.APPLE_ID, | ||
| appleIdPassword: process.env.APPLE_APP_SPECIFIC_PASSWORD, | ||
| teamId: process.env.APPLE_TEAM_ID, | ||
| }, | ||
| cleanup: undefined, | ||
| }; | ||
| } | ||
|
|
||
| if (hasSomeEnv(apiKeyEnv)) { | ||
| throw new Error(`Incomplete App Store Connect API key credentials. Required: ${apiKeyEnv.join(", ")}`); | ||
| } | ||
| if (process.env.APPLE_ID || process.env.APPLE_APP_SPECIFIC_PASSWORD) { | ||
| throw new Error(`Incomplete Apple ID notarization credentials. Required: ${appleIdEnv.join(", ")}`); | ||
| } | ||
|
|
||
| return { options: undefined, cleanup: undefined }; | ||
| } | ||
|
|
||
| exports.default = async function notarizing(context) { | ||
| const { electronPlatformName, appOutDir } = context; | ||
| if (electronPlatformName !== "darwin") return; | ||
|
|
||
| if (!process.env.APPLE_ID || !process.env.APPLE_APP_SPECIFIC_PASSWORD) { | ||
| console.log("Skipping notarization: no Apple credentials set"); | ||
| return; | ||
| } | ||
|
|
||
| const { notarize } = require("@electron/notarize"); | ||
| const appName = context.packager.appInfo.productFilename; | ||
| const appPath = path.join(appOutDir, `${appName}.app`); | ||
| const releaseBuild = isReleaseBuild(); | ||
| const { options, cleanup } = resolveNotarizationOptions(); | ||
|
|
||
| try { | ||
| if (!options) { | ||
| if (releaseBuild) { | ||
| throw new Error("Refusing to publish an unnotarized macOS release. Configure Apple notarization credentials."); | ||
| } | ||
| console.log("Skipping notarization: no Apple notarization credentials set"); | ||
| return; | ||
| } | ||
|
|
||
| console.log(`Notarizing ${appName}...`); | ||
| await notarize({ | ||
| appBundleId: "com.harnss.app", | ||
| appPath: `${appOutDir}/${appName}.app`, | ||
| appleId: process.env.APPLE_ID, | ||
| appleIdPassword: process.env.APPLE_APP_SPECIFIC_PASSWORD, | ||
| teamId: process.env.APPLE_TEAM_ID, | ||
| }); | ||
| console.log("Notarization complete"); | ||
| if (releaseBuild && !hasSigningCredentials()) { | ||
| throw new Error("Refusing to publish an unsigned macOS release. Configure Developer ID signing credentials."); | ||
| } | ||
|
|
||
| const { notarize } = require("@electron/notarize"); | ||
|
|
||
| console.log(`Notarizing ${appName}...`); | ||
| await notarize({ | ||
| appPath, | ||
| ...options, | ||
| }); | ||
| console.log("Notarization complete"); | ||
| } finally { | ||
| cleanup?.(); | ||
| } | ||
| }; | ||
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.
Treat
APPLE_TEAM_IDas part of the incomplete Apple ID check.With only
APPLE_TEAM_IDset, this falls through as “no credentials” instead of flagging a partial Apple ID configuration. That makes local misconfigurations easy to miss and downgrades tagged builds to the generic error path.Proposed fix
🤖 Prompt for AI Agents