From f3c05dfbe43e8650aecbbef3b7b2bc808ba00609 Mon Sep 17 00:00:00 2001 From: zarzet <42882290+zarzet@users.noreply.github.com> Date: Tue, 20 Oct 2026 17:04:56 +0700 Subject: [PATCH] build(macos): sign with a stable self-signed identity when available Ad hoc signatures change on every build, so macOS asks for the keychain password again after each rebuild or update. A self-signed certificate keeps the designated requirement stable; Gatekeeper behavior is unchanged. --- .github/workflows/release.yml | 10 +++ macos/.gitignore | 3 + macos/Runner.xcodeproj/project.pbxproj | 6 +- macos/Runner/Configs/AppInfo.xcconfig | 9 ++ scripts/package_macos_dmg.sh | 22 +++-- scripts/setup_macos_signing.sh | 120 +++++++++++++++++++++++++ 6 files changed, 158 insertions(+), 12 deletions(-) create mode 100644 scripts/setup_macos_signing.sh diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 933b79880..9958ef2fe 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -450,6 +450,16 @@ jobs: - name: Get Flutter dependencies run: flutter pub get + # A stable self-signed identity keeps keychain access across updates. + # Without the secrets the build stays ad hoc signed. + - name: Set up the signing identity + env: + MACOS_SIGNING_P12_BASE64: ${{ secrets.MACOS_SIGNING_P12_BASE64 }} + MACOS_SIGNING_P12_PASSWORD: ${{ secrets.MACOS_SIGNING_P12_PASSWORD }} + run: | + identity="$(bash scripts/setup_macos_signing.sh)" + echo "MORNYE_CODE_SIGN_IDENTITY=$identity" >> "$GITHUB_ENV" + # The Xcode "Embed Rust Backend" and "Embed FFmpeg" phases embed the # library and the FFmpeg tools built above. - name: Build macOS app diff --git a/macos/.gitignore b/macos/.gitignore index 746adbb6b..574d5a75d 100644 --- a/macos/.gitignore +++ b/macos/.gitignore @@ -5,3 +5,6 @@ # Xcode-related **/dgph **/xcuserdata/ + +# Written by scripts/setup_macos_signing.sh +Runner/Configs/LocalSigning.xcconfig diff --git a/macos/Runner.xcodeproj/project.pbxproj b/macos/Runner.xcodeproj/project.pbxproj index b8de51fdd..80f95db02 100644 --- a/macos/Runner.xcodeproj/project.pbxproj +++ b/macos/Runner.xcodeproj/project.pbxproj @@ -526,7 +526,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; CODE_SIGN_ENTITLEMENTS = Runner/DebugProfile.entitlements; - CODE_SIGN_STYLE = Automatic; + CODE_SIGN_STYLE = Manual; COMBINE_HIDPI_IMAGES = YES; INFOPLIST_FILE = Runner/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( @@ -658,7 +658,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; CODE_SIGN_ENTITLEMENTS = Runner/DebugProfile.entitlements; - CODE_SIGN_STYLE = Automatic; + CODE_SIGN_STYLE = Manual; COMBINE_HIDPI_IMAGES = YES; INFOPLIST_FILE = Runner/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( @@ -678,7 +678,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; CODE_SIGN_ENTITLEMENTS = Runner/Release.entitlements; - CODE_SIGN_STYLE = Automatic; + CODE_SIGN_STYLE = Manual; COMBINE_HIDPI_IMAGES = YES; INFOPLIST_FILE = Runner/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( diff --git a/macos/Runner/Configs/AppInfo.xcconfig b/macos/Runner/Configs/AppInfo.xcconfig index 997582f4c..61ce7d81f 100644 --- a/macos/Runner/Configs/AppInfo.xcconfig +++ b/macos/Runner/Configs/AppInfo.xcconfig @@ -12,3 +12,12 @@ PRODUCT_BUNDLE_IDENTIFIER = com.zarz.mornye // The copyright displayed in application information PRODUCT_COPYRIGHT = Copyright © 2026 zarz. All rights reserved. + +// Code signing: ad hoc, unless scripts/setup_macos_signing.sh wrote a stable +// self-signed identity. A stable identity keeps keychain access across +// rebuilds and updates. +MORNYE_CODE_SIGN_IDENTITY = - +#include? "LocalSigning.xcconfig" +CODE_SIGN_IDENTITY = $(MORNYE_CODE_SIGN_IDENTITY) +// Manual signing would otherwise add the debugger entitlement to releases. +CODE_SIGN_INJECT_BASE_ENTITLEMENTS[config=Release] = NO diff --git a/scripts/package_macos_dmg.sh b/scripts/package_macos_dmg.sh index 35912fedd..f383b5afd 100644 --- a/scripts/package_macos_dmg.sh +++ b/scripts/package_macos_dmg.sh @@ -4,13 +4,16 @@ # Usage: bash scripts/package_macos_dmg.sh # # The image holds the app and an Applications symlink (no background or -# window layout). The app is ad hoc signed, inside out, because releases are -# not notarized yet (docs: users open it once through System Settings > +# window layout). The app is re-signed inside out with the stable self-signed +# identity from scripts/setup_macos_signing.sh when one is given (it keeps +# keychain access across updates), otherwise ad hoc. Releases are not +# notarized yet (docs: users open it once through System Settings > # Privacy & Security > "Open Anyway"). The script mounts the result read-only # and checks its contents, the universal binaries and the signature. # # Environment: -# MORNYE_DMG_VOLUME_NAME volume name (default: Mornye) +# MORNYE_DMG_VOLUME_NAME volume name (default: Mornye) +# MORNYE_CODE_SIGN_IDENTITY signing identity (default: - for ad hoc) set -euo pipefail @@ -26,6 +29,7 @@ fi APP_SOURCE="${1%/}" OUTPUT="$2" VOLUME_NAME="${MORNYE_DMG_VOLUME_NAME:-Mornye}" +SIGN_IDENTITY="${MORNYE_CODE_SIGN_IDENTITY:--}" APP_NAME="$(basename "$APP_SOURCE")" if [[ ! -f "$APP_SOURCE/Contents/Info.plist" ]]; then @@ -67,29 +71,29 @@ is_macho() { # Sign nested code first (helper executables such as a bundled ffmpeg, # loose dylibs, then frameworks), and the app itself last with the # entitlements it was built with. -echo "Ad hoc signing $APP_NAME" +echo "Signing $APP_NAME with identity $SIGN_IDENTITY" while IFS= read -r -d '' candidate; do if is_macho "$candidate"; then - codesign --force --sign - --timestamp=none "$candidate" + codesign --force --sign "$SIGN_IDENTITY" --timestamp=none "$candidate" fi done < <(find "$STAGED_APP/Contents/MacOS" "$STAGED_APP/Contents/Resources" \ -type f -perm -u+x ! -path "$STAGED_APP/Contents/MacOS/$EXECUTABLE" -print0) if [[ -d "$STAGED_APP/Contents/Frameworks" ]]; then while IFS= read -r -d '' library; do - codesign --force --sign - --timestamp=none "$library" + codesign --force --sign "$SIGN_IDENTITY" --timestamp=none "$library" done < <(find "$STAGED_APP/Contents/Frameworks" -maxdepth 1 -type f -name '*.dylib' -print0) while IFS= read -r -d '' framework; do - codesign --force --sign - --timestamp=none "$framework" + codesign --force --sign "$SIGN_IDENTITY" --timestamp=none "$framework" done < <(find "$STAGED_APP/Contents/Frameworks" -maxdepth 1 -type d -name '*.framework' -print0) fi ENTITLEMENTS="$WORK_DIR/entitlements.plist" codesign -d --entitlements - --xml "$APP_SOURCE" > "$ENTITLEMENTS" 2>/dev/null || true if [[ -s "$ENTITLEMENTS" ]]; then - codesign --force --sign - --timestamp=none --entitlements "$ENTITLEMENTS" "$STAGED_APP" + codesign --force --sign "$SIGN_IDENTITY" --timestamp=none --entitlements "$ENTITLEMENTS" "$STAGED_APP" else - codesign --force --sign - --timestamp=none "$STAGED_APP" + codesign --force --sign "$SIGN_IDENTITY" --timestamp=none "$STAGED_APP" fi codesign --verify --deep --strict "$STAGED_APP" diff --git a/scripts/setup_macos_signing.sh b/scripts/setup_macos_signing.sh new file mode 100644 index 000000000..615904bde --- /dev/null +++ b/scripts/setup_macos_signing.sh @@ -0,0 +1,120 @@ +#!/usr/bin/env bash +# Gives macOS builds a stable code signing identity without an Apple Developer +# account. Keychain items are bound to the signing identity; ad hoc builds get +# a new identity on every build, so macOS asks for the keychain password again +# after each rebuild or update. A self-signed certificate keeps the identity +# stable. Gatekeeper is unaffected: the app is still not notarized. +# +# Usage: +# bash scripts/setup_macos_signing.sh Use the identity if the login +# keychain has it, else ad hoc. +# bash scripts/setup_macos_signing.sh --create Create the certificate in the +# login keychain (once per Mac) +# and export it for CI. +# CI: set MACOS_SIGNING_P12_BASE64 and MACOS_SIGNING_P12_PASSWORD (the export +# from --create); the script imports it into a temporary keychain. +# +# Writes macos/Runner/Configs/LocalSigning.xcconfig (ignored by git), which +# AppInfo.xcconfig includes, and prints the identity for scripts that sign +# outside Xcode (export it as MORNYE_CODE_SIGN_IDENTITY). + +set -euo pipefail + +CERT_NAME="Mornye Local Signing" +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +PROJECT_DIR="$(dirname "$SCRIPT_DIR")" +XCCONFIG="$PROJECT_DIR/macos/Runner/Configs/LocalSigning.xcconfig" +EXPORT_DIR="${MORNYE_SIGNING_EXPORT_DIR:-$HOME/.mornye-signing}" +LOGIN_KEYCHAIN="$HOME/Library/Keychains/login.keychain-db" + +identity_hash() { + # SHA-1 of the certificate, so codesign never picks a same-named identity. + { security find-certificate -c "$CERT_NAME" -Z "$@" 2>/dev/null || true; } | + awk '/^SHA-1 hash:/ { print $3; exit }' +} + +write_xcconfig() { + local identity="$1" + cat > "$XCCONFIG" </dev/null + # -legacy keeps the PKCS#12 readable by `security import`. + openssl pkcs12 -export -legacy -name "$CERT_NAME" \ + -inkey "$workdir/key.pem" -in "$workdir/cert.pem" \ + -out "$workdir/signing.p12" -passout "pass:$password" + + security import "$workdir/signing.p12" -k "$LOGIN_KEYCHAIN" \ + -P "$password" -T /usr/bin/codesign -T /usr/bin/security >/dev/null + + mkdir -p "$EXPORT_DIR" + chmod 700 "$EXPORT_DIR" + cp "$workdir/signing.p12" "$EXPORT_DIR/signing.p12" + cp "$workdir/cert.pem" "$EXPORT_DIR/certificate.pem" + printf '%s\n' "$password" > "$EXPORT_DIR/p12-password.txt" + chmod 600 "$EXPORT_DIR"/* + echo "Exported the certificate for CI to $EXPORT_DIR (keep it private)." >&2 +} + +import_for_ci() { + local keychain="$RUNNER_TEMP/mornye-signing.keychain-db" + local keychain_password p12 + keychain_password="$(openssl rand -hex 24)" + p12="$RUNNER_TEMP/mornye-signing.p12" + printf '%s' "$MACOS_SIGNING_P12_BASE64" | base64 --decode > "$p12" + + security create-keychain -p "$keychain_password" "$keychain" + security set-keychain-settings -lut 21600 "$keychain" + security unlock-keychain -p "$keychain_password" "$keychain" + security import "$p12" -k "$keychain" -P "$MACOS_SIGNING_P12_PASSWORD" \ + -T /usr/bin/codesign -T /usr/bin/security >/dev/null + security set-key-partition-list -S apple-tool:,apple:,codesign: -s \ + -k "$keychain_password" "$keychain" >/dev/null + # Keep the runner's existing keychains searchable. + local existing=() + while IFS= read -r line; do + line="${line#"${line%%[![:space:]]*}"}" + existing+=("${line//\"/}") + done < <(security list-keychains -d user) + security list-keychains -d user -s "$keychain" "${existing[@]}" + rm -f "$p12" + identity_hash "$keychain" +} + +if [[ -n "${MACOS_SIGNING_P12_BASE64:-}" ]]; then + : "${MACOS_SIGNING_P12_PASSWORD:?MACOS_SIGNING_P12_PASSWORD is required with MACOS_SIGNING_P12_BASE64}" + : "${RUNNER_TEMP:?RUNNER_TEMP is required for CI imports}" + hash="$(import_for_ci)" + [[ -n "$hash" ]] || { echo "error: the CI certificate did not import." >&2; exit 1; } + write_xcconfig "$hash" + exit 0 +fi + +hash="$(identity_hash "$LOGIN_KEYCHAIN")" +if [[ -z "$hash" && "${1:-}" == "--create" ]]; then + create_certificate + hash="$(identity_hash "$LOGIN_KEYCHAIN")" +fi + +if [[ -n "$hash" ]]; then + write_xcconfig "$hash" +else + rm -f "$XCCONFIG" + echo "No \"$CERT_NAME\" certificate; builds stay ad hoc. Run with --create to add one." >&2 + echo "-" +fi