Skip to content

ci: sign every Windows binary on a self-hosted token runner - #47

Merged
lynnlink merged 14 commits into
mainfrom
ci/windows-codesign
Oct 9, 2026
Merged

lynnlink merged 14 commits into
mainfrom
ci/windows-codesign

Conversation

@NiceCode666

@NiceCode666 NiceCode666 commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Release builds of Windows are now Authenticode-signed, every PE image included, not just the installer. Nightlies stay unsigned and build on a hosted runner, so an internal build never needs the signing machine switched on.

The GlobalSign OV code-signing key lives on a SafeNet eToken that cannot leave its USB port, so a signed Windows package job runs on a self-hosted runner ([self-hosted, windows, codesign]) that has the token plugged in. meta sends a job there only for a real Release (tag push or release_tag) or when the new sign input is ticked; every other manual run stays on windows-latest. Signing goes through jsign over PKCS#11. signtool cannot run unattended here: the token's CSP raises a PIN dialog on every signature.

First fully green signed run: 36684020671. It signed 128 + 51 files plus the executables, the audit reported 238 PE files checked, 0 without a valid signature, all 9 installer smoke scenarios passed, and it published nightly-20260930-0729. I downloaded that installer and checked it independently: the signer is 北京比特天空科技有限公司 (cert SHA-1 7E6BB778…36 99, valid until 2027-09-23), it carries a GlobalSign RFC 3161 timestamp, and latest.yml's sha512 and size match the file.

Hosted path verified on 37918170649: the Windows job ran on windows-latest, the three signing steps were skipped, win-sign.cjs left 239 + 19 files unsigned by design, and it published nightly-20261009-1032 in 6 min 39 s without touching the signing machine.

What changed

Signing

  • build/win-sign.cjs (new): signs through the token with jsign. It signs only files that are NotSigned, so third-party publisher signatures (Microsoft, PSF, OpenJS) are kept, and it signs in chunks of 100 so one JVM start and one token login cover each chunk. It is a no-op without WIN_SIGN_PIN, so local and fork builds still work.
  • electron-builder.yml: signtoolOptions.sign hook for every .exe (app, extraResources, uninstaller, installer), sha256 only.
  • after-pack.cjs: batch-signs the DLL / .pyd / .node images that the hook never sees.
  • package.yml: signs dist/amphi right after PyInstaller. This is needed because Smart App Control on the runner blocks a freshly built unsigned exe, so the windowless smoke test could not launch it. A final audit step fails the job if any PE file in the app or the installer lacks a valid signature.

Routing: signed releases, unsigned nightlies

  • package.yml: meta emits windows_signed and windows_runs_on. The Windows job takes runs-on from the latter through fromJSON, and its timeout is 150 min on the signing machine, 45 on a hosted runner. Check the signing machine, Sign the amphi bundle and Audit Authenticode signatures run only when signed, and WIN_SIGN_PIN is injected only then — a hosted runner never holds the token PIN, and withholding it is what makes the build unsigned, since win-sign.cjs already skips without it. No change to the signing scripts.
  • New workflow_dispatch input sign (default off) forces a nightly onto the signing machine, so the signing pipeline can be exercised without cutting a release.
  • README Releasing now opens with a table of what each trigger produces: release shape, platforms, Windows runner, signing and smoke for tag push, release_tag, nightly and nightly with sign.

Token safety. The token's user PIN (repository secret WIN_TOKEN_PIN) allows 5 wrong attempts before the token locks and has to be reset; 5 wrong admin PINs brick it for good. Only the user PIN is ever used, and the job never retries a signature by itself.

  • Halt file: C:\actions-runner\signing-halted.txt on the signing machine (codesign-win, the laptop the runner and the token live on — not in the repo, not in the job workspace, so it survives between runs). win-sign.cjs writes it when a jsign failure may have been a wrong PIN; it holds a timestamp and the last 40 lines of jsign's output.
  • While it exists: every Windows package job fails in seconds at Check the signing machine, which prints the file's content into the job log, before anything is built; win-sign.cjs refuses as well. No job logs in to the token.
  • To recover: (1) read the cause — in the failed job's log, or open the file on codesign-win; (2) fix it (an empty or wrong WIN_TOKEN_PIN, the token unplugged, the timestamp server unreachable, …); (3) delete C:\actions-runner\signing-halted.txt on codesign-win; (4) re-run the workflow. Re-running without deleting is harmless but pointless — it fails at the check. Never delete the file just to retry: if the PIN is wrong, every run spends one of the 5 attempts.
  • No halt when the PIN is proven: if the token accepted the PIN earlier in the same job (marker %RUNNER_TEMP%\win-sign-pin-accepted, per-job temp — nothing to clean up), or any file of the failing chunk came out signed, the failure cannot be a wrong PIN and no halt is written. A successful login also resets the token's wrong-PIN counter, so a retry cannot lock it. The error text is deliberately not parsed: signed-or-not is observable, wording is not.
  • One build spends at most one attempt: the first failure aborts packaging before any other chunk is sent to the token.

