sign-release.yml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. # Sign a release's Windows binaries after the fact (round 190ij, docs/CODE_SIGNING.md).
  2. #
  3. # The production SignPath policy (release-signing) needs a manual approval in the SignPath web app. release.yml's own
  4. # `sign` job waits for it for up to 5 h 45 min inside the release run; this workflow does the same for a tag that is
  5. # already built, and can be run again whenever the approval comes late:
  6. # 1. prepare downloads LYNX-<label>-windows-x64.zip of the tag's draft or published release and takes LYNX.exe and
  7. # LYNX-bridge32.exe out of it (the unsigned files)
  8. # 2. sign submits them to SignPath (same project / policy / variables as release.yml), waits for the approval, or,
  9. # with signing_request_id, skips the submission and picks up a request that was already completed
  10. # 3. publish (Windows) checks the Authenticode signatures, rebuilds the portable zip and the installer with the signed
  11. # files, self-tests them and ONLY THEN replaces the release's LYNX.exe, zip, setup and SHA256SUMS.txt
  12. # dry_run: true runs step 1 only (no SignPath request, no change to the release).
  13. # Secrets / variables: the same as release.yml (SIGNPATH_API_TOKEN, SIGNPATH_ORGANIZATION_ID, SIGNPATH_PROJECT_SLUG,
  14. # SIGNPATH_POLICY_SLUG). SIGNPATH_ENABLED is not consulted: running this workflow is the explicit switch.
  15. name: Sign release
  16. on:
  17. workflow_dispatch:
  18. inputs:
  19. tag:
  20. description: 'Release tag whose Windows files are signed, e.g. v0.1.30-alpha (draft or published release)'
  21. required: true
  22. type: string
  23. signing_request_id:
  24. description: 'Optional: id of an already completed SignPath signing request to use instead of submitting a new one'
  25. required: false
  26. default: ''
  27. type: string
  28. dry_run:
  29. description: 'Only download and prepare the unsigned files; no SignPath request, no change to the release'
  30. required: false
  31. default: false
  32. type: boolean
  33. permissions:
  34. contents: read
  35. concurrency:
  36. group: sign-release-${{ inputs.tag }}
  37. cancel-in-progress: false
  38. jobs:
  39. prepare:
  40. name: Unsigned files of the release
  41. runs-on: ubuntu-24.04
  42. permissions:
  43. contents: write # reading a draft release needs write access
  44. outputs:
  45. label: ${{ steps.get.outputs.label }}
  46. artifact-id: ${{ steps.up.outputs.artifact-id }}
  47. steps:
  48. - name: Download and unpack
  49. id: get
  50. shell: bash
  51. env:
  52. TAG: ${{ inputs.tag }}
  53. REQID: ${{ inputs.signing_request_id }}
  54. GH_TOKEN: ${{ github.token }}
  55. GH_REPO: ${{ github.repository }}
  56. run: |
  57. [[ "$TAG" =~ ^v[0-9]+\.[0-9]+\.[0-9]+(-[A-Za-z0-9.]+)?$ ]] || { echo "::error::'$TAG' is not a release tag like v0.1.30-alpha"; exit 1; }
  58. [[ -z "$REQID" || "$REQID" =~ ^[0-9a-fA-F-]{36}$ ]] || { echo "::error::signing_request_id must be a GUID"; exit 1; }
  59. label="${TAG#v}"
  60. gh release view "$TAG" --json tagName,isDraft,isPrerelease,assets --jq '"release \(.tagName) draft=\(.isDraft) prerelease=\(.isPrerelease) assets=\(.assets|length)"'
  61. mkdir -p rel unsigned
  62. gh release download "$TAG" --dir rel --pattern "LYNX-$label-windows-x64.zip"
  63. unzip -q "rel/LYNX-$label-windows-x64.zip" -d rel/x
  64. for f in LYNX.exe LYNX-bridge32.exe; do
  65. [[ -f "rel/x/LYNX-$label-windows-x64/$f" ]] || { echo "::error::$f is not in the release zip"; exit 1; }
  66. cp "rel/x/LYNX-$label-windows-x64/$f" unsigned/
  67. done
  68. sha256sum unsigned/* | tee "$RUNNER_TEMP/unsigned.sha"
  69. echo "label=$label" >> "$GITHUB_OUTPUT"
  70. {
  71. echo "### Sign release $TAG"
  72. echo "Unsigned files taken from the release zip:"
  73. echo '```'; cat "$RUNNER_TEMP/unsigned.sha"; echo '```'
  74. echo "dry_run: ${{ inputs.dry_run }}"
  75. } >> "$GITHUB_STEP_SUMMARY"
  76. - name: Upload the unsigned files (SignPath input)
  77. id: up
  78. uses: actions/upload-artifact@v7
  79. with:
  80. name: LYNX-unsigned-${{ steps.get.outputs.label }}
  81. path: |
  82. unsigned/LYNX.exe
  83. unsigned/LYNX-bridge32.exe
  84. if-no-files-found: error
  85. - name: Keep the release zip (rebuilt with the signed files later)
  86. if: ${{ !inputs.dry_run }}
  87. uses: actions/upload-artifact@v7
  88. with:
  89. name: release-zip-${{ steps.get.outputs.label }}
  90. path: rel/LYNX-${{ steps.get.outputs.label }}-windows-x64.zip
  91. if-no-files-found: error
  92. sign:
  93. name: Sign (SignPath)
  94. needs: prepare
  95. if: ${{ !inputs.dry_run }}
  96. runs-on: ubuntu-24.04
  97. timeout-minutes: 355 # GitHub-hosted jobs stop at 6 h; if the approval is later, run this workflow again with signing_request_id
  98. permissions:
  99. actions: read # SignPath reads the job details and downloads the unsigned artifact
  100. contents: read
  101. outputs:
  102. request-id: ${{ steps.id.outputs.request-id }}
  103. env:
  104. SP_ORG: ${{ vars.SIGNPATH_ORGANIZATION_ID }}
  105. SP_TOKEN: ${{ secrets.SIGNPATH_API_TOKEN }}
  106. steps:
  107. - name: Check the SignPath settings
  108. shell: bash
  109. run: |
  110. [[ -n "$SP_ORG" && -n "$SP_TOKEN" ]] || { echo "::error::SIGNPATH_ORGANIZATION_ID / SIGNPATH_API_TOKEN are not set"; exit 1; }
  111. - name: Submit signing request
  112. id: submit
  113. if: ${{ inputs.signing_request_id == '' }}
  114. uses: signpath/github-action-submit-signing-request@v2
  115. with:
  116. api-token: ${{ secrets.SIGNPATH_API_TOKEN }}
  117. organization-id: ${{ vars.SIGNPATH_ORGANIZATION_ID }}
  118. project-slug: ${{ vars.SIGNPATH_PROJECT_SLUG || 'LYNX' }}
  119. signing-policy-slug: ${{ vars.SIGNPATH_POLICY_SLUG || 'release-signing' }}
  120. github-artifact-id: ${{ needs.prepare.outputs.artifact-id }}
  121. wait-for-completion: false # waiting is done below, so the request id is known (and printed) before the wait
  122. - name: Request id
  123. id: id
  124. shell: bash
  125. env:
  126. GIVEN: ${{ inputs.signing_request_id }}
  127. SUBMITTED: ${{ steps.submit.outputs.signing-request-id }}
  128. WEB: ${{ steps.submit.outputs.signing-request-web-url }}
  129. run: |
  130. id="${GIVEN:-$SUBMITTED}"
  131. [[ -n "$id" ]] || { echo "::error::no signing request id"; exit 1; }
  132. echo "request-id=$id" >> "$GITHUB_OUTPUT"
  133. {
  134. echo "### SignPath request"
  135. echo "id: \`$id\`"
  136. [[ -n "$WEB" ]] && echo "Approve it here: $WEB"
  137. echo "If the approval comes after this job stops, run **Sign release** again with signing_request_id = \`$id\`."
  138. } >> "$GITHUB_STEP_SUMMARY"
  139. echo "::notice title=SignPath::request $id ${WEB:+($WEB)} waits for approval in the SignPath web app"
  140. - name: Wait for approval and download the signed files
  141. shell: bash
  142. env:
  143. REQ: ${{ steps.id.outputs.request-id }}
  144. API: ${{ steps.submit.outputs.signpath-api-url || 'https://app.signpath.io/API' }}
  145. run: |
  146. base="$API/v1/$SP_ORG/SigningRequests/$REQ"
  147. deadline=$(( $(date +%s) + 20700 )) # 5 h 45 min
  148. while :; do
  149. code=$(curl -sS -o req.json -w '%{http_code}' -H "Authorization: Bearer $SP_TOKEN" "$base") || code=000
  150. if [[ "$code" == 200 ]]; then
  151. status=$(jq -r '.status' req.json)
  152. echo "$(date -u +%T) $status"
  153. case "$status" in
  154. Completed) break ;;
  155. Failed|Denied|Canceled|Cancelled|Rejected) echo "::error::SignPath request ended as $status"; exit 1 ;;
  156. esac
  157. elif [[ "$code" == 401 || "$code" == 403 || "$code" == 404 ]]; then
  158. echo "::error::SignPath answered HTTP $code for the request (token, organization or id wrong)"; exit 1
  159. else
  160. echo "SignPath answered HTTP $code, retrying"
  161. fi
  162. (( $(date +%s) < deadline )) || { echo "::error::no approval in time; run this workflow again with signing_request_id=$REQ"; exit 1; }
  163. sleep 60
  164. done
  165. link=$(jq -r '.signedArtifactLink' req.json)
  166. [[ "$link" == https://* ]] || { echo "::error::no signed artifact link"; exit 1; }
  167. curl -fsSL -H "Authorization: Bearer $SP_TOKEN" -o signed.zip "$link"
  168. mkdir signed && unzip -q signed.zip -d signed
  169. for f in LYNX.exe LYNX-bridge32.exe; do [[ -s "signed/$f" ]] || { echo "::error::$f missing in the signed artifact"; exit 1; }; done
  170. sha256sum signed/*
  171. - name: Upload the signed files
  172. uses: actions/upload-artifact@v7
  173. with:
  174. name: LYNX-signed-${{ needs.prepare.outputs.label }}
  175. path: |
  176. signed/LYNX.exe
  177. signed/LYNX-bridge32.exe
  178. if-no-files-found: error
  179. publish:
  180. name: Verify and replace the release files
  181. needs: [prepare, sign]
  182. if: ${{ !inputs.dry_run }}
  183. runs-on: windows-latest
  184. permissions:
  185. contents: write # replace the release assets
  186. env:
  187. LABEL: ${{ needs.prepare.outputs.label }}
  188. TAG: ${{ inputs.tag }}
  189. GH_TOKEN: ${{ github.token }}
  190. GH_REPO: ${{ github.repository }}
  191. steps:
  192. - uses: actions/checkout@v5
  193. with:
  194. ref: ${{ inputs.tag }} # the installer script of the release being signed
  195. - uses: actions/download-artifact@v8
  196. with:
  197. name: LYNX-signed-${{ env.LABEL }}
  198. path: signed
  199. - uses: actions/download-artifact@v8
  200. with:
  201. name: release-zip-${{ env.LABEL }}
  202. path: rel
  203. - name: Verify the signatures
  204. shell: pwsh
  205. run: |
  206. foreach ($f in 'LYNX.exe', 'LYNX-bridge32.exe') {
  207. $exe = Get-Item "signed\$f"
  208. $sig = Get-AuthenticodeSignature $exe.FullName
  209. "${f}: $($sig.Status) $($sig.SignerCertificate.Subject)"
  210. # same rule as release.yml: the test-signing policy's self-signed certificate is not trusted by Windows
  211. if ('${{ vars.SIGNPATH_POLICY_SLUG }}' -eq 'test-signing') {
  212. if ($sig.Status -eq 'NotSigned' -or $sig.SignerCertificate.Subject -notmatch 'Test certificate') { throw "$f carries no test signature" }
  213. } elseif ($sig.Status -ne 'Valid') { throw "$f is not validly signed ($($sig.Status))" }
  214. }
  215. $v = (Get-Item signed\LYNX.exe).VersionInfo
  216. if ($v.ProductName -ne 'LYNX SHARE') { throw "signed LYNX.exe lost its version resource" }
  217. - name: Rebuild the zip and the installer with the signed files
  218. shell: pwsh
  219. run: |
  220. $name = "LYNX-$env:LABEL-windows-x64"
  221. $version = ($env:LABEL -split '-')[0]
  222. Expand-Archive "rel\$name.zip" -DestinationPath rel\x
  223. Copy-Item signed\LYNX.exe, signed\LYNX-bridge32.exe "rel\x\$name\" -Force
  224. New-Item -ItemType Directory -Force dist | Out-Null
  225. Compress-Archive -Path "rel\x\$name" -DestinationPath "dist\$name.zip" -Force
  226. Copy-Item signed\LYNX.exe dist\LYNX.exe
  227. # same installer build as release.yml (NSIS is preinstalled on GitHub's Windows images)
  228. $nsis = (Get-Command makensis -ErrorAction SilentlyContinue).Source
  229. if (-not $nsis) { $nsis = "${env:ProgramFiles(x86)}\NSIS\makensis.exe" }
  230. if (-not (Test-Path $nsis)) { choco install nsis -y --no-progress; $nsis = "${env:ProgramFiles(x86)}\NSIS\makensis.exe" }
  231. & $nsis -V2 -INPUTCHARSET UTF8 "-DVERSION=$version" "-DSTAGE=$PWD\rel\x\$name" "-DOUTFILE=$PWD\dist\LYNX-$env:LABEL-setup-x64.exe" `
  232. "-DICON=$PWD\resources\LYNX.ico" resources\installer\LYNX.nsi
  233. if ($LASTEXITCODE -ne 0) { throw "makensis failed ($LASTEXITCODE)" }
  234. - name: Self-test the signed build and the installer
  235. shell: pwsh
  236. run: |
  237. $p = Start-Process signed\LYNX.exe -ArgumentList '--selftest', "$PWD\selftest.txt", "$PWD\selftest-demo.lynx" -Wait -PassThru
  238. Get-Content selftest.txt
  239. if ($p.ExitCode -ne 0 -or -not (Select-String -Path selftest.txt -Pattern '^result=OK' -Quiet)) { throw "self-test failed (exit $($p.ExitCode))" }
  240. $inst = "$env:RUNNER_TEMP\lynx-inst"
  241. Start-Process "dist\LYNX-$env:LABEL-setup-x64.exe" -ArgumentList '/S', "/D=$inst" -Wait
  242. if (-not (Test-Path "$inst\LYNX.exe")) { throw "installer did not install LYNX.exe" }
  243. Start-Process "$inst\Uninstall.exe" -ArgumentList '/S', "_?=$inst" -Wait
  244. if (Test-Path "$inst\LYNX.exe") { throw "uninstaller left LYNX.exe" }
  245. # Only here, after every check above passed, does the release change.
  246. - name: Replace the release files and SHA256SUMS.txt
  247. shell: pwsh
  248. run: |
  249. $names = @("LYNX-$env:LABEL-windows-x64.zip", "LYNX.exe", "LYNX-$env:LABEL-setup-x64.exe")
  250. gh release download $env:TAG --dir old --pattern SHA256SUMS.txt
  251. if ($LASTEXITCODE -ne 0) { throw "release has no SHA256SUMS.txt" }
  252. $lines = Get-Content old\SHA256SUMS.txt | Where-Object { $_.Trim() -ne '' }
  253. $new = foreach ($l in $lines) {
  254. $n = ($l -replace '^\S+\s+\*?', '')
  255. if ($names -contains $n) { "$((Get-FileHash "dist\$n" -Algorithm SHA256).Hash.ToLower()) $n" } else { $l }
  256. }
  257. [IO.File]::WriteAllText("$PWD\dist\SHA256SUMS.txt", (($new -join "`n") + "`n"))
  258. Get-Content dist\SHA256SUMS.txt
  259. $up = $names | ForEach-Object { "dist\$_" }
  260. gh release upload $env:TAG @up dist\SHA256SUMS.txt --clobber
  261. if ($LASTEXITCODE -ne 0) { throw "release upload failed" }
  262. "Signed files uploaded to $env:TAG" | Out-File -Append $env:GITHUB_STEP_SUMMARY