pitboard-core 0.13.0

The engine behind Pitboard: parking and restoring Claude Code and Codex logins. Serves Pitboard's own front ends.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! The macOS backend. Every call execs `/usr/bin/security`, the only application the
//! item's access list trusts.
//!
//! Measured on throwaway items: a foreign in-process read adds the caller to that list, and
//! a foreign in-process write replaces the item's partition list, after which every read by
//! `/usr/bin/security` takes 1-3 seconds instead of 0.02, or waits on a keychain password
//! dialog in a desktop session, a cost Claude Code then pays on every credential re-read.
//! A write through `security -U` changes only the item's mtime.

use crate::context::Context;
use crate::host::SECURITY;
use crate::service::Permit;
use crate::store::{Backend, Cost, Error, RawStore};
use std::process::{Command, Output};
use std::time::Duration;

/// Claude Code's own ceiling on an interactive `security` command line. Past it Claude
/// Code passes the credential as an argument instead, where `ps` can read it; Pitboard
/// refuses rather than do that, so this is a real ceiling here and not a transport choice.
const MAX_COMMAND_BYTES: usize = 4032;

/// `security` exits with the low byte of the `OSStatus`: `errSecItemNotFound`.
const ITEM_NOT_FOUND: i32 = 44;
/// `errSecInteractionNotAllowed`: the keychain is locked and may not prompt.
const INTERACTION_NOT_ALLOWED: i32 = 36;

/// Claude Code reads an empty answer from its own slot as "absent". Nothing else is: a
/// locked keychain says nothing about what it holds, and reading it as empty would fall
/// through to a stale plaintext file and park or overwrite the wrong login.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Owner {
    ClaudeCode,
    Pitboard,
}

pub(super) struct Keychain {
    owner: Owner,
    /// The keychain account every item is stored under, as Claude Code names it.
    account: String,
    /// Whether a login past the stdin limit may go on the argument line instead.
    argv_fallback: bool,
}

impl Keychain {
    /// Items some other program owns, stored under `account`.
    ///
    /// Which items, and under what account, is that program's business: this takes the
    /// name rather than deriving it, because deriving it was how Claude Code's own slot
    /// hashing came to live inside what claimed to be an operating-system abstraction.
    pub(super) fn foreign(ctx: &Context, account: String) -> Keychain {
        Keychain {
            owner: Owner::ClaudeCode,
            account,
            argv_fallback: ctx.argv_fallback(),
        }
    }

    /// Where Pitboard parks credentials of its own.
    pub(super) fn vault(ctx: &Context) -> Keychain {
        Keychain {
            owner: Owner::Pitboard,
            account: super::vault_account(ctx),
            argv_fallback: ctx.argv_fallback(),
        }
    }
}

enum Presence {
    Present(String),
    Absent,
    Locked,
    Failed(String),
}

fn classify(owner: Owner, code: Option<i32>, stdout: String, stderr: String) -> Presence {
    match code {
        Some(0) if !stdout.trim().is_empty() => Presence::Present(stdout.trim_end().to_string()),
        Some(0) if owner == Owner::ClaudeCode => Presence::Absent,
        Some(ITEM_NOT_FOUND) => Presence::Absent,
        Some(INTERACTION_NOT_ALLOWED) => Presence::Locked,
        other => Presence::Failed(format!(
            "security exited {}: {}",
            other.map_or_else(|| "on a signal".into(), |c| c.to_string()),
            stderr.trim()
        )),
    }
}

/// `security` answers in milliseconds, unless the keychain is locked and it waits on an
/// unlock prompt. This leaves a person time to answer one and stops a wait no one sees.
const SECURITY_TIMEOUT: Duration = Duration::from_secs(60);

fn security(args: &[&str], input: &str) -> std::io::Result<Output> {
    let mut command = Command::new(SECURITY);
    command.args(args);
    super::helper::output_within(command, input.as_bytes(), SECURITY_TIMEOUT)
}

/// What a read makes of an answer.
fn answered(presence: Presence) -> Result<Option<String>, Error> {
    match presence {
        Presence::Present(found) => Ok(Some(found)),
        Presence::Absent => Ok(None),
        Presence::Locked => Err(Error::Locked),
        Presence::Failed(why) => Err(Error::Unreadable(why)),
    }
}

fn run(args: &[&str], owner: Owner) -> Presence {
    match security(args, "") {
        Err(e) => Presence::Failed(format!("{SECURITY} did not answer: {e}")),
        Ok(out) => classify(
            owner,
            out.status.code(),
            String::from_utf8_lossy(&out.stdout).into_owned(),
            String::from_utf8_lossy(&out.stderr).into_owned(),
        ),
    }
}

/// One line of `dump-keychain` output, as the service name it names.
///
/// The shape is `    "svce"<blob>="pitboard-park-<uuid>-<millis>"`. A name Pitboard made
/// contains no quote and no backslash, so a plain read to the closing quote is exact for
/// every name this is asked about, and anything stranger simply does not match.
fn service_of(line: &str) -> Option<String> {
    let rest = line.trim_start().strip_prefix("\"svce\"<blob>=\"")?;
    let name = rest.strip_suffix('"')?;
    (!name.contains('\\') && !name.contains('"')).then(|| name.to_string())
}

