使用 notarytool 公證 macOS App:有哪些改變,以及如何自動化
Apple 公證服務自 2023 年 11 月 1 日起不再接受 altool 的上傳。本文說明公證會檢查什麼、notarytool 如何儲存憑證、等待結果與取得記錄、為何要 staple,以及如何在 CI 中以指令碼自動化。

公證(notarization)是 Apple 針對 Mac App Store 以外發行的 Mac 軟體所進行的自動化安全檢查。公證服務會掃描以 Developer ID 簽署的 App、磁碟映像檔與安裝程式套件,檢查是否含有惡意內容及程式碼簽署問題;若未發現問題,就會核發一張票證,Gatekeeper 可以在線上查詢,也可以將票證 staple 到檔案上。Apple 強調,這並不是 App 審查。自 macOS 10.15 起,2019 年 6 月 1 日之後建置的 Developer ID 軟體都必須經過公證;而在 macOS Sequoia 中,對於未正確簽署或未經公證的軟體,使用者已無法再按住 Control 鍵點選來略過 Gatekeeper。
有哪些改變:altool 退出公證,notarytool 接手
Apple 在 WWDC 2021 發表 notarytool,並首次隨 Xcode 13 提供。自 2023 年 11 月 1 日起,公證服務不再接受來自 altool 或 Xcode 13 或更早版本的上傳;Apple 的公告引導開發者改用 notarytool 或 Xcode 14 以上版本。宣布此停止日期(當時僅預告為 2023 年秋季)的 WWDC22 session 指出,這項變更涵蓋所有使用 altool 的公證方式,以及從 Xcode 13 介面進行的上傳,而 notarytool(包括 Xcode 13 內附的版本)仍會繼續運作。先前已公證的軟體不受影響。
Apple 的遷移說明 TN3147 列出了實際的差異,並指出 altool 仍適用於其他工作,例如將 App 提交到 App Store;Apple 的 App Store Connect 說明也仍將它列為上傳建置版本的方式。憑證的種類相同:App 專用密碼,或 App Store Connect API 金鑰。--primary-bundle-id 沒有對應的替代選項,因為服務從未使用過它。提交 ID 與歷史記錄與 altool 各自獨立,而且不會寄送完成通知電子郵件,請改用 --wait 或 --webhook。
公證服務的要求
Apple 的公證指南要求:
- 每個可執行檔都有有效的簽章,且使用 Developer ID 憑證簽署,而不是 Mac Distribution、ad hoc 或開發用憑證;
- App 與命令列目標必須啟用 Hardened Runtime;
- 簽章中包含安全時間戳記;
- 不得將
com.apple.security.get-task-allowentitlement 設為 true; - 連結 macOS 10.9 SDK 或更新版本,且 entitlements 的 XML 格式正確。
Xcode 的封存與匯出流程已處理了其中大部分項目。若是手動簽署,Apple 的發行用簽署程式碼指南說明:要先簽署巢狀的程式碼,加上 --timestamp,主要可執行檔要加上 -o runtime,且簽署時避免使用 --deep。Hardened Runtime 會阻擋某些功能(例如 JIT 編譯),除非你加入對應的例外 entitlement。
儲存 notarytool 憑證
在長期運作的建置用 Mac 上,請將憑證一次儲存為具名的設定檔。若使用 Apple ID,notarytool 會提示輸入 App 專用密碼並加以驗證:
xcrun notarytool store-credentials "notary-ci" \
--apple-id "[email protected]" --team-id "TEAMID"
若使用 App Store Connect API 金鑰,請傳入 --key(.p8 檔案)、--key-id 與 --issuer;man page 指出,團隊金鑰需要 issuer ID,個人金鑰則必須省略。之後的指令請加上 --keychain-profile "notary-ci",並可用 --keychain 指定特定的鑰匙圈檔案。TN3147 建議以 xcrun notarytool history 進行測試。在用完即丟的執行環境中,直接傳入金鑰選項會比較簡單。
使用 --wait 提交並讀取記錄
服務不接受單獨的 .app,因此 Apple 的工作流程指南以 ditto 將它壓縮。磁碟映像檔與已簽署的 flat 安裝程式套件則可直接提交。
ditto -c -k --keepParent "MyApp.app" "MyApp.zip"
xcrun notarytool submit "MyApp.zip" --keychain-profile "notary-ci" --wait
xcrun notarytool log <submission-id> --keychain-profile "notary-ci" notary-log.json
--wait 會在處理結束時才傳回,省去輪詢;--timeout 則限制等待時間的上限。JSON 記錄會列出每個問題的嚴重程度、路徑與訊息。即使狀態為 Accepted 也請閱讀記錄,因為 Apple 指出其中可能含有警告。Apple 表示,大多數軟體會在 5 分鐘內完成,98% 在 15 分鐘內完成,並請開發者將每日公證次數限制在 75 次以內。
Staple 票證並檢查 Gatekeeper
Gatekeeper 可以透過網路取得票證;staple 則是將票證附加到檔案上,讓離線時也能完成檢查。
xcrun stapler staple "MyApp.app"
xcrun stapler validate "MyApp.app"
spctl -vvv --assess --type exec "MyApp.app"
App、磁碟映像檔與 flat 安裝程式套件都可以 staple。ZIP 封存檔不行,因此請先 staple App 再重新壓縮;獨立的二進位檔目前同樣無法 staple。對於巢狀的容器,Apple 的封裝指南指出,只需公證最外層的那一個。
常見的退件訊息與解法
| 記錄訊息 | 一般解法 |
|---|---|
| The signature of the binary is invalid. | 簽署所有內容,且簽署後不要再更動 bundle。 |
| The binary is not signed with a valid Developer ID certificate. | 使用 Developer ID Application;套件則使用 Developer ID Installer。 |
| The signature does not include a secure timestamp. | 簽署時加上 --timestamp。 |
| The executable does not have the hardened runtime enabled. | 啟用該功能,或以 -o runtime 簽署。 |
| The executable requests the com.apple.security.get-task-allow entitlement. | 透過 Xcode 封存或匯出,或在發行用建置中將 CODE_SIGN_INJECT_BASE_ENTITLEMENTS 設為 NO。 |
Apple 的疑難排解頁面建議使用 codesign -vvv --deep --strict 測試簽章;安裝程式套件則使用 pkgutil --check-signature。
最精簡的 CI 指令碼範例
這份範例假設你已有一個簽署並匯出的 App,以及取自 CI 機密儲存區的團隊 App Store Connect API 金鑰。與 Apple 的封存指令碼範例一樣,它以 PlistBuddy 讀取 plist 輸出。
set -eu
APP="build/MyApp.app"
codesign -vvv --deep --strict "$APP"
ditto -c -k --keepParent "$APP" upload.zip
xcrun notarytool submit upload.zip \
--key "$ASC_KEY_PATH" --key-id "$ASC_KEY_ID" --issuer "$ASC_ISSUER_ID" \
--wait --timeout 2h --output-format plist > notary.plist || true
STATUS=$(/usr/libexec/PlistBuddy -c "Print :status" notary.plist)
ID=$(/usr/libexec/PlistBuddy -c "Print :id" notary.plist)
xcrun notarytool log "$ID" \
--key "$ASC_KEY_PATH" --key-id "$ASC_KEY_ID" --issuer "$ASC_ISSUER_ID" \
notary-log.json
[ "$STATUS" = "Accepted" ] || exit 1
xcrun stapler staple "$APP"
ditto -c -k --keepParent "$APP" MyApp-notarized.zip
Apple 的範例指令碼只讀取 status 與 message,因此請檢查一次 notary.plist,以確認提交 ID 的鍵名。工作結束時請刪除金鑰檔案。在受限制的網路環境中,Apple 的指南列出了需要允許的位址,包括 S3 上傳端點、stapler 使用的 CloudKit 範圍,以及 timestamp.apple.com。
資料來源
- Apple Developer Documentation: Notarizing macOS software before distribution
- Apple Developer Documentation: Customizing the notarization workflow
- Apple Developer Documentation: Resolving common notarization issues
- Apple Technote TN3147: Migrating to the latest notarization tool
- Apple Developer Documentation: Hardened Runtime
- Apple Developer Documentation: Creating distribution-signed code for macOS
- Apple Developer Documentation: Packaging Mac software for distribution
- Apple Developer Documentation: Customizing the Xcode archive process
- App Store Connect Help: Upload builds
- Apple Developer News: Apple notary service update (May 26, 2023)
- Apple Developer News: Updates to runtime protection in macOS Sequoia (August 6, 2024)
- WWDC22: What's new in notarization for Mac apps
- notarytool(1) man page (community mirror of the Xcode man pages)
- stapler(1) man page (community mirror of the Xcode man pages)


