OXYGENHUB
# b689779e9e74 oxygen / crates / oxy-core / src / credentials.rs
19.6 KiB · Rust · last changed Strata: a repository kept on a store, as a remote 1 month ago source blame history raw
ff1f62fe Andy McCall ↶ 1
//! Per-user credentials for hubs: one sign-in serves every repository on a
2
//! hub, and nothing secret is written into a checkout.
3
//!
4
//! The store is `credentials.toml` in the user's oxy directory (see
5
//! [`crate::repo::default_home`]), one section per hub:
6
//!
7
//! ```text
8
//! [https://oxygenhub.io]
9
//! account = "andymccall"
10
//! token = "oxyhub_…"
11
//! ```
12
//!
13
//! A hub is a scheme, host and port: a token is only ever sent to the hub
14
//! it was stored for. The file is written readable by its owner only, and
15
//! is ignored with a warning when anyone else can read it. With
16
//! `auth.helper` set in the user's `config.toml`, tokens go to that helper
17
//! instead (Git's credential-helper protocol, so `libsecret`,
18
//! `osxkeychain` and `manager` keep them in the system keyring) and the
19
//! file keeps only what is not secret.
20
//!
21
//! A token is sent over `https`, or over plain `http` to this machine only
22
//! ([`token_allowed`]); hub transports drop it on any redirect.
23
24
use crate::protocol::RemoteOptions;
25
use std::collections::{BTreeMap, HashMap};
26
use std::io::Write;
27
use std::path::{Path, PathBuf};
28
use std::process::{Command, Stdio};
29
use std::sync::{Mutex, OnceLock};
30
31
/// The user name oxy's tokens are stored under in a credential helper, so
32
/// they stay apart from Git credentials for the same host.
33
const HELPER_USER: &str = "oxy-token";
34
35
/// The hub of a URL: `scheme://host[:port]`, lower case, for `http` and
36
/// `https` URLs only.
37
pub fn hub_of(url: &str) -> Option<String> {
38
    let (scheme, rest) = url.split_once("://")?;
39
    let scheme = scheme.to_ascii_lowercase();
40
    if scheme != "http" && scheme != "https" {
41
        return None;
42
    }
43
    let host = rest.split(['/', '?', '#']).next()?;
44
    // Never key a hub on credentials embedded in the URL.
45
    let host = host.rsplit('@').next()?;
46
    if host.is_empty() {
47
        return None;
48
    }
49
    Some(format!("{scheme}://{}", host.to_ascii_lowercase()))
50
}
51
52
/// Whether a token may be sent to `url`: always over `https`, and over
53
/// plain `http` only to this machine (a hub under test, or a runner's own).
54
pub fn token_allowed(url: &str) -> bool {
55
    let Some(hub) = hub_of(url) else {
56
        return true;
57
    };
58
    match hub.split_once("://") {
59
        Some(("https", _)) => true,
60
        Some((_, host)) => {
61
            let name = match host.strip_prefix('[') {
62
                Some(v6) => v6.split(']').next().unwrap_or(""),
63
                None => host.rsplit_once(':').map_or(host, |(h, _)| h),
64
            };
65
            name == "localhost"
66
                || name == "::1"
67
                || name
68
                    .parse::<std::net::Ipv4Addr>()
69
                    .is_ok_and(|a| a.is_loopback())
70
        }
71
        None => false,
72
    }
73
}
74
75
/// What the store holds for one hub.
76
#[derive(Clone, Debug, Default, PartialEq, Eq)]
77
pub struct HubEntry {
78
    /// `scheme://host[:port]`.
79
    pub hub: String,
80
    /// The account the token signs in as, recorded at sign-in.
81
    pub account: Option<String>,
82
    /// The token, when it is in the file (not with a helper).
83
    pub token: Option<String>,
84
    /// Extra roots to trust for this hub (a self-signed hub).
85
    pub ca: Option<PathBuf>,
86
}
87
88
/// Where a saved token went.
89
#[derive(Clone, Debug, PartialEq, Eq)]
90
pub enum Stored {
91
    /// The credentials file, owner-only.
92
    File(PathBuf),
93
    /// A credential helper, by its `auth.helper` value.
94
    Helper(String),
95
}
96
97
/// The credential store of one oxy home directory.
98
#[derive(Clone, Debug)]
99
pub struct Credentials {
100
    home: PathBuf,
101
}
102
103
impl Credentials {
104
    /// The store in `home` (an oxy home directory).
105
    pub fn at(home: impl Into<PathBuf>) -> Credentials {
106
        Credentials { home: home.into() }
107
    }
108
109
    /// The store in the user's oxy home directory.
110
    pub fn user() -> Credentials {
111
        Credentials::at(crate::repo::default_home())
112
    }
113
114
    /// `credentials.toml` in the home directory.
115
    pub fn path(&self) -> PathBuf {
116
        self.home.join("credentials.toml")
117
    }
118
119
    /// The configured credential helper (`auth.helper` in the user file).
120
    pub fn helper(&self) -> Option<String> {
121
        crate::repo::read_config_file(&self.home.join("config.toml"))
122
            .ok()?
123
            .remove("auth.helper")
124
            .filter(|h| !h.trim().is_empty())
125
    }
126
127
    /// Every hub in the file, tokens as stored there (none with a helper).
128
    pub fn entries(&self) -> Result<Vec<HubEntry>, String> {
129
        Ok(self.read()?.into_values().collect())
130
    }
131
132
    /// Whether the file can be read by anyone but its owner, in which case
133
    /// lookups ignore it.
134
    pub fn too_open(&self) -> bool {
135
        #[cfg(unix)]
136
        {
137
            use std::os::unix::fs::PermissionsExt;
138
            if let Ok(m) = std::fs::metadata(self.path()) {
139
                return m.permissions().mode() & 0o077 != 0;
140
            }
141
        }
142
        false
143
    }
144
145
    /// The entry for `hub`, its token fetched from the helper if one is set.
146
    pub fn get(&self, hub: &str) -> Result<Option<HubEntry>, String> {
147
        let entry = self.read()?.remove(hub);
148
        let helper = self.helper();
149
        let mut entry = match (entry, &helper) {
150
            (Some(e), _) => e,
151
            // A helper may hold a token stored before the file was cleared.
152
            (None, Some(_)) => HubEntry {
153
                hub: hub.to_string(),
154
                ..HubEntry::default()
155
            },
156
            (None, None) => return Ok(None),
157
        };
158
        if entry.token.is_none() {
159
            if let Some(h) = &helper {
160
                entry.token = run_helper(h, "get", hub, None)?;
161
            }
162
        }
163
        if entry.token.is_none() && entry.account.is_none() && entry.ca.is_none() {
164
            return Ok(None);
165
        }
166
        Ok(Some(entry))
167
    }
168
169
    /// Save a token (and what is known about it) for `hub`.
170
    pub fn save(
171
        &self,
172
        hub: &str,
173
        token: &str,
174
        account: Option<&str>,
175
        ca: Option<&Path>,
176
    ) -> Result<Stored, String> {
177
        let mut all = self.read()?;
178
        let helper = self.helper();
179
        let entry = all.entry(hub.to_string()).or_insert_with(|| HubEntry {
180
            hub: hub.to_string(),
181
            ..HubEntry::default()
182
        });
183
        entry.account = account.map(String::from).or(entry.account.take());
184
        if let Some(ca) = ca {
185
            entry.ca = Some(ca.to_path_buf());
186
        }
187
        let stored = match &helper {
188
            Some(h) => {
189
                run_helper(h, "store", hub, Some(token))?;
190
                entry.token = None;
191
                Stored::Helper(h.clone())
192
            }
193
            None => {
194
                entry.token = Some(token.to_string());
195
                Stored::File(self.path())
196
            }
197
        };
198
        self.write(&all)?;
199
        forget_cached();
200
        Ok(stored)
201
    }
202
203
    /// Remove `hub` from the file and the helper; returns what it held,
204
    /// with the token, so the caller can revoke it.
205
    pub fn remove(&self, hub: &str) -> Result<Option<HubEntry>, String> {
206
        let found = self.get(hub)?;
207
        let mut all = self.read()?;
208
        if all.remove(hub).is_some() {
209
            self.write(&all)?;
210
        }
211
        if let Some(h) = self.helper() {
212
            run_helper(&h, "erase", hub, None)?;
213
        }
214
        forget_cached();
215
        Ok(found)
216
    }
217
218
    fn read(&self) -> Result<BTreeMap<String, HubEntry>, String> {
219
        let path = self.path();
220
        let text = match std::fs::read_to_string(&path) {
221
            Ok(t) => t,
222
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(BTreeMap::new()),
223
            Err(e) => return Err(format!("reading {}: {e}", path.display())),
224
        };
225
        let mut out = BTreeMap::new();
226
        let mut current: Option<HubEntry> = None;
227
        for (n, line) in text.lines().enumerate() {
228
            let line = line.trim();
229
            if line.is_empty() || line.starts_with('#') {
230
                continue;
231
            }
232
            if let Some(hub) = line.strip_prefix('[').and_then(|l| l.strip_suffix(']')) {
233
                if let Some(e) = current.take() {
234
                    out.insert(e.hub.clone(), e);
235
                }
236
                current = Some(HubEntry {
237
                    hub: hub.trim().to_string(),
238
                    ..HubEntry::default()
239
                });
240
                continue;
241
            }
242
            let bad = || {
243
                format!(
244
                    "{} line {}: expected key = \"value\"",
245
                    path.display(),
246
                    n + 1
247
                )
248
            };
249
            let (k, v) = line.split_once('=').ok_or_else(bad)?;
250
            let v = unquote(v.trim()).ok_or_else(bad)?;
251
            let Some(e) = current.as_mut() else {
252
                return Err(format!(
253
                    "{} line {}: a key before any [hub] section",
254
                    path.display(),
255
                    n + 1
256
                ));
257
            };
258
            match k.trim() {
259
                "account" => e.account = Some(v),
260
                "token" => e.token = Some(v),
261
                "ca" => e.ca = Some(PathBuf::from(v)),
262
                // Unknown keys are kept out of lookups, not fatal.
263
                _ => {}
264
            }
265
        }
266
        if let Some(e) = current {
267
            out.insert(e.hub.clone(), e);
268
        }
269
        Ok(out)
270
    }
271
272
    fn write(&self, all: &BTreeMap<String, HubEntry>) -> Result<(), String> {
273
        let mut text =
274
            String::from("# oxy credentials: a token for each hub. Keep this file private.\n");
275
        for e in all.values() {
276
            text.push_str(&format!("\n[{}]\n", e.hub));
277
            if let Some(a) = &e.account {
278
                text.push_str(&format!("account = {}\n", quote(a)));
279
            }
280
            if let Some(t) = &e.token {
281
                text.push_str(&format!("token = {}\n", quote(t)));
282
            }
283
            if let Some(c) = &e.ca {
284
                text.push_str(&format!("ca = {}\n", quote(&c.to_string_lossy())));
285
            }
286
        }
287
        let path = self.path();
288
        std::fs::create_dir_all(&self.home)
289
            .map_err(|e| format!("creating {}: {e}", self.home.display()))?;
290
        crate::durable::write_atomic_private(&path, text.as_bytes())
291
            .map_err(|e| format!("writing {}: {e}", path.display()))
292
    }
293
294
    /// The stored token and CA for `url`'s hub, if any: memoized, so a
295
    /// helper runs once per hub in a process. A file anyone else can read
296
    /// is ignored, with a warning.
297
    pub fn lookup(&self, url: &str) -> (Option<String>, Option<PathBuf>) {
298
        let Some(hub) = hub_of(url) else {
299
            return (None, None);
300
        };
301
        let key = (self.home.clone(), hub.clone());
302
        if let Some(hit) = cache().lock().unwrap().get(&key) {
303
            return hit.clone();
304
        }
305
        let found = if self.too_open() {
306
            warn_once(&format!(
307
                "warning: {} can be read by other users, so oxy ignores it; run chmod 600 on it",
308
                self.path().display()
309
            ));
310
            (None, None)
311
        } else {
312
            match self.get(&hub) {
313
                Ok(Some(e)) => (e.token, e.ca),
314
                Ok(None) => (None, None),
315
                Err(e) => {
316
                    warn_once(&format!("warning: credentials for {hub}: {e}"));
317
                    (None, None)
318
                }
319
            }
320
        };
321
        cache().lock().unwrap().insert(key, found.clone());
322
        found
323
    }
324
325
    /// Options for reaching `url` without a configured remote: `$OXY_TOKEN`
326
    /// and `$OXY_CA`, else what this store holds for its hub.
327
    pub fn options_for(&self, url: &str) -> RemoteOptions {
328
        let env_token = std::env::var("OXY_TOKEN").ok().filter(|t| !t.is_empty());
329
        let env_ca = std::env::var("OXY_CA").ok().map(PathBuf::from);
330
        let (token, ca) = match (&env_token, &env_ca) {
331
            (Some(_), Some(_)) => (None, None),
332
            _ => self.lookup(url),
333
        };
334
        RemoteOptions {
335
            token: env_token.or(token),
336
            ca: env_ca.or(ca),
337
            repo_id: None,
9f9e72b9 Andy McCall ↶ 338
            cache_dir: None,
ff1f62fe Andy McCall ↶ 339
        }
340
    }
341
}
342
343
type Cache = Mutex<HashMap<(PathBuf, String), (Option<String>, Option<PathBuf>)>>;
344
345
fn cache() -> &'static Cache {
346
    static CACHE: OnceLock<Cache> = OnceLock::new();
