From 76298d590c38988e6618f86a0944d38c91a64496 Mon Sep 17 00:00:00 2001 From: Formsmith746 <214276465+Formsmith746@users.noreply.github.com> Date: Wed, 19 Aug 2026 18:52:28 +0300 Subject: [PATCH] fix(release): harden macOS publishing --- .github/SETUP.md | 2 +- .github/actions/build-mac/action.yml | 29 ++++++++++++-- .github/workflows/desktop-release.yml | 55 +++++++++++++++++++++------ package-lock.json | 1 + package.json | 1 + 5 files changed, 72 insertions(+), 16 deletions(-) diff --git a/.github/SETUP.md b/.github/SETUP.md index 15860b6..340cc0f 100644 --- a/.github/SETUP.md +++ b/.github/SETUP.md @@ -49,7 +49,7 @@ The macOS job reads these repository secrets: - `CSC_LINK`: base64-encoded Developer ID Application certificate - `CSC_KEY_PASSWORD`: certificate password -- `APPLE_API_KEY`: App Store Connect API private key path +- `APPLE_API_KEY`: App Store Connect API private key contents or base64-encoded contents - `APPLE_API_KEY_ID`: App Store Connect API key ID - `APPLE_API_ISSUER`: App Store Connect API issuer ID diff --git a/.github/actions/build-mac/action.yml b/.github/actions/build-mac/action.yml index 62058db..280900d 100644 --- a/.github/actions/build-mac/action.yml +++ b/.github/actions/build-mac/action.yml @@ -19,7 +19,7 @@ inputs: description: Developer ID Application certificate password. required: false apple-api-key: - description: App Store Connect API private key path. + description: App Store Connect API private key contents, base64, or path. required: false apple-api-key-id: description: App Store Connect API key ID. @@ -55,13 +55,31 @@ runs: env: CSC_LINK: ${{ inputs.csc-link }} CSC_KEY_PASSWORD: ${{ inputs.csc-key-password }} - APPLE_API_KEY: ${{ inputs.apple-api-key }} + APPLE_API_KEY_INPUT: ${{ inputs.apple-api-key }} APPLE_API_KEY_ID: ${{ inputs.apple-api-key-id }} APPLE_API_ISSUER: ${{ inputs.apple-api-issuer }} CSC_IDENTITY_AUTO_DISCOVERY: ${{ steps.signing.outputs.signing_enabled }} run: | if [[ "${{ steps.signing.outputs.signing_enabled }}" == "false" ]]; then unset CSC_LINK CSC_KEY_PASSWORD APPLE_API_KEY APPLE_API_KEY_ID APPLE_API_ISSUER + else + if [[ -f "$APPLE_API_KEY_INPUT" ]]; then + export APPLE_API_KEY="$APPLE_API_KEY_INPUT" + else + export APPLE_API_KEY="$RUNNER_TEMP/sketchforge-notary-${{ inputs.arch }}.p8" + node <<'NODE' + const fs = require("node:fs"); + const input = process.env.APPLE_API_KEY_INPUT || ""; + const output = process.env.APPLE_API_KEY; + const decoded = input.includes("BEGIN PRIVATE KEY") + ? input + : Buffer.from(input, "base64").toString("utf8"); + if (!decoded.includes("BEGIN PRIVATE KEY")) { + throw new Error("APPLE_API_KEY must be a .p8 path, key contents, or base64-encoded key contents."); + } + fs.writeFileSync(output, decoded, { mode: 0o600 }); + NODE + fi fi npm run desktop:dist -- --publish never --mac --${{ inputs.arch }} \ "-c.mac.artifactName=\${productName}-\${version}-\${arch}${{ steps.signing.outputs.artifact_suffix }}.\${ext}" @@ -74,9 +92,14 @@ runs: suffix="${{ steps.signing.outputs.artifact_suffix }}" test -f "dist/desktop/SketchForge-$VERSION-${{ inputs.arch }}${suffix}.dmg" test -f "dist/desktop/SketchForge-$VERSION-${{ inputs.arch }}${suffix}.zip" - test -f dist/desktop/latest-mac.yml + if [[ "${{ steps.signing.outputs.signing_enabled }}" == "true" ]]; then + test -f dist/desktop/latest-mac.yml + else + rm -f dist/desktop/latest-mac.yml + fi - name: Rename macOS update manifest + if: steps.signing.outputs.signing_enabled == 'true' shell: bash run: mv dist/desktop/latest-mac.yml "dist/desktop/latest-mac-${{ inputs.arch }}.yml" diff --git a/.github/workflows/desktop-release.yml b/.github/workflows/desktop-release.yml index 2df852a..4f9b0ec 100644 --- a/.github/workflows/desktop-release.yml +++ b/.github/workflows/desktop-release.yml @@ -172,7 +172,12 @@ jobs: run: | git fetch --tags --force if git rev-parse "$TAG" >/dev/null 2>&1; then - echo "$TAG already exists." + tag_sha=$(git rev-list -n 1 "$TAG") + source_sha=$(git rev-parse "$SOURCE_SHA^{commit}") + if [[ "$tag_sha" != "$source_sha" ]]; then + echo "$TAG already points to $tag_sha instead of $source_sha." >&2 + exit 1 + fi else git tag "$TAG" "$SOURCE_SHA" git push origin "$TAG" @@ -213,14 +218,24 @@ jobs: "dist/release/macos-x64/latest-mac-x64.yml", "dist/release/macos-arm64/latest-mac-arm64.yml", ]; - const manifests = paths.map((file) => yaml.load(fs.readFileSync(file, "utf8"))); - const merged = { - ...manifests[0], - files: manifests.flatMap((manifest) => manifest.files || []), - }; - delete merged.path; - delete merged.sha512; - fs.writeFileSync("dist/release/latest-mac.yml", yaml.dump(merged, { noRefs: true })); + const existingPaths = paths.filter((file) => fs.existsSync(file)); + if (existingPaths.length === 1) { + throw new Error("Only one macOS architecture produced an updater manifest."); + } + if (existingPaths.length === 2) { + const manifests = existingPaths.map((file) => yaml.load(fs.readFileSync(file, "utf8"))); + const versions = new Set(manifests.map((manifest) => manifest.version)); + if (versions.size !== 1) { + throw new Error("macOS updater manifests have different versions."); + } + const merged = { + ...manifests[0], + files: manifests.flatMap((manifest) => manifest.files || []), + }; + delete merged.path; + delete merged.sha512; + fs.writeFileSync("dist/release/latest-mac.yml", yaml.dump(merged, { noRefs: true })); + } for (const directory of ["macos-x64", "macos-arm64"]) { for (const file of fs.readdirSync(`dist/release/${directory}`)) { if (file === "latest-mac-x64.yml" || file === "latest-mac-arm64.yml") continue; @@ -254,7 +269,15 @@ jobs: env: GH_TOKEN: ${{ github.token }} TAG: ${{ steps.version.outputs.tag }} - run: gh release upload "$TAG" dist/release/* --clobber + run: | + while IFS= read -r asset; do + case "$asset" in + SketchForge-*|latest.yml|latest-linux.yml|latest-mac.yml) + gh release delete-asset "$TAG" "$asset" --yes + ;; + esac + done < <(gh release view "$TAG" --json assets --jq '.assets[].name') + gh release upload "$TAG" dist/release/* - name: Verify release assets env: @@ -283,13 +306,21 @@ jobs: "SketchForge-$VERSION-x64${suffix}.dmg" \ "SketchForge-$VERSION-x64${suffix}.zip" \ "SketchForge-$VERSION-arm64${suffix}.dmg" \ - "SketchForge-$VERSION-arm64${suffix}.zip" \ - "latest-mac.yml"; do + "SketchForge-$VERSION-arm64${suffix}.zip"; do grep -Fxq "$expected" <<< "$names" || { echo "GitHub release is missing $expected" >&2 exit 1 } done + if [[ -z "$suffix" ]]; then + grep -Fxq "latest-mac.yml" <<< "$names" || { + echo "GitHub release is missing latest-mac.yml" >&2 + exit 1 + } + elif grep -Fxq "latest-mac.yml" <<< "$names"; then + echo "Unsigned macOS builds must not publish latest-mac.yml" >&2 + exit 1 + fi - name: Publish release as latest env: diff --git a/package-lock.json b/package-lock.json index e24ac9a..8ab53eb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -34,6 +34,7 @@ "@types/react-dom": "^19.1.5", "electron": "^43.4.0", "electron-builder": "^26.15.3", + "js-yaml": "^4.3.1", "typescript": "^5.8.3", "vitest": "^4.1.8" } diff --git a/package.json b/package.json index 2ce010f..b83017f 100644 --- a/package.json +++ b/package.json @@ -61,6 +61,7 @@ "@types/react-dom": "^19.1.5", "electron": "^43.4.0", "electron-builder": "^26.15.3", + "js-yaml": "^4.3.1", "typescript": "^5.8.3", "vitest": "^4.1.8" }