Skip to main content

runner_manager_platform/
secrets.rs

1// owner: d2-machine-secret-store
2
3//! The one persisted GitHub credential, in a place a boot-time service can
4//! read.
5//!
6//! `07-security.md` counts the persisted credential surface and gets to one:
7//! *"The product now holds exactly one persisted GitHub credential and one
8//! short-lived sensitive value."* This module is where that one credential
9//! lives. The short-lived one — the encoded JIT configuration — belongs to
10//! [`crate::process::RestrictiveHandoff`] and is not stored here or anywhere
11//! else.
12//!
13//! # The constraint that decides the whole design
14//!
15//! D13 requires the service to start at **machine boot**.
16//! `05-infrastructure.md`: *"A boot-time service runs outside any user's login
17//! session and cannot read a per-user keychain on any supported OS: macOS
18//! LaunchAgents start only at login, and Windows Credential Manager vaults are
19//! per-user."* So the default store is machine-scoped, and every per-user
20//! secret facility on all three operating systems is unavailable to it by
21//! construction rather than by preference.
22//!
23//! | OS | [`SecretScope::Machine`] | [`SecretScope::User`] |
24//! |---|---|---|
25//! | Windows | DPAPI **machine** scope, in a file under `%ProgramData%` with its own protected DACL | DPAPI **user** scope, in a file under `%LOCALAPPDATA%` with its own protected DACL |
26//! | macOS | System Keychain (`/Library/Keychains/System.keychain`) | the account's login keychain |
27//! | Linux | `0600` file under `/var/lib/runner-manager`, plus the systemd credential the service is started with | `0600` file under `$XDG_DATA_HOME/runner-manager` |
28//!
29//! `service install --start-at login` is the escape hatch
30//! `07-security.md` promises operators who reject machine-scoped storage:
31//! *"Operators who reject this can use `service install --start-at login` and
32//! keep a user-scoped store, accepting no unattended restart."* Both columns
33//! implement [`SecretStore`], the active one is chosen by
34//! [`SecretScope::for_start_mode`], and [`ActiveStore`] is what `host show`
35//! and `service status` print so the choice is inspectable rather than
36//! implied.
37//!
38//! # The accepted trade-off, implemented honestly
39//!
40//! A local administrator or `root` on this machine can read a machine-scoped
41//! secret. `07-security.md` records that as an accepted consequence, on the
42//! grounds that such an account *"can already read the runner's own
43//! credentials and job workspaces"*. Nothing here tries to defeat it, and
44//! nothing here pretends to. What is defended is the case that is actually in
45//! the threat model: **an ordinary local user who is not an administrator must
46//! not be able to read the stored value**, and [`SecretStore::protection`]
47//! reports whether that holds, per OS, through the one cross-platform name
48//! [`crate::process::permissions_summary`] already defines.
49//!
50//! Delete is delete, not erasure. [`crate::process::RestrictiveHandoff`] sets
51//! out why no userspace program can promise that the bytes are unrecoverable —
52//! a journal, a snapshot, or an SSD's wear levelling each keep copies an
53//! overwrite never reaches — and the same disclaimer applies here. The Linux
54//! backend overwrites before unlinking because the value is at rest in
55//! plaintext there and the overwrite is free; that is a best effort and is not
56//! a claim.
57//!
58//! # What never holds this value
59//!
60//! SQLite, TOML configuration, logs, diagnostics, UI state, and command-line
61//! arguments. The type system carries as much of that as it can: the value
62//! crosses this module's surface only as a [`SecretString`], which has no
63//! `Display` and a redacting `Debug`, and no error in [`SecretStoreError`]
64//! carries the value or any part of it. `client_id` is public by design
65//! (`07-security.md`: *"Public by design; may appear in logs and
66//! documentation"*) and is not a secret this store handles.
67//!
68//! # Blocking, not async
69//!
70//! DPAPI, Security.framework and `open(2)` are blocking calls that take
71//! microseconds. This runs twice in the life of a process — once at `auth
72//! login`, once at startup — so an async surface would buy nothing and would
73//! oblige every caller to be in a runtime to read a file.
74//!
75//! # Where a test may point it, and where it may not
76//!
77//! [`PlatformSecretStore::standard`] resolves the production location in the
78//! table above. [`PlatformSecretStore::rooted_at`] puts the same backend under
79//! a directory the caller names, exactly as
80//! [`crate::paths::AppPaths::rooted_at`] does for the four application-data
81//! directories, and for the same two reasons: a test needs a disposable store,
82//! and a service installed against an explicitly configured root has to
83//! reproduce one.
84//!
85//! **No test in this crate writes to a standard location**, and that is a
86//! deliberate constraint rather than an oversight. Two of the six standard
87//! locations need `root` to create (`/var/lib`, the System Keychain), and the
88//! other four are the operator's real store — a suite that wrote there would
89//! destroy a developer's `auth login` every time it ran. So the standard
90//! locations are asserted by *resolution*, and every round trip runs against a
91//! rooted store. What that does and does not cover is written out at
92//! [`PlatformSecretStore::rooted_at`].
93
94use std::fmt;
95use std::path::{Path, PathBuf};
96
97use runner_manager_domain::model::StartMode;
98use secrecy::{ExposeSecret, SecretString};
99
100/// The product identity, resolved from the one place that defines it.
101///
102/// `crate::paths` owns these three segments and builds the four
103/// application-data directories out of them. This module builds *different*
104/// locations — `%ProgramData%\<org>\<app>`, `$XDG_DATA_HOME/<app>`, the macOS
105/// keychain service — but out of the same identity, and it must stay the same
106/// identity. Spelled a second time here, a drift would move the secret store
107/// out from under an upgraded binary and the token would read as simply
108/// absent, which is the one failure mode this store must never produce
109/// silently.
110use crate::paths::{APPLICATION, ORGANIZATION, QUALIFIER};
111
112/// Reverse-domain service name for the macOS keychain item.
113///
114/// Composed rather than written out, for the reason above. It is a `LazyLock`
115/// because the three segments are `const`s and neither `concat!` nor a `const
116/// fn` can join them — `concat!` takes literals, not constant items. The
117/// composition is asserted against the documented literal by
118/// `the_standard_locations_are_the_documented_ones`, which keeps its own
119/// hard-coded strings precisely so that it is an *independent* oracle rather
120/// than a second copy of this expression.
121#[cfg(target_os = "macos")]
122static KEYCHAIN_SERVICE: std::sync::LazyLock<String> =
123    std::sync::LazyLock::new(|| format!("{QUALIFIER}.{ORGANIZATION}.{APPLICATION}"));
124
125/// The keychain account, and the stem of the file name on the two platforms
126/// that use a file. One value, one name, everywhere.
127const ITEM: &str = "user-access-token";
128
129/// The directory a file-backed store keeps its value in, under whichever root
130/// the scope resolved to. Never `config/` — `05-infrastructure.md` reserves
131/// that for *"non-secret TOML and SQLite"*.
132const DIRECTORY: &str = "secrets";
133
134// ---------------------------------------------------------------------------
135// Scope
136// ---------------------------------------------------------------------------
137
138/// Which of the two stores a value lives in.
139///
140/// Not a preference and not a tuning knob: it is a direct function of the
141/// service's start mode, because a service that starts at boot has no login
142/// session to read a user-scoped store from. [`SecretScope::for_start_mode`]
143/// is that function, and it is total in both directions —
144/// [`SecretScope::start_mode`] is its inverse, which is what lets
145/// [`ActiveStore`] state whether the store in use agrees with the start mode
146/// actually configured.
147#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
148pub enum SecretScope {
149    /// Readable by a service running at machine boot, outside any login
150    /// session. The default, and what `--start-at boot` requires.
151    Machine,
152    /// Readable only inside the operator's login session. What
153    /// `--start-at login` gets, at the cost of no unattended restart.
154    User,
155}
156
157impl SecretScope {
158    /// The store a given start mode obliges.
159    #[must_use]
160    pub const fn for_start_mode(mode: StartMode) -> Self {
161        match mode {
162            StartMode::Boot => Self::Machine,
163            StartMode::Login => Self::User,
164        }
165    }
166
167    /// The start mode this store is the one correct choice for.
168    #[must_use]
169    pub const fn start_mode(self) -> StartMode {
170        match self {
171            Self::Machine => StartMode::Boot,
172            Self::User => StartMode::Login,
173        }
174    }
175
176    /// The name `host show`, `service status`, and the `scope` log field use.
177    #[must_use]
178    pub const fn as_str(self) -> &'static str {
179        match self {
180            Self::Machine => "machine",
181            Self::User => "user",
182        }
183    }
184}
185
186impl fmt::Display for SecretScope {
187    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
188        f.write_str(self.as_str())
189    }
190}
191
192// ---------------------------------------------------------------------------
193// Errors
194// ---------------------------------------------------------------------------
195
196/// Something went wrong reaching the secret store.
197///
198/// **No variant carries the stored value, and none ever may.** Every one of
199/// them is formatted into an operator-facing message and into a `tracing`
200/// event, and `07-security.md`'s release gate is that the user access token is
201/// *"absent from logs, databases, snapshots, crash reports, and CLI output"*.
202/// A variant that carried the value would put it in all four at once.
203///
204/// Note what is *not* here: an "absent" variant. A load that finds nothing is
205/// [`Ok(None)`], because absence is the ordinary state of this store before
206/// `auth login` and after `auth logout`, and a caller that has to distinguish
207/// "not logged in" from "the keychain is unreachable" cannot be asked to do it
208/// by matching on an error kind.
209#[derive(Debug, thiserror::Error)]
210pub enum SecretStoreError {
211    /// The store's location could not be worked out at all.
212    #[error("cannot work out where the {scope}-scoped secret store lives: {reason}")]
213    Resolve {
214        /// Which store was being resolved.
215        scope: SecretScope,
216        /// What was missing, in terms an operator can act on.
217        reason: String,
218    },
219
220    /// The value could not be written.
221    #[error(
222        "cannot write the user access token to the {scope}-scoped store at {location}: {source}"
223    )]
224    Store {
225        /// Which store.
226        scope: SecretScope,
227        /// Where it lives, as [`SecretStore::location`] reports it.
228        location: String,
229        /// The underlying platform error.
230        #[source]
231        source: std::io::Error,
232    },
233
234    /// The value could not be read back.
235    #[error(
236        "cannot read the user access token from the {scope}-scoped store at {location}: {source}"
237    )]
238    Load {
239        /// Which store.
240        scope: SecretScope,
241        /// Where it lives.
242        location: String,
243        /// The underlying platform error.
244        #[source]
245        source: std::io::Error,
246    },
247
248    /// The value could not be removed.
249    ///
250    /// Worth an error rather than a shrug, for the reason
251    /// `05-infrastructure.md` gives the credential-disclosure response: step 2
252    /// is *"Run `auth logout` on every host to purge the machine-scoped secret
253    /// store"*, and an operator following that procedure has to be told when a
254    /// host did not comply.
255    #[error(
256        "cannot delete the user access token from the {scope}-scoped store at {location}: {source}"
257    )]
258    Delete {
259        /// Which store.
260        scope: SecretScope,
261        /// Where it lives.
262        location: String,
263        /// The underlying platform error.
264        #[source]
265        source: std::io::Error,
266    },
267
268    /// Something is stored, and it is not a value this store wrote.
269    ///
270    /// Deliberately not folded into [`SecretStoreError::Load`] and deliberately
271    /// not reported as absence. A caller that saw absence would silently start
272    /// a device-flow login and overwrite whatever is there; a caller that saw a
273    /// transient read failure would retry forever. This is neither: it is an
274    /// operator-actionable condition whose remedy is `auth logout` followed by
275    /// `auth login`.
276    #[error(
277        "the {scope}-scoped store at {location} does not hold a user access token this product \
278         wrote: {detail}. Run `auth logout` to purge it and `auth login` to obtain a fresh token."
279    )]
280    Corrupt {
281        /// Which store.
282        scope: SecretScope,
283        /// Where it lives.
284        location: String,
285        /// What is wrong with what is there, in terms that name no part of it.
286        /// A byte count is not a disclosure; the bytes would be, so they are
287        /// never carried.
288        detail: String,
289    },
290
291    /// The access control protecting the value could not be read back.
292    #[error("cannot inspect what protects the {scope}-scoped store at {}: {source}", guard.display())]
293    Inspect {
294        /// Which store.
295        scope: SecretScope,
296        /// The filesystem object whose access control was being read.
297        guard: PathBuf,
298        /// The underlying error.
299        #[source]
300        source: crate::process::HandoffError,
301    },
302}
303
304// ---------------------------------------------------------------------------
305// What the operations report
306// ---------------------------------------------------------------------------
307
308/// What [`SecretStore::delete`] found.
309///
310/// Both variants are success. `auth logout` on a host that was never logged in
311/// is not a failure, and the credential-disclosure procedure in
312/// `05-infrastructure.md` is run across *every* host precisely because the
313/// operator does not know which ones hold a value.
314#[derive(Debug, Clone, Copy, PartialEq, Eq)]
315pub enum Removal {
316    /// A value was there and is not any more.
317    Removed,
318    /// There was nothing to remove.
319    AlreadyAbsent,
320}
321
322impl Removal {
323    /// Whether this call is the one that removed something.
324    #[must_use]
325    pub const fn removed_something(self) -> bool {
326        matches!(self, Self::Removed)
327    }
328}
329
330/// What actually stands between the stored value and an ordinary local user.
331///
332/// The three operating systems protect it with three different mechanisms, so
333/// the useful cross-platform question is not "what is the mode" but "**which
334/// object's access control decides who can read this, and does it exclude an
335/// unprivileged local user**". [`Protection::guard`] answers the first half and
336/// [`Protection::readable_by_other_local_users`] the second.
337///
338/// | store | guard |
339/// |---|---|
340/// | Windows, either scope | the file holding the DPAPI blob, whose DACL is protected and names no broad trustee |
341/// | Linux, either scope | the `0600` file |
342/// | macOS, login or rooted keychain | the keychain database file |
343/// | macOS, System Keychain | `/var/db/SystemKey`, the root-only master key that unlocks it |
344///
345/// That last row is the one worth reading twice.
346/// `/Library/Keychains/System.keychain` is itself world-readable, and saying so
347/// and stopping would be both true and useless: its contents are encrypted, and
348/// what decides who can decrypt them is the mode of the master key beside it.
349/// Reporting the keychain database there would answer a question nobody asked
350/// and would answer it wrongly.
351#[derive(Debug, Clone, PartialEq, Eq)]
352pub struct Protection {
353    guard: PathBuf,
354    description: String,
355    readable_by_other_local_users: bool,
356}
357
358impl Protection {
359    /// The filesystem object whose access control was inspected.
360    #[must_use]
361    pub fn guard(&self) -> &Path {
362        &self.guard
363    }
364
365    /// The platform's own description of it — a Unix mode, or a DACL in SDDL
366    /// form. For diagnostics and for test failure messages.
367    #[must_use]
368    pub fn description(&self) -> &str {
369        &self.description
370    }
371
372    /// Whether an ordinary local user other than the owner could read the
373    /// stored value.
374    ///
375    /// A local administrator or `root` is deliberately outside this question;
376    /// see this module's documentation and `07-security.md`.
377    #[must_use]
378    pub const fn readable_by_other_local_users(&self) -> bool {
379        self.readable_by_other_local_users
380    }
381}
382
383impl fmt::Display for Protection {
384    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
385        write!(
386            f,
387            "{} ({}){}",
388            self.guard.display(),
389            self.description,
390            if self.readable_by_other_local_users {
391                " -- READABLE BY OTHER LOCAL USERS"
392            } else {
393                ""
394            }
395        )
396    }
397}
398
399/// Which store is in use, and whether that is the one the configured start
400/// mode obliges.
401///
402/// `05-infrastructure.md` requires the start mode to be visible in `host show`
403/// and in TUI host settings, and requires `service status` to say when
404/// `--start-at login` means the agent does not run until the operator logs in.
405/// This is the value both of those print. It exists as a type rather than as a
406/// formatted string in two places so that the two cannot drift.
407///
408/// [`ActiveStore::agrees_with_start_mode`] is the check worth having: the store
409/// a process opened and the start mode recorded for the installed service are
410/// two independently persisted facts, and a host whose service was switched
411/// from `boot` to `login` without the token being moved is a host whose daemon
412/// will start and then fail to find a credential. Saying so in `service status`
413/// is cheaper than finding out at three in the morning.
414#[derive(Debug, Clone, PartialEq, Eq)]
415pub struct ActiveStore {
416    scope: SecretScope,
417    start_mode: StartMode,
418    location: String,
419}
420
421impl ActiveStore {
422    /// Pairs a store with the start mode currently configured for the service.
423    #[must_use]
424    pub fn of(store: &dyn SecretStore, start_mode: StartMode) -> Self {
425        Self {
426            scope: store.scope(),
427            start_mode,
428            location: store.location(),
429        }
430    }
431
432    /// The store in use.
433    #[must_use]
434    pub const fn scope(&self) -> SecretScope {
435        self.scope
436    }
437
438    /// The start mode configured for the service.
439    #[must_use]
440    pub const fn start_mode(&self) -> StartMode {
441        self.start_mode
442    }
443
444    /// Where the store keeps its value, in the platform's own terms.
445    #[must_use]
446    pub fn location(&self) -> &str {
447        &self.location
448    }
449
450    /// Whether the store in use is the one this start mode obliges.
451    #[must_use]
452    pub fn agrees_with_start_mode(&self) -> bool {
453        SecretScope::for_start_mode(self.start_mode) == self.scope
454    }
455}
456
457impl fmt::Display for ActiveStore {
458    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
459        write!(
460            f,
461            "{}-scoped secret store at {} (service starts at {})",
462            self.scope, self.location, self.start_mode
463        )?;
464        if !self.agrees_with_start_mode() {
465            write!(
466                f,
467                " -- MISMATCH: starting at {} needs the {}-scoped store",
468                self.start_mode,
469                SecretScope::for_start_mode(self.start_mode)
470            )?;
471        }
472        Ok(())
473    }
474}
475
476// ---------------------------------------------------------------------------
477// The port
478// ---------------------------------------------------------------------------
479
480/// Store, load, delete — and say where the value lives and what protects it.
481///
482/// `Send + Sync` because the agent holds one across `tokio` tasks; `Debug`
483/// because every other port in this workspace is, and because a store that
484/// could not be printed in an error context would be replaced by a path that
485/// could.
486pub trait SecretStore: fmt::Debug + Send + Sync {
487    /// Which of the two stores this is.
488    fn scope(&self) -> SecretScope;
489
490    /// Where the value lives, in the platform's own terms. What `host show`
491    /// and `service status` print, and never the value itself.
492    fn location(&self) -> String;
493
494    /// Writes the value, replacing whatever was there.
495    ///
496    /// # Errors
497    ///
498    /// [`SecretStoreError::Store`].
499    fn store(&self, secret: &SecretString) -> Result<(), SecretStoreError>;
500
501    /// Refuses when [`SecretStore::store`] would, without writing anything.
502    ///
503    /// For a caller about to do something that only a successful write makes
504    /// safe. Renewal is that caller: GitHub retires a refresh token the moment
505    /// it is exchanged, so a process that exchanges it and then cannot store
506    /// the replacement has destroyed the credential for every process sharing
507    /// this store. On a boot-mode host that is any TUI or CLI command started
508    /// without the privilege the machine-scoped store needs to be written,
509    /// even though it can read it.
510    ///
511    /// A permission check, not a promise: a write can still fail for reasons
512    /// no check can foresee. It answers the question that is stable across a
513    /// process's lifetime -- whether *this* account may write here at all.
514    ///
515    /// # Errors
516    ///
517    /// [`SecretStoreError::Store`], carrying why a write would be refused.
518    fn ensure_writable(&self) -> Result<(), SecretStoreError>;
519
520    /// Reads the value back, or reports that there is none.
521    ///
522    /// `Ok(None)` is absence and is not an error; see [`SecretStoreError`].
523    ///
524    /// # Errors
525    ///
526    /// [`SecretStoreError::Load`] when the store cannot be reached, and
527    /// [`SecretStoreError::Corrupt`] when what is there is not a value this
528    /// store wrote.
529    fn load(&self) -> Result<Option<SecretString>, SecretStoreError>;
530
531    /// Removes the value.
532    ///
533    /// # Errors
534    ///
535    /// [`SecretStoreError::Delete`].
536    fn delete(&self) -> Result<Removal, SecretStoreError>;
537
538    /// What stands between the stored value and an ordinary local user.
539    ///
540    /// # Errors
541    ///
542    /// [`SecretStoreError::Inspect`].
543    fn protection(&self) -> Result<Protection, SecretStoreError>;
544}
545
546// ---------------------------------------------------------------------------
547// The platform store
548// ---------------------------------------------------------------------------
549
550/// The real store: DPAPI on Windows, a keychain on macOS, a `0600` file plus
551/// systemd credentials on Linux.
552///
553/// One type with three bodies rather than three types, because the choice is
554/// made by `cfg` at compile time and a caller never has one without the other
555/// two being impossible. The scope, by contrast, is a runtime choice and is a
556/// field.
557#[derive(Debug, Clone, PartialEq, Eq)]
558pub struct PlatformSecretStore {
559    scope: SecretScope,
560    site: sys::Site,
561}
562
563impl PlatformSecretStore {
564    /// Resolves the platform-standard location for `scope`.
565    ///
566    /// This is what production uses and what `host show` reports. Nothing is
567    /// touched on disk here; the directory, the file, and the keychain are
568    /// created by the first [`SecretStore::store`].
569    ///
570    /// # Errors
571    ///
572    /// [`SecretStoreError::Resolve`] when the platform cannot say where the
573    /// location is — no `%ProgramData%` on Windows, no home directory for this
574    /// account on the two Unixes. A service account configured with no profile
575    /// is the way that actually happens, and the message says so.
576    pub fn standard(scope: SecretScope) -> Result<Self, SecretStoreError> {
577        let site = sys::standard_site(scope).map_err(|reason| SecretStoreError::Resolve {
578            scope,
579            reason: reason.to_string(),
580        })?;
581        Ok(Self { scope, site })
582    }
583
584    /// Resolves the store the configured start mode obliges.
585    ///
586    /// The one constructor a daemon should use: it makes the store a function
587    /// of the recorded start mode rather than of whichever constructor the
588    /// call site happened to reach for.
589    ///
590    /// # Errors
591    ///
592    /// As [`PlatformSecretStore::standard`].
593    pub fn for_start_mode(mode: StartMode) -> Result<Self, SecretStoreError> {
594        Self::standard(SecretScope::for_start_mode(mode))
595    }
596
597    /// Places the same backend under a root the caller names.
598    ///
599    /// [`crate::paths::AppPaths::rooted_at`] is the precedent and the reasoning
600    /// is the same: a test needs a disposable store, and a service installed
601    /// against an explicitly configured root has to reproduce one. A relative
602    /// root stays relative, which is the caller's decision to make.
603    ///
604    /// # What a rooted store does and does not exercise
605    ///
606    /// Everything that makes a scope a scope, on two of the three platforms.
607    /// The Windows backend picks its DPAPI flag and its DACL from the scope,
608    /// not from the site, so a rooted machine-scoped store is protected and
609    /// encrypted exactly as the standard one is. The Linux backend's `0600`
610    /// file, its atomic replace, and its systemd-credential read path are the
611    /// same code under either root.
612    ///
613    /// macOS is where a rooted store is genuinely weaker, and it is worth being
614    /// precise about how. A rooted store is a keychain this program creates in
615    /// the root directory, and both scopes get one; what the standard sites
616    /// have and it does not is the *choice of keychain* — the System Keychain
617    /// against the login keychain — and the System Keychain's root-only master
618    /// key. The keychain calls themselves, the item, the not-found handling and
619    /// the delete are identical. The password protecting a rooted keychain is
620    /// [`ROOTED_KEYCHAIN_PASSWORD`], which is a constant in a public binary and
621    /// therefore protects nothing on its own: what protects a rooted keychain
622    /// is the mode of the directory it is in, which is the same protection the
623    /// Linux backend relies on for a value it stores in plaintext.
624    ///
625    /// # Errors
626    ///
627    /// [`SecretStoreError::Resolve`]; in practice it cannot fail, because the
628    /// caller supplied the root.
629    pub fn rooted_at(scope: SecretScope, root: impl AsRef<Path>) -> Result<Self, SecretStoreError> {
630        let site =
631            sys::rooted_site(scope, root.as_ref()).map_err(|reason| SecretStoreError::Resolve {
632                scope,
633                reason: reason.to_string(),
634            })?;
635        Ok(Self { scope, site })
636    }
637
638    /// The filesystem object whose access control decides who can read this
639    /// store, whether or not anything is stored yet.
640    ///
641    /// Separate from [`SecretStore::protection`] because the guard can be named
642    /// before the store exists, which is what lets `host show` report the
643    /// machine store's protection on a host that has never logged in.
644    #[must_use]
645    pub fn guard(&self) -> PathBuf {
646        sys::guard(&self.site)
647    }
648
649    /// Turns platform bytes into the value, or says why they are not one.
650    fn decode(&self, bytes: Vec<u8>) -> Result<SecretString, SecretStoreError> {
651        use secrecy::zeroize::Zeroize as _;
652
653        let length = bytes.len();
654        // A zero-length value is not a credential and cannot have been written
655        // by `store`, which refuses one. Treating it as absence would hide a
656        // truncated write behind a device-flow login.
657        if length == 0 {
658            return Err(self.corrupt("it is empty"));
659        }
660
661        match String::from_utf8(bytes) {
662            Ok(text) => Ok(SecretString::from(text)),
663            Err(error) => {
664                // The bytes are not the value, but they may still be *a* value
665                // — something else's, or a partially overwritten one. Scrub the
666                // buffer rather than dropping it, and report only its length.
667                let mut bytes = error.into_bytes();
668                bytes.zeroize();
669                Err(self.corrupt(&format!("the {length} bytes there are not valid UTF-8")))
670            }
671        }
672    }
673
674    fn corrupt(&self, detail: &str) -> SecretStoreError {
675        SecretStoreError::Corrupt {
676            scope: self.scope,
677            location: self.location(),
678            detail: detail.to_string(),
679        }
680    }
681}
682
683impl SecretStore for PlatformSecretStore {
684    fn scope(&self) -> SecretScope {
685        self.scope
686    }
687
688    fn location(&self) -> String {
689        sys::describe(&self.site)
690    }
691
692    fn store(&self, secret: &SecretString) -> Result<(), SecretStoreError> {
693        let failed = |source| SecretStoreError::Store {
694            scope: self.scope,
695            location: self.location(),
696            source,
697        };
698
699        let exposed = secret.expose_secret();
700        if exposed.is_empty() {
701            // Refused here rather than accepted and stored, because an empty
702            // value is indistinguishable from a truncated one on the way back
703            // out and `decode` has to reject it either way.
704            return Err(failed(std::io::Error::new(
705                std::io::ErrorKind::InvalidInput,
706                "an empty value is not a user access token",
707            )));
708        }
709
710        sys::store(&self.site, self.scope, exposed.as_bytes()).map_err(failed)?;
711
712        tracing::info!(
713            event = "secret_store_written",
714            scope = self.scope.as_str(),
715            "the user access token was written to the secret store"
716        );
717        Ok(())
718    }
719
720    fn ensure_writable(&self) -> Result<(), SecretStoreError> {
721        sys::ensure_writable(&self.site).map_err(|source| SecretStoreError::Store {
722            scope: self.scope,
723            location: self.location(),
724            source,
725        })
726    }
727
728    fn load(&self) -> Result<Option<SecretString>, SecretStoreError> {
729        let bytes = sys::load(&self.site, self.scope).map_err(|source| {
730            // A backend reports "there is something here and it is not ours"
731            // as `InvalidData` -- a DPAPI blob this machine's key cannot
732            // unprotect, a keychain item of the wrong shape. That is the
733            // `Corrupt` condition, not a transient read failure, and the
734            // difference decides whether a caller retries or tells the
735            // operator to run `auth logout`.
736            if source.kind() == std::io::ErrorKind::InvalidData {
737                self.corrupt(&source.to_string())
738            } else {
739                SecretStoreError::Load {
740                    scope: self.scope,
741                    location: self.location(),
742                    source,
743                }
744            }
745        })?;
746
747        match bytes {
748            Some(bytes) => self.decode(bytes).map(Some),
749            None => Ok(None),
750        }
751    }
752
753    fn delete(&self) -> Result<Removal, SecretStoreError> {
754        let removed = sys::delete(&self.site).map_err(|source| SecretStoreError::Delete {
755            scope: self.scope,
756            location: self.location(),
757            source,
758        })?;
759
760        let removal = if removed {
761            Removal::Removed
762        } else {
763            Removal::AlreadyAbsent
764        };
765
766        tracing::info!(
767            event = "secret_store_purged",
768            scope = self.scope.as_str(),
769            outcome = if removal.removed_something() {
770                "removed"
771            } else {
772                "already_absent"
773            },
774            "the user access token was purged from the secret store"
775        );
776        Ok(removal)
777    }
778
779    fn protection(&self) -> Result<Protection, SecretStoreError> {
780        let guard = sys::guard(&self.site);
781        let summary = crate::process::permissions_summary(&guard).map_err(|source| {
782            SecretStoreError::Inspect {
783                scope: self.scope,
784                guard: guard.clone(),
785                source,
786            }
787        })?;
788
789        Ok(Protection {
790            guard,
791            description: summary.description,
792            readable_by_other_local_users: summary.readable_by_other_local_users,
793        })
794    }
795}
796
797/// The password protecting a keychain created by
798/// [`PlatformSecretStore::rooted_at`] on macOS.
799///
800/// A constant in a public binary, and therefore not a secret. It is here
801/// because `SecKeychainCreate` requires *a* password and the alternative —
802/// passing `NULL` — prompts the operator for one, which a daemon must never
803/// do. What protects a rooted keychain is the mode of the directory it is
804/// created in; see [`PlatformSecretStore::rooted_at`].
805///
806/// Public so that a test can assert this is what it is, rather than discovering
807/// it by reading the backend.
808pub const ROOTED_KEYCHAIN_PASSWORD: &str = "runner-manager-rooted-keychain";
809
810/// The name of the systemd credential the Linux machine-scoped store reads
811/// before it reads its own file.
812///
813/// `05-infrastructure.md` puts the Linux machine store at *"`0600` file plus
814/// systemd credentials"*, and the second half is a read path rather than a
815/// write path: `systemd` decrypts a credential into a private `ramfs` at
816/// `$CREDENTIALS_DIRECTORY` and mounts it read-only, so a service given
817/// `LoadCredentialEncrypted=` gets the value without the file ever being
818/// readable by anything but that unit. A store that ignored it would oblige an
819/// operator who had set one up to keep a second plaintext copy on disk.
820pub const SYSTEMD_CREDENTIAL: &str = "runner-manager.user-access-token";
821
822/// The environment variable `systemd` sets for a unit that was given
823/// credentials. Read once, at [`PlatformSecretStore::standard`] time.
824pub const CREDENTIALS_DIRECTORY: &str = "CREDENTIALS_DIRECTORY";
825
826// ---------------------------------------------------------------------------
827// Platform implementations
828// ---------------------------------------------------------------------------
829//
830// Each `sys` module offers the same nine items, and the shared code above is
831// the only caller:
832//
833//   Site                        -- where one store keeps its value
834//   standard_site(scope)        -> the production location for that scope
835//   rooted_site(scope, root)    -> the same backend under a caller-named root
836//   describe(&Site)             -> what `host show` prints
837//   guard(&Site)                -> the object whose access control decides
838//                                  who can read the value
839//   store(&Site, scope, bytes)  -> write, replacing whatever was there
840//   ensure_writable(&Site)      -> refuse when `store` would be refused by
841//                                  permissions, without writing anything
842//   load(&Site, scope)          -> Ok(None) when nothing is stored;
843//                                  ErrorKind::InvalidData when something is
844//                                  stored and it is not ours
845//   delete(&Site)               -> Ok(false) when there was nothing to remove
846//
847// `standard_site` and `rooted_site` fail with a `String` rather than an
848// `io::Error`, because a resolution failure is never an `errno`: it is "this
849// account has no home directory" or "this Windows reports no %ProgramData%",
850// and both want a sentence an operator can act on.
851
852/// The stem every temporary file this module writes shares, so that a crash
853/// leaves something [`sweep_temporaries`] can recognise rather than an
854/// anonymous file beside the store.
855#[cfg(not(target_os = "macos"))]
856const TEMP_PREFIX: &str = "user-access-token.";
857
858/// Removes temporary files left in `directory` by an interrupted write.
859///
860/// Called from `delete` and not from `store`, and that order is deliberate:
861/// two `store` calls can be in flight at once and a sweep there could remove a
862/// live temporary out from under the other one. `delete` is `auth logout`,
863/// which is where "leave no remnant" is the actual requirement.
864#[cfg(not(target_os = "macos"))]
865fn sweep_temporaries(directory: &Path) {
866    let Ok(entries) = std::fs::read_dir(directory) else {
867        return;
868    };
869    for entry in entries.flatten() {
870        let name = entry.file_name();
871        let Some(name) = name.to_str() else { continue };
872        if name.starts_with(TEMP_PREFIX) && name.ends_with(".tmp") {
873            let _ = std::fs::remove_file(entry.path());
874        }
875    }
876}
877
878/// Overwrites every byte of a file with zeroes, in place.
879///
880/// Returns `Ok(false)` when there is nothing there, which is the ordinary state
881/// of a store that was never written.
882///
883/// # What this is and is not
884///
885/// It is **not** secure erasure and nothing here claims it is. The reasoning is
886/// [`crate::process::RestrictiveHandoff`]'s, unchanged: a journal, a
887/// copy-on-write snapshot, or an SSD's wear levelling can each keep a copy that
888/// an overwrite never reaches. It is here because it costs one `write` and
889/// because on Linux the stored value is at rest in **plaintext** — the mode is
890/// the whole access control there — so leaving the bytes in the block after the
891/// inode is unlinked is worse than not.
892///
893/// # Why it is a separate function, and not `cfg`-gated to Linux
894///
895/// Both are the same reason, and it is a testing reason rather than a
896/// portability one. Folded into the delete path, the zero fill was asserted by
897/// nothing: a test could only observe the file's *absence* afterwards, which is
898/// what removing it proves too, so the fill could have been deleted outright
899/// and the suite would have stayed green. Split out, the fill is observable —
900/// [`tests::overwrite_zeroes_every_byte_and_leaves_the_file_there`] reads the
901/// bytes back before anything unlinks them. Compiled on Windows as well as
902/// Linux, that test runs on every leg of the matrix rather than on one, and the
903/// Windows delete path calls it too: the value there is ciphertext, so the fill
904/// buys less, but it costs the same and it keeps the mechanism on a path a
905/// developer can execute.
906#[cfg(not(target_os = "macos"))]
907fn overwrite(path: &Path) -> std::io::Result<bool> {
908    use std::io::Write as _;
909
910    let mut file = match std::fs::OpenOptions::new().write(true).open(path) {
911        Ok(file) => file,
912        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(false),
913        Err(error) => return Err(error),
914    };
915
916    let length = usize::try_from(file.metadata()?.len()).unwrap_or(0);
917    file.write_all(&vec![0u8; length])?;
918    file.flush()?;
919    file.sync_all()?;
920    Ok(true)
921}
922
923/// Drops trailing ASCII whitespace from a value that came from outside this
924/// module.
925///
926/// There is exactly one such value: the systemd credential. `store` writes no
927/// newline and its own file is read back verbatim, deliberately — trimming
928/// there would quietly repair a corruption this store would rather report. A
929/// credential, though, is produced by the operator through `systemd-creds
930/// encrypt`, and `echo`, `printf '%s\n'`, and every text editor put a newline
931/// on the end. Used byte for byte, that newline becomes part of the token and
932/// surfaces much later inside an `Authorization` header, where it looks like a
933/// bad credential rather than a bad read.
934///
935/// Trailing only. A token has no interior whitespace, but if one ever did,
936/// silently rewriting its middle would be a worse bug than the one this fixes.
937#[cfg(not(target_os = "macos"))]
938// `allow` rather than `expect`: on Windows this is dead in a normal build and
939// live in a test build, because the test below is the whole reason it is
940// compiled there, and an `expect` that is fulfilled in only one of the two
941// configurations warns in the other.
942#[cfg_attr(
943    windows,
944    allow(
945        dead_code,
946        reason = "the systemd credential path is Linux-only; this is compiled on Windows so \
947                  that its unit test runs on every leg of the matrix rather than on the one \
948                  platform a developer usually cannot execute"
949    )
950)]
951fn trim_trailing_ascii_whitespace(mut bytes: Vec<u8>) -> Vec<u8> {
952    while bytes.last().is_some_and(u8::is_ascii_whitespace) {
953        bytes.pop();
954    }
955    bytes
956}
957
958/// Refuses when this process may not create entries in the directory a write
959/// to `target` replaces it from.
960///
961/// Both Unix backends write by creating a new file beside the old one and
962/// renaming it over. The Linux store does that itself; a keychain does it
963/// inside Security.framework, in this process -- `create
964/// /Library/Keychains/System.keychain.sb-...: Permission denied` is what an
965/// unprivileged TUI logged there. So the right to create entries in that
966/// directory is exactly the permission a write turns on.
967///
968/// The nearest directory that exists is the one asked, because `store` creates
969/// any that are missing. `AT_EACCESS` asks with the effective IDs, which are
970/// the ones the write itself would use, so a command run under `sudo` passes.
971#[cfg(unix)]
972fn ensure_directory_writable(target: &Path) -> std::io::Result<()> {
973    use std::os::unix::ffi::OsStrExt as _;
974
975    let Some(directory) = target.ancestors().skip(1).find(|path| path.is_dir()) else {
976        return Ok(());
977    };
978    let c_path =
979        std::ffi::CString::new(directory.as_os_str().as_bytes()).map_err(std::io::Error::other)?;
980    // SAFETY: `c_path` is a NUL-terminated string that outlives the call, and
981    // `faccessat` only reads it.
982    let status = unsafe {
983        libc::faccessat(
984            libc::AT_FDCWD,
985            c_path.as_ptr(),
986            libc::W_OK | libc::X_OK,
987            libc::AT_EACCESS,
988        )
989    };
990    if status == 0 {
991        return Ok(());
992    }
993    let error = std::io::Error::last_os_error();
994    Err(std::io::Error::new(
995        error.kind(),
996        format!(
997            "this account may not create files in {} ({error}), so the store would refuse the \
998             write",
999            directory.display()
1000        ),
1001    ))
1002}
1003
1004// ---------------------------------------------------------------------------
1005
1006#[cfg(windows)]
1007mod sys {
1008    //! DPAPI, in a file whose DACL is this store's real access control.
1009    //!
1010    //! # Two mechanisms, and which one is doing the work
1011    //!
1012    //! `CryptProtectData` with `CRYPTPROTECT_LOCAL_MACHINE` produces a blob
1013    //! that **any process on this machine** can unprotect. That is not a
1014    //! weakness in the flag, it is the definition of machine scope, and it is
1015    //! what D13 needs: a service starting at boot, under an account that has
1016    //! never logged in, has no user master key to decrypt with. The
1017    //! consequence is that the encryption is not the access control — the
1018    //! file's DACL is. `07-security.md` says the same thing from the other
1019    //! end: *"Stored only in the machine-scoped secret store, ACL'd to the
1020    //! service account."*
1021    //!
1022    //! The user-scoped store passes no scope flag, so the blob is bound to the
1023    //! calling account's master key. There the encryption *is* an access
1024    //! control, and the DACL is a second one.
1025    //!
1026    //! # The DACL, and why it is not `process.rs`'s
1027    //!
1028    //! [`crate::process::RestrictiveHandoff`] writes
1029    //! `D:P(A;;FA;;;BA)(A;;FA;;;<this account>)` and documents at length why it
1030    //! carries no `SY` ACE: that file's writer and reader are the same
1031    //! process, so if the process is `LocalSystem` its own SID *is* S-1-5-18
1032    //! and a separate ACE adds nothing.
1033    //!
1034    //! This store's writer and reader are **not** the same process. `auth
1035    //! login` runs as the interactive operator; the daemon runs as whatever
1036    //! `service install` registered, which defaults to `LocalSystem`. So `SY`
1037    //! is load-bearing here in exactly the case where it was redundant there,
1038    //! and the machine-scoped DACL carries it. The user-scoped DACL does not,
1039    //! because a user-scoped store is deliberately not for a service.
1040    //!
1041    //! The third trustee is `OW` — OWNER RIGHTS, S-1-3-4 — rather than the
1042    //! creating account's SID read back out of the process token. When a DACL
1043    //! contains an ACE for OWNER RIGHTS, that ACE is what the object's owner
1044    //! gets, and the owner of a file is the account that created it. So `OW`
1045    //! says "the account that ran `auth login` keeps access to what it stored"
1046    //! without opening a process token, without a `TOKEN_USER` buffer, and
1047    //! without a second copy of the SID lookup `process.rs` already carries.
1048    //!
1049    //! **That sentence held only while `auth login` was the sole writer.** Token
1050    //! renewal made the daemon a second writer under a different account, and
1051    //! because every write creates a new file, the owner moved with it and `OW`
1052    //! stopped meaning the operator. [`replacement_sddl`] is what keeps the
1053    //! grant `OW` describes from evaporating; it carries the previous owner's
1054    //! SID onto the replacement explicitly.
1055    //!
1056    //! # One store per host, and what a second operator actually gets
1057    //!
1058    //! An earlier version of this paragraph said that ownership moves with the
1059    //! write, so a second non-administrative operator's `auth login` "takes"
1060    //! the store. **That was wrong, and it was wrong in the optimistic
1061    //! direction.** Ownership does not move, because the write never lands:
1062    //! `store` finishes with a replacing rename, a replacing rename deletes the
1063    //! target, and delete is granted by `DELETE` on the file or
1064    //! `FILE_DELETE_CHILD` on its parent. `sddl(Machine)` names `SY`, `BA` and
1065    //! `OW`, none of which is operator B; and a stock `%ProgramData%` grants
1066    //! `BUILTIN\Users` only `(OI)(CI)(RX)` plus `(CI)(WD,AD,WEA,WA)` — `WD`
1067    //! lets B create the temporary, and the absence of `DC` and `DE` is what
1068    //! denies the rename. So B's `auth login` **fails**.
1069    //!
1070    //! That is the correct behaviour, and it is the behaviour this store now
1071    //! states rather than stumbles into. **The machine-scoped value is the
1072    //! host's one credential, not an operator's.** `07-security.md` counts the
1073    //! persisted credential surface and gets to one; `service install`
1074    //! registers one service reading one store; the domain has one `Host` and
1075    //! `d1`'s lock permits one agent. A second operator does not get a second
1076    //! store, and whether B may overwrite A's token is a policy question whose
1077    //! answer is "only if B is trusted with this host" — which on Windows is
1078    //! spelled *administrator*, and `BA` already grants it.
1079    //!
1080    //! What changed is the *failure*, not the policy. [`cannot_replace`] asks
1081    //! before anything is encrypted or written, so B gets a message naming the
1082    //! three ways forward instead of a bare "Access is denied" raised at the
1083    //! last step of the write, after a valid machine-decryptable blob of a live
1084    //! token has already been placed on disk.
1085    //!
1086    //! Widening the DACL was considered and rejected. The DACL is the **entire**
1087    //! access control here — a machine-scope DPAPI blob is unprotectable by any
1088    //! process on the host, by definition — so an ACE that let B replace the
1089    //! file would also let every interactive account on the machine read the
1090    //! one credential the product holds. The reader that actually matters is
1091    //! the service account, which `SY` covers by default and which `d3` grants
1092    //! explicitly when `service install` registers a least-privilege account
1093    //! instead (`05-infrastructure.md`, service behaviour, item 2).
1094    //!
1095    //! [`crate::process::permissions_summary`] reads the result back, and it
1096    //! treats an *unprotected* DACL as broadly readable because an inherited
1097    //! one is not this program's to vouch for. That is why every DACL below
1098    //! begins `D:P`, and why the file carries its own rather than inheriting
1099    //! `%ProgramData%`'s, which grants `BU` read.
1100    //!
1101    //! # Write, then replace
1102    //!
1103    //! The blob goes to a temporary file *carrying its final DACL from the
1104    //! moment it exists*, is `sync_all`ed, and is then renamed over the target.
1105    //! `process.rs` argues the first half — a file created and then tightened
1106    //! is readable for however long the gap lasts. The second half is this
1107    //! store's own: the value is long-lived, so a write interrupted half way
1108    //! must not leave a truncated blob where a whole one was.
1109
1110    use std::fs::File;
1111    use std::io::{self, Write as _};
1112    use std::os::windows::ffi::OsStrExt;
1113    use std::os::windows::io::FromRawHandle;
1114    use std::path::{Path, PathBuf};
1115
1116    use windows::Win32::Foundation::{ERROR_SUCCESS, HLOCAL, LocalFree};
1117    use windows::Win32::Security::Authorization::{
1118        ConvertSidToStringSidW, ConvertStringSecurityDescriptorToSecurityDescriptorW,
1119        GetNamedSecurityInfoW, SDDL_REVISION_1, SE_FILE_OBJECT,
1120    };
1121    use windows::Win32::Security::Cryptography::{
1122        CRYPT_INTEGER_BLOB, CRYPTPROTECT_LOCAL_MACHINE, CRYPTPROTECT_UI_FORBIDDEN,
1123        CryptProtectData, CryptUnprotectData,
1124    };
1125    use windows::Win32::Security::{
1126        OWNER_SECURITY_INFORMATION, PSECURITY_DESCRIPTOR, PSID, SECURITY_ATTRIBUTES,
1127    };
1128    use windows::Win32::Storage::FileSystem::{
1129        CREATE_NEW, CreateFileW, FILE_ATTRIBUTE_NORMAL, FILE_GENERIC_READ, FILE_GENERIC_WRITE,
1130        FILE_SHARE_NONE,
1131    };
1132    use windows::core::{PCWSTR, PWSTR};
1133
1134    use super::{
1135        APPLICATION, DIRECTORY, ITEM, ORGANIZATION, QUALIFIER, SecretScope, TEMP_PREFIX, overwrite,
1136        sweep_temporaries,
1137    };
1138
1139    /// Optional entropy mixed into every blob.
1140    ///
1141    /// **Not a secret**, and nothing here pretends otherwise: it is a constant
1142    /// in a published binary. It binds a blob to this product, so that a
1143    /// machine-scoped blob found on disk is not readable by a passing program
1144    /// that merely calls `CryptUnprotectData` with no arguments, and so that a
1145    /// value written by some future second store cannot be silently read back
1146    /// by this one. Against an attacker holding the binary it buys nothing.
1147    const ENTROPY: &[u8] = b"io.github.IvanMurzak.runner-manager/user-access-token/v1";
1148
1149    /// The file the blob lives in.
1150    #[derive(Debug, Clone, PartialEq, Eq)]
1151    pub(super) struct Site {
1152        file: PathBuf,
1153    }
1154
1155    pub(super) fn standard_site(scope: SecretScope) -> Result<Site, String> {
1156        let root = match scope {
1157            // `%ProgramData%` rather than `%LOCALAPPDATA%`, because the whole
1158            // requirement is that an account which has never logged in can
1159            // read it — and `%LOCALAPPDATA%` does not exist for such an
1160            // account until its profile is loaded.
1161            SecretScope::Machine => std::env::var_os("ProgramData")
1162                .map(PathBuf::from)
1163                .ok_or_else(|| {
1164                    "this Windows reports no %ProgramData%, so the machine-wide \
1165                     application-data directory cannot be resolved. Set ProgramData, or \
1166                     install the service with --start-at login to use the user-scoped store."
1167                        .to_string()
1168                })?
1169                .join(ORGANIZATION)
1170                .join(APPLICATION),
1171            SecretScope::User => {
1172                directories::ProjectDirs::from(QUALIFIER, ORGANIZATION, APPLICATION)
1173                    .ok_or_else(|| {
1174                        "the operating system reports no home directory for this account, so \
1175                         the user-scoped store cannot be resolved. A service account \
1176                         configured with no profile normally hits this; give the account a \
1177                         home directory, or use the machine-scoped store."
1178                            .to_string()
1179                    })?
1180                    .data_local_dir()
1181                    .to_path_buf()
1182            }
1183        };
1184        Ok(Site {
1185            file: root.join(DIRECTORY).join(format!("{ITEM}.dpapi")),
1186        })
1187    }
1188
1189    pub(super) fn rooted_site(scope: SecretScope, root: &Path) -> Result<Site, String> {
1190        // The scope is a path segment here and is not one under a standard
1191        // site, because a standard site gets its separation from the root:
1192        // `%ProgramData%` against `%LOCALAPPDATA%`. Under one caller-named root
1193        // there is no such separation, and two stores sharing a file is the
1194        // failure `the_two_variants_do_not_share_a_value` exists to catch --
1195        // `auth logout` under one scope would silently purge the other.
1196        Ok(Site {
1197            file: root
1198                .join(DIRECTORY)
1199                .join(scope.as_str())
1200                .join(format!("{ITEM}.dpapi")),
1201        })
1202    }
1203
1204    pub(super) fn describe(site: &Site) -> String {
1205        format!("DPAPI blob at {}", site.file.display())
1206    }
1207
1208    pub(super) fn guard(site: &Site) -> PathBuf {
1209        site.file.clone()
1210    }
1211
1212    /// The protected DACL a store of this scope gets, in SDDL.
1213    ///
1214    /// Split out and `pub(super)` so a test can assert the exact string rather
1215    /// than infer it from a file, and so a reviewer can read the two DACLs
1216    /// side by side.
1217    pub(super) const fn sddl(scope: SecretScope) -> &'static str {
1218        match scope {
1219            SecretScope::Machine => "D:P(A;;FA;;;SY)(A;;FA;;;BA)(A;;FA;;;OW)",
1220            SecretScope::User => "D:P(A;;FA;;;BA)(A;;FA;;;OW)",
1221        }
1222    }
1223
1224    /// The DACL a *replacement* gets: [`sddl`], plus the account that owned
1225    /// what is being replaced.
1226    ///
1227    /// # Why `OW` alone stopped being enough
1228    ///
1229    /// `OW` is OWNER RIGHTS, and the owner of a file is whoever created it.
1230    /// That made `OW` an exact statement of *"the account that ran `auth login`
1231    /// keeps access to what it stored"* for as long as `auth login` was the
1232    /// only writer.
1233    ///
1234    /// Token renewal made the daemon a second writer with a different
1235    /// identity. [`store`] finishes by renaming a temporary over the target, so
1236    /// every write creates a new file, and the new file's owner is whoever
1237    /// created it — `LocalSystem` for a daemon installed at boot. `OW` then
1238    /// resolves to `LocalSystem`, the operator matches none of the three ACEs,
1239    /// and an unelevated `auth status` cannot read the store or even its ACL.
1240    ///
1241    /// Observed on 2026-08-29: a store readable at `07:50Z` and unreadable at
1242    /// `07:58Z`, with nothing between the two but the daemon renewing its
1243    /// eight-hour token. See `docs/spikes/token-expiry-and-renewal.md`.
1244    ///
1245    /// # Why the previous owner, rather than preserving ownership
1246    ///
1247    /// Setting a new file's owner to an account that is not the caller's needs
1248    /// `SE_RESTORE_NAME`, which `LocalSystem` holds but a least-privilege
1249    /// service account — which `service install` can register — does not. An
1250    /// ACE needs nothing: the writer created the file and so may say who else
1251    /// reaches it. This works the same for both, which is why it is the
1252    /// mechanism rather than the fallback.
1253    ///
1254    /// This widens nothing. The previous owner already had full control
1255    /// through `OW`; carrying the SID forward is what keeps a grant that
1256    /// already existed from evaporating when somebody else writes.
1257    ///
1258    /// # Why the previous *grants* and not just the previous owner
1259    ///
1260    /// Carrying the owner alone survives exactly one write and then undoes
1261    /// itself. After the first renewal the owner is the daemon and the operator
1262    /// is named by an ACE; at the second renewal the owner read back is the
1263    /// *daemon's*, so a DACL rebuilt from the owner drops the operator and
1264    /// locks them out again — the same failure, deferred by eight hours. What
1265    /// is carried is therefore every SID the previous DACL granted, which
1266    /// includes the ACE the previous write added.
1267    ///
1268    /// [`carried_grants`] reads that set and drops the SIDs the constant
1269    /// already covers, so the DACL cannot grow by one ACE per write.
1270    pub(super) fn replacement_sddl(scope: SecretScope, carried: &[String]) -> String {
1271        let mut text = sddl(scope).to_owned();
1272        for sid in carried {
1273            // The SID goes in where a two-letter alias usually does; SDDL takes
1274            // either.
1275            text.push_str("(A;;FA;;;");
1276            text.push_str(sid);
1277            text.push(')');
1278        }
1279        text
1280    }
1281
1282    /// Trustees the constant DACL already grants, in both spellings.
1283    ///
1284    /// The aliases are what [`sddl`] itself writes, so re-emitting them would
1285    /// double every ACE in the base. The two SIDs are the same accounts as
1286    /// `SY` and `BA`, which is the spelling [`previous_owner`] hands back:
1287    /// `ConvertSidToStringSidW` always answers `S-1-…`, never an alias.
1288    const ALREADY_GRANTED: [&str; 5] = ["SY", "BA", "OW", "S-1-5-18", "S-1-5-32-544"];
1289
1290    /// Every account the value being replaced was readable by, so that
1291    /// replacing it does not quietly take the store away from one of them.
1292    ///
1293    /// The owner, because `OW` granted it; plus the SIDs already named
1294    /// explicitly, because those are the owners of earlier writes that this
1295    /// mechanism already rescued. Bounded by [`ALREADY_GRANTED`] and by
1296    /// deduplication: in practice the set is the one operator who signed in.
1297    ///
1298    /// Deliberately infallible, for the reason [`previous_owner`] gives.
1299    fn carried_grants(path: &Path) -> Vec<String> {
1300        // The reader `protection` already goes through, rather than a second
1301        // descriptor round trip of this module's own.
1302        let dacl = crate::process::permissions_summary(path)
1303            .map(|summary| summary.description)
1304            .unwrap_or_default();
1305        let mut grants = merge_grants(previous_owner(path).as_deref(), &dacl);
1306
1307        // ------------------------------------------------------------------
1308        // AND THIS WRITER, BY NAME, BECAUSE `OW` DOES NOT SURVIVE.
1309        // ------------------------------------------------------------------
1310        // `OW` was chosen to mean "the account that wrote this keeps access",
1311        // and it does -- until anybody changes the owner, at which point
1312        // Windows **deletes the OWNER RIGHTS ACE**. Taking ownership of the
1313        // file therefore removes the only thing granting the new owner access:
1314        // they end up owning a file they cannot read, holding `READ_CONTROL`
1315        // and `WRITE_DAC` and nothing else. Read back from a real host on
1316        // 2026-08-30, after `takeown` had been offered as the repair:
1317        //
1318        //     O:S-1-5-21-...-1001 G:SY D:P(A;;FA;;;SY)(A;;FA;;;BA)
1319        //
1320        // -- the owner is the operator, and `OW` is simply gone.
1321        //
1322        // So the writer is named by SID as well. `OW` stays because it costs
1323        // nothing and still covers the ordinary case, but nothing depends on
1324        // it any more. `ALREADY_GRANTED` drops the service accounts, so a
1325        // daemon writing under `LocalSystem` adds no ACE at all and the set
1326        // stays at the one operator who signed in.
1327        if let Ok(writer) = crate::process::current_user_sid()
1328            && !ALREADY_GRANTED.contains(&&*writer)
1329            && !grants.contains(&writer)
1330        {
1331            grants.push(writer);
1332        }
1333        grants
1334    }
1335
1336    /// [`carried_grants`] without the two reads, which is where its one rule
1337    /// lives and the only part a test can drive.
1338    ///
1339    /// A test process is one account: it cannot become `LocalSystem`, so it
1340    /// cannot make the owner move between writes, so driving [`store`] can
1341    /// never reach the case this rule exists for. Passing the owner and the
1342    /// DACL in is what makes "the writer is not the previous owner" reachable
1343    /// at all.
1344    pub(super) fn merge_grants(previous_owner: Option<&str>, previous_dacl: &str) -> Vec<String> {
1345        let mut carried: Vec<String> = Vec::new();
1346        let mut add = |sid: &str| {
1347            if !ALREADY_GRANTED.contains(&sid) && !carried.iter().any(|seen| seen == sid) {
1348                carried.push(sid.to_owned());
1349            }
1350        };
1351        if let Some(owner) = previous_owner {
1352            add(owner);
1353        }
1354        for trustee in trustees(previous_dacl) {
1355            add(&trustee);
1356        }
1357        carried
1358    }
1359
1360    /// Every trustee an SDDL DACL names, in whatever spelling it names them.
1361    ///
1362    /// An ACE is `(type;flags;rights;object;inherit;trustee)`, so the trustee is
1363    /// what follows the last `;` before the closing parenthesis.
1364    ///
1365    /// # Why not "the ones written as `S-1-…`"
1366    ///
1367    /// Because Windows does not read back what was written. A DACL built with
1368    /// `S-1-5-21-…-500` comes out of
1369    /// `ConvertSecurityDescriptorToStringSecurityDescriptorW` as `(A;;FA;;;LA)`
1370    /// — the alias for the built-in Administrator — and any other well-known
1371    /// account behaves the same way. Filtering on the `S-1-` prefix therefore
1372    /// dropped exactly the accounts whose grant had already been rescued once,
1373    /// so the carry survived one write and undid itself on the next, which is
1374    /// the bug it exists to prevent.
1375    ///
1376    /// Found by CI, whose Windows runner signs in as that account. It passes on
1377    /// an ordinary developer machine, where the operator has a plain
1378    /// `S-1-5-21-…-1001` that survives the round trip unchanged.
1379    ///
1380    /// [`ALREADY_GRANTED`] is what keeps the base DACL's own aliases out.
1381    pub(super) fn trustees(sddl: &str) -> Vec<String> {
1382        let mut found = Vec::new();
1383        for ace in sddl.split('(').skip(1) {
1384            let Some(body) = ace.split(')').next() else {
1385                continue;
1386            };
1387            let Some(trustee) = body.rsplit(';').next() else {
1388                continue;
1389            };
1390            if !trustee.is_empty() && !found.iter().any(|seen| seen == trustee) {
1391                found.push(trustee.to_owned());
1392            }
1393        }
1394        found
1395    }
1396
1397    /// The SID of the account that owns the file already there, as a string.
1398    ///
1399    /// `None` when there is no file, when its owner cannot be read, or when the
1400    /// SID will not convert — all of which mean the same thing to the caller:
1401    /// there is no previous grant to carry forward, so write [`sddl`] as it
1402    /// stands. A first write reaches this with nothing on disk and takes that
1403    /// path.
1404    ///
1405    /// Deliberately infallible. A store that refused to write because it could
1406    /// not read a security descriptor would trade a working credential for a
1407    /// tidier ACL.
1408    fn previous_owner(path: &Path) -> Option<String> {
1409        let wide = to_wide(path);
1410        let mut owner = PSID::default();
1411        let mut descriptor = PSECURITY_DESCRIPTOR(std::ptr::null_mut());
1412        // SAFETY: `wide` is NUL-terminated and outlives the call. `descriptor`
1413        // receives a LocalAlloc'd descriptor freed below on every path, and
1414        // `owner` points into it rather than owning anything itself.
1415        let status = unsafe {
1416            GetNamedSecurityInfoW(
1417                PCWSTR(wide.as_ptr()),
1418                SE_FILE_OBJECT,
1419                OWNER_SECURITY_INFORMATION,
1420                Some(&mut owner),
1421                None,
1422                None,
1423                None,
1424                &mut descriptor,
1425            )
1426        };
1427        if status != ERROR_SUCCESS {
1428            return None;
1429        }
1430
1431        let mut sid_string = PWSTR::null();
1432        // SAFETY: `owner` points into the descriptor, which is still live.
1433        let converted = unsafe { ConvertSidToStringSidW(owner, &mut sid_string) };
1434        let text = match converted {
1435            // SAFETY: the conversion succeeded, so `sid_string` is a
1436            // LocalAlloc'd NUL-terminated string, freed immediately after.
1437            Ok(()) => {
1438                let text = unsafe { sid_string.to_string() }.ok();
1439                unsafe {
1440                    let _ = LocalFree(Some(HLOCAL(sid_string.0.cast())));
1441                }
1442                text
1443            }
1444            Err(_) => None,
1445        };
1446        // SAFETY: LocalAlloc'd by `GetNamedSecurityInfoW`, freed exactly once.
1447        unsafe {
1448            let _ = LocalFree(Some(HLOCAL(descriptor.0)));
1449        }
1450        text
1451    }
1452
1453    /// The access right a replacing rename needs on the file it replaces.
1454    ///
1455    /// `MOVEFILE_REPLACE_EXISTING` deletes the target, and delete is granted by
1456    /// `DELETE` on the object or by `FILE_DELETE_CHILD` on its parent. Probing
1457    /// for it is `CreateFileW` with `dwDesiredAccess = DELETE`, which
1458    /// [`std::os::windows::fs::OpenOptionsExt::access_mode`] reaches without
1459    /// another FFI declaration.
1460    const DELETE: u32 = 0x0001_0000;
1461
1462    /// Whether this account may replace the value already in the store, and if
1463    /// not, why an operator is seeing it.
1464    ///
1465    /// Returns `None` when there is nothing there, or when the existing value
1466    /// can be replaced — the ordinary cases.
1467    ///
1468    /// # What this catches
1469    ///
1470    /// A second non-administrative operator on a shared host. Their `auth
1471    /// login` inherits `WD` (create file) from `%ProgramData%` so the temporary
1472    /// file is written happily, and then the rename over the first operator's
1473    /// file is denied, because `sddl(Machine)` names `SY`, `BA` and `OW` and
1474    /// this account is none of the three. Left to the rename, that surfaces as
1475    /// a bare "Access is denied" at the last step of the write, after a valid
1476    /// machine-decryptable blob of a real token has already been put on disk.
1477    ///
1478    /// # Why it refuses rather than widening the DACL
1479    ///
1480    /// **This product keeps one machine-scoped credential per host, and that is
1481    /// the intended model rather than an accident of the ACL.**
1482    /// `07-security.md` counts the persisted credential surface and gets to
1483    /// one; `05-infrastructure.md` has `service install` register one service
1484    /// reading one store; the domain has one `Host`, and `d1`'s single-instance
1485    /// lock allows one agent. The value is *the host's* credential, not an
1486    /// operator's — so a second operator does not get a second store, and the
1487    /// question "may B overwrite A's token" is a policy question with a policy
1488    /// answer: only if B is trusted with the host, which on Windows means
1489    /// administrator.
1490    ///
1491    /// Widening the DACL to make the rename succeed would give every
1492    /// interactive account on the machine write access to the one credential
1493    /// the product holds, and read access with it, since the DACL is the
1494    /// **entire** access control here — a machine-scope DPAPI blob is
1495    /// unprotectable by any process on the host by definition. That trade is
1496    /// not worth a smoother second login.
1497    fn cannot_replace(site: &Site) -> Option<io::Error> {
1498        use std::os::windows::fs::OpenOptionsExt as _;
1499
1500        match std::fs::OpenOptions::new()
1501            .access_mode(DELETE)
1502            .open(&site.file)
1503        {
1504            // Replaceable, or nothing there to replace.
1505            Ok(_) => None,
1506            Err(error) if error.kind() == io::ErrorKind::NotFound => None,
1507            Err(error) if error.kind() == io::ErrorKind::PermissionDenied => Some(io::Error::new(
1508                io::ErrorKind::PermissionDenied,
1509                format!(
1510                    "the machine-scoped store at {} already holds a token that belongs to \
1511                     another account on this host, and this account may not replace it. This \
1512                     product keeps one machine-scoped credential per host -- it is the host's \
1513                     credential, not an operator's -- so a second operator does not get a \
1514                     second store. Either run `auth logout` as the account that stored it, or \
1515                     run `auth login` from an elevated prompt, since the local Administrators \
1516                     group is granted access, or install the service with `--start-at login`, \
1517                     which uses the per-user store instead. Nothing was written.",
1518                    site.file.display()
1519                ),
1520            )),
1521            // Anything else is not this condition. Say nothing and let the
1522            // write report whatever it actually runs into, rather than
1523            // inventing a diagnosis from an unrelated errno.
1524            Err(_) => None,
1525        }
1526    }
1527
1528    pub(super) fn ensure_writable(site: &Site) -> io::Result<()> {
1529        if let Some(refusal) = cannot_replace(site) {
1530            return Err(refusal);
1531        }
1532        Ok(())
1533    }
1534
1535    pub(super) fn store(site: &Site, scope: SecretScope, plaintext: &[u8]) -> io::Result<()> {
1536        let directory = site
1537            .file
1538            .parent()
1539            .ok_or_else(|| io::Error::other("the store path has no parent directory"))?;
1540        std::fs::create_dir_all(directory)?;
1541
1542        // Before anything is encrypted or written. The point of asking here is
1543        // that the alternative -- finding out at the rename -- means a valid,
1544        // machine-decryptable blob of a live token has already been placed on
1545        // disk before the refusal.
1546        if let Some(refusal) = cannot_replace(site) {
1547            return Err(refusal);
1548        }
1549
1550        let blob = protect(plaintext, scope)?;
1551
1552        // Read before the temporary exists, from the file about to be replaced.
1553        // A renewal writes as the daemon's account and would otherwise take
1554        // `OW` away from the operator who signed in; see `replacement_sddl`.
1555        let descriptor = replacement_sddl(scope, &carried_grants(&site.file));
1556
1557        let temporary = directory.join(format!("{TEMP_PREFIX}{}.tmp", uuid::Uuid::new_v4()));
1558        let written = (|| -> io::Result<()> {
1559            let mut file = create_protected_file(&temporary, &descriptor)?;
1560            file.write_all(&blob)?;
1561            file.flush()?;
1562            file.sync_all()
1563        })();
1564        if let Err(error) = written {
1565            return Err(discard(&temporary, error));
1566        }
1567
1568        // `std::fs::rename` is `MoveFileExW(.., MOVEFILE_REPLACE_EXISTING)` on
1569        // Windows, so this replaces a previous token rather than failing. The
1570        // file keeps its own security descriptor across the move, so the DACL
1571        // applied at creation is the one the store ends up with.
1572        if let Err(error) = std::fs::rename(&temporary, &site.file) {
1573            // The probe above should have caught the second-operator case, but
1574            // it is a probe and this is a race: another account can take the
1575            // file between the two calls. Report the same diagnosis rather than
1576            // the bare denial, then discard the blob either way.
1577            let error = if error.kind() == io::ErrorKind::PermissionDenied {
1578                cannot_replace(site).unwrap_or(error)
1579            } else {
1580                error
1581            };
1582            return Err(discard(&temporary, error));
1583        }
1584        Ok(())
1585    }
1586
1587    /// Scrubs and removes a temporary that will not become the store, and folds
1588    /// a failure to do so into the error the caller is already returning.
1589    ///
1590    /// A temporary that survives a failed `store` is a valid machine-scope
1591    /// DPAPI blob of a real token, sitting under a name nobody looks at.
1592    /// Removing it was already the behaviour; what was missing is that the
1593    /// removal was `let _ =`, so a temporary that could *not* be removed left
1594    /// the token on disk with nothing said about it.
1595    ///
1596    /// A blanket [`sweep_temporaries`] is deliberately not used here. Its own
1597    /// documentation says why: two `store` calls can be in flight at once, and
1598    /// sweeping the directory would remove the other one's live temporary. This
1599    /// removes exactly the file this call created.
1600    fn discard(temporary: &Path, error: io::Error) -> io::Error {
1601        let _ = overwrite(temporary);
1602        match std::fs::remove_file(temporary) {
1603            Ok(()) => error,
1604            Err(removal) if removal.kind() == io::ErrorKind::NotFound => error,
1605            Err(removal) => io::Error::new(
1606                error.kind(),
1607                format!(
1608                    "{error}. A temporary file holding the encrypted token was also left at \
1609                     {} and could not be removed ({removal}); delete it by hand.",
1610                    temporary.display()
1611                ),
1612            ),
1613        }
1614    }
1615
1616    /// Why an operator cannot read a store that is theirs, and the one command
1617    /// that gives it back.
1618    ///
1619    /// # The state this diagnoses
1620    ///
1621    /// A file that exists and that this account may not read. On this store
1622    /// that has one cause: the DACL grants `SY`, `BA` and `OW`, and the owner
1623    /// is whoever wrote last. A daemon under `LocalSystem` renewing the token
1624    /// became that owner, so `OW` stopped meaning the operator.
1625    ///
1626    /// # Why it names `icacls` and not `takeown`
1627    ///
1628    /// **Because `takeown` makes it permanent.** Changing an object's owner
1629    /// makes Windows delete its OWNER RIGHTS ACE, so taking ownership removes
1630    /// the one thing that would have granted the new owner access: they end up
1631    /// owning a file they cannot read, holding `READ_CONTROL` and `WRITE_DAC`
1632    /// and nothing else.
1633    ///
1634    /// This was offered as the repair on 2026-08-30 and appeared to work,
1635    /// because the `auth status` that followed ran in the same elevated prompt
1636    /// and succeeded through `BA`. The store read back:
1637    ///
1638    /// ```text
1639    /// O:S-1-5-21-...-1001 G:SY D:P(A;;FA;;;SY)(A;;FA;;;BA)
1640    /// ```
1641    ///
1642    /// The operator owns it. `OW` is gone. An unelevated read is still denied,
1643    /// and no renewal will ever bring it back.
1644    ///
1645    /// An explicit grant has none of that: it adds an ACE, changes no owner,
1646    /// deletes nothing, and is exactly the shape [`carried_grants`] then
1647    /// preserves on every later write.
1648    ///
1649    /// # Why a message and not a repair
1650    ///
1651    /// Naming an account in the DACL of the file holding this host's
1652    /// credential is not something a status command should do under an
1653    /// operator, and the account that can is an administrator, which this
1654    /// process may not be. So it says exactly what to run.
1655    fn locked_out(site: &Site, source: &io::Error) -> io::Error {
1656        io::Error::new(
1657            io::ErrorKind::PermissionDenied,
1658            format!(
1659                "{source}. The file exists but this account may not read it, which on this \
1660                 store means its owner changed: the service renews the token under its own \
1661                 account. Grant this account access explicitly from an elevated prompt -- it \
1662                 returns immediately, and stays, because every later renewal carries the \
1663                 grant forward:\n    icacls \"{}\" /grant \"%USERNAME%:(F)\"\nDo NOT use \
1664                 `takeown`: changing the owner makes Windows delete the OWNER RIGHTS ACE, \
1665                 which leaves this account owning a file it still cannot read. An `auth \
1666                 logout` followed by `auth login`, also elevated, is the heavier alternative \
1667                 and costs a fresh sign-in.",
1668                site.file.display()
1669            ),
1670        )
1671    }
1672
1673    pub(super) fn load(site: &Site, _scope: SecretScope) -> io::Result<Option<Vec<u8>>> {
1674        let blob = match std::fs::read(&site.file) {
1675            Ok(blob) => blob,
1676            Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(None),
1677            // Asked before the bare denial is returned, for the same reason
1678            // `cannot_replace` exists on the write path: `Access is denied` on
1679            // a file the operator owns the machine of is a true statement that
1680            // helps nobody.
1681            Err(error) if error.kind() == io::ErrorKind::PermissionDenied => {
1682                return Err(locked_out(site, &error));
1683            }
1684            Err(error) => return Err(error),
1685        };
1686        if blob.is_empty() {
1687            // An empty file is the remnant of an interrupted write, not a
1688            // value. Reported as `InvalidData` so the caller says `Corrupt`
1689            // rather than starting a fresh login over the top of it.
1690            return Err(io::Error::new(
1691                io::ErrorKind::InvalidData,
1692                "the stored file is empty",
1693            ));
1694        }
1695        unprotect(&blob).map(Some)
1696    }
1697
1698    pub(super) fn delete(site: &Site) -> io::Result<bool> {
1699        // Zeroed before it is unlinked, the same as the Linux backend. The
1700        // value here is a DPAPI blob rather than plaintext, so the fill buys
1701        // less; it costs one `write`, and it keeps one mechanism instead of two
1702        // on the path the Definition of Done calls "leaves no recoverable
1703        // remnant". `overwrite`'s documentation carries the disclaimer.
1704        let _ = overwrite(&site.file);
1705
1706        let removed = match std::fs::remove_file(&site.file) {
1707            Ok(()) => true,
1708            Err(error) if error.kind() == io::ErrorKind::NotFound => false,
1709            Err(error) => return Err(error),
1710        };
1711        if let Some(directory) = site.file.parent() {
1712            sweep_temporaries(directory);
1713        }
1714        Ok(removed)
1715    }
1716
1717    // -- DPAPI ---------------------------------------------------------------
1718
1719    /// Turns a windows-rs error into one whose [`io::Error::kind`] means
1720    /// something.
1721    ///
1722    /// `Error::code()` is an **HRESULT**, and handing that straight to
1723    /// `from_raw_os_error` is what an earlier version did. It produces an
1724    /// `io::Error` whose `raw_os_error` is `0x8007_0005` rather than `5`, so
1725    /// `kind()` is `Uncategorized` and a caller cannot tell "access denied"
1726    /// from anything else — which matters here, because the second operator
1727    /// case below is precisely an access denial a caller has to recognise.
1728    ///
1729    /// The `0x8007_xxxx` range is `HRESULT_FROM_WIN32`, so its low sixteen bits
1730    /// are the Win32 code and unwrapping them restores the classification.
1731    /// Anything outside that range is not a Win32 error and is passed through
1732    /// as it is.
1733    pub(super) fn io_error(error: &windows::core::Error) -> io::Error {
1734        let code = error.code().0;
1735        if (code as u32) & 0xffff_0000 == 0x8007_0000 {
1736            io::Error::from_raw_os_error(code & 0xffff)
1737        } else {
1738            io::Error::from_raw_os_error(code)
1739        }
1740    }
1741
1742    /// A blob descriptor over a buffer the caller keeps alive.
1743    fn blob_of(bytes: &mut [u8]) -> CRYPT_INTEGER_BLOB {
1744        CRYPT_INTEGER_BLOB {
1745            cbData: u32::try_from(bytes.len()).unwrap_or(u32::MAX),
1746            pbData: bytes.as_mut_ptr(),
1747        }
1748    }
1749
1750    /// Copies `len` bytes out of `ptr` and zeroes the source.
1751    ///
1752    /// Split out from [`take_blob`] for one reason: a scrub that happens inside
1753    /// an `unsafe` block around a DPAPI buffer can be asserted by nothing —
1754    /// once `LocalFree` has run there is nothing left to look at. Given a
1755    /// pointer, the scrub is a property a test can check against a buffer Rust
1756    /// owns, which is what
1757    /// [`tests::windows::the_dpapi_buffer_is_scrubbed_before_it_is_freed`]
1758    /// does.
1759    ///
1760    /// # Safety
1761    ///
1762    /// `ptr` must be valid for reads *and writes* of `len` bytes, and must not
1763    /// be aliased for the duration of the call.
1764    pub(super) unsafe fn copy_and_scrub(ptr: *mut u8, len: usize) -> Vec<u8> {
1765        use secrecy::zeroize::Zeroize as _;
1766
1767        // SAFETY: the caller guarantees `ptr` is valid for `len` bytes and
1768        // unaliased, so one exclusive slice over it is sound.
1769        let source = unsafe { std::slice::from_raw_parts_mut(ptr, len) };
1770        let copy = source.to_vec();
1771        source.zeroize();
1772        copy
1773    }
1774
1775    /// Copies a DPAPI-allocated output blob into a `Vec`, scrubs the original,
1776    /// and frees it.
1777    ///
1778    /// The scrub is not optional and is not only for tidiness. On the
1779    /// [`unprotect`] path this buffer holds **the user access token in
1780    /// plaintext**, and `LocalFree` returns it to the process heap exactly as
1781    /// it is — where a later allocation, a crash dump, or a core file can pick
1782    /// it up. Every other copy this module makes is scrubbed (`protect`'s
1783    /// input, `decode`'s rejected bytes); this one was the exception.
1784    ///
1785    /// It runs on the [`protect`] path too, where the buffer is ciphertext and
1786    /// the scrub buys nothing. Making it conditional would buy a branch and an
1787    /// opportunity to get the condition wrong.
1788    ///
1789    /// # Safety
1790    ///
1791    /// `out` must be an output blob DPAPI filled in and that has not yet been
1792    /// freed.
1793    unsafe fn take_blob(out: &mut CRYPT_INTEGER_BLOB) -> Vec<u8> {
1794        if out.pbData.is_null() {
1795            return Vec::new();
1796        }
1797        // SAFETY: DPAPI filled this blob in, so the pointer is valid for
1798        // `cbData` bytes and nothing else holds a reference to it.
1799        let bytes = unsafe { copy_and_scrub(out.pbData, out.cbData as usize) };
1800        // SAFETY: the buffer was `LocalAlloc`ed by DPAPI and is freed once.
1801        unsafe {
1802            let _ = LocalFree(Some(HLOCAL(out.pbData.cast())));
1803        }
1804        out.pbData = std::ptr::null_mut();
1805        out.cbData = 0;
1806        bytes
1807    }
1808
1809    fn protect(plaintext: &[u8], scope: SecretScope) -> io::Result<Vec<u8>> {
1810        use secrecy::zeroize::Zeroize as _;
1811
1812        let mut input = plaintext.to_vec();
1813        let mut entropy = ENTROPY.to_vec();
1814        let input_blob = blob_of(&mut input);
1815        let entropy_blob = blob_of(&mut entropy);
1816        let mut out = CRYPT_INTEGER_BLOB::default();
1817
1818        // `CRYPTPROTECT_UI_FORBIDDEN` on both scopes, without exception. A
1819        // daemon that starts at boot has no desktop to draw a prompt on, and a
1820        // call blocked waiting for one is indistinguishable from a hang.
1821        let flags = CRYPTPROTECT_UI_FORBIDDEN
1822            | match scope {
1823                SecretScope::Machine => CRYPTPROTECT_LOCAL_MACHINE,
1824                SecretScope::User => 0,
1825            };
1826
1827        // SAFETY: both input descriptors point into `input` and `entropy`,
1828        // which outlive the call; `out` is a zeroed descriptor DPAPI fills in
1829        // and `take_blob` frees exactly once.
1830        let result = unsafe {
1831            CryptProtectData(
1832                &raw const input_blob,
1833                PCWSTR::null(),
1834                Some(&raw const entropy_blob),
1835                None,
1836                None,
1837                flags,
1838                &raw mut out,
1839            )
1840        };
1841
1842        // The plaintext copy this function made is scrubbed on every path out,
1843        // the failure path included.
1844        input.zeroize();
1845
1846        result.map_err(|error| io_error(&error))?;
1847        // SAFETY: `CryptProtectData` returned success, so `out` is a blob it
1848        // allocated and has not freed.
1849        Ok(unsafe { take_blob(&mut out) })
1850    }
1851
1852    fn unprotect(blob: &[u8]) -> io::Result<Vec<u8>> {
1853        let mut input = blob.to_vec();
1854        let mut entropy = ENTROPY.to_vec();
1855        let input_blob = blob_of(&mut input);
1856        let entropy_blob = blob_of(&mut entropy);
1857        let mut out = CRYPT_INTEGER_BLOB::default();
1858
1859        // SAFETY: as `protect`.
1860        let result = unsafe {
1861            CryptUnprotectData(
1862                &raw const input_blob,
1863                None,
1864                Some(&raw const entropy_blob),
1865                None,
1866                None,
1867                CRYPTPROTECT_UI_FORBIDDEN,
1868                &raw mut out,
1869            )
1870        };
1871
1872        match result {
1873            // SAFETY: success, so `out` is DPAPI's and unfreed.
1874            Ok(()) => Ok(unsafe { take_blob(&mut out) }),
1875            // Every way this fails means one thing to a caller: there are bytes
1876            // here and they are not a value this store can read back. Reported
1877            // as `InvalidData` so it surfaces as `Corrupt` and sends the
1878            // operator to `auth logout`, rather than as a transient read
1879            // failure that something would retry forever.
1880            Err(error) => Err(io::Error::new(
1881                io::ErrorKind::InvalidData,
1882                format!(
1883                    "the stored bytes could not be unprotected with this machine's DPAPI key \
1884                     ({error})"
1885                ),
1886            )),
1887        }
1888    }
1889
1890    // -- The protected file --------------------------------------------------
1891
1892    fn to_wide(path: &Path) -> Vec<u16> {
1893        path.as_os_str()
1894            .encode_wide()
1895            .chain(std::iter::once(0))
1896            .collect()
1897    }
1898
1899    /// Creates a new file carrying `descriptor` from the moment it exists.
1900    ///
1901    /// Takes the SDDL rather than the scope because a replacement's DACL is
1902    /// [`sddl`] plus the previous owner — see [`replacement_sddl`] — and a file
1903    /// that is created and then widened is unreadable to the account it is
1904    /// being widened for for however long the gap lasts.
1905    fn create_protected_file(path: &Path, descriptor: &str) -> io::Result<File> {
1906        let sddl_wide: Vec<u16> = descriptor
1907            .encode_utf16()
1908            .chain(std::iter::once(0))
1909            .collect();
1910
1911        let mut descriptor = PSECURITY_DESCRIPTOR(std::ptr::null_mut());
1912        // SAFETY: `sddl_wide` is a NUL-terminated UTF-16 buffer that outlives
1913        // the call, and `descriptor` receives a LocalAlloc'd descriptor freed
1914        // below on both paths.
1915        unsafe {
1916            ConvertStringSecurityDescriptorToSecurityDescriptorW(
1917                PCWSTR(sddl_wide.as_ptr()),
1918                SDDL_REVISION_1,
1919                &mut descriptor,
1920                None,
1921            )
1922        }
1923        .map_err(|error| io_error(&error))?;
1924
1925        let attributes = SECURITY_ATTRIBUTES {
1926            nLength: u32::try_from(size_of::<SECURITY_ATTRIBUTES>()).unwrap_or(u32::MAX),
1927            lpSecurityDescriptor: descriptor.0,
1928            // False: no child process this agent spawns has any business
1929            // inheriting a handle to the token.
1930            bInheritHandle: windows::core::BOOL(0),
1931        };
1932
1933        let wide = to_wide(path);
1934        // SAFETY: `wide` is NUL-terminated and outlives the call; `attributes`
1935        // points at a descriptor that is still live here.
1936        let handle = unsafe {
1937            CreateFileW(
1938                PCWSTR(wide.as_ptr()),
1939                FILE_GENERIC_READ.0 | FILE_GENERIC_WRITE.0,
1940                FILE_SHARE_NONE,
1941                Some(&raw const attributes),
1942                CREATE_NEW,
1943                FILE_ATTRIBUTE_NORMAL,
1944                None,
1945            )
1946        };
1947
1948        // SAFETY: the descriptor was LocalAlloc'd by the conversion above and
1949        // is freed exactly once, after the last use of `attributes`.
1950        unsafe {
1951            let _ = LocalFree(Some(HLOCAL(descriptor.0)));
1952        }
1953
1954        let handle = handle.map_err(|error| io_error(&error))?;
1955        // SAFETY: `CreateFileW` returned success, so the handle is a valid
1956        // owned file handle this `File` takes over.
1957        Ok(unsafe { File::from_raw_handle(handle.0) })
1958    }
1959}
1960
1961// ---------------------------------------------------------------------------
1962
1963#[cfg(target_os = "macos")]
1964mod sys {
1965    //! A generic-password item in a keychain, and which keychain is the whole
1966    //! of the scope.
1967    //!
1968    //! | scope | keychain | why |
1969    //! |---|---|---|
1970    //! | [`SecretScope::Machine`] | `/Library/Keychains/System.keychain` | the only keychain a process with no login session can open. A LaunchAgent starts at login; a LaunchDaemon starts at boot and has no user keychain to reach for. |
1971    //! | [`SecretScope::User`] | `~/Library/Keychains/login.keychain-db` | the operator's own keychain, unlocked by their login, gone when they log out. |
1972    //!
1973    //! # Why the login keychain is opened by path
1974    //!
1975    //! `SecKeychainCopyDefault` would give the account's *current* default
1976    //! keychain, which is more nearly what an operator expects. It hands back a
1977    //! handle and no path — and this store has to be able to name the file
1978    //! whose mode protects the value, both for `host show` and for
1979    //! [`super::SecretStore::protection`]. A store that could not say what
1980    //! protects it would be asserting the security property by assumption.
1981    //! So the login keychain is resolved by path, `login.keychain-db` first and
1982    //! the pre-Sierra `login.keychain` second, and an operator who has moved
1983    //! their default elsewhere gets the store in their login keychain rather
1984    //! than wherever the default now points.
1985    //!
1986    //! # User interaction is disabled around every call
1987    //!
1988    //! `SecKeychainSetUserInteractionAllowed(false)` for the duration of each
1989    //! operation, unconditionally. A daemon started at boot has no desktop, and
1990    //! a keychain call that decides to draw an unlock panel there does not fail
1991    //! — it waits. `errSecInteractionNotAllowed` returned in a second is a
1992    //! diagnosable condition; a process wedged behind an invisible dialog is
1993    //! not.
1994    //!
1995    //! # What a rooted keychain is for
1996    //!
1997    //! [`super::PlatformSecretStore::rooted_at`] creates a keychain of its own
1998    //! under the caller's root. That is the only way the suite can exercise
1999    //! this backend at all: writing to `/Library/Keychains/System.keychain`
2000    //! needs `root`, and writing to the login keychain would destroy a
2001    //! developer's real `auth login` every time the tests ran. What it covers
2002    //! and what it does not is set out on `rooted_at` itself.
2003
2004    use std::io;
2005    use std::os::unix::fs::{DirBuilderExt as _, PermissionsExt as _};
2006    use std::path::{Path, PathBuf};
2007
2008    use security_framework::os::macos::keychain::{CreateOptions, KeychainSettings, SecKeychain};
2009
2010    use super::{DIRECTORY, ITEM, KEYCHAIN_SERVICE, ROOTED_KEYCHAIN_PASSWORD, SecretScope};
2011
2012    /// The composed product identity, as a plain `&str`.
2013    ///
2014    /// [`KEYCHAIN_SERVICE`] is a `LazyLock<String>` so that it is built from
2015    /// `crate::paths`'s three segments rather than written out a second time.
2016    /// This is the one place that unwraps it, so the call sites below read as
2017    /// they did when it was a constant.
2018    pub(super) fn service() -> &'static str {
2019        &KEYCHAIN_SERVICE
2020    }
2021
2022    /// `errSecItemNotFound`. Hard-coded rather than imported because
2023    /// `security-framework-sys` is not a dependency of this workspace and
2024    /// adding one would be an A-group change for a single integer.
2025    const ERR_SEC_ITEM_NOT_FOUND: i32 = -25300;
2026    /// `errSecNoSuchKeychain`. What a keychain file that is not there answers
2027    /// with, which is absence rather than failure.
2028    const ERR_SEC_NO_SUCH_KEYCHAIN: i32 = -25294;
2029
2030    /// The System Keychain's master key. Root-only, and the reason an
2031    /// unprivileged local user cannot read a machine-scoped item even though
2032    /// the keychain database beside it is world-readable.
2033    const SYSTEM_KEYCHAIN_MASTER_KEY: &str = "/var/db/SystemKey";
2034    /// The machine-scoped keychain itself.
2035    const SYSTEM_KEYCHAIN: &str = "/Library/Keychains/System.keychain";
2036
2037    /// Which keychain, and where it is.
2038    #[derive(Debug, Clone, PartialEq, Eq)]
2039    pub(super) struct Site {
2040        path: PathBuf,
2041        kind: Kind,
2042    }
2043
2044    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
2045    enum Kind {
2046        /// The System Keychain. Written by `root`, readable by a LaunchDaemon.
2047        System,
2048        /// The operator's login keychain.
2049        Login,
2050        /// A keychain this program created under a caller-named root.
2051        Rooted,
2052    }
2053
2054    pub(super) fn standard_site(scope: SecretScope) -> Result<Site, String> {
2055        match scope {
2056            SecretScope::Machine => Ok(Site {
2057                path: PathBuf::from(SYSTEM_KEYCHAIN),
2058                kind: Kind::System,
2059            }),
2060            SecretScope::User => {
2061                let home = directories::BaseDirs::new()
2062                    .ok_or_else(|| {
2063                        "the operating system reports no home directory for this account, so \
2064                         the login keychain cannot be resolved. A service account configured \
2065                         with no profile normally hits this; use the machine-scoped store, \
2066                         which is what --start-at boot installs."
2067                            .to_string()
2068                    })?
2069                    .home_dir()
2070                    .join("Library")
2071                    .join("Keychains");
2072
2073                // Sierra renamed the login keychain and kept the old one
2074                // working, so a machine upgraded across that boundary can have
2075                // either. Prefer the modern name, fall back to the legacy one
2076                // only when it is the one that actually exists, and resolve to
2077                // the modern name when neither does so that a first `auth
2078                // login` creates the right thing.
2079                let modern = home.join("login.keychain-db");
2080                let legacy = home.join("login.keychain");
2081                let path = if modern.exists() || !legacy.exists() {
2082                    modern
2083                } else {
2084                    legacy
2085                };
2086                Ok(Site {
2087                    path,
2088                    kind: Kind::Login,
2089                })
2090            }
2091        }
2092    }
2093
2094    pub(super) fn rooted_site(scope: SecretScope, root: &Path) -> Result<Site, String> {
2095        // A keychain per scope, for the reason the Windows and Linux backends
2096        // give a directory per scope: under one caller-named root the two
2097        // stores have nothing else keeping them apart, and two stores sharing
2098        // one item means `auth logout` under either scope purges both.
2099        Ok(Site {
2100            path: root
2101                .join(DIRECTORY)
2102                .join(scope.as_str())
2103                .join("runner-manager.keychain-db"),
2104            kind: Kind::Rooted,
2105        })
2106    }
2107
2108    pub(super) fn describe(site: &Site) -> String {
2109        let kind = match site.kind {
2110            Kind::System => "System",
2111            Kind::Login => "login",
2112            Kind::Rooted => "rooted",
2113        };
2114        format!(
2115            "{kind} keychain {}, item {}/{ITEM}",
2116            site.path.display(),
2117            service()
2118        )
2119    }
2120
2121    pub(super) fn guard(site: &Site) -> PathBuf {
2122        match site.kind {
2123            // Not the keychain database. `/Library/Keychains/System.keychain`
2124            // is world-readable and its contents are encrypted; the mode that
2125            // decides who can decrypt them belongs to the master key beside it.
2126            // Reporting the database here would answer a question nobody asked,
2127            // and answer it wrongly.
2128            Kind::System => PathBuf::from(SYSTEM_KEYCHAIN_MASTER_KEY),
2129            Kind::Login | Kind::Rooted => site.path.clone(),
2130        }
2131    }
2132
2133    fn sec_error(error: &security_framework::base::Error) -> io::Error {
2134        io::Error::other(format!(
2135            "Security.framework returned {} ({error})",
2136            error.code()
2137        ))
2138    }
2139
2140    fn is_absence(error: &security_framework::base::Error) -> bool {
2141        matches!(
2142            error.code(),
2143            ERR_SEC_ITEM_NOT_FOUND | ERR_SEC_NO_SUCH_KEYCHAIN
2144        )
2145    }
2146
2147    /// `errSecDuplicateItem`, which this store only ever meets in one
2148    /// situation. See [`replace_unreadable`].
2149    const ERR_SEC_DUPLICATE_ITEM: i32 = -25299;
2150
2151    /// `errSecAuthFailed`, which a keychain answers when the item is there and
2152    /// the caller may not have it — because the ACL does not name the program
2153    /// asking, or because the keychain's master key is out of this account's
2154    /// reach. [`locked_out`] tells those two apart.
2155    const ERR_SEC_AUTH_FAILED: i32 = -25293;
2156
2157    fn is_duplicate(error: &security_framework::base::Error) -> bool {
2158        error.code() == ERR_SEC_DUPLICATE_ITEM
2159    }
2160
2161    /// Whether this process is `root`.
2162    ///
2163    /// Only ever used to *explain* a refusal, never to decide whether to
2164    /// attempt one: the keychain is the authority on what this process may
2165    /// read, and a privilege check of our own that disagreed with it would be a
2166    /// second, wrong answer.
2167    fn is_root() -> bool {
2168        // SAFETY: `geteuid` takes no argument, reads only this process's own
2169        // credentials, and is documented never to fail.
2170        unsafe { libc::geteuid() == 0 }
2171    }
2172
2173    /// Removes the item without reading it, so that a value this program may
2174    /// not read can still be replaced.
2175    ///
2176    /// # Why this is needed at all
2177    ///
2178    /// A keychain grants access per *application*, and the daemon runs a copy
2179    /// of the binary that `service install` makes. Replacing that copy — which
2180    /// is what an upgrade is — produces a program the item's ACL does not name,
2181    /// and it reads `-25293` from the credential it is supposed to own.
2182    ///
2183    /// `auth login` is the remedy, and before this it could not be: the
2184    /// crate's `set_generic_password` is
2185    ///
2186    /// ```text
2187    /// match self.find_generic_password(service, account) {
2188    ///     Ok((_, mut item)) => item.set_password(password),
2189    ///     _ => self.add_generic_password(service, account, password),
2190    /// }
2191    /// ```
2192    ///
2193    /// so a `find` refused by the ACL falls through to `add`, and `add` meets
2194    /// the item that is already there: **`-25299`**. Update refused because the
2195    /// item cannot be read, add refused because it exists. A device flow that
2196    /// had already completed then had nowhere to put the token it obtained,
2197    /// and the refresh half went with it.
2198    ///
2199    /// Watched on 2026-08-30, upgrading a real host from 0.1.12 to 0.1.15.
2200    ///
2201    /// # Why deleting works where updating does not
2202    ///
2203    /// `SecItemDelete` matches on the query — class, keychain, service,
2204    /// account — and never asks for the data, so the ACL that guards *reading*
2205    /// does not apply. The value is being replaced either way, which is the
2206    /// only reason destroying it is the right move here.
2207    fn replace_unreadable(keychain: &SecKeychain) -> io::Result<()> {
2208        delete_by_query(keychain).map_err(|error| sec_error(&error))
2209    }
2210
2211    /// The delete itself, with the platform error kept intact.
2212    ///
2213    /// Separate from [`replace_unreadable`] only so that
2214    /// [`remove_any_existing`] can read the status code rather than the
2215    /// rendered message.
2216    fn delete_by_query(keychain: &SecKeychain) -> Result<(), security_framework::base::Error> {
2217        use security_framework::item::{ItemClass, ItemSearchOptions};
2218
2219        ItemSearchOptions::new()
2220            .class(ItemClass::generic_password())
2221            .keychains(std::slice::from_ref(keychain))
2222            .service(service())
2223            .account(ITEM)
2224            .delete()
2225    }
2226
2227    /// [`replace_unreadable`], for a caller that does not know whether there is
2228    /// anything there.
2229    ///
2230    /// Nothing to delete is the ordinary state of a first `auth login`, and is
2231    /// success.
2232    fn remove_any_existing(keychain: &SecKeychain) -> io::Result<()> {
2233        match delete_by_query(keychain) {
2234            Ok(()) => Ok(()),
2235            Err(error) if is_absence(&error) => Ok(()),
2236            Err(error) => Err(sec_error(&error)),
2237        }
2238    }
2239
2240    /// Suppresses keychain UI for as long as the returned guard lives.
2241    ///
2242    /// Best effort: a platform that refuses the call is not a reason to fail
2243    /// the operation, only a reason not to have the guarantee. The guard
2244    /// re-enables interaction on drop, so this never leaks process-wide state
2245    /// past the call that took it.
2246    fn without_user_interaction()
2247    -> Option<security_framework::os::macos::keychain::KeychainUserInteractionLock> {
2248        SecKeychain::disable_user_interaction().ok()
2249    }
2250
2251    /// Opens the keychain, creating a rooted one if asked and if it is missing.
2252    ///
2253    /// `Ok(None)` means "there is no such keychain", which for a read is
2254    /// absence rather than failure.
2255    fn open(site: &Site, create_if_missing: bool) -> io::Result<Option<SecKeychain>> {
2256        match site.kind {
2257            Kind::System | Kind::Login => {
2258                if !site.path.exists() {
2259                    return Ok(None);
2260                }
2261                SecKeychain::open(&site.path)
2262                    .map(Some)
2263                    .map_err(|error| sec_error(&error))
2264            }
2265            Kind::Rooted if site.path.exists() => {
2266                let mut keychain =
2267                    SecKeychain::open(&site.path).map_err(|error| sec_error(&error))?;
2268                keychain
2269                    .unlock(Some(ROOTED_KEYCHAIN_PASSWORD))
2270                    .map_err(|error| sec_error(&error))?;
2271                Ok(Some(keychain))
2272            }
2273            Kind::Rooted if create_if_missing => Ok(Some(create_rooted(site)?)),
2274            Kind::Rooted => Ok(None),
2275        }
2276    }
2277
2278    fn create_rooted(site: &Site) -> io::Result<SecKeychain> {
2279        let directory = site
2280            .path
2281            .parent()
2282            .ok_or_else(|| io::Error::other("the rooted keychain path has no parent directory"))?;
2283        // `0700` at `mkdir(2)` time, for the reason `crate::paths` gives: a
2284        // two-step create-then-chmod leaves a window, and here the window is
2285        // over the directory whose mode is the only thing protecting a rooted
2286        // keychain.
2287        std::fs::DirBuilder::new()
2288            .mode(0o700)
2289            .recursive(true)
2290            .create(directory)?;
2291        std::fs::set_permissions(directory, std::fs::Permissions::from_mode(0o700))?;
2292
2293        let mut keychain = CreateOptions::new()
2294            .password(ROOTED_KEYCHAIN_PASSWORD)
2295            // Never. A daemon has no operator to ask.
2296            .prompt_user(false)
2297            .create(&site.path)
2298            .map_err(|error| sec_error(&error))?;
2299
2300        // No auto-lock and no lock on sleep: an agent that has been running for
2301        // a week must not find its own store locked. The keychain is inside a
2302        // `0700` directory, so locking would add nothing an unprivileged local
2303        // user could defeat anyway.
2304        let mut settings = KeychainSettings::new();
2305        settings.set_lock_on_sleep(false);
2306        settings.set_lock_interval(None);
2307        keychain
2308            .set_settings(&settings)
2309            .map_err(|error| sec_error(&error))?;
2310
2311        // `SecKeychainCreate` is documented to take a POSIX path and is
2312        // expected to use it verbatim; the `-db` suffix `security
2313        // create-keychain` is known for is appended to a *bare name*, and the
2314        // name above already carries it. If that expectation is ever wrong the
2315        // next line fails with a bare `NotFound` on a path nobody would think
2316        // to look at, so say what actually appeared instead.
2317        if !site.path.exists() {
2318            return Err(io::Error::other(format!(
2319                "SecKeychainCreate reported success but there is no keychain at {}. {} holds {:?}",
2320                site.path.display(),
2321                directory.display(),
2322                std::fs::read_dir(directory)
2323                    .map(|entries| entries
2324                        .flatten()
2325                        .map(|entry| entry.file_name().to_string_lossy().into_owned())
2326                        .collect::<Vec<_>>())
2327                    .unwrap_or_default()
2328            )));
2329        }
2330
2331        restrict(site)?;
2332        Ok(keychain)
2333    }
2334
2335    /// Sets a keychain this program created to `0600`.
2336    ///
2337    /// Called after creation *and* after every write. A keychain database is
2338    /// rewritten by `securityd` rather than by this process, and a rewrite that
2339    /// went through a fresh file would take the umask's mode rather than the
2340    /// one set at creation — which is the mode this store's entire access
2341    /// control rests on for a rooted keychain, and which
2342    /// `the_guard_is_0600_and_its_directory_is_0700` asserts.
2343    ///
2344    /// Never applied to the System or login keychains: their modes are the
2345    /// operating system's business, not this program's.
2346    fn restrict(site: &Site) -> io::Result<()> {
2347        if site.kind != Kind::Rooted {
2348            return Ok(());
2349        }
2350        std::fs::set_permissions(&site.path, std::fs::Permissions::from_mode(0o600))
2351    }
2352
2353    /// The keychain database is rewritten beside itself, in this process, so
2354    /// its directory is what a write needs. Root's alone for the System
2355    /// keychain -- which every local account can nevertheless *read* an item
2356    /// from, once it is granted to every application.
2357    pub(super) fn ensure_writable(site: &Site) -> io::Result<()> {
2358        super::ensure_directory_writable(&site.path)
2359    }
2360
2361    pub(super) fn store(site: &Site, _scope: SecretScope, plaintext: &[u8]) -> io::Result<()> {
2362        let _no_ui = without_user_interaction();
2363        let keychain = open(site, true)?.ok_or_else(|| {
2364            io::Error::new(
2365                io::ErrorKind::NotFound,
2366                format!(
2367                    "there is no keychain at {}. The machine-scoped store needs the System \
2368                     Keychain, which only root may write; the user-scoped store needs a login \
2369                     keychain, which exists once the account has logged in.",
2370                    site.path.display()
2371                ),
2372            )
2373        })?;
2374
2375        if grants_every_application(site) {
2376            // Removed and written afresh, never updated. An item keeps the
2377            // access it was created with, so `set_generic_password` on an
2378            // existing item would leave the per-binary grant in place — which
2379            // is the whole condition this branch exists to end. Deleting by
2380            // query needs no permission to *read* the value, so it works even
2381            // against an item this program is already locked out of.
2382            remove_any_existing(&keychain)?;
2383            add_granted_to_every_application(&keychain, plaintext)?;
2384        } else {
2385            match keychain.set_generic_password(service(), ITEM, plaintext) {
2386                Ok(()) => {}
2387                // The item is there and this program may not read it, so the
2388                // crate's update-or-add could do neither. Take it away and
2389                // write a fresh one, which is what replacing a credential means
2390                // anyway.
2391                Err(error) if is_duplicate(&error) => {
2392                    replace_unreadable(&keychain)?;
2393                    keychain
2394                        .set_generic_password(service(), ITEM, plaintext)
2395                        .map_err(|error| sec_error(&error))?;
2396                }
2397                Err(error) => return Err(sec_error(&error)),
2398            }
2399        }
2400
2401        restrict(site)
2402    }
2403
2404    // -----------------------------------------------------------------------
2405    // Who the stored item is granted to
2406    // -----------------------------------------------------------------------
2407
2408    /// The name the keychain shows for this grant, if it ever shows one.
2409    const ACCESS_DESCRIPTOR: &str = "runner-manager user access token";
2410
2411    /// Whether items in this keychain are granted to **every** application
2412    /// rather than to the one binary that wrote them.
2413    ///
2414    /// # The failure this ends
2415    ///
2416    /// A keychain ACL names *applications*, and identifies an unsigned one by
2417    /// its code directory hash — so replacing the binary produces a program the
2418    /// ACL does not name. Replacing the binary is what an upgrade is. The
2419    /// daemon then reads `errSecAuthFailed` from the credential it is supposed
2420    /// to own, exits `13`, and launchd restarts it every fifteen seconds
2421    /// forever; watched on a real host upgrading 0.1.12 to 0.1.15, and again on
2422    /// 0.1.16 to 0.1.17.
2423    ///
2424    /// The remedy the store used to print — sign in again, from a graphical
2425    /// terminal, with the exact copy of the binary that will do the reading —
2426    /// works exactly once, until the next upgrade. It is not a remedy, it is a
2427    /// recurring outage with instructions.
2428    ///
2429    /// # Why widening the grant costs nothing on these two keychains
2430    ///
2431    /// The per-application ACL is only a boundary where something else is
2432    /// **not** already the boundary, and on both of these it is:
2433    ///
2434    /// * The **System Keychain** is decrypted with `/var/db/SystemKey`, which
2435    ///   is `root`-only. Every process that can read an item there is already
2436    ///   `root`, and `07-security.md` records that *"a local administrator or
2437    ///   `root` can read the token"* as an accepted trade-off of machine-scoped
2438    ///   storage. Granting every application does not admit one caller that the
2439    ///   master key was keeping out.
2440    /// * A **rooted** keychain is protected by the `0700` directory it sits in
2441    ///   and is unlocked with [`ROOTED_KEYCHAIN_PASSWORD`], a constant in a
2442    ///   public binary. Its ACL was never the thing holding anybody out either.
2443    ///
2444    /// The **login** keychain is the one place where the ACL is a real boundary
2445    /// — every process running as the operator can reach that keychain, and the
2446    /// per-application grant is what stops one of them reading this token
2447    /// silently. So it keeps the default, and pays the upgrade prompt instead.
2448    /// A user-scoped host has an operator present to answer that prompt, which
2449    /// is exactly what a boot-mode daemon does not.
2450    fn grants_every_application(site: &Site) -> bool {
2451        match site.kind {
2452            Kind::System | Kind::Rooted => true,
2453            Kind::Login => false,
2454        }
2455    }
2456
2457    /// Adds the item with its access already decided.
2458    ///
2459    /// # Why not add it and then widen it
2460    ///
2461    /// That was the first shape of this, and it deadlocks a headless process.
2462    /// `SecKeychainItemSetAccess` *changes* an existing item's access control,
2463    /// which is itself an ACL-guarded operation, and macOS asks the person at
2464    /// the desk to confirm it — through Authorization Services, which
2465    /// `SecKeychainSetUserInteractionAllowed(false)` does not suppress. The
2466    /// call does not fail; `SecurityAgent` starts and the process waits behind
2467    /// a panel that a daemon has no desktop to draw on. Watched here: the store
2468    /// hung indefinitely with `SecurityAgent` running.
2469    ///
2470    /// Supplying the access at creation asks nobody anything, because there is
2471    /// no existing access to be authorized against. It is also what
2472    /// `security add-generic-password -A` does, and for the same reason.
2473    fn add_granted_to_every_application(
2474        keychain: &SecKeychain,
2475        plaintext: &[u8],
2476    ) -> io::Result<()> {
2477        use core_foundation::base::TCFType as _;
2478
2479        let access = AnyApplicationAccess::create()?;
2480
2481        // The two attributes that identify a generic password. Both buffers
2482        // outlive the call below, which is the whole of the safety argument for
2483        // the raw pointers in the attribute list.
2484        let service_name = service();
2485        let account = ITEM;
2486        let mut attributes = [
2487            ffi::SecKeychainAttribute {
2488                tag: ffi::SEC_SERVICE_ITEM_ATTR,
2489                length: u32::try_from(service_name.len()).map_err(|_| {
2490                    io::Error::other(
2491                        "the keychain service name is longer than an attribute can carry",
2492                    )
2493                })?,
2494                data: service_name.as_ptr().cast::<std::ffi::c_void>().cast_mut(),
2495            },
2496            ffi::SecKeychainAttribute {
2497                tag: ffi::SEC_ACCOUNT_ITEM_ATTR,
2498                length: u32::try_from(account.len()).map_err(|_| {
2499                    io::Error::other(
2500                        "the keychain account name is longer than an attribute can carry",
2501                    )
2502                })?,
2503                data: account.as_ptr().cast::<std::ffi::c_void>().cast_mut(),
2504            },
2505        ];
2506        let mut list = ffi::SecKeychainAttributeList {
2507            count: 2,
2508            attr: attributes.as_mut_ptr(),
2509        };
2510        let length = u32::try_from(plaintext.len())
2511            .map_err(|_| io::Error::other("the value is longer than a keychain item can hold"))?;
2512
2513        // SAFETY: the attribute list, its two buffers and `plaintext` all
2514        // outlive the call; `keychain` and `access` are live references; the
2515        // item is not asked for, so nothing is returned that must be released.
2516        let status = unsafe {
2517            ffi::SecKeychainItemCreateFromContent(
2518                ffi::SEC_GENERIC_PASSWORD_ITEM_CLASS,
2519                &raw mut list,
2520                length,
2521                plaintext.as_ptr().cast(),
2522                keychain.as_concrete_TypeRef().cast(),
2523                access.raw(),
2524                std::ptr::null_mut(),
2525            )
2526        };
2527        os_status("SecKeychainItemCreateFromContent", status)
2528    }
2529
2530    /// A `SecAccess` whose every access-control entry names no application,
2531    /// which is how Security.framework spells *"any application, without
2532    /// prompting"*.
2533    ///
2534    /// `SecAccessCreate` with a null trusted list produces the system default:
2535    /// entries naming just the calling application. Each is then rewritten with
2536    /// a null application list, keeping the prompt text and selector the system
2537    /// chose. This is what `security add-generic-password -A` does, in the same
2538    /// order.
2539    struct AnyApplicationAccess(ffi::SecAccessRef);
2540
2541    impl AnyApplicationAccess {
2542        fn create() -> io::Result<Self> {
2543            use core_foundation::base::TCFType as _;
2544            use core_foundation::string::CFString;
2545
2546            let descriptor = CFString::new(ACCESS_DESCRIPTOR);
2547            let mut access: ffi::SecAccessRef = std::ptr::null_mut();
2548            // SAFETY: `descriptor` outlives the call; a null trusted list is
2549            // the documented way to ask for the default entries; `access` is a
2550            // valid out-pointer and is only read when the call reports success.
2551            let status = unsafe {
2552                ffi::SecAccessCreate(
2553                    descriptor.as_concrete_TypeRef().cast(),
2554                    std::ptr::null(),
2555                    &raw mut access,
2556                )
2557            };
2558            os_status("SecAccessCreate", status)?;
2559            if access.is_null() {
2560                return Err(io::Error::other(
2561                    "SecAccessCreate reported success and produced no access",
2562                ));
2563            }
2564            let owned = Self(access);
2565            owned.widen()?;
2566            Ok(owned)
2567        }
2568
2569        fn raw(&self) -> ffi::SecAccessRef {
2570            self.0
2571        }
2572
2573        /// Rewrites every entry to name no application.
2574        fn widen(&self) -> io::Result<()> {
2575            let mut list: ffi::CFArrayRef = std::ptr::null();
2576            // SAFETY: `self.0` is a live `SecAccessRef`; `list` is a valid
2577            // out-pointer, and the array it returns is owned by this call.
2578            let status = unsafe { ffi::SecAccessCopyACLList(self.0, &raw mut list) };
2579            os_status("SecAccessCopyACLList", status)?;
2580            let entries = CfOwned(list.cast());
2581
2582            // SAFETY: `list` came back retained from the call above.
2583            let count = unsafe { ffi::CFArrayGetCount(list) };
2584            for index in 0..count {
2585                // SAFETY: `index` is within the count the array just reported.
2586                let entry = unsafe { ffi::CFArrayGetValueAtIndex(list, index) };
2587                if entry.is_null() {
2588                    continue;
2589                }
2590                widen_one(entry.cast_mut().cast())?;
2591            }
2592            drop(entries);
2593            Ok(())
2594        }
2595    }
2596
2597    impl Drop for AnyApplicationAccess {
2598        fn drop(&mut self) {
2599            // SAFETY: created retained by `SecAccessCreate` and released once.
2600            unsafe { ffi::CFRelease(self.0.cast_const().cast()) };
2601        }
2602    }
2603
2604    /// One access-control entry, rewritten to name no application.
2605    ///
2606    /// An entry whose contents cannot be read in this simple form is left
2607    /// alone rather than failing the store: `SecAccessCreate`'s default
2608    /// includes entries that are not application lists at all, and the one that
2609    /// decides whether the value can be decrypted is not among them.
2610    fn widen_one(entry: ffi::SecACLRef) -> io::Result<()> {
2611        let mut applications: ffi::CFArrayRef = std::ptr::null();
2612        let mut description: ffi::CFStringRef = std::ptr::null();
2613        let mut prompt: u16 = 0;
2614        // SAFETY: `entry` is an element of a live ACL array, and all three
2615        // out-pointers are valid. Anything returned is owned by this call.
2616        let copied = unsafe {
2617            ffi::SecACLCopyContents(
2618                entry,
2619                &raw mut applications,
2620                &raw mut description,
2621                &raw mut prompt,
2622            )
2623        };
2624        if copied != 0 {
2625            return Ok(());
2626        }
2627        let previous = CfOwned(applications.cast());
2628        let text = CfOwned(description.cast());
2629
2630        // SAFETY: `entry` is live and `description` is either null or a live
2631        // string; a null application list is the documented "any application".
2632        let status =
2633            unsafe { ffi::SecACLSetContents(entry, std::ptr::null(), description, prompt) };
2634        drop(previous);
2635        drop(text);
2636        os_status("SecACLSetContents", status)
2637    }
2638
2639    /// A Core Foundation value this code owns a reference to.
2640    struct CfOwned(*const std::ffi::c_void);
2641
2642    impl Drop for CfOwned {
2643        fn drop(&mut self) {
2644            if !self.0.is_null() {
2645                // SAFETY: every construction site holds a reference returned by
2646                // a `Copy`/`Create` call, and releases it exactly once.
2647                unsafe { ffi::CFRelease(self.0) };
2648            }
2649        }
2650    }
2651
2652    /// `noErr` is success; anything else is named with the call that returned
2653    /// it, because an operator reading `-25244` needs to know which of five
2654    /// calls produced it.
2655    fn os_status(call: &'static str, status: i32) -> io::Result<()> {
2656        if status == 0 {
2657            Ok(())
2658        } else {
2659            Err(io::Error::other(format!("{call} returned {status}")))
2660        }
2661    }
2662
2663    /// The Security and Core Foundation entry points that `security-framework`
2664    /// does not wrap.
2665    ///
2666    /// Declared here rather than pulled in as another dependency:
2667    /// `security-framework-sys` binds neither `SecAccess*` nor `SecACL*`, so a
2668    /// dependency would have to be a new one, for five functions whose
2669    /// signatures are fixed platform ABI.
2670    mod ffi {
2671        use std::ffi::c_void;
2672
2673        pub type CFArrayRef = *const c_void;
2674        pub type CFStringRef = *const c_void;
2675        pub type CFIndex = isize;
2676        pub type SecAccessRef = *mut c_void;
2677        pub type SecACLRef = *mut c_void;
2678        pub type SecKeychainRef = *mut c_void;
2679        pub type SecKeychainItemRef = *mut c_void;
2680
2681        /// `kSecGenericPasswordItemClass`, the four-character code `'genp'`.
2682        pub const SEC_GENERIC_PASSWORD_ITEM_CLASS: u32 = u32::from_be_bytes(*b"genp");
2683        /// `kSecServiceItemAttr`, `'svce'`.
2684        pub const SEC_SERVICE_ITEM_ATTR: u32 = u32::from_be_bytes(*b"svce");
2685        /// `kSecAccountItemAttr`, `'acct'`.
2686        pub const SEC_ACCOUNT_ITEM_ATTR: u32 = u32::from_be_bytes(*b"acct");
2687
2688        /// `SecKeychainAttribute` from `SecBase.h`.
2689        #[repr(C)]
2690        pub struct SecKeychainAttribute {
2691            pub tag: u32,
2692            pub length: u32,
2693            pub data: *mut c_void,
2694        }
2695
2696        /// `SecKeychainAttributeList` from `SecBase.h`.
2697        #[repr(C)]
2698        pub struct SecKeychainAttributeList {
2699            pub count: u32,
2700            pub attr: *mut SecKeychainAttribute,
2701        }
2702
2703        #[link(name = "CoreFoundation", kind = "framework")]
2704        unsafe extern "C" {
2705            pub fn CFRelease(value: *const c_void);
2706            pub fn CFArrayGetCount(array: CFArrayRef) -> CFIndex;
2707            pub fn CFArrayGetValueAtIndex(array: CFArrayRef, index: CFIndex) -> *const c_void;
2708        }
2709
2710        #[link(name = "Security", kind = "framework")]
2711        unsafe extern "C" {
2712            pub fn SecAccessCreate(
2713                descriptor: CFStringRef,
2714                trusted_list: CFArrayRef,
2715                access: *mut SecAccessRef,
2716            ) -> i32;
2717            pub fn SecAccessCopyACLList(access: SecAccessRef, list: *mut CFArrayRef) -> i32;
2718            pub fn SecACLCopyContents(
2719                entry: SecACLRef,
2720                applications: *mut CFArrayRef,
2721                description: *mut CFStringRef,
2722                prompt_selector: *mut u16,
2723            ) -> i32;
2724            pub fn SecACLSetContents(
2725                entry: SecACLRef,
2726                applications: CFArrayRef,
2727                description: CFStringRef,
2728                prompt_selector: u16,
2729            ) -> i32;
2730            pub fn SecKeychainItemCreateFromContent(
2731                item_class: u32,
2732                attributes: *mut SecKeychainAttributeList,
2733                length: u32,
2734                data: *const c_void,
2735                keychain: SecKeychainRef,
2736                initial_access: SecAccessRef,
2737                item: *mut SecKeychainItemRef,
2738            ) -> i32;
2739        }
2740    }
2741
2742    pub(super) fn load(site: &Site, _scope: SecretScope) -> io::Result<Option<Vec<u8>>> {
2743        let _no_ui = without_user_interaction();
2744        let Some(keychain) = open(site, false)? else {
2745            return Ok(None);
2746        };
2747
2748        match keychain.find_generic_password(service(), ITEM) {
2749            Ok((password, _item)) => Ok(Some(password.as_ref().to_vec())),
2750            Err(error) if is_absence(&error) => Ok(None),
2751            // The one refusal an operator can act on, and the one they cannot
2752            // guess. See `locked_out`.
2753            Err(error) if error.code() == ERR_SEC_AUTH_FAILED => Err(locked_out(site, &error)),
2754            Err(error) => Err(sec_error(&error)),
2755        }
2756    }
2757
2758    /// Why a keychain refuses a program its own credential, and what ends it.
2759    ///
2760    /// # Two states wear the same number
2761    ///
2762    /// `errSecAuthFailed` is what a keychain says when the item is there and
2763    /// the caller may not have it, and there are two quite different reasons
2764    /// for that. Telling an operator the wrong one sends them to a remedy that
2765    /// cannot work, so this reads which one it is instead of guessing:
2766    ///
2767    /// 1. **The account may not open the keychain at all.** The System Keychain
2768    ///    is decrypted with `/var/db/SystemKey`, which is `root`-only, so an
2769    ///    ordinary `runner-manager status` gets `-25293` on a perfectly healthy
2770    ///    credential. Nothing is broken and nothing needs repairing — the value
2771    ///    belongs to the account the boot-mode daemon runs as.
2772    /// 2. **The item was written by an older version.** Before the store
2773    ///    granted its items to every application, the ACL named the single
2774    ///    binary that wrote them, and an upgrade replaces that binary. This is
2775    ///    what took a real host down twice; [`grants_every_application`] is the
2776    ///    fix, and one more `auth login` rewrites the item so it cannot happen
2777    ///    again.
2778    fn locked_out(site: &Site, error: &security_framework::base::Error) -> io::Error {
2779        if site.kind == Kind::System && !is_root() {
2780            return io::Error::new(
2781                io::ErrorKind::PermissionDenied,
2782                format!(
2783                    "Security.framework returned {} ({error}). The machine-scoped store is the \
2784                     System Keychain, and what decrypts it is /var/db/SystemKey, which only \
2785                     root may read -- so this is what a healthy credential looks like to an \
2786                     account that is not the one holding it. The boot-mode daemon runs as root \
2787                     and reads it. Nothing here needs repairing: run this command with sudo if \
2788                     you need the value itself, or install with `--start-at login` to keep the \
2789                     token in your own login keychain instead.",
2790                    error.code()
2791                ),
2792            );
2793        }
2794        io::Error::other(format!(
2795            "Security.framework returned {} ({error}). The item is there and this keychain does \
2796             not grant it to the program asking. An earlier version granted the stored token to \
2797             the single binary that wrote it, and an upgrade replaces that binary -- so an item \
2798             written by one of those versions locks out every later copy, the daemon's included. \
2799             Signing in once more rewrites it with a grant that survives upgrades: run \
2800             `runner-manager auth login`, with sudo if this is the machine-scoped store.",
2801            error.code()
2802        ))
2803    }
2804
2805    pub(super) fn delete(site: &Site) -> io::Result<bool> {
2806        let _no_ui = without_user_interaction();
2807        let Some(keychain) = open(site, false)? else {
2808            return Ok(false);
2809        };
2810
2811        match keychain.find_generic_password(service(), ITEM) {
2812            Ok((password, item)) => {
2813                // The password buffer holds the value. Drop it before anything
2814                // else happens, so it exists for as few instructions as it can.
2815                drop(password);
2816                // `SecKeychainItem::delete` consumes the item and discards the
2817                // OSStatus, so the only way to know it worked is to look. An
2818                // `auth logout` that reported success without removing anything
2819                // would be a lie told during a credential-disclosure response.
2820                item.delete();
2821                match keychain.find_generic_password(service(), ITEM) {
2822                    Err(error) if is_absence(&error) => Ok(true),
2823                    Ok(_) => Err(io::Error::other(
2824                        "the keychain item is still present after being deleted",
2825                    )),
2826                    Err(error) => Err(sec_error(&error)),
2827                }
2828            }
2829            // `auth logout` is one of the remedies this store hands out, and
2830            // before this it could not run against the state that most needs
2831            // it: `find` is refused by the ACL, so the item this program may
2832            // not read was also an item it could not remove. The query-based
2833            // delete never asks for the data. See `replace_unreadable`.
2834            Err(error) if error.code() == ERR_SEC_AUTH_FAILED => {
2835                replace_unreadable(&keychain)?;
2836                Ok(true)
2837            }
2838            Err(error) if is_absence(&error) => Ok(false),
2839            Err(error) => Err(sec_error(&error)),
2840        }
2841    }
2842}
2843
2844// ---------------------------------------------------------------------------
2845
2846#[cfg(all(unix, not(target_os = "macos")))]
2847mod sys {
2848    //! A `0600` file, and the systemd credential that takes precedence over it.
2849    //!
2850    //! `05-infrastructure.md` gives the Linux machine store as *"`0600` file
2851    //! plus systemd credentials"*, and the two halves are not alternatives.
2852    //!
2853    //! **The file** is what `auth login` writes and what `auth logout` removes.
2854    //! It holds the token at rest with no encryption, because there is no key
2855    //! to encrypt it with that a boot-time service could also reach — a key in
2856    //! a second file is not a key, it is an indirection. Its mode is therefore
2857    //! the entire access control, which is why it is `0600` inside a `0700`
2858    //! directory and why [`super::SecretStore::protection`] reports the mode
2859    //! rather than something more reassuring.
2860    //!
2861    //! **The systemd credential** is a *read* path, not a write path. A unit
2862    //! given `LoadCredentialEncrypted=` has the value decrypted into a private
2863    //! `ramfs` at `$CREDENTIALS_DIRECTORY`, mounted read-only and visible to
2864    //! no other unit, and nothing is ever written to the agent's own disk. An
2865    //! operator who has gone to that trouble should not also have to keep a
2866    //! plaintext copy beside it, so a credential — when one is present — wins.
2867    //!
2868    //! That precedence is the reason `store` and `delete` refuse rather than
2869    //! pretend. A `store` whose value would be shadowed by the credential on
2870    //! the very next `load` has not stored anything useful, and an `auth
2871    //! logout` that removed the file while the credential kept the daemon
2872    //! authenticated would be a false negative in the one procedure —
2873    //! `05-infrastructure.md`'s credential-disclosure response — where a false
2874    //! negative is worst. Both say what the operator has to change instead.
2875    //!
2876    //! # Machine scope is `/var/lib`, and that is the point
2877    //!
2878    //! `$XDG_DATA_HOME` resolves under `$HOME`, and an account that has never
2879    //! logged in has no `$HOME` to speak of; `$XDG_RUNTIME_DIR` is cleared when
2880    //! the session ends, which is exactly the event a boot-time service starts
2881    //! before. `/var/lib/runner-manager` belongs to no session and survives a
2882    //! reboot, which is the whole requirement.
2883
2884    use std::io::{self, Write as _};
2885    use std::os::unix::fs::{DirBuilderExt as _, OpenOptionsExt as _, PermissionsExt as _};
2886    use std::path::{Path, PathBuf};
2887
2888    use super::{
2889        APPLICATION, CREDENTIALS_DIRECTORY, DIRECTORY, ITEM, ORGANIZATION, QUALIFIER,
2890        SYSTEMD_CREDENTIAL, SecretScope, TEMP_PREFIX, overwrite, sweep_temporaries,
2891        trim_trailing_ascii_whitespace,
2892    };
2893
2894    /// Where a machine-scoped store lives. Not under any home directory and
2895    /// not under any runtime directory; see the module documentation.
2896    /// The FHS directory for state a program owns; the product's own segment
2897    /// is [`APPLICATION`], not a second spelling of it.
2898    const MACHINE_PREFIX: &str = "/var/lib";
2899
2900    /// The file, and the systemd credential that outranks it.
2901    #[derive(Debug, Clone, PartialEq, Eq)]
2902    pub(super) struct Site {
2903        file: PathBuf,
2904        credential: Option<PathBuf>,
2905    }
2906
2907    impl Site {
2908        /// Points this site at a credentials directory the caller names.
2909        pub(super) fn with_credentials_directory(mut self, directory: &Path) -> Self {
2910            self.credential = Some(directory.join(SYSTEMD_CREDENTIAL));
2911            self
2912        }
2913
2914        /// The systemd credential this site would read, if it has one.
2915        pub(super) fn credential(&self) -> Option<&Path> {
2916            self.credential.as_deref()
2917        }
2918    }
2919
2920    /// `$CREDENTIALS_DIRECTORY/<name>`, when systemd set one.
2921    fn credential_from_environment() -> Option<PathBuf> {
2922        std::env::var_os(CREDENTIALS_DIRECTORY)
2923            .filter(|value| !value.is_empty())
2924            .map(|value| PathBuf::from(value).join(SYSTEMD_CREDENTIAL))
2925    }
2926
2927    pub(super) fn standard_site(scope: SecretScope) -> Result<Site, String> {
2928        match scope {
2929            SecretScope::Machine => Ok(Site {
2930                file: Path::new(MACHINE_PREFIX)
2931                    .join(APPLICATION)
2932                    .join(DIRECTORY)
2933                    .join(ITEM),
2934                credential: credential_from_environment(),
2935            }),
2936            SecretScope::User => {
2937                let root = directories::ProjectDirs::from(QUALIFIER, ORGANIZATION, APPLICATION)
2938                    .ok_or_else(|| {
2939                        "the operating system reports no home directory for this account, \
2940                             so the user-scoped store cannot be resolved. A service account \
2941                             configured with no profile normally hits this; use the \
2942                             machine-scoped store, which is what --start-at boot installs."
2943                            .to_string()
2944                    })?
2945                    .data_local_dir()
2946                    .to_path_buf();
2947                Ok(Site {
2948                    file: root.join(DIRECTORY).join(ITEM),
2949                    // A user-scoped store is deliberately not for a service, so
2950                    // it never consults a service's credentials.
2951                    credential: None,
2952                })
2953            }
2954        }
2955    }
2956
2957    pub(super) fn rooted_site(scope: SecretScope, root: &Path) -> Result<Site, String> {
2958        // A directory per scope. A standard site gets its separation from the
2959        // root -- `/var/lib` against `$XDG_DATA_HOME` -- and under one
2960        // caller-named root there is none, so two stores would share a file
2961        // and `auth logout` under either scope would purge both.
2962        Ok(Site {
2963            file: root.join(DIRECTORY).join(scope.as_str()).join(ITEM),
2964            credential: None,
2965        })
2966    }
2967
2968    pub(super) fn describe(site: &Site) -> String {
2969        match &site.credential {
2970            Some(credential) => format!(
2971                "0600 file at {} (superseded by the systemd credential at {})",
2972                site.file.display(),
2973                credential.display()
2974            ),
2975            None => format!("0600 file at {}", site.file.display()),
2976        }
2977    }
2978
2979    pub(super) fn guard(site: &Site) -> PathBuf {
2980        // The object that actually holds the value being read. When systemd
2981        // supplied one, that is its credential file in the unit's private
2982        // ramfs, and reporting the agent's own file instead would describe the
2983        // protection of something nothing reads.
2984        match &site.credential {
2985            Some(credential) if credential.exists() => credential.clone(),
2986            _ => site.file.clone(),
2987        }
2988    }
2989
2990    /// The refusal both `store` and `delete` owe an operator whose unit
2991    /// supplies a credential.
2992    fn shadowed_by_credential(site: &Site, verb: &str) -> Option<io::Error> {
2993        let credential = site.credential.as_ref()?;
2994        if !credential.exists() {
2995            return None;
2996        }
2997        Some(io::Error::other(format!(
2998            "this process was started with the systemd credential `{SYSTEMD_CREDENTIAL}`, which \
2999             takes precedence over {}. {verb} Change the credential in the unit that supplies \
3000             it -- `systemd-creds` and `LoadCredentialEncrypted=` -- and restart the service.",
3001            site.file.display()
3002        )))
3003    }
3004
3005    pub(super) fn ensure_writable(site: &Site) -> io::Result<()> {
3006        if let Some(error) = shadowed_by_credential(
3007            site,
3008            "A token written here would be shadowed by it on the very next load.",
3009        ) {
3010            return Err(error);
3011        }
3012        super::ensure_directory_writable(&site.file)
3013    }
3014
3015    pub(super) fn store(site: &Site, _scope: SecretScope, plaintext: &[u8]) -> io::Result<()> {
3016        if let Some(error) = shadowed_by_credential(
3017            site,
3018            "A token written here would be shadowed by it on the very next load, so nothing \
3019             was written.",
3020        ) {
3021            return Err(error);
3022        }
3023
3024        let directory = site
3025            .file
3026            .parent()
3027            .ok_or_else(|| io::Error::other("the store path has no parent directory"))?;
3028        // `0700` through `mkdir(2)` rather than a following `chmod`, exactly as
3029        // `crate::paths::AppPaths::create_all` argues; and then set explicitly,
3030        // because `mkdir` applies the umask and a directory that was already
3031        // there may predate this rule.
3032        std::fs::DirBuilder::new()
3033            .mode(0o700)
3034            .recursive(true)
3035            .create(directory)?;
3036        std::fs::set_permissions(directory, std::fs::Permissions::from_mode(0o700))?;
3037
3038        let temporary = directory.join(format!("{TEMP_PREFIX}{}.tmp", uuid::Uuid::new_v4()));
3039        let written = (|| -> io::Result<()> {
3040            // `mode` is applied by `open(2)`, so the file never exists at any
3041            // other permissions; `create_new` makes it exclusive, so a path
3042            // pre-created by another account is an error rather than a file
3043            // this process writes a token into.
3044            let mut file = std::fs::OpenOptions::new()
3045                .write(true)
3046                .create_new(true)
3047                .mode(0o600)
3048                .open(&temporary)?;
3049            file.write_all(plaintext)?;
3050            file.flush()?;
3051            file.sync_all()
3052        })();
3053        if let Err(error) = written {
3054            let _ = std::fs::remove_file(&temporary);
3055            return Err(error);
3056        }
3057
3058        if let Err(error) = std::fs::rename(&temporary, &site.file) {
3059            let _ = std::fs::remove_file(&temporary);
3060            return Err(error);
3061        }
3062
3063        // Best effort: without it the rename can still be lost to a power cut
3064        // that the file's own `sync_all` survived. Not worth failing a store
3065        // that has already succeeded.
3066        if let Ok(handle) = std::fs::File::open(directory) {
3067            let _ = handle.sync_all();
3068        }
3069        Ok(())
3070    }
3071
3072    pub(super) fn load(site: &Site, _scope: SecretScope) -> io::Result<Option<Vec<u8>>> {
3073        if let Some(credential) = &site.credential {
3074            match std::fs::read(credential) {
3075                // Trimmed, and *only* here. The operator produced this file
3076                // with `systemd-creds encrypt`, and `echo`, `printf '%s\n'` and
3077                // every text editor leave a newline on the end. Byte for byte
3078                // that newline becomes part of the token and fails much later
3079                // inside an `Authorization` header, where it reads as a bad
3080                // credential rather than as a bad read.
3081                Ok(bytes) => return Ok(Some(trim_trailing_ascii_whitespace(bytes))),
3082                // No credential of that name: fall through to the file. A unit
3083                // can be given credentials without being given this one.
3084                Err(error) if error.kind() == io::ErrorKind::NotFound => {}
3085                Err(error) => return Err(error),
3086            }
3087        }
3088
3089        // Not trimmed. `store` writes the value verbatim and with no newline,
3090        // so anything trailing here is corruption rather than formatting, and
3091        // silently repairing it would hide the one thing `Corrupt` exists to
3092        // report.
3093        match std::fs::read(&site.file) {
3094            Ok(bytes) => Ok(Some(bytes)),
3095            Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(None),
3096            Err(error) => Err(error),
3097        }
3098    }
3099
3100    pub(super) fn delete(site: &Site) -> io::Result<bool> {
3101        let shadowed = shadowed_by_credential(
3102            site,
3103            "The file below was removed, but the credential is still supplying a token and \
3104             this host is not purged.",
3105        );
3106
3107        let removed = overwrite_then_remove(&site.file)?;
3108        if let Some(directory) = site.file.parent() {
3109            sweep_temporaries(directory);
3110        }
3111
3112        match shadowed {
3113            Some(error) => Err(error),
3114            None => Ok(removed),
3115        }
3116    }
3117
3118    /// Zeroes the file's bytes, then unlinks it.
3119    ///
3120    /// The zero fill is [`overwrite`], which lives in the parent module so that
3121    /// it is a function a test can watch rather than a side effect inside a
3122    /// delete; its documentation carries the "best effort, not a claim"
3123    /// reasoning in full.
3124    ///
3125    /// Not being able to overwrite is not a reason to skip the unlink. The
3126    /// unlink is the part that matters, it may still succeed, and a store that
3127    /// refused to purge because it could not scrub first would fail `auth
3128    /// logout` in exactly the situation where finishing it matters most.
3129    fn overwrite_then_remove(path: &Path) -> io::Result<bool> {
3130        let _ = overwrite(path);
3131
3132        match std::fs::remove_file(path) {
3133            Ok(()) => Ok(true),
3134            Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(false),
3135            Err(error) => Err(error),
3136        }
3137    }
3138}
3139
3140// ---------------------------------------------------------------------------
3141// The Linux-only half of the surface
3142// ---------------------------------------------------------------------------
3143
3144/// Systemd credentials, which exist on no other platform.
3145///
3146/// A separate `impl` block rather than a cross-platform method that does
3147/// nothing on two of three operating systems: a caller that names
3148/// [`PlatformSecretStore::with_credentials_directory`] is writing code that
3149/// only means something on Linux, and it should not compile anywhere else.
3150#[cfg(all(unix, not(target_os = "macos")))]
3151impl PlatformSecretStore {
3152    /// Points this store at a systemd credentials directory the caller names,
3153    /// instead of the one `$CREDENTIALS_DIRECTORY` named.
3154    ///
3155    /// [`PlatformSecretStore::standard`] already reads the environment
3156    /// variable, so a daemon started by systemd needs none of this. It is here
3157    /// for the two callers that cannot use the environment: a test, which must
3158    /// not mutate a process-wide variable that every other test is reading at
3159    /// the same time, and `service install`, which resolves what the unit it is
3160    /// about to write will set.
3161    #[must_use]
3162    pub fn with_credentials_directory(mut self, directory: impl AsRef<Path>) -> Self {
3163        self.site = self.site.with_credentials_directory(directory.as_ref());
3164        self
3165    }
3166
3167    /// The systemd credential this store reads before it reads its own file,
3168    /// when it has one.
3169    #[must_use]
3170    pub fn credential_path(&self) -> Option<&Path> {
3171        self.site.credential()
3172    }
3173}
3174
3175// ---------------------------------------------------------------------------
3176// Tests
3177// ---------------------------------------------------------------------------
3178
3179#[cfg(test)]
3180mod tests {
3181    use super::*;
3182
3183    use tempfile::TempDir;
3184
3185    /// Shaped like a real `ghu_` user access token and unmistakably not one.
3186    ///
3187    /// **Assembled at run time from fragments on purpose.** The whole literal
3188    /// therefore appears in no source file and in no compiled artifact, which
3189    /// is what lets `tests/no_token_outside_the_store.rs` scan this repository
3190    /// — `target/` included — and treat any hit as a real leak rather than as
3191    /// its own test data. A `const` here, or a `concat!`, would be folded into
3192    /// the binary and would make that scan meaningless.
3193    fn fixture_token() -> SecretString {
3194        SecretString::from(format!("{}{}", "ghu_", "d2FixtureNotARealCredential000000"))
3195    }
3196
3197    /// A second, equally fake token, for the tests that need two.
3198    fn other_token() -> SecretString {
3199        SecretString::from(format!("{}{}", "ghu_", "d2SecondFixtureNotARealOne000000"))
3200    }
3201
3202    fn exposed(secret: &SecretString) -> String {
3203        secret.expose_secret().to_string()
3204    }
3205
3206    fn rooted(scope: SecretScope, root: &TempDir) -> PlatformSecretStore {
3207        PlatformSecretStore::rooted_at(scope, root.path()).expect("a rooted store resolves")
3208    }
3209
3210    fn stored(store: &PlatformSecretStore) -> String {
3211        exposed(
3212            &store
3213                .load()
3214                .expect("the store is readable")
3215                .expect("a value was stored"),
3216        )
3217    }
3218
3219    // -----------------------------------------------------------------------
3220    // The scope is a function of the start mode, in both directions
3221    // -----------------------------------------------------------------------
3222
3223    #[test]
3224    fn the_scope_is_decided_by_the_start_mode() {
3225        assert_eq!(
3226            SecretScope::for_start_mode(StartMode::Boot),
3227            SecretScope::Machine,
3228            "a service that starts at boot has no login session to read a user-scoped store from"
3229        );
3230        assert_eq!(
3231            SecretScope::for_start_mode(StartMode::Login),
3232            SecretScope::User
3233        );
3234    }
3235
3236    #[test]
3237    fn every_scope_names_the_start_mode_it_is_the_answer_to() {
3238        for scope in [SecretScope::Machine, SecretScope::User] {
3239            assert_eq!(SecretScope::for_start_mode(scope.start_mode()), scope);
3240        }
3241        for mode in [StartMode::Boot, StartMode::Login] {
3242            assert_eq!(SecretScope::for_start_mode(mode).start_mode(), mode);
3243        }
3244    }
3245
3246    #[test]
3247    fn for_start_mode_opens_the_store_that_start_mode_obliges() {
3248        for mode in [StartMode::Boot, StartMode::Login] {
3249            let Ok(store) = PlatformSecretStore::for_start_mode(mode) else {
3250                // Resolution needs a home directory or a %ProgramData%. A host
3251                // with neither cannot run the product at all, and saying so is
3252                // more useful than a panic that names neither.
3253                panic!("the standard store for --start-at {mode} could not be resolved");
3254            };
3255            assert_eq!(store.scope(), SecretScope::for_start_mode(mode));
3256        }
3257    }
3258
3259    // -----------------------------------------------------------------------
3260    // Store, load, delete -- both variants
3261    // -----------------------------------------------------------------------
3262
3263    #[test]
3264    fn a_machine_scoped_store_round_trips() {
3265        let root = TempDir::new().expect("a temporary directory");
3266        let store = rooted(SecretScope::Machine, &root);
3267        let token = fixture_token();
3268
3269        assert!(
3270            store
3271                .load()
3272                .expect("an empty store reads cleanly")
3273                .is_none(),
3274            "nothing is stored before the first `auth login`"
3275        );
3276
3277        store.store(&token).expect("the token is stored");
3278        assert_eq!(stored(&store), exposed(&token));
3279
3280        assert_eq!(
3281            store.delete().expect("the token is purged"),
3282            Removal::Removed
3283        );
3284        assert!(
3285            store
3286                .load()
3287                .expect("a purged store reads cleanly")
3288                .is_none()
3289        );
3290    }
3291
3292    #[test]
3293    fn a_user_scoped_store_round_trips() {
3294        let root = TempDir::new().expect("a temporary directory");
3295        let store = rooted(SecretScope::User, &root);
3296        let token = fixture_token();
3297
3298        store.store(&token).expect("the token is stored");
3299        assert_eq!(stored(&store), exposed(&token));
3300        assert_eq!(
3301            store.delete().expect("the token is purged"),
3302            Removal::Removed
3303        );
3304        assert!(
3305            store
3306                .load()
3307                .expect("a purged store reads cleanly")
3308                .is_none()
3309        );
3310    }
3311
3312    #[test]
3313    fn the_two_variants_do_not_share_a_value() {
3314        // Both under one root, because that is the arrangement most likely to
3315        // let them collide by accident.
3316        let root = TempDir::new().expect("a temporary directory");
3317        let machine = rooted(SecretScope::Machine, &root);
3318        let user = rooted(SecretScope::User, &root);
3319
3320        machine.store(&fixture_token()).expect("stored");
3321        user.store(&other_token()).expect("stored");
3322
3323        assert_eq!(stored(&machine), exposed(&fixture_token()));
3324        assert_eq!(stored(&user), exposed(&other_token()));
3325
3326        machine.delete().expect("purged");
3327        assert!(
3328            machine.load().expect("readable").is_none(),
3329            "the machine store is empty"
3330        );
3331        assert_eq!(
3332            stored(&user),
3333            exposed(&other_token()),
3334            "purging one variant must not purge the other; `auth logout` under \
3335             --start-at boot has no business touching a user-scoped store"
3336        );
3337    }
3338
3339    #[test]
3340    fn storing_again_replaces_the_value() {
3341        let root = TempDir::new().expect("a temporary directory");
3342        let store = rooted(SecretScope::Machine, &root);
3343
3344        store.store(&fixture_token()).expect("stored");
3345        store.store(&other_token()).expect("stored again");
3346
3347        assert_eq!(
3348            stored(&store),
3349            exposed(&other_token()),
3350            "a re-issued token replaces the old one rather than being refused"
3351        );
3352    }
3353
3354    // -----------------------------------------------------------------------
3355    // Absence is not an error, and delete is idempotent
3356    // -----------------------------------------------------------------------
3357
3358    #[test]
3359    fn a_load_after_delete_reports_absence_rather_than_a_failure() {
3360        let root = TempDir::new().expect("a temporary directory");
3361        let store = rooted(SecretScope::Machine, &root);
3362        store.store(&fixture_token()).expect("stored");
3363        store.delete().expect("purged");
3364
3365        // Phrased as a match rather than as `is_none`, because the property
3366        // under test is which *arm* the caller lands in. An `Err` here is the
3367        // failure the Definition of Done names: "a load after delete reports
3368        // absence rather than an error that a caller might mistake for a
3369        // transient failure".
3370        match store.load() {
3371            Ok(None) => {}
3372            Ok(Some(_)) => panic!("the value survived a delete"),
3373            Err(error) => panic!("absence was reported as a failure a caller may retry: {error}"),
3374        }
3375    }
3376
3377    #[test]
3378    fn deleting_what_is_not_there_is_success_and_says_so() {
3379        let root = TempDir::new().expect("a temporary directory");
3380        let store = rooted(SecretScope::Machine, &root);
3381
3382        assert_eq!(
3383            store
3384                .delete()
3385                .expect("purging an empty store is not a failure"),
3386            Removal::AlreadyAbsent,
3387            "`auth logout` is run on every host during a credential-disclosure \
3388             response, including the ones that were never logged in"
3389        );
3390
3391        store.store(&fixture_token()).expect("stored");
3392        assert_eq!(store.delete().expect("purged"), Removal::Removed);
3393        assert_eq!(
3394            store.delete().expect("purged again"),
3395            Removal::AlreadyAbsent
3396        );
3397    }
3398
3399    /// "Delete leaves no recoverable remnant" — stated as the same property on
3400    /// all three platforms, and checked through whatever *carries* the value on
3401    /// each.
3402    ///
3403    /// The distinction is one CI caught rather than one this test was written
3404    /// with. A file-backed store keeps the value *in* the guard, so the remnant
3405    /// question is "is the file gone". A keychain-backed store keeps it in an
3406    /// item *inside* the guard, and the guard is a database that legitimately
3407    /// outlives every item it ever held — asserting the keychain disappears
3408    /// would be asserting that `auth logout` deletes the operator's login
3409    /// keychain, which would be a bug rather than a purge.
3410    ///
3411    /// So the two things asserted everywhere are the two that mean the same
3412    /// thing everywhere: nothing readable is left, and no byte of the value is
3413    /// lying in whatever does remain.
3414    #[test]
3415    fn deleting_leaves_no_recoverable_remnant() {
3416        let root = TempDir::new().expect("a temporary directory");
3417        let store = rooted(SecretScope::Machine, &root);
3418        store.store(&fixture_token()).expect("stored");
3419
3420        let guard = store.guard();
3421        assert!(guard.exists(), "something was written");
3422        store.delete().expect("purged");
3423
3424        assert!(
3425            store
3426                .load()
3427                .expect("a purged store reads cleanly")
3428                .is_none(),
3429            "the value is still readable through the store's own API"
3430        );
3431        if let Ok(bytes) = std::fs::read(&guard) {
3432            let token = exposed(&fixture_token());
3433            assert!(
3434                !bytes
3435                    .windows(token.len())
3436                    .any(|window| window == token.as_bytes()),
3437                "the value is still lying in {} after a purge",
3438                guard.display()
3439            );
3440        }
3441
3442        // A file-backed store additionally leaves nothing at all: neither the
3443        // named file, nor a `user-access-token.<uuid>.tmp` from a write that was
3444        // interrupted, which is the token on disk under a name nobody looks at.
3445        #[cfg(not(target_os = "macos"))]
3446        {
3447            assert!(
3448                !guard.exists(),
3449                "the stored value is still at {}",
3450                guard.display()
3451            );
3452            if let Some(directory) = guard.parent()
3453                && let Ok(entries) = std::fs::read_dir(directory)
3454            {
3455                let remnants: Vec<_> = entries
3456                    .flatten()
3457                    .map(|entry| entry.file_name().to_string_lossy().into_owned())
3458                    .filter(|name| name.starts_with("user-access-token"))
3459                    .collect();
3460                assert!(
3461                    remnants.is_empty(),
3462                    "a purge left {remnants:?} in {}",
3463                    directory.display()
3464                );
3465            }
3466        }
3467    }
3468
3469    /// The negative control for the sweep above: a temporary file from an
3470    /// interrupted write is a token on disk, and a purge that only removed the
3471    /// file it was told about would leave it there.
3472    #[cfg(not(target_os = "macos"))]
3473    #[test]
3474    fn a_purge_sweeps_a_temporary_left_by_an_interrupted_write() {
3475        let root = TempDir::new().expect("a temporary directory");
3476        let store = rooted(SecretScope::Machine, &root);
3477        store.store(&fixture_token()).expect("stored");
3478
3479        let directory = store
3480            .guard()
3481            .parent()
3482            .expect("the guard has a directory")
3483            .to_path_buf();
3484        let abandoned = directory.join(format!("{TEMP_PREFIX}00000000-dead-beef.tmp"));
3485        std::fs::write(&abandoned, exposed(&fixture_token())).expect("planted");
3486
3487        store.delete().expect("purged");
3488        assert!(
3489            !abandoned.exists(),
3490            "a purge left {} behind, which is the token on disk under a name nobody looks at",
3491            abandoned.display()
3492        );
3493    }
3494
3495    // -----------------------------------------------------------------------
3496    // What is not a value
3497    // -----------------------------------------------------------------------
3498
3499    #[test]
3500    fn an_empty_value_is_refused_rather_than_stored() {
3501        let root = TempDir::new().expect("a temporary directory");
3502        let store = rooted(SecretScope::Machine, &root);
3503
3504        let error = store
3505            .store(&SecretString::from(String::new()))
3506            .expect_err("an empty value is not a token");
3507        assert!(matches!(error, SecretStoreError::Store { .. }));
3508        assert!(
3509            store.load().expect("readable").is_none(),
3510            "a refused store wrote nothing"
3511        );
3512    }
3513
3514    #[test]
3515    fn bytes_that_are_not_a_token_are_reported_as_corrupt_and_not_as_absence() {
3516        let root = TempDir::new().expect("a temporary directory");
3517        let store = rooted(SecretScope::Machine, &root);
3518
3519        // `decode` is the half of the read path that is the same on all three
3520        // operating systems, and it is reachable here because these tests are a
3521        // child of the module. The platform half -- a DPAPI blob this key
3522        // cannot unprotect, a file of raw bytes -- is exercised per OS below.
3523        let error = store
3524            .decode(vec![0xff, 0xfe, 0xfd])
3525            .expect_err("invalid UTF-8 is not a token");
3526        assert!(
3527            matches!(error, SecretStoreError::Corrupt { .. }),
3528            "got {error:?}"
3529        );
3530        assert!(
3531            store.decode(Vec::new()).is_err(),
3532            "an empty value read back is a truncated write, not an absence"
3533        );
3534    }
3535
3536    #[test]
3537    fn no_error_this_module_produces_repeats_the_value() {
3538        let root = TempDir::new().expect("a temporary directory");
3539        let store = rooted(SecretScope::Machine, &root);
3540        let token = exposed(&fixture_token());
3541
3542        // Every variant that can be constructed without a real platform
3543        // failure, rendered both ways a caller can render one.
3544        let errors = vec![
3545            SecretStoreError::Resolve {
3546                scope: SecretScope::Machine,
3547                reason: "no %ProgramData%".to_string(),
3548            },
3549            store
3550                .store(&SecretString::from(String::new()))
3551                .expect_err("empty is refused"),
3552            store
3553                .decode(token.clone().into_bytes())
3554                .err()
3555                .unwrap_or_else(|| store.corrupt("a placeholder")),
3556            store.corrupt("the bytes there are not valid UTF-8"),
3557            store
3558                .protection()
3559                .err()
3560                .unwrap_or_else(|| SecretStoreError::Inspect {
3561                    scope: SecretScope::Machine,
3562                    guard: store.guard(),
3563                    source: crate::process::permissions_summary(std::path::Path::new(
3564                        "a-path-that-is-not-there",
3565                    ))
3566                    .expect_err("a missing path cannot be inspected"),
3567                }),
3568        ];
3569
3570        for error in errors {
3571            let rendered = format!("{error} / {error:?}");
3572            assert!(
3573                !rendered.contains(&token),
3574                "an error rendered the token: {rendered}"
3575            );
3576        }
3577    }
3578
3579    // -----------------------------------------------------------------------
3580    // Who can read it
3581    // -----------------------------------------------------------------------
3582
3583    #[test]
3584    fn a_stored_value_is_not_readable_by_an_unprivileged_local_user() {
3585        for scope in [SecretScope::Machine, SecretScope::User] {
3586            let root = TempDir::new().expect("a temporary directory");
3587            let store = rooted(scope, &root);
3588            store.store(&fixture_token()).expect("stored");
3589
3590            let protection = store.protection().expect("the guard is inspectable");
3591            assert!(
3592                !protection.readable_by_other_local_users(),
3593                "the {scope}-scoped store is readable by other local users: {protection}"
3594            );
3595        }
3596    }
3597
3598    /// The negative control for the assertion above.
3599    ///
3600    /// A check that only ever returns "not readable" proves nothing, and this
3601    /// one is the whole of the evidence for a `security_critical` Definition of
3602    /// Done item. So: take a store that has just passed, loosen the one thing
3603    /// that was protecting it, and require the same call to report the leak.
3604    #[test]
3605    fn the_readability_check_reports_a_guard_that_was_loosened() {
3606        let root = TempDir::new().expect("a temporary directory");
3607        let store = rooted(SecretScope::Machine, &root);
3608        store.store(&fixture_token()).expect("stored");
3609        let guard = store.guard();
3610
3611        assert!(
3612            !store
3613                .protection()
3614                .expect("inspectable")
3615                .readable_by_other_local_users(),
3616            "the control starts from a store that passes"
3617        );
3618
3619        #[cfg(unix)]
3620        {
3621            use std::os::unix::fs::PermissionsExt as _;
3622            std::fs::set_permissions(&guard, std::fs::Permissions::from_mode(0o644))
3623                .expect("the guard can be loosened");
3624        }
3625        #[cfg(windows)]
3626        {
3627            // Replaced with a file created the ordinary way, which inherits the
3628            // temporary directory's DACL instead of carrying a protected one of
3629            // its own. That is exactly the mistake the backend exists to avoid:
3630            // an inherited DACL is whatever the parent grants, and under
3631            // `%ProgramData%` the parent grants Builtin Users read.
3632            std::fs::remove_file(&guard).expect("the guard can be replaced");
3633            std::fs::write(&guard, b"not a protected file").expect("written");
3634        }
3635
3636        let protection = store
3637            .protection()
3638            .expect("the loosened guard is still inspectable");
3639        assert!(
3640            protection.readable_by_other_local_users(),
3641            "a loosened guard was reported as safe, so the assertion above proves nothing: \
3642             {protection}"
3643        );
3644    }
3645
3646    #[test]
3647    fn the_protection_names_the_object_it_inspected() {
3648        let root = TempDir::new().expect("a temporary directory");
3649        let store = rooted(SecretScope::Machine, &root);
3650        store.store(&fixture_token()).expect("stored");
3651
3652        let protection = store.protection().expect("inspectable");
3653        assert_eq!(protection.guard(), store.guard());
3654        assert!(
3655            !protection.description().is_empty(),
3656            "a protection with no description is not a diagnosis"
3657        );
3658    }
3659
3660    // -----------------------------------------------------------------------
3661    // What `host show` and `service status` print
3662    // -----------------------------------------------------------------------
3663
3664    #[test]
3665    fn the_active_store_is_reported_and_agrees_with_the_start_mode() {
3666        let root = TempDir::new().expect("a temporary directory");
3667
3668        for mode in [StartMode::Boot, StartMode::Login] {
3669            let store = rooted(SecretScope::for_start_mode(mode), &root);
3670            let active = ActiveStore::of(&store, mode);
3671
3672            assert_eq!(active.scope(), SecretScope::for_start_mode(mode));
3673            assert_eq!(active.start_mode(), mode);
3674            assert!(active.agrees_with_start_mode());
3675
3676            let rendered = active.to_string();
3677            assert!(
3678                rendered.contains(active.scope().as_str()),
3679                "`host show` must name the variant in use: {rendered}"
3680            );
3681            assert!(
3682                rendered.contains(&mode.to_string()),
3683                "`service status` must name the start mode: {rendered}"
3684            );
3685            assert!(
3686                !rendered.contains("MISMATCH"),
3687                "a matching pair must not be reported as a mismatch: {rendered}"
3688            );
3689        }
3690    }
3691
3692    #[test]
3693    fn a_store_that_disagrees_with_the_start_mode_says_so() {
3694        let root = TempDir::new().expect("a temporary directory");
3695        // The failure this exists to catch: a service switched to `--start-at
3696        // boot` while the token is still in the operator's user-scoped store.
3697        // The daemon starts, finds nothing, and the only clue is here.
3698        let store = rooted(SecretScope::User, &root);
3699        let active = ActiveStore::of(&store, StartMode::Boot);
3700
3701        assert!(!active.agrees_with_start_mode());
3702        let rendered = active.to_string();
3703        assert!(rendered.contains("MISMATCH"), "{rendered}");
3704        assert!(rendered.contains("machine"), "{rendered}");
3705    }
3706
3707    #[test]
3708    fn the_reported_location_is_not_the_value() {
3709        let root = TempDir::new().expect("a temporary directory");
3710        let store = rooted(SecretScope::Machine, &root);
3711        store.store(&fixture_token()).expect("stored");
3712
3713        let token = exposed(&fixture_token());
3714        let active = ActiveStore::of(&store, StartMode::Boot);
3715        for rendered in [
3716            store.location(),
3717            active.to_string(),
3718            format!("{store:?}"),
3719            format!("{active:?}"),
3720            store.protection().expect("inspectable").to_string(),
3721        ] {
3722            assert!(
3723                !rendered.contains(&token),
3724                "a report carried the value: {rendered}"
3725            );
3726        }
3727    }
3728
3729    // -----------------------------------------------------------------------
3730    // Where the standard stores live
3731    // -----------------------------------------------------------------------
3732
3733    /// The property that makes a machine-scoped store readable after a reboot,
3734    /// stated as a location rather than as a hope.
3735    ///
3736    /// Every per-user directory on all three operating systems hangs off the
3737    /// account's home directory, and an account that has never logged in does
3738    /// not have one mounted. A machine store under `$HOME` would work on the
3739    /// developer's laptop and fail on the first boot of the machine it was
3740    /// installed on, which is the failure this assertion exists to make
3741    /// impossible to introduce.
3742    #[test]
3743    fn the_machine_store_is_not_under_the_home_directory() {
3744        let store = PlatformSecretStore::standard(SecretScope::Machine)
3745            .expect("the machine store resolves");
3746        let guard = store.guard();
3747
3748        let Some(base) = directories::BaseDirs::new() else {
3749            panic!("this account has no home directory, so the assertion cannot be made");
3750        };
3751        assert!(
3752            !guard.starts_with(base.home_dir()),
3753            "the machine store at {} is under the home directory {}",
3754            guard.display(),
3755            base.home_dir().display()
3756        );
3757    }
3758
3759    #[test]
3760    fn the_user_store_is_under_the_home_directory() {
3761        // The mirror of the assertion above, and the reason `--start-at login`
3762        // means what it says: this store is gone when the operator logs out.
3763        let store =
3764            PlatformSecretStore::standard(SecretScope::User).expect("the user store resolves");
3765        let Some(base) = directories::BaseDirs::new() else {
3766            panic!("this account has no home directory, so the assertion cannot be made");
3767        };
3768        assert!(
3769            store.guard().starts_with(base.home_dir()),
3770            "the user store at {} is not under the home directory {}",
3771            store.guard().display(),
3772            base.home_dir().display()
3773        );
3774    }
3775
3776    #[test]
3777    fn the_standard_locations_are_the_documented_ones() {
3778        let machine = PlatformSecretStore::standard(SecretScope::Machine).expect("resolves");
3779        let user = PlatformSecretStore::standard(SecretScope::User).expect("resolves");
3780
3781        #[cfg(windows)]
3782        {
3783            let program_data = std::path::PathBuf::from(
3784                std::env::var_os("ProgramData").expect("Windows sets ProgramData"),
3785            );
3786            assert_eq!(
3787                machine.guard(),
3788                program_data
3789                    .join("IvanMurzak")
3790                    .join("runner-manager")
3791                    .join("secrets")
3792                    .join("user-access-token.dpapi")
3793            );
3794            assert!(user.location().contains("DPAPI"), "{}", user.location());
3795        }
3796        #[cfg(target_os = "macos")]
3797        {
3798            assert_eq!(
3799                machine.guard(),
3800                std::path::Path::new("/var/db/SystemKey"),
3801                "the System Keychain's protection is its root-only master key, \
3802                 not the world-readable database beside it"
3803            );
3804            assert!(
3805                machine
3806                    .location()
3807                    .contains("/Library/Keychains/System.keychain"),
3808                "{}",
3809                machine.location()
3810            );
3811            assert!(
3812                user.location().contains("login.keychain"),
3813                "{}",
3814                user.location()
3815            );
3816        }
3817        #[cfg(all(unix, not(target_os = "macos")))]
3818        {
3819            assert_eq!(
3820                machine.guard(),
3821                std::path::Path::new("/var/lib/runner-manager/secrets/user-access-token")
3822            );
3823            assert!(user.location().contains("0600"), "{}", user.location());
3824        }
3825    }
3826
3827    /// The independent oracle for the identity `secrets.rs` now *borrows* from
3828    /// [`crate::paths`] rather than spelling out.
3829    ///
3830    /// Borrowing removes the drift the reviewer named — two files can no longer
3831    /// disagree — but it moves the risk rather than removing it: a change in
3832    /// `paths.rs` now silently moves the secret store as well as the four
3833    /// application-data directories, and a token that has moved reads as simply
3834    /// absent. The literals below are the only place in this module that is
3835    /// *not* derived from those constants, which is exactly what makes them
3836    /// able to catch such a change.
3837    #[test]
3838    fn the_product_identity_is_the_one_paths_defines() {
3839        assert_eq!(QUALIFIER, "io.github");
3840        assert_eq!(ORGANIZATION, "IvanMurzak");
3841        assert_eq!(APPLICATION, "runner-manager");
3842
3843        #[cfg(target_os = "macos")]
3844        assert_eq!(
3845            sys::service(),
3846            "io.github.IvanMurzak.runner-manager",
3847            "the keychain service names the product; a change here moves every \
3848             stored item and the token reads as absent"
3849        );
3850    }
3851
3852    /// Finding 2's replacement, and the whole of what the zero fill claims.
3853    ///
3854    /// The test it replaces stored, deleted, and asserted the file was gone —
3855    /// which is what *removing* it proves too, so
3856    /// `overwrite`'s `write_all` could have been deleted outright and the suite
3857    /// would have stayed green. This one observes the fill itself: it reads the
3858    /// bytes back **before** anything unlinks them, so the subject of the test
3859    /// cannot be removed without it failing.
3860    ///
3861    /// Not `cfg`-gated to Linux, although Linux is the platform whose stored
3862    /// value is plaintext, because the mechanism is shared and a test that only
3863    /// a CI leg can run is a test its author never watched fail.
3864    #[cfg(not(target_os = "macos"))]
3865    #[test]
3866    fn overwrite_zeroes_every_byte_and_leaves_the_file_there() {
3867        let root = TempDir::new().expect("a temporary directory");
3868        let path = root.path().join("value");
3869        let token = exposed(&fixture_token());
3870        std::fs::write(&path, &token).expect("written");
3871
3872        assert!(overwrite(&path).expect("overwritten"), "the file was there");
3873
3874        let after = std::fs::read(&path).expect("still there, so the fill is observable");
3875        assert_eq!(
3876            after.len(),
3877            token.len(),
3878            "the overwrite must not truncate; a shorter file leaves the tail of the old \
3879             value in the block"
3880        );
3881        assert!(
3882            after.iter().all(|byte| *byte == 0),
3883            "the file still holds non-zero bytes after an overwrite: {after:?}"
3884        );
3885        assert!(
3886            !after
3887                .windows(token.len())
3888                .any(|window| window == token.as_bytes()),
3889            "the value survived the overwrite"
3890        );
3891    }
3892
3893    #[cfg(not(target_os = "macos"))]
3894    #[test]
3895    fn overwriting_what_is_not_there_is_not_a_failure() {
3896        let root = TempDir::new().expect("a temporary directory");
3897        assert!(
3898            !overwrite(&root.path().join("absent")).expect("absence is not an error"),
3899            "a store that was never written has nothing to scrub"
3900        );
3901    }
3902
3903    /// Finding 6's pure half, run on every leg.
3904    ///
3905    /// The systemd credential path that uses this is Linux-only, so its
3906    /// end-to-end test is too; the decision the trim makes is not
3907    /// platform-specific, and testing it here is what let this be watched
3908    /// failing on the machine it was written on.
3909    #[cfg(not(target_os = "macos"))]
3910    #[test]
3911    fn a_trailing_newline_is_not_part_of_the_token() {
3912        let token = exposed(&fixture_token());
3913
3914        for suffix in ["\n", "\r\n", "\n\n", " ", "\t\n", ""] {
3915            let raw = format!("{token}{suffix}").into_bytes();
3916            assert_eq!(
3917                trim_trailing_ascii_whitespace(raw),
3918                token.clone().into_bytes(),
3919                "a credential written with {suffix:?} on the end yielded a different token"
3920            );
3921        }
3922
3923        // Interior whitespace is left alone. A token has none, and silently
3924        // rewriting the middle of a value would be a worse bug than the one
3925        // this fixes.
3926        let interior = b"gh u_x\n".to_vec();
3927        assert_eq!(trim_trailing_ascii_whitespace(interior), b"gh u_x".to_vec());
3928
3929        // A value that is nothing but whitespace becomes empty, which `decode`
3930        // reports as `Corrupt` rather than as absence.
3931        assert!(trim_trailing_ascii_whitespace(b"\n\n".to_vec()).is_empty());
3932    }
3933
3934    // -----------------------------------------------------------------------
3935    // Windows
3936    // -----------------------------------------------------------------------
3937
3938    #[cfg(windows)]
3939    mod windows {
3940        use super::*;
3941        use crate::secrets::sys::{merge_grants, replacement_sddl, sddl, trustees};
3942
3943        /// How much a [`DeniedReplace`] takes away.
3944        #[derive(Debug, Clone, Copy, PartialEq, Eq)]
3945        enum AlsoDeny {
3946            /// Only what denies a *replacing rename*. Creating a file in the
3947            /// directory still works, exactly as it does for operator B, so the
3948            /// temporary is still written if the store gets that far.
3949            Nothing,
3950            /// Additionally `FILE_ADD_FILE`, so nothing can be created in the
3951            /// directory at all. What makes
3952            /// [`the_refusal_is_made_before_anything_is_written`] able to tell
3953            /// a pre-write refusal from a post-write one.
3954            Creation,
3955        }
3956
3957        /// Reproduces what a second non-administrative operator meets, without
3958        /// needing a second account, and puts the rights back on **every** path
3959        /// out — including an unwind.
3960        ///
3961        /// **Both denies are required, and finding that out was the point of
3962        /// running this against the un-fixed code.** Windows grants delete two
3963        /// ways — `DELETE` on the object, or `FILE_DELETE_CHILD` on its parent
3964        /// — and `MOVEFILE_REPLACE_EXISTING`, which is what `std::fs::rename`
3965        /// is on Windows, takes either. Operator B has neither: the file's DACL
3966        /// names `SY`, `BA` and `OW`, and a stock `%ProgramData%` grants
3967        /// `BUILTIN\Users` no `DC`. The test process, by contrast, *owns* its
3968        /// temporary directory and so does hold `FILE_DELETE_CHILD` — with only
3969        /// the file denied, the rename went through and the simulation
3970        /// reproduced nothing while looking exactly right.
3971        ///
3972        /// # Why the restore is a `Drop` and not a line at the end of the body
3973        ///
3974        /// Because a failing assertion unwinds past that line, and what it
3975        /// leaves behind is not tidy: `Everyone:(D)` denies deleting the guard
3976        /// and `Everyone:(DC)` denies deleting the directory's children, so
3977        /// `TempDir::drop` — which ignores removal errors — cannot clean up. A
3978        /// DPAPI blob of the fixture token would sit in `%TEMP%` under an ACL
3979        /// ordinary cleanup cannot remove, on exactly the run where somebody is
3980        /// already debugging a failure.
3981        struct DeniedReplace {
3982            file: std::path::PathBuf,
3983            directory: std::path::PathBuf,
3984            restored: bool,
3985        }
3986
3987        impl DeniedReplace {
3988            fn new(file: &std::path::Path, directory: &std::path::Path, also: AlsoDeny) -> Self {
3989                let denied = Self {
3990                    file: file.to_path_buf(),
3991                    directory: directory.to_path_buf(),
3992                    restored: false,
3993                };
3994                // Constructed first, so that a failure in any `icacls` below
3995                // unwinds through this guard's `Drop` and undoes the ones that
3996                // did take effect.
3997                icacls(&[&denied.file.display().to_string(), "/deny", "*S-1-1-0:(D)"]);
3998                icacls(&[
3999                    &denied.directory.display().to_string(),
4000                    "/deny",
4001                    "*S-1-1-0:(DC)",
4002                ]);
4003                if also == AlsoDeny::Creation {
4004                    icacls(&[
4005                        &denied.directory.display().to_string(),
4006                        "/deny",
4007                        "*S-1-1-0:(WD)",
4008                    ]);
4009                }
4010                denied
4011            }
4012
4013            /// Puts the rights back now, for a test that needs to go on and
4014            /// observe the store working again. Idempotent, so [`Drop`] running
4015            /// afterwards is a no-op.
4016            fn restore(&mut self) {
4017                if self.restored {
4018                    return;
4019                }
4020                self.restored = true;
4021                // `/remove:d` drops every deny ACE this SID has on the object,
4022                // so one call per object undoes all of the above.
4023                //
4024                // Deliberately not asserting: this runs on the unwind path,
4025                // where a panic would abort the process and replace a readable
4026                // test failure with one nobody can diagnose.
4027                for path in [&self.file, &self.directory] {
4028                    let arguments = [
4029                        path.display().to_string(),
4030                        "/remove:d".into(),
4031                        "*S-1-1-0".into(),
4032                    ];
4033                    match run_icacls(&arguments.each_ref().map(String::as_str)) {
4034                        Ok(output) if output.status.success() => {}
4035                        other => eprintln!(
4036                            "could not restore the ACL on {}: {other:?}; {} may survive in \
4037                             the temporary directory",
4038                            path.display(),
4039                            path.display()
4040                        ),
4041                    }
4042                }
4043            }
4044        }
4045
4046        impl Drop for DeniedReplace {
4047            fn drop(&mut self) {
4048                self.restore();
4049            }
4050        }
4051
4052        fn run_icacls(arguments: &[&str]) -> std::io::Result<std::process::Output> {
4053            std::process::Command::new("icacls.exe")
4054                .args(arguments)
4055                .output()
4056        }
4057
4058        fn icacls(arguments: &[&str]) {
4059            let output = run_icacls(arguments).expect("icacls is present on every Windows");
4060            assert!(
4061                output.status.success(),
4062                "icacls {arguments:?} failed: {}{}",
4063                String::from_utf8_lossy(&output.stdout),
4064                String::from_utf8_lossy(&output.stderr)
4065            );
4066        }
4067
4068        /// The DACL string alone, with no OS in the way.
4069        #[test]
4070        fn a_replacement_carries_the_previous_owner_and_a_first_write_does_not() {
4071            for scope in [SecretScope::Machine, SecretScope::User] {
4072                assert_eq!(
4073                    replacement_sddl(scope, &[]),
4074                    sddl(scope),
4075                    "a first write has nothing to carry, so it gets the constant DACL and \
4076                     nothing else"
4077                );
4078                assert_eq!(
4079                    replacement_sddl(scope, &["S-1-5-21-1-2-3-1001".to_owned()]),
4080                    format!("{}(A;;FA;;;S-1-5-21-1-2-3-1001)", sddl(scope)),
4081                    "one carried grant is one appended ACE"
4082                );
4083            }
4084        }
4085
4086        /// The second renewal, which is where carrying only the owner undoes
4087        /// itself.
4088        ///
4089        /// After one renewal the file is owned by `LocalSystem` and the
4090        /// operator is named by the ACE that renewal carried. A mechanism that
4091        /// rebuilds the DACL from the owner alone reads `S-1-5-18` here, drops
4092        /// the operator, and locks them out again eight hours after the fix
4093        /// appeared to work.
4094        ///
4095        /// Driving `store` cannot reach this: a test process is one account and
4096        /// cannot make the owner move. So the rule is tested where it lives.
4097        #[test]
4098        fn a_grant_survives_a_renewal_by_an_account_that_is_not_the_previous_owner() {
4099            let operator = "S-1-5-21-9-8-7-1001";
4100            let after_one_renewal = format!("{}(A;;FA;;;{operator})", sddl(SecretScope::Machine));
4101
4102            assert_eq!(
4103                merge_grants(Some("S-1-5-18"), &after_one_renewal),
4104                vec![operator.to_owned()],
4105                "the owner is now LocalSystem, which `SY` already grants; what must survive is \
4106                 the operator named in the DACL the previous renewal wrote"
4107            );
4108
4109            assert_eq!(
4110                merge_grants(Some(operator), sddl(SecretScope::Machine)),
4111                vec![operator.to_owned()],
4112                "and the first renewal, where the operator is still the owner and the DACL \
4113                 names nobody, carries the same one account"
4114            );
4115
4116            assert!(
4117                merge_grants(None, "").is_empty(),
4118                "a first write has no owner and no DACL to read, and carries nothing"
4119            );
4120
4121            assert_eq!(
4122                merge_grants(Some(operator), &after_one_renewal),
4123                vec![operator.to_owned()],
4124                "an account reachable both ways is named once, so the DACL cannot grow by an \
4125                 ACE per write"
4126            );
4127
4128            // The CI runner's own case, which an ordinary developer machine
4129            // does not reach: Windows renders a well-known account's ACE by
4130            // alias, so what the previous renewal wrote as `S-1-5-21-...-500`
4131            // reads back as `LA`. Carrying only trustees spelled `S-1-` drops
4132            // it and the operator is locked out one renewal later.
4133            let after_a_renewal_for_a_builtin =
4134                format!("{}(A;;FA;;;LA)", sddl(SecretScope::Machine));
4135            assert_eq!(
4136                merge_grants(Some("S-1-5-18"), &after_a_renewal_for_a_builtin),
4137                vec!["LA".to_owned()],
4138                "a grant is a grant whichever spelling the DACL reads back in"
4139            );
4140
4141            assert!(
4142                merge_grants(None, sddl(SecretScope::Machine)).is_empty(),
4143                "and the constant DACL's own aliases are not carried, or every write would \
4144                 double the base"
4145            );
4146        }
4147
4148        /// Every trustee, because after the first replacement one of them is
4149        /// where the operator's access lives — and Windows chooses the
4150        /// spelling, not this code.
4151        #[test]
4152        fn the_trustee_scan_reads_both_spellings() {
4153            assert_eq!(
4154                trustees("D:P(A;;FA;;;SY)(A;;FA;;;BA)(A;;FA;;;OW)(A;;FA;;;S-1-5-21-9-8-7-1001)"),
4155                vec![
4156                    "SY".to_owned(),
4157                    "BA".to_owned(),
4158                    "OW".to_owned(),
4159                    "S-1-5-21-9-8-7-1001".to_owned(),
4160                ],
4161                "the scan reads trustees; deciding which of them are already granted is \
4162                 `merge_grants`'s job and not this one's"
4163            );
4164            assert_eq!(
4165                trustees("D:P(A;;FA;;;LA)"),
4166                vec!["LA".to_owned()],
4167                "an alias is a trustee too, which is what CI's built-in Administrator account \
4168                 reads back as"
4169            );
4170        }
4171
4172        /// The lockout, as a test: renewal writes as a different account, and
4173        /// the operator who signed in must still be able to read the store.
4174        ///
4175        /// # What this can and cannot reproduce
4176        ///
4177        /// A test process is one account, so it cannot *become* `LocalSystem`
4178        /// and take ownership the way a daemon does. What it pins is the
4179        /// mechanism that makes that survivable: after a replacement, the
4180        /// previous owner is named in the DACL by SID rather than left to
4181        /// `OW` — so when the owner does move, the grant does not move with it.
4182        ///
4183        /// Un-fixed, the second description equals the first: `OW` and nothing
4184        /// else, which is exactly the state that locked an operator out of
4185        /// their own credential on 2026-08-29.
4186        ///
4187        /// # What it asserts, and why not the SID
4188        ///
4189        /// That the very first write already names somebody beyond the
4190        /// constant, and that the DACL then stops changing. Growth would be an
4191        /// ACE per write; a return to the constant would be the account
4192        /// dropped, which is the lockout.
4193        ///
4194        /// The first write counts because `OW` is not enough on its own: the
4195        /// system deletes the OWNER RIGHTS ACE whenever an owner changes, so a
4196        /// store that leans on it loses the grant to a single `takeown`. The
4197        /// writer is named by SID from the start.
4198        ///
4199        /// It deliberately does not name the account. Windows chooses the
4200        /// spelling: this machine's operator reads back as
4201        /// `S-1-5-21-…-1001`, and CI's, being the built-in Administrator,
4202        /// reads back as the alias `LA`. An earlier version of this test
4203        /// asserted the SID and failed on CI for that reason alone —
4204        /// and the same assumption was in the code, where it was a real
4205        /// defect. The spelling-sensitive rule is pinned in
4206        /// [`the_trustee_scan_reads_both_spellings`] instead.
4207        #[test]
4208        fn replacing_a_stored_credential_keeps_the_previous_owners_grant() {
4209            let root = TempDir::new().expect("a temporary directory");
4210            let store = rooted(SecretScope::Machine, &root);
4211            let base = sddl(SecretScope::Machine);
4212
4213            store.store(&fixture_token()).expect("the first write");
4214            let first = store
4215                .protection()
4216                .expect("the first file's DACL is readable")
4217                .description()
4218                .to_string();
4219            assert_ne!(
4220                first, base,
4221                "even a first write names the account that made it, because `OW` alone does \
4222                 not survive a change of owner"
4223            );
4224
4225            let mut previous: Option<String> = Some(first);
4226            for round in 1..=3 {
4227                store.store(&other_token()).expect("the replacement");
4228                let dacl = store
4229                    .protection()
4230                    .expect("the replacement's DACL is readable")
4231                    .description()
4232                    .to_string();
4233                assert_ne!(
4234                    dacl, base,
4235                    "write {round}: the account that owned what was replaced must stay granted \
4236                     explicitly, because a writer under another account takes `OW` with it"
4237                );
4238                if let Some(previous) = &previous {
4239                    assert_eq!(
4240                        &dacl, previous,
4241                        "write {round}: and the set must settle -- a DACL that keeps growing is \
4242                         an ACE per renewal, and one that shrinks back to the constant is the \
4243                         lockout returning"
4244                    );
4245                }
4246                previous = Some(dacl);
4247            }
4248
4249            assert_eq!(
4250                store
4251                    .load()
4252                    .expect("the replacement is readable")
4253                    .map(|secret| secret.expose_secret().to_string()),
4254                Some(other_token().expose_secret().to_string()),
4255                "carrying an ACE forward must not disturb what the store holds"
4256            );
4257        }
4258
4259        /// Denies *reading* the store, which is the state a renewal under a
4260        /// service account leaves an operator in, and puts the right back on
4261        /// every path out including an unwind.
4262        ///
4263        /// A deny ACE for `Everyone` binds administrators too, which is what
4264        /// makes this reproducible on CI's elevated runner as well as on an
4265        /// ordinary developer machine.
4266        struct DeniedRead {
4267            file: std::path::PathBuf,
4268            restored: bool,
4269        }
4270
4271        impl DeniedRead {
4272            fn new(file: &std::path::Path) -> Self {
4273                let denied = Self {
4274                    file: file.to_path_buf(),
4275                    restored: false,
4276                };
4277                icacls(&[&denied.file.display().to_string(), "/deny", "*S-1-1-0:(R)"]);
4278                denied
4279            }
4280
4281            fn restore(&mut self) {
4282                if self.restored {
4283                    return;
4284                }
4285                self.restored = true;
4286                let arguments = [
4287                    self.file.display().to_string(),
4288                    "/remove:d".into(),
4289                    "*S-1-1-0".into(),
4290                ];
4291                // Not asserted: this runs on the unwind path, where a panic
4292                // would replace a readable failure with an aborted process.
4293                match run_icacls(&arguments.each_ref().map(String::as_str)) {
4294                    Ok(output) if output.status.success() => {}
4295                    other => eprintln!(
4296                        "could not restore the ACL on {}: {other:?}",
4297                        self.file.display()
4298                    ),
4299                }
4300            }
4301        }
4302
4303        impl Drop for DeniedRead {
4304            fn drop(&mut self) {
4305                self.restore();
4306            }
4307        }
4308
4309        /// A store the operator may not read says how to get it back.
4310        ///
4311        /// Carrying the previous owner keeps this from *starting*, but it
4312        /// cannot end it on a host where it already happened: the file grants
4313        /// nobody but the service and has nothing left in its DACL to carry, so
4314        /// every renewal rebuilds the same DACL. Without a message the operator
4315        /// is told `Access is denied` by every command, forever, with no way to
4316        /// find out that one elevated `takeown` ends it.
4317        ///
4318        /// Watched on a real host on 2026-08-30, on 0.1.13, which had the carry
4319        /// and still could not read its own credential.
4320        #[test]
4321        fn a_store_this_account_may_not_read_names_the_command_that_gives_it_back() {
4322            let root = TempDir::new().expect("a temporary directory");
4323            let store = rooted(SecretScope::Machine, &root);
4324            store.store(&fixture_token()).expect("the first write");
4325
4326            let guard = store.guard();
4327            let _denied = DeniedRead::new(&guard);
4328
4329            let error = store
4330                .load()
4331                .expect_err("a store this account may not read is not a store it can load");
4332            let rendered = error.to_string();
4333
4334            for expected in ["icacls", "/grant", &guard.display().to_string(), "elevated"] {
4335                assert!(
4336                    rendered.contains(expected),
4337                    "the refusal must name the remedy and the file it applies to. Wanted \
4338                     {expected:?} in: {rendered}"
4339                );
4340            }
4341            assert!(
4342                !rendered.contains(fixture_token().expose_secret()),
4343                "and it must not carry the value it could not read"
4344            );
4345            assert!(
4346                rendered.contains("Do NOT use `takeown`"),
4347                "and it must warn off the repair that looks right and is not. Changing the \
4348                 owner makes Windows delete the OWNER RIGHTS ACE, so the account ends up \
4349                 owning a file it still cannot read -- permanently. Offered on a real host on \
4350                 2026-08-30, where it appeared to work only because the check that followed \
4351                 ran in the same elevated prompt. Got: {rendered}"
4352            );
4353        }
4354
4355        /// Finding 1: a second operator is refused with a remedy, before
4356        /// anything is written.
4357        ///
4358        /// The behaviour is right — one machine-scoped credential per host, and
4359        /// replacing it is an administrator's call; see the backend's module
4360        /// documentation for why widening the DACL is the wrong answer. What
4361        /// this pins is that the refusal *says so*, that it is classifiable as
4362        /// a permission problem, and that it leaves nothing on disk.
4363        #[test]
4364        fn a_second_operator_is_refused_with_a_remedy_and_writes_nothing() {
4365            let root = TempDir::new().expect("a temporary directory");
4366            let store = rooted(SecretScope::Machine, &root);
4367            store
4368                .store(&fixture_token())
4369                .expect("the first operator stores");
4370
4371            let guard = store.guard();
4372            let directory = guard
4373                .parent()
4374                .expect("the guard has a directory")
4375                .to_path_buf();
4376            let mut denied = DeniedReplace::new(&guard, &directory, AlsoDeny::Nothing);
4377
4378            let error = store
4379                .store(&other_token())
4380                .expect_err("a value this account may not replace is not a value it may store");
4381
4382            let rendered = error.to_string();
4383            for expected in [
4384                "one machine-scoped credential per host",
4385                "auth logout",
4386                "elevated",
4387                "--start-at login",
4388                "Nothing was written",
4389            ] {
4390                assert!(
4391                    rendered.contains(expected),
4392                    "the refusal does not name {expected:?}, so an operator cannot act on \
4393                     it: {rendered}"
4394                );
4395            }
4396            assert!(
4397                matches!(
4398                    &error,
4399                    SecretStoreError::Store { source, .. }
4400                        if source.kind() == std::io::ErrorKind::PermissionDenied
4401                ),
4402                "a caller cannot classify this as a permission problem: {error:?}"
4403            );
4404
4405            // Nothing was written, so the refusal did not also leave a valid
4406            // machine-decryptable blob of a live token under a name nobody
4407            // looks at. This is the half the old code got wrong: it found out
4408            // at the rename, after the blob was already on disk.
4409            let strays: Vec<_> = std::fs::read_dir(&directory)
4410                .expect("readable")
4411                .flatten()
4412                .map(|entry| entry.file_name().to_string_lossy().into_owned())
4413                .filter(|name| name.ends_with(".tmp"))
4414                .collect();
4415            assert!(strays.is_empty(), "a refused store left {strays:?}");
4416
4417            // And the first operator's value is untouched.
4418            denied.restore();
4419            assert_eq!(stored(&store), exposed(&fixture_token()));
4420
4421            // With the denial lifted, the same call succeeds -- which is what
4422            // makes the assertions above a statement about the denial rather
4423            // than about the store being broken.
4424            store
4425                .store(&other_token())
4426                .expect("stored once allowed again");
4427            assert_eq!(stored(&store), exposed(&other_token()));
4428        }
4429
4430        /// The ordering the fix is built around: the refusal is made **before**
4431        /// anything is encrypted or written.
4432        ///
4433        /// The test above cannot tell. The rename path re-derives the identical
4434        /// diagnosis — on `PermissionDenied` it calls `cannot_replace` itself —
4435        /// so moving the probe back behind the write leaves all five message
4436        /// assertions and the `kind()` assertion passing, and `discard` removes
4437        /// the temporary either way, so the `strays` assertion does not catch it
4438        /// either. The property stated at `store`'s call site and twice in the
4439        /// module documentation was asserted by nothing: the same shape as the
4440        /// zero fill that could be deleted with the suite staying green.
4441        ///
4442        /// [`AlsoDeny::Creation`] closes that. With `FILE_ADD_FILE` denied on
4443        /// the directory:
4444        ///
4445        /// - a probe that runs **first** still produces the remedy, because it
4446        ///   only reads the target's ACL, and nothing has been written;
4447        /// - a probe that runs **after** the write never runs at all, because
4448        ///   `create_protected_file` dies first with a bare denial that carries
4449        ///   no remedy in it.
4450        ///
4451        /// So this pins the ordering rather than the wording.
4452        #[test]
4453        fn the_refusal_is_made_before_anything_is_written() {
4454            let root = TempDir::new().expect("a temporary directory");
4455            let store = rooted(SecretScope::Machine, &root);
4456            store
4457                .store(&fixture_token())
4458                .expect("the first operator stores");
4459
4460            let guard = store.guard();
4461            let directory = guard
4462                .parent()
4463                .expect("the guard has a directory")
4464                .to_path_buf();
4465            let _denied = DeniedReplace::new(&guard, &directory, AlsoDeny::Creation);
4466
4467            let error = store
4468                .store(&other_token())
4469                .expect_err("nothing can be created here, so nothing can be stored");
4470
4471            let rendered = error.to_string();
4472            for expected in [
4473                "one machine-scoped credential per host",
4474                "auth logout",
4475                "elevated",
4476                "--start-at login",
4477                "Nothing was written",
4478            ] {
4479                assert!(
4480                    rendered.contains(expected),
4481                    "the refusal does not name {expected:?}. With creation denied, the only \
4482                     way to produce that text is a check that ran BEFORE the write -- so this \
4483                     message came from `create_protected_file` instead, and the store reached \
4484                     the write before it refused: {rendered}"
4485                );
4486            }
4487        }
4488
4489        /// Finding 3: the DPAPI output buffer is zeroed before it is freed.
4490        ///
4491        /// Asserted against a buffer Rust owns, because once `LocalFree` has
4492        /// run there is nothing left to look at. `copy_and_scrub` is the whole
4493        /// of what `take_blob` does to that buffer before freeing it, so this
4494        /// is the property and not a rehearsal of it.
4495        #[test]
4496        fn the_dpapi_buffer_is_scrubbed_before_it_is_freed() {
4497            let token = exposed(&fixture_token());
4498            let mut buffer = token.clone().into_bytes();
4499            let length = buffer.len();
4500
4501            // SAFETY: `buffer` is a live allocation of exactly `length` bytes
4502            // and nothing else refers to it for the duration of the call.
4503            let copy = unsafe { sys::copy_and_scrub(buffer.as_mut_ptr(), length) };
4504
4505            assert_eq!(
4506                copy,
4507                token.clone().into_bytes(),
4508                "the caller must still receive the value"
4509            );
4510            assert!(
4511                buffer.iter().all(|byte| *byte == 0),
4512                "the source buffer still holds the token after the copy, and on the \
4513                 unprotect path that buffer is handed back to the heap by LocalFree: \
4514                 {buffer:?}"
4515            );
4516        }
4517
4518        #[test]
4519        fn a_win32_error_keeps_its_kind_through_the_hresult_wrapper() {
4520            // ERROR_ACCESS_DENIED as windows-rs reports it: HRESULT 0x80070005.
4521            // Handed straight to `from_raw_os_error` this is `Uncategorized`,
4522            // and finding 1's refusal could not be classified by a caller.
4523            let denied = ::windows::core::Error::from_hresult(::windows::core::HRESULT(
4524                0x8007_0005_u32 as i32,
4525            ));
4526            assert_eq!(
4527                sys::io_error(&denied).kind(),
4528                std::io::ErrorKind::PermissionDenied
4529            );
4530
4531            // ERROR_FILE_NOT_FOUND, 0x80070002.
4532            let missing = ::windows::core::Error::from_hresult(::windows::core::HRESULT(
4533                0x8007_0002_u32 as i32,
4534            ));
4535            assert_eq!(sys::io_error(&missing).kind(), std::io::ErrorKind::NotFound);
4536        }
4537
4538        #[test]
4539        fn both_dacls_are_protected_and_name_no_broad_trustee() {
4540            for scope in [SecretScope::Machine, SecretScope::User] {
4541                let sddl = sys::sddl(scope);
4542                assert!(
4543                    sddl.starts_with("D:P"),
4544                    "an unprotected DACL inherits whatever %ProgramData% grants, which is \
4545                     Builtin Users read: {sddl}"
4546                );
4547                for broad in ["WD", "AU", "BU", "IU", "AN"] {
4548                    assert!(
4549                        !sddl.contains(&format!(";{broad})")),
4550                        "{scope}: {sddl} names the broad trustee {broad}"
4551                    );
4552                }
4553            }
4554        }
4555
4556        #[test]
4557        fn only_the_machine_dacl_carries_the_local_system_ace() {
4558            // `process.rs` argues at length that a `SY` ACE adds nothing to the
4559            // JIT handoff, and it is right there: that file's writer and reader
4560            // are one process. Here they are not -- `auth login` writes as the
4561            // operator and the daemon reads as the service account -- so `SY`
4562            // is load-bearing on the machine store and out of place on the
4563            // user store, which is deliberately not for a service.
4564            assert!(sys::sddl(SecretScope::Machine).contains("(A;;FA;;;SY)"));
4565            assert!(!sys::sddl(SecretScope::User).contains(";SY)"));
4566        }
4567
4568        #[test]
4569        fn the_dacl_on_disk_is_the_one_the_backend_asked_for() {
4570            let root = TempDir::new().expect("a temporary directory");
4571            for scope in [SecretScope::Machine, SecretScope::User] {
4572                let store = rooted(scope, &root);
4573                store.store(&fixture_token()).expect("stored");
4574
4575                let description = store.protection().expect("inspectable").description;
4576                assert!(
4577                    description.contains("D:P"),
4578                    "{scope}: the stored file did not keep its protected DACL: {description}"
4579                );
4580                assert!(
4581                    description.contains("FA;;;BA"),
4582                    "{scope}: an administrator must still be able to clean up: {description}"
4583                );
4584                // The one ACE that keeps a *non-administrative* operator able
4585                // to read back what they just stored. Nothing else in the DACL
4586                // names them, so if `CreateFileW` had quietly dropped it the
4587                // round trip would still pass on an administrator's machine and
4588                // fail on everybody else's.
4589                assert!(
4590                    description.contains(";OW)") || description.contains(";S-1-3-4)"),
4591                    "{scope}: the OWNER RIGHTS ACE did not survive to disk, so a \
4592                     non-administrative operator cannot read their own token: {description}"
4593                );
4594                if scope == SecretScope::Machine {
4595                    assert!(
4596                        description.contains("FA;;;SY"),
4597                        "a LocalSystem daemon must be able to read the machine store: \
4598                         {description}"
4599                    );
4600                }
4601            }
4602        }
4603
4604        #[test]
4605        fn the_bytes_on_disk_are_not_the_value() {
4606            let root = TempDir::new().expect("a temporary directory");
4607            let store = rooted(SecretScope::Machine, &root);
4608            store.store(&fixture_token()).expect("stored");
4609
4610            let blob = std::fs::read(store.guard()).expect("the blob is readable");
4611            let token = exposed(&fixture_token());
4612            assert!(
4613                !blob
4614                    .windows(token.len())
4615                    .any(|window| window == token.as_bytes()),
4616                "the DPAPI blob contains the plaintext token"
4617            );
4618        }
4619
4620        #[test]
4621        fn bytes_that_are_not_a_blob_are_reported_as_corrupt() {
4622            let root = TempDir::new().expect("a temporary directory");
4623            let store = rooted(SecretScope::Machine, &root);
4624            store.store(&fixture_token()).expect("stored");
4625
4626            // A blob truncated by an interrupted write, or one written by
4627            // another machine and copied here. Either way DPAPI refuses it, and
4628            // the caller must be told to purge rather than to retry.
4629            std::fs::write(store.guard(), b"this is not a DPAPI blob").expect("planted");
4630            let error = store.load().expect_err("a foreign blob is not a value");
4631            assert!(
4632                matches!(error, SecretStoreError::Corrupt { .. }),
4633                "got {error:?}"
4634            );
4635        }
4636    }
4637
4638    // -----------------------------------------------------------------------
4639    // Unix -- the mode bits, on both Unixes
4640    // -----------------------------------------------------------------------
4641
4642    #[cfg(unix)]
4643    mod unix {
4644        use super::*;
4645
4646        use std::os::unix::fs::PermissionsExt as _;
4647
4648        fn mode_of(path: &std::path::Path) -> u32 {
4649            std::fs::metadata(path)
4650                .unwrap_or_else(|error| panic!("{} is not there: {error}", path.display()))
4651                .permissions()
4652                .mode()
4653                & 0o777
4654        }
4655
4656        /// Asked before anything irreversible, so it has to agree with what a
4657        /// write would meet -- in both directions.
4658        #[test]
4659        fn ensure_writable_refuses_exactly_when_the_store_directory_is_not_writable() {
4660            // SAFETY: `geteuid` reads this process's own credentials, takes no
4661            // pointer, and cannot fail.
4662            if unsafe { libc::geteuid() } == 0 {
4663                // Modes refuse nobody running as root, so the refusal half
4664                // cannot be staged here.
4665                return;
4666            }
4667            for scope in [SecretScope::Machine, SecretScope::User] {
4668                let root = TempDir::new().expect("a temporary directory");
4669                let store = rooted(scope, &root);
4670                store
4671                    .ensure_writable()
4672                    .unwrap_or_else(|error| panic!("{scope}: nothing written yet: {error}"));
4673                store.store(&fixture_token()).expect("stored");
4674                store
4675                    .ensure_writable()
4676                    .unwrap_or_else(|error| panic!("{scope}: the writer's own store: {error}"));
4677
4678                let guard = store.guard();
4679                let directory = guard.parent().expect("the guard has a directory");
4680                std::fs::set_permissions(directory, std::fs::Permissions::from_mode(0o500))
4681                    .expect("the directory is made read-only");
4682                let refusal = store.ensure_writable();
4683                // A new entry beside the store, which is what both backends'
4684                // replacing write creates first. Not `store` itself: the Linux
4685                // backend re-applies `0700` to a directory this test's account
4686                // owns, so it would undo the staging rather than meet it. The
4687                // production case -- a directory another account owns -- is
4688                // one no chmod of ours can repair.
4689                let write = std::fs::File::create(directory.join("probe.tmp"));
4690                std::fs::set_permissions(directory, std::fs::Permissions::from_mode(0o700))
4691                    .expect("the directory is restored for cleanup");
4692
4693                let refusal = refusal.expect_err("a read-only directory refuses a write");
4694                assert!(
4695                    matches!(
4696                        &refusal,
4697                        SecretStoreError::Store { source, .. }
4698                            if source.kind() == std::io::ErrorKind::PermissionDenied
4699                    ),
4700                    "{scope}: got {refusal:?}"
4701                );
4702                assert!(
4703                    write.is_err(),
4704                    "{scope}: the check refused an entry the directory then accepted"
4705                );
4706            }
4707        }
4708
4709        #[test]
4710        fn the_guard_is_0600_and_its_directory_is_0700() {
4711            for scope in [SecretScope::Machine, SecretScope::User] {
4712                let root = TempDir::new().expect("a temporary directory");
4713                let store = rooted(scope, &root);
4714                store.store(&fixture_token()).expect("stored");
4715
4716                let guard = store.guard();
4717                assert_eq!(
4718                    mode_of(&guard),
4719                    0o600,
4720                    "{scope}: {} is not 0600",
4721                    guard.display()
4722                );
4723                let directory = guard.parent().expect("the guard has a directory");
4724                assert_eq!(
4725                    mode_of(directory),
4726                    0o700,
4727                    "{scope}: {} is not 0700",
4728                    directory.display()
4729                );
4730            }
4731        }
4732    }
4733
4734    // -----------------------------------------------------------------------
4735    // Linux -- systemd credentials
4736    // -----------------------------------------------------------------------
4737
4738    #[cfg(all(unix, not(target_os = "macos")))]
4739    mod linux {
4740        use super::*;
4741
4742        use std::os::unix::fs::PermissionsExt as _;
4743
4744        /// Writes `bytes` where systemd would have put a credential.
4745        fn plant_credential(directory: &std::path::Path, bytes: &[u8]) -> std::path::PathBuf {
4746            std::fs::create_dir_all(directory).expect("the credentials directory");
4747            let path = directory.join(SYSTEMD_CREDENTIAL);
4748            std::fs::write(&path, bytes).expect("the credential is written");
4749            // systemd mounts the credentials directory read-only and gives each
4750            // credential 0400. Reproduced so that the protection assertion sees
4751            // what production would.
4752            std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o400))
4753                .expect("the credential is tightened");
4754            path
4755        }
4756
4757        #[test]
4758        fn a_systemd_credential_is_read_in_preference_to_the_file() {
4759            let root = TempDir::new().expect("a temporary directory");
4760            let credentials = TempDir::new().expect("a credentials directory");
4761
4762            // The file first, so that reading the credential is a preference
4763            // rather than the only thing there is to read.
4764            let plain = rooted(SecretScope::Machine, &root);
4765            plain.store(&other_token()).expect("stored");
4766
4767            plant_credential(credentials.path(), exposed(&fixture_token()).as_bytes());
4768            let store =
4769                rooted(SecretScope::Machine, &root).with_credentials_directory(credentials.path());
4770
4771            assert_eq!(
4772                stored(&store),
4773                exposed(&fixture_token()),
4774                "a unit given LoadCredentialEncrypted= must not be overridden by a stale file"
4775            );
4776            assert_eq!(
4777                store.credential_path(),
4778                Some(credentials.path().join(SYSTEMD_CREDENTIAL).as_path())
4779            );
4780            assert!(
4781                store.location().contains("systemd credential"),
4782                "`host show` must say where the value is actually coming from: {}",
4783                store.location()
4784            );
4785        }
4786
4787        /// Finding 6, end to end: what an operator's `systemd-creds encrypt`
4788        /// actually produces.
4789        ///
4790        /// `echo`, `printf '%s\n'` and every text editor leave a newline. Used
4791        /// byte for byte it becomes part of the token and fails much later
4792        /// inside an `Authorization` header, where it reads as a bad credential
4793        /// rather than as a bad read — which is the failure this pins.
4794        #[test]
4795        fn a_credential_written_with_a_trailing_newline_yields_the_token_without_it() {
4796            let root = TempDir::new().expect("a temporary directory");
4797            let credentials = TempDir::new().expect("a credentials directory");
4798            let token = exposed(&fixture_token());
4799
4800            plant_credential(credentials.path(), format!("{token}\n").as_bytes());
4801            let store =
4802                rooted(SecretScope::Machine, &root).with_credentials_directory(credentials.path());
4803
4804            assert_eq!(stored(&store), token);
4805        }
4806
4807        /// The file half of the same decision, which must *not* be trimmed.
4808        ///
4809        /// `store` writes no newline, so anything trailing in its own file is
4810        /// corruption rather than formatting, and repairing it silently would
4811        /// hide the one thing `Corrupt` exists to report.
4812        #[test]
4813        fn the_stores_own_file_is_read_back_verbatim() {
4814            let root = TempDir::new().expect("a temporary directory");
4815            let store = rooted(SecretScope::Machine, &root);
4816            store.store(&fixture_token()).expect("stored");
4817
4818            let token = exposed(&fixture_token());
4819            std::fs::write(store.guard(), format!("{token}\n")).expect("planted");
4820
4821            assert_eq!(
4822                stored(&store),
4823                format!("{token}\n"),
4824                "the store's own file is used verbatim; only a systemd credential is trimmed"
4825            );
4826        }
4827
4828        #[test]
4829        fn a_credentials_directory_without_this_credential_falls_back_to_the_file() {
4830            let root = TempDir::new().expect("a temporary directory");
4831            let credentials = TempDir::new().expect("a credentials directory");
4832
4833            let plain = rooted(SecretScope::Machine, &root);
4834            plain.store(&fixture_token()).expect("stored");
4835
4836            // A unit can be given credentials without being given this one.
4837            std::fs::write(credentials.path().join("something.else"), b"x").expect("written");
4838            let store =
4839                rooted(SecretScope::Machine, &root).with_credentials_directory(credentials.path());
4840
4841            assert_eq!(stored(&store), exposed(&fixture_token()));
4842        }
4843
4844        #[test]
4845        fn the_credential_is_what_protection_inspects_when_there_is_one() {
4846            let root = TempDir::new().expect("a temporary directory");
4847            let credentials = TempDir::new().expect("a credentials directory");
4848            let planted =
4849                plant_credential(credentials.path(), exposed(&fixture_token()).as_bytes());
4850
4851            let store =
4852                rooted(SecretScope::Machine, &root).with_credentials_directory(credentials.path());
4853            let protection = store.protection().expect("inspectable");
4854
4855            assert_eq!(protection.guard(), planted);
4856            assert!(!protection.readable_by_other_local_users(), "{protection}");
4857        }
4858
4859        #[test]
4860        fn storing_under_a_systemd_credential_is_refused_rather_than_shadowed() {
4861            let root = TempDir::new().expect("a temporary directory");
4862            let credentials = TempDir::new().expect("a credentials directory");
4863            plant_credential(credentials.path(), exposed(&fixture_token()).as_bytes());
4864
4865            // The same site without the credential, so the test can name the
4866            // file the refusal must not have written.
4867            let file = rooted(SecretScope::Machine, &root).guard();
4868
4869            let store =
4870                rooted(SecretScope::Machine, &root).with_credentials_directory(credentials.path());
4871            let error = store
4872                .store(&other_token())
4873                .expect_err("a write that the next read would ignore is not a write");
4874            let rendered = error.to_string();
4875            assert!(rendered.contains(SYSTEMD_CREDENTIAL), "{rendered}");
4876
4877            // And nothing was written, so the refusal did not also leave a
4878            // second copy of a token on disk.
4879            assert!(!file.exists(), "{} was written anyway", file.display());
4880        }
4881
4882        #[test]
4883        fn purging_under_a_systemd_credential_removes_the_file_and_reports_the_remainder() {
4884            let root = TempDir::new().expect("a temporary directory");
4885            let credentials = TempDir::new().expect("a credentials directory");
4886
4887            let plain = rooted(SecretScope::Machine, &root);
4888            plain.store(&other_token()).expect("stored");
4889            let file = plain.guard();
4890            plant_credential(credentials.path(), exposed(&fixture_token()).as_bytes());
4891
4892            let store =
4893                rooted(SecretScope::Machine, &root).with_credentials_directory(credentials.path());
4894            let error = store
4895                .delete()
4896                .expect_err("this host is not purged and `auth logout` must not say it is");
4897
4898            assert!(!file.exists(), "the file it could remove was removed");
4899            assert!(
4900                error.to_string().contains(SYSTEMD_CREDENTIAL),
4901                "the operator has to be told what is still supplying a token: {error}"
4902            );
4903        }
4904
4905        #[test]
4906        #[serial_test::serial]
4907        fn the_standard_machine_store_reads_the_credentials_directory_from_the_environment() {
4908            let credentials = TempDir::new().expect("a credentials directory");
4909
4910            // SAFETY: `serial_test` guarantees no other test in this binary is
4911            // running, and this is the only test that touches this variable.
4912            unsafe {
4913                std::env::set_var(CREDENTIALS_DIRECTORY, credentials.path());
4914            }
4915            let store = PlatformSecretStore::standard(SecretScope::Machine).expect("resolves");
4916            let resolved = store.credential_path().map(std::path::Path::to_path_buf);
4917
4918            // SAFETY: as above.
4919            unsafe {
4920                std::env::remove_var(CREDENTIALS_DIRECTORY);
4921            }
4922
4923            assert_eq!(
4924                resolved,
4925                Some(credentials.path().join(SYSTEMD_CREDENTIAL)),
4926                "a daemon started by systemd gets its credential without being told to"
4927            );
4928
4929            let without = PlatformSecretStore::standard(SecretScope::Machine).expect("resolves");
4930            assert_eq!(
4931                without.credential_path(),
4932                None,
4933                "a daemon started by anything else must not invent one"
4934            );
4935        }
4936
4937        #[test]
4938        fn a_user_scoped_store_never_consults_a_service_credential() {
4939            let store = PlatformSecretStore::standard(SecretScope::User).expect("resolves");
4940            assert_eq!(store.credential_path(), None);
4941        }
4942    }
4943}