347
    CACHE.get_or_init(Default::default)
348
}
349
350
fn forget_cached() {
351
    cache().lock().unwrap().clear();
352
}
353
354
fn warn_once(message: &str) {
355
    static SEEN: OnceLock<Mutex<std::collections::HashSet<String>>> = OnceLock::new();
356
    if SEEN
357
        .get_or_init(Default::default)
358
        .lock()
359
        .unwrap()
360
        .insert(message.to_string())
361
    {
362
        eprintln!("{message}");
363
    }
364
}
365
366
fn quote(v: &str) -> String {
367
    let mut out = String::from('"');
368
    for c in v.chars() {
369
        match c {
370
            '"' => out.push_str("\\\""),
371
            '\\' => out.push_str("\\\\"),
372
            '\n' => out.push_str("\\n"),
373
            c => out.push(c),
374
        }
375
    }
376
    out.push('"');
377
    out
378
}
379
380
fn unquote(v: &str) -> Option<String> {
381
    let inner = v.strip_prefix('"')?.strip_suffix('"')?;
382
    let mut out = String::new();
383
    let mut chars = inner.chars();
384
    while let Some(c) = chars.next() {
385
        if c == '\\' {
386
            match chars.next()? {
387
                'n' => out.push('\n'),
388
                c => out.push(c),
389
            }
390
        } else {
391
            out.push(c);
392
        }
393
    }
394
    Some(out)
395
}
396
397
/// Run a credential helper (Git's protocol): `get`, `store` or `erase` for
398
/// `hub`. `get` returns the password the helper answered with.
399
///
400
/// `auth.helper` is a helper name (run as `git credential-NAME`), an
401
/// absolute path, or a shell command after `!`, as in Git.
402
fn run_helper(
403
    helper: &str,
404
    action: &str,
405
    hub: &str,
406
    token: Option<&str>,
407
) -> Result<Option<String>, String> {
408
    let (protocol, host) = hub
409
        .split_once("://")
410
        .ok_or_else(|| format!("{hub} is not a hub URL"))?;
411
    let mut cmd = if let Some(shell) = helper.strip_prefix('!') {
412
        let mut c = Command::new("sh");
413
        c.arg("-c").arg(format!("{shell} {action}"));
414
        c
415
    } else if Path::new(helper).is_absolute() {
416
        let mut c = Command::new(helper);
417
        c.arg(action);
418
        c
419
    } else {
420
        let mut c = Command::new("git");
421
        c.arg(format!("credential-{helper}")).arg(action);
422
        c
423
    };
424
    let mut input = format!("protocol={protocol}\nhost={host}\nusername={HELPER_USER}\n");
425
    if let Some(t) = token {
426
        input.push_str(&format!("password={t}\n"));
427
    }
428
    input.push('\n');
429
    let mut child = cmd
430
        .stdin(Stdio::piped())
431
        .stdout(Stdio::piped())
432
        .stderr(Stdio::inherit())
433
        .spawn()
434
        .map_err(|e| format!("running credential helper {helper:?}: {e}"))?;
435
    child
436
        .stdin
437
        .take()
438
        .expect("piped")
439
        .write_all(input.as_bytes())
440
        .map_err(|e| format!("credential helper {helper:?}: {e}"))?;
441
    let out = child
442
        .wait_with_output()
443
        .map_err(|e| format!("credential helper {helper:?}: {e}"))?;
444
    if !out.status.success() {
445
        return Err(format!("credential helper {helper:?} {action} failed"));
446
    }
447
    if action != "get" {
448
        return Ok(None);
449
    }
450
    Ok(String::from_utf8_lossy(&out.stdout)
451
        .lines()
452
        .find_map(|l| l.strip_prefix("password="))
453
        .map(String::from)
454
        .filter(|p| !p.is_empty()))
455
}
456
457
#[cfg(test)]
458
mod tests {
459
    use super::*;
460
461
    #[test]
462
    fn hubs_are_scheme_host_and_port() {
463
        assert_eq!(
464
            hub_of("https://OxygenHub.io/doomsdayone/metalgear").as_deref(),
465
            Some("https://oxygenhub.io")
466
        );
467
        assert_eq!(
468
            hub_of("http://127.0.0.1:7300/a/b").as_deref(),
469
            Some("http://127.0.0.1:7300")
470
        );
471
        assert_eq!(
472
            hub_of("https://user:[email protected]/x").as_deref(),
473
            Some("https://hub.example")
474
        );
475
        assert_eq!(hub_of("/srv/repo"), None);
476
        assert_eq!(hub_of("file:///srv/repo"), None);
477
    }
478
479
    #[test]
480
    fn tokens_go_over_https_or_to_this_machine() {
481
        assert!(token_allowed("https://oxygenhub.io/a/b"));
482
        assert!(token_allowed("http://127.0.0.1:7300/a/b"));
483
        assert!(token_allowed("http://localhost/a"));
484
        assert!(token_allowed("http://[::1]:8080/a"));
485
        assert!(!token_allowed("http://oxygenhub.io/a/b"));
486
        assert!(!token_allowed("http://10.0.0.1/a"));
487
        assert!(!token_allowed("http://127.0.0.1.evil.example/a"));
488
    }
489
490
    #[test]
491
    fn save_get_and_remove_round_trip_privately() {
492
        let dir = tempfile::tempdir().unwrap();
493
        let store = Credentials::at(dir.path());
494
        assert_eq!(store.get("https://hub.example").unwrap(), None);
495
        let stored = store
496
            .save("https://hub.example", "tok\"en", Some("andy"), None)
497
            .unwrap();
498
        assert_eq!(stored, Stored::File(store.path()));
499
        store
500
            .save(
501
                "http://127.0.0.1:7300",
502
                "local",
503
                None,
504
                Some(Path::new("/ca.pem")),
505
            )
506
            .unwrap();
507
        let e = store.get("https://hub.example").unwrap().unwrap();
508
        assert_eq!(e.token.as_deref(), Some("tok\"en"));
509
        assert_eq!(e.account.as_deref(), Some("andy"));
510
        assert!(!store.too_open());
511
        assert_eq!(
512
            store.lookup("https://hub.example/o/r"),
513
            (Some("tok\"en".into()), None)
514
        );
515
        // Another host, scheme or port gets nothing.
516
        assert_eq!(store.lookup("https://other.example/o/r"), (None, None));
517
        assert_eq!(store.lookup("http://hub.example/o/r"), (None, None));
518
        assert_eq!(store.lookup("https://hub.example:8443/o/r"), (None, None));
519
        let gone = store.remove("https://hub.example").unwrap().unwrap();
520
        assert_eq!(gone.token.as_deref(), Some("tok\"en"));
521
        assert_eq!(store.lookup("https://hub.example/o/r"), (None, None));
522
        assert_eq!(store.entries().unwrap().len(), 1);
523
    }
524
525
    #[cfg(unix)]
526
    #[test]
527
    fn a_file_others_can_read_is_ignored() {
528
        use std::os::unix::fs::PermissionsExt;
529
        let dir = tempfile::tempdir().unwrap();
530
        let store = Credentials::at(dir.path());
531
        store.save("https://open.example", "t", None, None).unwrap();
532
        let mode = std::fs::metadata(store.path())
533
            .unwrap()
534
            .permissions()
535
            .mode();
536
        assert_eq!(mode & 0o777, 0o600);
537
        std::fs::set_permissions(store.path(), std::fs::Permissions::from_mode(0o644)).unwrap();
538
        assert!(store.too_open());
539
        assert_eq!(store.lookup("https://open.example/o/r"), (None, None));
540
    }
541
542
    #[cfg(unix)]
543
    #[test]
544
    fn a_helper_keeps_the_token_out_of_the_file() {
545
        let dir = tempfile::tempdir().unwrap();
546
        let vault = dir.path().join("vault");
547
        // A helper that keeps one password in a file, as a keyring would.
548
        let helper = format!(
549
            "!f() {{ case $1 in get) [ -f {v} ] && echo password=$(cat {v});; store) sed -n 's/^password=//p' > {v};; erase) rm -f {v};; esac; }}; f",
550
            v = vault.display()
551
        );
552
        std::fs::write(
553
            dir.path().join("config.toml"),
554
            format!("auth.helper = \"{helper}\"\n"),
555
        )
556
        .unwrap();
557
        let store = Credentials::at(dir.path());
558
        assert!(store.helper().is_some());
559
        let stored = store
560
            .save("https://hub.example", "secret", Some("andy"), None)
561
            .unwrap();
562
        assert!(matches!(stored, Stored::Helper(_)));
563
        let text = std::fs::read_to_string(store.path()).unwrap();
564
        assert!(!text.contains("secret"), "{text}");
565
        assert_eq!(
566
            store
567
                .get("https://hub.example")
568
                .unwrap()
569
                .unwrap()
570
                .token
571
                .as_deref(),
572
            Some("secret")
573
        );
574
        store.remove("https://hub.example").unwrap();
575
        assert!(!vault.exists());
576
    }
577
}