devcert icon indicating copy to clipboard operation
devcert copied to clipboard

Persist root CA only after trust-store install succeeds (fixes #118)

Open ccstr opened this issue 6 months ago • 0 comments

Fixes #118.

Problem

installCertificateAuthority writes the root CA cert + encrypted key to their canonical paths before attempting the trust-store install. If addToTrustStores fails or is declined (e.g. the Windows "Security Warning" → No), the on-disk state remains and certificateFor's !existsSync(rootCAKeyPath) guard skips re-installation forever. The browser stays broken with NET::ERR_CERT_AUTHORITY_INVALID and devcert never re-prompts. See #118 for the full repro.

Fix

  1. src/certificate-authority.ts: generate the cert + key into temp paths, run addToTrustStores first, and write to the canonical paths only after the trust install succeeds. If it throws, clean up the temp files and propagate.
  2. src/platforms/win32.ts: rethrow when certutil -addstore fails. The current swallow is what hides the user's "No" click from the caller — without rethrowing, the reorder in (1) doesn't help, because Windows would still report success. The Firefox open-failure tolerance is preserved (most users don't have Firefox).

I considered the alternative of leaving the swallow in place and using certutil -verifystore -user root <fingerprint> to detect missing-from-store after the fact. Rethrowing the underlying error is simpler, surfaces the real failure to the caller, and matches the existing macOS/Linux behavior where run('sudo', [...]) already propagates.

Cross-platform impact

addToTrustStores on all three platforms reads the cert from its path argument as a one-time input (no assumptions about the path being canonical):

  • macOS (security add-trusted-cert ... <path>)
  • Linux (cp <path> /usr/local/share/ca-certificates/devcert.crt)
  • Windows (certutil -addstore -user root <path>)

So passing a temp path is safe everywhere. The removeFromTrustStores calls in uninstall() look up by the existing canonical path / by subject name, so the initial uninstall() at the top of installCertificateAuthority is unaffected.

Tests

The package's npm test script is a stub (echo Ha.; exit 1) and there are no test files, so no test changes. npm run build passes.

ccstr avatar May 19 '26 22:05 ccstr