yamine 0.22.0 → 0.22.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 69848a3d38cd9997ca18e52ea892aa57962524e0cd41df16199a651409e86b76
4
- data.tar.gz: aebe3dd18c4bab95865491850bb940ca9a7e42c83f53d851b4d3ddd691f80305
3
+ metadata.gz: 1f20b021d3a702ab4aef776ac24e10683b12c1b4ba58b7ad4e5b11b91316eb78
4
+ data.tar.gz: 4ff8a2e59f075f38aa81d084d9aea45041346790aefd687ef2fc08669bc34737
5
5
  SHA512:
6
- metadata.gz: 7c1bb5ef1104c29a53c8e4274a5b4a42f7231f895f40bb7d12defc97dee72232944340c24b4bec62b9a736d29bdab01d975b32cf6e1af7aaf7f1b643ec0ac602
7
- data.tar.gz: 66ef5d87789c27a94994c0d24f1567c1a762d592ecbfb00b3d95db8a7f538f3c6868be3703a9870a88d0101aabb37dcfaba8f6db85b2e4a74799b4a4381c7280
6
+ metadata.gz: 2e2542be9656d87375b9ab29ac2862f33b2be289c7932aa99f2f29abf3b2a47f40e24623f2ab8b902021e6b42fe4ed48ebe7dfc4a86a3184ddf4f859cb359d40
7
+ data.tar.gz: 85d76ec111ee796d23b89879d414bf5a248504f58983a0064c3d43ffaa88d532a0f3b5d6542f21b09d2fba5acb57f20e673fcb5f7d13490108f321adbf1eb896
data/CHANGELOG.md CHANGED
@@ -2,6 +2,44 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [0.22.1] - 2026-09-29
6
+
7
+ ### Fixed
8
+
9
+ - **macOS trusts the local CA now, instead of merely filing it.** A
10
+ non-elevated `yamine trust` added the certificate without naming any
11
+ policies, so macOS recorded a trust-store entry with no settings at
12
+ all: the CA sat in the login keychain trusted for nothing, and every
13
+ browser answered `ERR_CERT_AUTHORITY_INVALID` on every
14
+ `https://*.localhost` route. The add now passes `-p ssl -p basic` —
15
+ the same thing Keychain Access writes for Secure Sockets Layer + X.509
16
+ Basic — and `yamine trust` re-reads the trust store afterwards rather
17
+ than believing the exit status, so an add that recorded nothing is a
18
+ failure that names the exact `security add-trusted-cert` to run
19
+ instead of a success the browser will contradict.
20
+ - **A CA that is in the keychain but untrusted repairs itself.** The
21
+ check that decided whether to trust compared fingerprints alone, so
22
+ once that bare certificate was in a keychain every later boot
23
+ concluded it was trusted and never touched it again — the check that
24
+ should have caught the broken state was the one hiding it. It now
25
+ needs both halves: the certificate in a keychain *and* a trust
26
+ setting recorded for its fingerprint. A machine left with a
27
+ policy-less entry therefore self-heals on the next `yamine trust` or
28
+ proxy boot, and one whose trust store cannot be read is left alone
29
+ rather than re-adding on every boot.
30
+ - **A proxy running as a normal user trusts the CA too.** Trust used to
31
+ be attempted only when the proxy was root, so a first run without
32
+ `sudo` never tried and installed a CA that could not work. Every proxy
33
+ now ensures trust, elevated or not, and reports a failed attempt
34
+ rather than swallowing it. `YAMINE_SKIP_CA_TRUST` is the escape
35
+ hatch for machines whose CA arrives by MDM or a hand-run
36
+ `security add-trusted-cert`.
37
+ - **`yamine doctor` names a CA the OS does not trust.** The recorded
38
+ marker says yamine trusted this certificate; it says nothing about
39
+ whether macOS agreed. `doctor` asks the OS too, so a CA that is
40
+ installed but untrusted — the state where every route fails TLS —
41
+ reads as "CA is installed but macOS does not trust it" and points at
42
+ `yamine trust` instead of passing as healthy.
5
43
  ## [0.22.0] - 2026-09-29
6
44
 
7
45
  ### Added
@@ -517,15 +517,15 @@ module Yamine
517
517
  # expiring, or renamed.