/// The command `write` will send, so its length can be checked before anything changes.
fn command_for(account: &str, service: &str, secret: &str) -> String {
    format!(
        "add-generic-password -U -a \"{account}\" -s \"{service}\" -X {}\n",
        hex::encode(secret.as_bytes())
    )
}

impl Keychain {
    /// What the command that writes this would cost against what `security -i` reads.
    fn price(&self, service: &str, contents: &str) -> Cost {
        Cost {
            needs: command_for(&self.account, service, contents).len(),
            limit: MAX_COMMAND_BYTES,
            second_route: self.argv_fallback,
        }
    }

    fn find(&self, service: &str, with_data: bool) -> Presence {
        let account = self.account.as_str();
        let mut args = vec!["find-generic-password", "-a", account, "-s", service];
        if with_data {
            args.push("-w");
        }
        run(&args, self.owner)
    }
}

impl RawStore for Keychain {
    fn kind(&self) -> Backend {
        Backend::Keychain
    }

    fn contains(&self, service: &str) -> Result<bool, Error> {
        answered(self.find(service, false)).map(|found| found.is_some())
    }

    fn read(&self, service: &str) -> Result<Option<String>, Error> {
        answered(self.find(service, true))
    }

    /// Update in place, secret on stdin.
    ///
    /// `-X` takes hex so the value survives any byte; `-i` keeps it out of argv, where `ps`
    /// would expose it. Both names are quoted because `security -i` splits on whitespace
    /// and every real service name contains a space.
    fn write(&self, _: Permit, service: &str, contents: &str) -> Result<(), Error> {
        let account = self.account.as_str();
        if account.contains('"') || service.contains('"') {
            return Err(Error::Write(
                "account or service name contains a quote".into(),
            ));
        }
        let price = self.price(service, contents);
        if price.refused() {
            return Err(Error::Write(format!(
                "this credential is {} bytes, past the {MAX_COMMAND_BYTES}-byte command limit",
                price.needs
            )));
        }

        // Measured on macOS 26: `security -i` reads at most 4097 bytes of command line and
        // treats the rest as another command. Past that the only route `security` offers is
        // the argument line, which is what Claude Code falls back to for the same login.
        let hex = hex::encode(contents.as_bytes());
        let out = if price.over() {
            let args = [
                "add-generic-password",
                "-U",
                "-a",
                account,
                "-s",
                service,
                "-X",
                &hex,
            ];
            security(&args, "")
                .map_err(|e| Error::Write(format!("{SECURITY} did not answer: {e}")))?
        } else {
            security(&["-i"], &command_for(account, service, contents))
                .map_err(|e| Error::Write(format!("{SECURITY} did not answer: {e}")))?
        };
        if out.status.code() != Some(0) {
            return Err(Error::Write(format!(
                "security exited {}: {}",
                out.status
                    .code()
                    .map_or_else(|| "on a signal".into(), |c| c.to_string()),
                String::from_utf8_lossy(&out.stderr).trim()
            )));
        }

        match self.read(service)? {
            Some(back) if back == contents => Ok(()),
            Some(_) => Err(Error::NotDurable(format!(
                "{service} holds different bytes"
            ))),
            None => Err(Error::NotDurable(format!("{service} reads back empty"))),
        }
    }

    fn delete(&self, _: Permit, service: &str) -> Result<(), Error> {
        let account = self.account.as_str();
        match run(
            &["delete-generic-password", "-a", account, "-s", service],
            self.owner,
        ) {
            Presence::Present(_) | Presence::Absent => Ok(()),
            Presence::Locked => Err(Error::Write(crate::store::LOCKED.into())),
            Presence::Failed(m) => Err(Error::Write(m)),
        }
    }

    /// Measured on macOS 26 against a keychain holding 362 items: `dump-keychain` without
    /// `-d` exits 0 in 0.06 seconds, never prompts, and emits attributes only, no secret of
    /// any item. Reads of Pitboard's own items afterwards take the usual 0.016 seconds, so
    /// listing does not carry the access-list side effect an in-process read does.
    ///
    /// Only Pitboard's own names are returned, and only from the vault: the live chain has
    /// nothing to enumerate and Claude Code's items are none of Pitboard's business.
    fn list(&self) -> Result<Option<Vec<String>>, Error> {
        if self.owner != Owner::Pitboard {
            return Ok(None);
        }
        let out = security(&["dump-keychain"], "")
            .map_err(|e| Error::Unreadable(format!("{SECURITY} did not answer: {e}")))?;
        if out.status.code() != Some(0) {
            return Err(Error::Unreadable(format!(
                "security dump-keychain exited {}",
                out.status
                    .code()
                    .map_or_else(|| "on a signal".into(), |c| c.to_string())
            )));
        }
        let mut names: Vec<String> = String::from_utf8_lossy(&out.stdout)
            .lines()
            .filter_map(service_of)
            .filter(|name| crate::park::is_park_name(name))
            .collect();
        names.sort();
        names.dedup();
        Ok(Some(names))
    }

    fn cost(&self, service: &str, contents: &str) -> Option<Cost> {
        Some(self.price(service, contents))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn claude_codes_absent_codes_are_absent_and_the_rest_abort() {
        for code in [0, ITEM_NOT_FOUND] {
            assert!(matches!(
                classify(Owner::ClaudeCode, Some(code), String::new(), String::new()),
                Presence::Absent
            ));
        }
        for code in [1, 37, 50, 128] {
            assert!(matches!(
                classify(Owner::ClaudeCode, Some(code), String::new(), String::new()),
                Presence::Failed(_)
            ));
        }
    }

    #[test]
    fn our_own_items_only_accept_item_not_found_as_absent() {
        assert!(matches!(
            classify(
                Owner::Pitboard,
                Some(ITEM_NOT_FOUND),
                String::new(),
                String::new()
            ),
            Presence::Absent
        ));
        for code in [0, 37, 50] {
            assert!(matches!(
                classify(Owner::Pitboard, Some(code), String::new(), String::new()),
                Presence::Failed(_)
            ));
        }
    }

    /// A locked keychain is neither absent nor broken: it says nothing of what it holds, and
    /// what to do about it is to unlock it, which no other failure calls for.
    #[test]
    fn a_locked_keychain_is_locked_whoever_owns_the_item() {
        for owner in [Owner::ClaudeCode, Owner::Pitboard] {
            assert!(matches!(
                classify(
                    owner,
                    Some(INTERACTION_NOT_ALLOWED),
                    String::new(),
                    "User interaction is not allowed.".into()
                ),
                Presence::Locked
            ));
        }
        assert!(matches!(answered(Presence::Locked), Err(Error::Locked)));
    }

    #[test]
    fn a_signal_is_a_failure_not_an_absence() {
        assert!(matches!(
            classify(Owner::ClaudeCode, None, String::new(), String::new()),
            Presence::Failed(_)
        ));
    }

    #[test]
    fn the_command_quotes_both_names_and_hexes_the_secret() {
        let c = command_for("me", "Claude Code-credentials", "{\"a\":1}");
        assert!(
            c.starts_with("add-generic-password -U -a \"me\" -s \"Claude Code-credentials\" -X ")
        );
        assert!(c.ends_with("7b2261223a317d\n"));
        assert!(
            !c.contains("{\"a\":1}"),
            "the secret must not appear as plain text"
        );
    }

    /// Measured on macOS 26: `security -i` reads 4097 bytes of a command and treats the
    /// rest as another command, so a hex-encoded secret of about two kilobytes is the most
    /// that route carries. Past it the argument line is the only way, which is what Claude
    /// Code uses for the same login.
    /// One line of `dump-keychain`, as the name it carries. Measured against real output.
    #[test]
    fn a_dumped_line_gives_up_the_service_it_names() {
        assert_eq!(
            service_of(r#"    "svce"<blob>="pitboard-park-acc-1790000000000""#).as_deref(),
            Some("pitboard-park-acc-1790000000000")
        );
        assert_eq!(
            service_of(r#"    "svce"<blob>="Claude Code-credentials""#).as_deref(),
            Some("Claude Code-credentials"),
            "parsing is not filtering; the caller decides what it wants"
        );
        // Everything else in a dump, and anything that would need unescaping.
        assert_eq!(service_of(r#"    "acct"<blob>="ngoquocdat""#), None);
        assert_eq!(
            service_of("keychain: \"/Users/x/Library/Keychains/login\""),
            None
        );
        assert_eq!(service_of(r#"    "svce"<blob>=0x00"#), None);
        assert_eq!(service_of(r#"    "svce"<blob>="has\\backslash""#), None);
        assert_eq!(service_of(""), None);
    }

    #[test]
    fn the_stdin_route_stops_at_about_two_kilobytes() {
        let ctx = Context::for_unit_test();
        let live = Keychain::foreign(&ctx, "someone".into());
        assert!(live.price("svc", &"x".repeat(2100)).over());
        assert!(!live.price("svc", &"x".repeat(1900)).over());
    }

    /// The refusal is available to whoever wants it, and is not the default: there is no
    /// third way to write a login this size.
    #[test]
    fn only_a_refusing_context_refuses_a_login_this_size() {
        let ctx = Context::for_unit_test();
        let big = "x".repeat(2100);
        let allowed = Keychain::foreign(&ctx, "someone".into()).price("svc", &big);
        assert!(allowed.on_the_second_route());
        assert!(!allowed.refused());

        let refusing =
            Keychain::foreign(&ctx.with_argv_fallback(false), "someone".into()).price("svc", &big);
        assert!(refusing.refused());
        assert!(!refusing.on_the_second_route());
    }
}