Fixes the self-hosted runner surfaced

  • VCRUNTIME140*.dll in the Python runtime arrives truncated exactly where Microsoft's signature began, while its header still points at it, and jsign rejects it. The dangling certificate-table entry is zeroed before signing. Reproduced and verified locally with jsign 7.5.
  • The signature check ran Windows PowerShell 5.1 with pwsh 7's PSModulePath inherited from node, so Get-AuthenticodeSignature failed. That error exited 0, which read as "nothing to sign". Now the variable is dropped and the check fails loudly.
  • Runtime self-check timeout raised from 5s to 60s: a cold first launch of the 60 MB uv.exe waits on antivirus.
  • Cleanup stops only the runner's own leftover processes. The installer smoke refuses to start while ports 7421/9123 are held by another Bridgic Agent, instead of killing it.
  • Windows job timeout raised from 90 to 150 min on the signing machine. The nine installer scenarios run about twice as slow as on a hosted runner.

Unrelated, but blocks every build on main today

  • models.dev renamed kimi-for-coding to kimi-code-plan-cn / kimi-code-plan-global, and packaging validates provider policies against the live catalog. The policy now points at kimi-code-plan-cn (same base URL api.kimi.com/coding/v1, same model IDs), and the checked-in snapshot is regenerated. Installed clients persist the provider id kimi and are unaffected.

Runner prerequisites (not in the repo)

A signed Windows job depends on this machine setup:

  • Runner codesign-win runs as the non-admin service account .\ghrunner, with delayed autostart and restart on failure. The account holds SeCreateSymbolicLinkPrivilege, because setup-bun creates a symlink.
  • C:\codesign\jre (Temurin 21) and C:\codesign\jsign.jar (7.5).
  • C:\actions-runner\.env: proxy 127.0.0.1:7890 for GitHub/PyPI, npmmirror mirrors for npm / Electron / python-build-standalone, and a longer uv HTTP timeout and more retries.
  • Repository secret WIN_TOKEN_PIN, which holds the token's user PIN. The admin PIN is never used.
  • Fork PR workflows now require approval for all external contributors. The repo is public, and a PR can edit a workflow to target this runner.

Known limits

  • If the signing machine is offline, a release's Windows job waits in the queue. It never falls back to an unsigned build. A nightly does not use that machine unless sign is ticked.
  • A full run with smoke takes about 95 min on this laptop. A dropped proxy mid-run fails the job (seen once). Nightly builds without smoke finish in about 35 min on the signing machine; a hosted unsigned nightly took under 7 min.
  • An OV certificate starts with no SmartScreen reputation, so early downloads may still see an "unrecognized app" prompt, now attributed to our publisher. This has not been verified on a clean Win11 machine yet.

Test plan

  • Full Windows package run with smoke on the self-hosted runner (36684020671): audit 238/0, 9/9 scenarios
  • Published installer verified off-machine: signer, chain, timestamp, latest.yml sha512/size
  • Hosted unsigned nightly (37918170649): runs-on resolved to windows-latest, signing steps skipped, 239 + 19 files left unsigned, nightly-20261009-1032 published with exe / blockmap / latest.yml
  • meta decision exercised for tag push, -rc tag, release_tag, nightly with and without sign; actionlint clean
  • Merged with current main locally: pytest 1231 passed; desktop test 2573 passed, typecheck, lint, check:chinese
  • Halt-file logic exercised with stubbed jsign: halts when nothing is signed, no halt after proven login, refuses while halted
  • Install the nightly on a clean Win11 with Smart App Control on
  • Auto-update from 0.1.5 to a signed release (needs a non-prerelease tag)
  • macOS jobs unaffected: platform: all run

🤖 Generated with Claude Code

NiceCode666 and others added 14 commits September 29, 2026 17:10
The code-signing key lives on a SafeNet eToken that cannot leave its USB
port, so the Windows package job moves to the self-hosted runner the token
is plugged into (label `codesign`) and signs through jsign over PKCS#11.

Every PE image is signed, not just the installer: electron-builder's sign
hook covers each .exe (app, extraResources, uninstaller, installer) and
afterPack batch-signs the DLLs and .pyd/.node modules. Files already carrying
a valid third-party signature keep it. An audit step fails the job if any
PE file in the app or the installer is not validly signed.

The token locks after 5 wrong PINs, so a signing failure is never retried:
it writes a halt file on the runner, and no later build logs in to the
token until a human clears it.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
models.dev split `kimi-for-coding` into `kimi-code-plan-cn` (kimi.com) and
`kimi-code-plan-global` (kimi.ai). The packaging step validates every
provider policy against the live catalog, so every build failed with
"models.dev provider 'kimi-for-coding' is missing".