518
518
  #
519
519
  # The ordering is the whole point, so it lives in one place rather
520
- # than at each call site. `trusted?` compares a marker against the
521
- # fingerprint of the certificate ON DISK, and a proxy regenerates
522
- # the CA at boot when the name changes. Checking the marker before
523
- # the CA is current therefore reads a stale match, skips trust, and
524
- # lets the proxy come up serving a CA the keychain does not trust —
525
- # breaking TLS for every route.
520
+ # than at each call site. The state-dir marker is only half of it
521
+ # (`Trust.trusted?` also asks the OS): a marker that says "we
522
+ # trusted this" survives a keychain that never took the setting,
523
+ # so checking it before the CA is current — or on its own — reads
524
+ # a stale match, skips trust, and lets the proxy come up serving a
525
+ # CA no browser trusts.
526
526
  def ca_current_and_trusted?(dir = Certs.state_dir)
527
527
  Certs.ensure_ca(dir)
528
- Certs.trusted?(dir)
528
+ Trust.trusted?(dir)
529
529
  end
530
530
 
531
531
  # Root-only CA trust: the System keychain (all users, no prompt).
data/lib/yamine/doctor.rb CHANGED
@@ -192,6 +192,15 @@ module Yamine
192
192
  unless Certs.trusted?(dir)
193
193
  return Check.new(name: "ca", ok: false, message: "CA not trusted — run: yamine trust")
194
194
  end
195
+ # The marker says WE trusted this certificate; it says nothing about
196
+ # whether the OS recorded a trust setting for it. A CA sitting in
197
+ # the keychain with no trust setting is the state that produced
198
+ # ERR_CERT_AUTHORITY_INVALID on every route, with the marker
199
+ # cheerfully reporting success.
200
+ unless Trust.trusted?(dir)
201
+ return Check.new(name: "ca", ok: false,
202
+ message: "CA is installed but macOS does not trust it — run: yamine trust")
203
+ end
195
204
 
196
205
  # Trusted CAs that are not the one now on disk. They accumulate from
197
206
  # CA regeneration (missing, expiring, or renamed — the ask-local
data/lib/yamine/proxy.rb CHANGED
@@ -107,7 +107,7 @@ module Yamine
107
107
  servers.each { |s| s.listen(@port) }
108
108
  @port = servers.first.addr[1]
109
109
  Ownership.chown_state_dir(@store.dir)
110
- ensure_system_ca_trust if Process.uid.zero?
110
+ ensure_ca_trust
111
111
  trap("INT") { stop(servers) }
112
112
  trap("TERM") do
113
113
  @supervisor&.shutdown
@@ -188,14 +188,30 @@ module Yamine
188
188
 
189
189
  private
190
190
 
191
- # A root proxy (launchd service or sudo daemon) is the one process
192
- # that can trust the CA into the System keychain silently — no GUI
193
- # popup. Idempotent: the state-dir marker makes repeat boots a no-op.
194
- def ensure_system_ca_trust
195
- return if Certs.trusted?(@state_dir)
191
+ # The proxy is the process the browser's TLS stack actually talks to,
192
+ # so it is where the CA has to be trusted — as root, into the System
193
+ # keychain silently, and as a normal user, into the login keychain.
194
+ # A non-elevated proxy used to skip this entirely, which is how a
195
+ # first run as an ordinary user installed a CA that could not work.
196
+ #
197
+ # `Trust.trusted?` asks the trust store, not just the state-dir
198
+ # marker, so a CA that is present but untrusted is repaired instead
199
+ # of being short-circuited. Trust.trust only writes the marker once
200
+ # the trust setting is confirmed, so a failure is retried (and
201
+ # reported) on the next boot rather than cached as a success.
202
+ #
203
+ # YAMINE_SKIP_CA_TRUST is for machines whose CA arrives some other
204
+ # way — an MDM profile, a hand-run `security add-trusted-cert` — and
205
+ # for the suite, which spawns real proxies and must never reach a real
206
+ # keychain.
207
+ def ensure_ca_trust
208
+ return if ENV["YAMINE_SKIP_CA_TRUST"]
209
+ return if Trust.trusted?(@state_dir)
196
210
 
197
211
  result = Trust.trust(@state_dir)
198
- @on_error.call("CA trust warning: #{result[:error]}") unless result[:trusted]
212
+ return if result[:trusted]
213
+
214
+ @on_error.call("CA trust warning: #{result[:error]}")
199
215
  end
200
216
 
201
217
  def admit?
data/lib/yamine/trust.rb CHANGED
@@ -1,10 +1,41 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "open3"
4
+ require "tmpdir"
4
5
 
5
6
  module Yamine
6
7
  # Install the local CA into the OS trust store.
7
8
  module Trust
9
+ SYSTEM_KEYCHAIN = "/Library/Keychains/System.keychain"
10
+
11
+ # The policies a browser's TLS stack evaluates a leaf under: SSL
12
+ # server (the hostname check) and X.509 basic (the chain build).
13
+ #
14
+ # They have to be named, and this is measured, not assumed.
15
+ # `add-trusted-cert -r trustRoot` with no `-p` exits 0 and records
16
+ # the certificate in the trust store with NO `trustSettings` array at
17
+ # all: `security dump-trust-settings` reports "Number of trust
18
+ # settings : 0", and `security trust-settings-export` shows a
19
+ # trustList entry holding only issuerName/modDate/serialNumber. What
20
+ # the CA is trusted FOR is then unstated, and whether such an entry
21
+ # is honoured at all depends on the certificate also being installed
22
+ # in a keychain the evaluator searches — on the machine this was
23
+ # fixed on, it was not, and every browser answered
24
+ # ERR_CERT_AUTHORITY_INVALID.
25
+ #
26
+ # With `-p ssl -p basic` the same call records
27
+ # kSecTrustSettingsPolicyName sslServer + basicX509, and per
28
+ # SecTrustSettings.h a settings entry with no explicit
29
+ # kSecTrustSettingsResult defaults to kSecTrustSettingsResultTrustRoot
30
+ # ("trust this root cert"). That is also what Keychain Access writes
31
+ # for a certificate set to Secure Sockets Layer + X.509 Basic.
32
+ MACOS_TRUST_POLICIES = %w[-p ssl -p basic].freeze
33
+
34
+ # SecTrustSettingsResult (Security/SecTrustSettings.h). Only these
35
+ # two grant trust; 3 is Deny and 4 is Unspecified, so a CA whose only
36
+ # setting is one of those is explicitly NOT trusted.
37
+ TRUST_RESULTS = [1, 2].freeze
38
+
8
39
  module_function
9
40
 
10
41
  def platform
@@ -31,6 +62,33 @@ module Yamine
31
62
  { trusted: false, error: e.message }
32
63
  end
33
64
 
65
+ # True when the OS trust store will honour the CA in `dir`.
66
+ #
67
+ # The state-dir marker is only half the answer, and the half that
68
+ # lies: it records that WE trusted this exact certificate at some
69
+ # point, so it survives a keychain that never took the setting, a
70
+ # trust run that recorded nothing, and a CA that was regenerated
71
+ # under it. Treating the marker as "trusted" is how a machine kept a
72
+ # CA in its keychain that no browser would accept.
73
+ def trusted?(dir = Certs.state_dir)
74
+ return false unless Certs.trusted?(dir)
75
+ return true unless platform == :macos
76
+
77
+ paths = Certs.ca_paths(dir)
78
+ return false unless File.file?(paths[:cert])
79
+
80
+ macos_keychains.any? { |keychain| already_trusted?(paths[:cert], keychain) }
81
+ end
82
+
83
+ # The keychains a CA can be trusted into: the user's own, and — for
84
+ # an elevated run — the System one, which every user on the machine
85
+ # reads. Both are checked because the certificate on disk may have
86
+ # been trusted into either, and a CA trusted only as root is not
87
+ # trusted for a later unprivileged run (or the reverse).
88
+ def macos_keychains
89
+ [login_keychain, SYSTEM_KEYCHAIN].uniq
90
+ end
91
+
34
92
  # The fingerprint `security -Z` prints: uppercase hex, no colons.
35
93
  # Certificates are identified by THIS, never by common name: every CA
36
94
  # this project has generated shares a name with the others, so a
@@ -81,7 +139,7 @@ module Yamine
81
139
  end
82
140
 
83
141
  def keychains
84
- [login_keychain, "/Library/Keychains/System.keychain"]
142
+ [login_keychain, SYSTEM_KEYCHAIN]
85
143
  end
86
144
 
87
145
  # Remove trusted certificates that carry one of our CA names but are
@@ -206,25 +264,142 @@ module Yamine
206
264
  return if already_trusted?(cert_path, keychain)
207
265
 
208
266
  _out, status = Command.capture2("security", "add-trusted-cert",
209
- "-d", "-r", "trustRoot", "-k", keychain, cert_path)
210
- raise CertError, "security add-trusted-cert (system) failed" unless status.success?
267
+ "-d", "-r", "trustRoot", *MACOS_TRUST_POLICIES, "-k", keychain, cert_path)
268
+ raise CertError, macos_trust_error(cert_path, keychain) unless status.success?
211
269
  else
270
+ # Not elevated: the user's own keychain, with the trust policies
271
+ # named. The certificate still has to be in a keychain — macOS
272
+ # builds the chain by searching keychains for the issuer, so a
273
+ # trust setting for a certificate that is not installed anywhere
274
+ # is dead weight. Measured: CA trusted but absent from every
275
+ # keychain => "unable to get local issuer certificate".
212
276
  keychain = login_keychain
213
277
  return if already_trusted?(cert_path, keychain)
214
278
 
215
279
  _out, status = Command.capture2("security", "add-trusted-cert",
216
- "-r", "trustRoot", "-k", keychain, cert_path)
217
- raise CertError, "security add-trusted-cert failed" unless status.success?
280
+ "-r", "trustRoot", *MACOS_TRUST_POLICIES, "-k", keychain, cert_path)
281
+ raise CertError, macos_trust_error(cert_path, keychain) unless status.success?
218
282
  end
283
+
284
+ # `add-trusted-cert` exits 0 for a certificate it merely filed —
285
+ # which is exactly what the policy-less invocation did on every
286
+ # non-elevated install. The exit status is not evidence of trust,
287
+ # only the trust store is: re-read it here, or the state-dir marker
288
+ # records a success no browser will ever agree with.
289
+ return if already_trusted?(cert_path, keychain)
290
+
291
+ raise CertError, macos_trust_error(cert_path, keychain)
219
292
  end
220
293
 
221
- # Adding the same certificate twice is not harmless: it is how the
222
- # trust store accumulated duplicates across repeated setup runs.
294
+ def macos_trust_error(cert_path, keychain)
295
+ domain = (keychain == "/Library/Keychains/System.keychain") ? ["-d"] : []
296
+ command = (["security", "add-trusted-cert", *domain, "-r", "trustRoot",
297
+ *MACOS_TRUST_POLICIES, "-k", keychain, cert_path]).join(" ")
298
+ "macOS recorded no trust setting for the CA in #{keychain}. " \
299
+ "Writing user trust settings needs authorization, which a detached " \
300
+ "or headless run cannot grant — run this in a terminal, then retry: " \
301
+ "#{command}"
302
+ end
303
+
304
+ # True when macOS will actually honour this certificate as a CA.
305
+ #
306
+ # BOTH halves are required, and the second one is the one that used
307
+ # to be missing:
308
+ #
309
+ # 1. the certificate is in the keychain, so the trust evaluator can
310
+ # find the issuer when it builds the chain; and
311
+ # 2. the trust store records a trust SETTING for its fingerprint.
312
+ #
313
+ # Presence alone is not trust: a certificate in a keychain with no
314
+ # trust setting fails with CSSMERR_TP_NOT_TRUSTED. Neither is a bare
315
+ # trust-list entry — the key with no `trustSettings` array that
316
+ # `add-trusted-cert` without `-p` leaves behind, which
317
+ # `security dump-trust-settings` counts as zero settings. Requiring
318
+ # a real setting costs one corrective add on a machine that has the
319
+ # bare kind (the add upgrades it in place, without duplicating the
320
+ # certificate) and nothing thereafter, so the stricter reading
321
+ # converges rather than looping.
322
+ #
323
+ # The fingerprint comparison stays: adding a certificate twice is not
324
+ # harmless, and it is how the trust store filled up with duplicates.
325
+ # It is now a necessary condition rather than the whole answer.
223
326
  def already_trusted?(cert_path, keychain)
224
327
  fp = fingerprint_of(cert_path)
225
328
  return false unless fp
329
+ return false unless keychain_fingerprints(keychain, common_name: nil).include?(fp)
226
330
 
227
- keychain_fingerprints(keychain, common_name: nil).include?(fp)
331
+ trust_setting?(fp, keychain)
332
+ end
333
+
334
+ # The trust settings the OS records, as plist XML, or nil when macOS
335
+ # cannot be asked at all.
336
+ #
337
+ # The trust store is keyed by the certificate's SHA-1 fingerprint —
338
+ # the same identity `security -Z` prints — so this never has to match
339
+ # on a common name, which every yamine CA shares.
340
+ #
341
+ # `security trust-settings-export` is the only `security` subcommand
342
+ # that reports the SETTING rather than the presence:
343
+ # `dump-trust-settings` lists certificates and a count of their
344
+ # settings, `find-certificate` lists certificates. The admin domain
345
+ # needs `-d`; without it only the user's own settings are exported,
346
+ # so a root install would look untrusted forever.
347
+ #
348
+ # The certificate's own key is looked up by the caller, so a store
349
+ # that holds no entry for it comes back as XML WITHOUT it — that is a
350
+ # definite answer (never trusted), not an unreadable one.
351
+ def trust_settings(keychain)
352
+ file = File.join(Dir.tmpdir, "yamine-trust-#{Process.pid}-#{rand(1 << 32)}.plist")
353
+ _out, status = Command.capture2("security", "trust-settings-export",
354
+ *("-d" if keychain == SYSTEM_KEYCHAIN), file)
355
+ return nil unless status.success?
356
+
357
+ # Read back through plutil rather than parsing the plist here: it
358
+ # is Apple's own reader, and it keeps this file free of a plist
359
+ # parser (rexml is a bundled gem, unavailable under bundler).
360
+ xml, converted = Command.capture2("plutil", "-convert", "xml1", "-o", "-", file)
361
+ converted.success? ? xml : nil
362
+ rescue SystemCallError
363
+ nil
364
+ ensure
365
+ File.unlink(file) if file && File.file?(file)
366
+ end
367
+
368
+ # True when the trust store records a trust SETTING that grants
369
+ # trust for this certificate.
370
+ #
371
+ # Returns TRUE when the trust store cannot be read. That direction
372
+ # is deliberate: a check that always answers "not trusted" turns
373
+ # every boot into a re-add, and a re-add in the user domain can
374
+ # raise a GUI authorization prompt — a worse failure than the one
375
+ # this fixes.
376
+ def trust_setting?(fingerprint, keychain)
377
+ settings = trust_settings(keychain)
378
+ return true if settings.nil?
379
+
380
+ entry = plist_dict_after(settings, fingerprint)
381
+ return false unless entry&.include?("<key>trustSettings</key>")
382
+
383
+ granted = entry.scan(%r{<key>kSecTrustSettingsResult</key>\s*<integer>(\d+)</integer>}).flatten
384
+ return true if granted.empty? # policies only: defaults to trustRoot
385
+
386
+ granted.any? { |result| TRUST_RESULTS.include?(result.to_i) }
387
+ end
388
+
389
+ # The `<dict>` that follows `key`, nested dicts included. A
390
+ # trustSettings array is a list of dicts, so a lazy `.*?</dict>`
391
+ # would stop in the middle of the very array being read.
392
+ def plist_dict_after(xml, key)
393
+ at = xml.index("<key>#{key}</key>")
394
+ return nil unless at
395
+
396
+ rest = xml[(at + 1)..]
397
+ depth = 0
398
+ rest.scan(/<dict>|<\/dict>/) do
399
+ depth += ($~[0] == "<dict>" ? 1 : -1)
400
+ return rest[0...$~.end(0)] if depth.zero?
401
+ end
402
+ nil
228
403
  end
229
404
 
230
405
  def login_keychain
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Yamine
4
- VERSION = "0.22.0"
4
+ VERSION = "0.22.1"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yamine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.22.0
4
+ version: 0.22.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kaka Ruto