`kimi-code-plan-cn` serves https://api.kimi.com/coding/v1, the base URL this
policy already targets, and carries the same model IDs. The checked-in
snapshot is regenerated too: the backend validates policies against it at
import time and would not start otherwise. Installed clients are unaffected;
they persist our provider id `kimi`, never the source id.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
ensure-runtime-resources validates each staged runtime by running it with
a 5s timeout. The first launch of a freshly downloaded binary waits on the
antivirus scan: on the self-hosted Windows runner the 60 MB uv.exe took
4.3s when idle and exceeded 5s under build load, so a correct runtime was
reported as "invalid". Hosted runners never showed it.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
win-sign.cjs asks Windows PowerShell 5.1 which files still need signing.
Every CI step runs under pwsh 7, and node hands the child pwsh 7's
PSModulePath untouched, so 5.1 tried to load pwsh 7's
Microsoft.PowerShell.Security and Get-AuthenticodeSignature was
unavailable. The error still exited 0 with empty output, which read as
"0 files need signing": the whole build went out unsigned (the audit step
would have caught it, but only at the end).

Drop PSModulePath for the child so 5.1 uses its own default, and set
ErrorActionPreference to Stop so a failed check fails the build instead
of skipping the signature. Reproduced and verified on the signing machine
by starting 5.1 from pwsh 7 through Process.Start, as node does.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Smart App Control stays on on the signing machine, and it blocks a freshly
built unsigned exe whenever it has no cloud reputation, which a new
PyInstaller build never has: the windowless smoke test could not launch
amphi-autostart.exe ("application control policy has blocked this file").
There is no allowlist for Smart App Control; a valid signature is the only
way through.

Sign dist/amphi right after PyInstaller, through a small CLI entry in
win-sign.cjs. prebuild-fetch-amphi copies the files as-is, so afterPack
later finds them validly signed and skips them. The signing-machine check
moves up with it, so a halt file stops the job before either signing pass.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Under the runner's service account Get-AuthenticodeSignature could not
confirm Microsoft's VCRUNTIME140.dll as Valid, so it was queued for
signing, and jsign rejected its existing certificate table ("Invalid data
directory (index=4)"). Sign only files that are NotSigned: publisher
signatures are kept, and the audit step still catches a broken one.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
The cleanup step killed every amphi / Bridgic Agent process by name. On the
self-hosted runner another account runs its own Bridgic Agent, whose daemon
the non-admin runner account cannot stop ("access denied"), so the job failed
before checkout. Limit it to processes under the runner's workspace or its
own LOCALAPPDATA; processes of other accounts expose no Path and are left
alone.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…orts

The signing runner doubles as a test machine. A Bridgic Agent running under
another account keeps the daemon's default port 7421, and the installer
scenarios would then fail, or pass against that daemon instead of the build
under test. Check 7421 and 9123 first and fail with a clear message rather
than kill a process that is not the runner's.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
The VCRUNTIME140.dll and VCRUNTIME140_1.dll copies in the PyInstaller
bundle end exactly where Microsoft's signature used to begin (size 103936,
certificate table at 103936 + 20608), yet their header still points at it.
Windows reports them NotSigned, so they are queued for signing, and jsign
rejects them with "Invalid data directory (index=4)".

Before signing, zero that one data-directory entry when it points past the
end of the file. The entry is excluded from the Authenticode digest and
ignored by the loader, so the file becomes a plain unsigned PE that jsign
signs normally. Intact tables are never touched. Reproduced and verified
locally with jsign 7.5 on a PE truncated the same way.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Every jsign failure wrote the halt file, including the two VCRUNTIME140
failures where the token had already signed a dozen files: the PIN was
right, a retry could not cost an attempt, and a human still had to clear
the file by hand.

After a failure, check whether any file of that chunk is now signed. If so,
the token accepted the PIN in this run, which also resets its wrong-PIN
counter, so no halt is written. If nothing got signed, or that cannot be
determined, the failure may be a wrong PIN and the halt is written as
before. The error text is deliberately not parsed: a PIN error worded in
an unexpected way would read as safe and let every later run spend an
attempt.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…machine

The first fully signed run reached a signed installer in ~35 min and then
spent ~55 min in the installer smoke, where the 90-minute limit cancelled
it during the ninth scenario (the first eight had passed). Antivirus scans
every install on this laptop, so the scenarios run at about half the speed
of a hosted runner. Raise the limit to 150.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
A job signs in several separate jsign runs: the PyInstaller bundle, then
one run per .exe during packaging. When the network dropped mid-packaging,
signing uv.exe failed alone, nothing in that one-file run got signed, and
the halt file was written, although the same PIN from the same secret had
signed 128 files minutes earlier in that job.

Record a successful login in RUNNER_TEMP, which every job starts empty, and
treat it as proof for the rest of the job. Outside CI there is no proof and
the conservative rule applies unchanged.

jsign's output is now captured, still echoed to the log, and its tail is
written into the halt file: that run's log never reached GitHub because the
runner lost its connection, and the halt file said only "Command failed".

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Only a real Release (tag push or release_tag) and a manual run with the new
`sign` input go to the self-hosted token runner. Every other manual run
builds Windows unsigned on windows-latest, so an internal build no longer
needs the signing machine switched on. `meta` decides runner and signing
together, and the signing-only steps and the token PIN follow that decision.
The Releasing section now opens with one table — tag push, release_tag,
nightly, nightly with `sign` — showing the release shape, platforms,
Windows runner, signing and smoke for each, so the rules the workflow's
`meta` job enforces can be read in one place.
@lynnlink
lynnlink merged commit 824425b into main Oct 9, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants