gitbound 1.0.0

A safety-first local GitHub identity manager
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
use crate::error::GitBoundError;
use clap::ValueEnum;
use directories::{BaseDirs, ProjectDirs};
use fs4::FileExt;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::{
    collections::BTreeMap,
    env,
    fs::{self, OpenOptions},
    io::Write,
    path::{Path, PathBuf},
};

pub const CURRENT_SCHEMA_VERSION: u32 = 3;

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Config {
    pub schema_version: u32,
    #[serde(default)]
    pub profiles: BTreeMap<String, Profile>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub directories: Vec<DirectoryRule>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub repository_roots: Vec<PathBuf>,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            schema_version: CURRENT_SCHEMA_VERSION,
            profiles: BTreeMap::new(),
            directories: Vec::new(),
            repository_roots: Vec::new(),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct DirectoryRule {
    pub profile: String,
    pub path: PathBuf,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Profile {
    pub github_user: String,
    pub git_name: String,
    pub git_email: String,
    /// The real GitHub host this profile belongs to, such as `github.com` or a
    /// GitHub Enterprise domain. This is the name the GitHub CLI, the HTTPS
    /// remote URL and the credential helper are all keyed by, so it must be a
    /// host that actually resolves — never an SSH config alias.
    #[serde(default = "default_hostname")]
    pub hostname: String,
    /// The `Host` alias from the user's SSH config, when they keep one account
    /// per alias (`github.com-work`). It is only ever used to build and match
    /// SSH remote URLs; `hostname` stays authoritative everywhere else. `None`
    /// means the profile has no alias and SSH uses `hostname` directly.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ssh_host: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ssh_key: Option<PathBuf>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub allowed_owners: Vec<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub signing_key: Option<String>,
    #[serde(default)]
    pub signing_format: SigningFormat,
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub require_signing: bool,
}

#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize, PartialEq, Eq, ValueEnum)]
#[serde(rename_all = "lowercase")]
pub enum SigningFormat {
    #[default]
    Openpgp,
    Ssh,
}

impl SigningFormat {
    pub fn as_git_value(self) -> &'static str {
        match self {
            Self::Openpgp => "openpgp",
            Self::Ssh => "ssh",
        }
    }
}

fn default_hostname() -> String {
    "github.com".to_string()
}

impl Profile {
    /// The host to put in an SSH remote URL and to authenticate against over
    /// SSH: the alias when the profile defines one, and the real host
    /// otherwise.
    pub fn ssh_host(&self) -> &str {
        self.ssh_host.as_deref().unwrap_or(&self.hostname)
    }

    pub fn validate(&self) -> Result<(), GitBoundError> {
        for (label, value) in [
            ("GitHub user", &self.github_user),
            ("Git name", &self.git_name),
            ("Git email", &self.git_email),
        ] {
            if value.trim().is_empty() {
                return Err(GitBoundError::usage(format!("{label} cannot be empty")));
            }
            if value.contains('\n') || value.contains('\r') || value.contains('\0') {
                return Err(GitBoundError::usage(format!(
                    "{label} cannot contain newlines or null bytes"
                )));
            }
        }
        if self
            .github_user
            .chars()
            .any(|c| c.is_whitespace() || c == '/' || c == '\\' || c == '"')
        {
            return Err(GitBoundError::usage(
                "GitHub user cannot contain whitespace or slashes",
            ));
        }
        validate_hostname(&self.hostname)?;
        if let Some(ssh_host) = &self.ssh_host {
            validate_hostname(ssh_host)?;
        }
        if self
            .ssh_key
            .as_ref()
            .is_some_and(|key| key.as_os_str().is_empty())
        {
            return Err(GitBoundError::usage("SSH key path cannot be empty"));
        }
        if self
            .allowed_owners
            .iter()
            .any(|owner| owner.trim().is_empty())
        {
            return Err(GitBoundError::usage("allowed owners cannot be empty"));
        }
        if let Some(key) = &self.signing_key {
            if key.trim().is_empty() {
                return Err(GitBoundError::usage("signing key cannot be empty"));
            }
            if key.contains('\n') || key.contains('\r') || key.contains('\0') {
                return Err(GitBoundError::usage(
                    "signing key cannot contain newlines or null bytes",
                ));
            }
        }
        if self.require_signing && self.signing_key.is_none() {
            return Err(GitBoundError::usage(
                "required commit signing needs a signing key",
            ));
        }
        Ok(())
    }

    pub fn validate_local_resources(&self) -> Result<(), GitBoundError> {
        if let Some(key) = &self.ssh_key {
            let expanded = expand_path(key)?;
            if !expanded.is_file() {
                return Err(GitBoundError::usage(format!(
                    "SSH key does not exist or is not a file: {}",
                    expanded.display()
                )));
            }
        }
        if self.signing_format == SigningFormat::Ssh
            && let Some(key) = &self.signing_key
            && !key.starts_with("key::")
            && !key.starts_with("ssh-")
            && !key.starts_with("ecdsa-")
        {
            let expanded = expand_path(Path::new(key))?;
            if !expanded.is_file() {
                return Err(GitBoundError::usage(format!(
                    "SSH signing key does not exist or is not a file: {}",
                    expanded.display()
                )));
            }
        }
        Ok(())
    }
}

pub fn validate_profile_name(name: &str) -> Result<(), GitBoundError> {
    static PATTERN: std::sync::LazyLock<Regex> = std::sync::LazyLock::new(|| {
        Regex::new(r"^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$").expect("valid regex")
    });
    if PATTERN.is_match(name) {
        Ok(())
    } else {
        Err(GitBoundError::usage(
            "profile name must match [A-Za-z0-9][A-Za-z0-9._-]{0,63}",
        ))
    }
}

/// Move an SSH config alias out of `hostname` and into `ssh_host`.
///
/// Before schema 3 there was one host field, so anyone running several accounts
/// off SSH aliases had to store the alias — `github.com-work` — as the
/// hostname. That silently broke every GitHub CLI call, because `gh` only knows
/// real hosts and reports the profile as unauthenticated. The split fixes it,
/// but only if the existing value is repaired on the way in.
///
/// An alias is recognised conservatively: the part before the dash must look
/// like a host (it contains a dot) and the part after it must not (a real
/// hostname such as `sub-domain.example.com` therefore never matches, because
/// its dash is followed by more dotted labels). Anything that does not match
/// is left exactly as it was.
fn migrate_ssh_alias(profile: &mut Profile) {
    if profile.ssh_host.is_some() {
        return;
    }
    let Some(host) = split_ssh_alias(&profile.hostname) else {
        return;
    };
    profile.ssh_host = Some(profile.hostname.clone());
    profile.hostname = host;
}

fn split_ssh_alias(value: &str) -> Option<String> {
    value.match_indices('-').find_map(|(index, _)| {
        let (host, alias) = (&value[..index], &value[index + 1..]);
        (host.contains('.') && !alias.is_empty() && !alias.contains('.')).then(|| host.to_string())
    })
}

pub fn validate_hostname(hostname: &str) -> Result<(), GitBoundError> {
    let value = hostname.trim();
    if value.is_empty()
        || value.contains("://")
        || value.contains('/')
        || value.chars().any(char::is_whitespace)
    {
        Err(GitBoundError::usage(
            "hostname must be a host name without a scheme or path",
        ))
    } else {
        Ok(())
    }
}

pub fn expand_path(path: &Path) -> Result<PathBuf, GitBoundError> {
    let text = path.to_string_lossy();
    if text == "~" || text.starts_with("~/") || text.starts_with("~\\") {
        let base = BaseDirs::new()
            .ok_or_else(|| GitBoundError::dependency("could not determine the home directory"))?;
        let suffix = text.trim_start_matches('~').trim_start_matches(['/', '\\']);
        Ok(base.home_dir().join(suffix))
    } else {
        Ok(path.to_path_buf())
    }
}

#[derive(Debug, Clone)]
pub struct ConfigStore {
    path: PathBuf,
}

impl ConfigStore {
    pub fn discover() -> Result<Self, GitBoundError> {
        if let Some(path) = env::var_os("GITBOUND_CONFIG") {
            return Ok(Self {
                path: PathBuf::from(path),
            });
        }
        let dirs = ProjectDirs::from("dev", "GitBound", "GitBound").ok_or_else(|| {
            GitBoundError::dependency("could not determine the operating-system config directory")
        })?;
        Ok(Self {
            path: dirs.config_dir().join("config.toml"),
        })
    }

    pub fn at(path: PathBuf) -> Self {
        Self { path }
    }
    pub fn path(&self) -> &Path {
        &self.path
    }

    pub fn load(&self) -> Result<Config, GitBoundError> {
        self.load_inner(true)
    }

    fn load_inner(&self, persist_migration: bool) -> Result<Config, GitBoundError> {
        if !self.path.exists() {
            return Ok(Config::default());
        }
        let text = fs::read_to_string(&self.path).map_err(|e| {
            GitBoundError::dependency(format!("could not read {}: {e}", self.path.display()))
        })?;
        let mut config: Config = toml::from_str(&text).map_err(|e| {
            GitBoundError::usage(format!("invalid config {}: {e}", self.path.display()))
        })?;
        if config.schema_version > CURRENT_SCHEMA_VERSION || config.schema_version == 0 {
            return Err(GitBoundError::usage(format!(
                "unsupported config schema version {}",
                config.schema_version
            )));
        }
        let migrated = config.schema_version < CURRENT_SCHEMA_VERSION;
        if migrated {
            if config.schema_version < 3 {
                for profile in config.profiles.values_mut() {
                    migrate_ssh_alias(profile);
                }
            }
            config.schema_version = CURRENT_SCHEMA_VERSION;
        }
        for (name, profile) in &config.profiles {
            validate_profile_name(name)?;
            profile.validate()?;
        }
        for rule in &config.directories {
            if !config.profiles.contains_key(&rule.profile) {
                return Err(GitBoundError::usage(format!(
                    "directory rule references missing profile '{}'",
                    rule.profile
                )));
            }
            if rule.path.as_os_str().is_empty() {
                return Err(GitBoundError::usage("directory rule path cannot be empty"));
            }
        }
        config.repository_roots.sort_by(|left, right| {
            left.to_string_lossy()
                .to_ascii_lowercase()
                .cmp(&right.to_string_lossy().to_ascii_lowercase())
        });
        config.repository_roots.dedup_by(|left, right| {
            if cfg!(windows) {
                left.to_string_lossy()
                    .eq_ignore_ascii_case(&right.to_string_lossy())
            } else {
                left == right
            }
        });
        if migrated && persist_migration {
            self.persist_migration(&config)?;
        }
        Ok(config)
    }

    fn persist_migration(&self, config: &Config) -> Result<(), GitBoundError> {
        let parent = self
            .path
            .parent()
            .ok_or_else(|| GitBoundError::dependency("config path has no parent directory"))?;
        fs::create_dir_all(parent).map_err(|error| {
            GitBoundError::dependency(format!("could not create {}: {error}", parent.display()))
        })?;
        // The lock file is intentionally kept on disk across operations.
        // Deleting it creates a TOCTOU race where concurrent processes can lock different inodes.
        let lock = OpenOptions::new()
            .create(true)
            .read(true)
            .write(true)
            .truncate(false)
            .open(parent.join("config.lock"))
            .map_err(|error| {
                GitBoundError::dependency(format!("could not open config lock: {error}"))
            })?;
        FileExt::lock(&lock).map_err(|error| {
            GitBoundError::dependency(format!("could not lock config: {error}"))
        })?;
        let rendered = toml::to_string_pretty(config).map_err(|error| {
            GitBoundError::dependency(format!("could not serialize config: {error}"))
        })?;
        let mut temp = tempfile::NamedTempFile::new_in(parent).map_err(|error| {
            GitBoundError::dependency(format!("could not create temporary config: {error}"))
        })?;
        temp.write_all(rendered.as_bytes())
            .and_then(|_| temp.as_file().sync_all())
            .map_err(|error| {
                GitBoundError::dependency(format!("could not write temporary config: {error}"))
            })?;
        temp.persist(&self.path).map_err(|error| {
            GitBoundError::dependency(format!("could not persist config: {}", error.error))
        })?;
        FileExt::unlock(&lock).ok();
        Ok(())
    }

    pub fn update<T>(
        &self,
        operation: impl FnOnce(&mut Config) -> Result<T, GitBoundError>,
    ) -> Result<T, GitBoundError> {
        let parent = self
            .path
            .parent()
            .ok_or_else(|| GitBoundError::dependency("config path has no parent directory"))?;
        fs::create_dir_all(parent).map_err(|e| {
            GitBoundError::dependency(format!("could not create {}: {e}", parent.display()))
        })?;
        // The lock file is intentionally kept on disk across operations.
        // Deleting it creates a TOCTOU race where concurrent processes can lock different inodes.
        let lock_path = parent.join("config.lock");
        let lock = OpenOptions::new()
            .create(true)
            .read(true)
            .write(true)
            .truncate(false)
            .open(&lock_path)
            .map_err(|e| GitBoundError::dependency(format!("could not open config lock: {e}")))?;
        FileExt::lock(&lock)
            .map_err(|e| GitBoundError::dependency(format!("could not lock config: {e}")))?;
        let mut config = self.load_inner(false)?;
        config.schema_version = CURRENT_SCHEMA_VERSION;
        let result = operation(&mut config)?;
        let rendered = toml::to_string_pretty(&config)
            .map_err(|e| GitBoundError::dependency(format!("could not serialize config: {e}")))?;
        let mut temp = tempfile::NamedTempFile::new_in(parent).map_err(|e| {
            GitBoundError::dependency(format!("could not create temporary config: {e}"))
        })?;
        temp.write_all(rendered.as_bytes())
            .and_then(|_| temp.as_file().sync_all())
            .map_err(|e| {
                GitBoundError::dependency(format!("could not write temporary config: {e}"))
            })?;
        temp.persist(&self.path).map_err(|e| {
            GitBoundError::dependency(format!("could not persist config: {}", e.error))
        })?;
        FileExt::unlock(&lock).ok();
        Ok(result)
    }
}

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

    #[test]
    fn profile_name_validation() {
        assert!(validate_profile_name("work-1.main").is_ok());
        assert!(validate_profile_name("bad name").is_err());
        assert!(validate_profile_name(&"a".repeat(65)).is_err());
    }

    #[test]
    fn config_round_trip() {
        let dir = tempfile::tempdir().unwrap();
        let store = ConfigStore::at(dir.path().join("config.toml"));
        store
            .update(|config| {
                config.profiles.insert(
                    "work".into(),
                    Profile {
                        github_user: "alice".into(),
                        git_name: "Alice".into(),
                        git_email: "a@example.com".into(),
                        hostname: "github.com".into(),
                        ssh_host: None,
                        ssh_key: None,
                        allowed_owners: vec!["Org".into()],
                        signing_key: None,
                        signing_format: SigningFormat::Openpgp,
                        require_signing: false,
                    },
                );
                Ok(())
            })
            .unwrap();
        store
            .update(|config| {
                config.profiles.get_mut("work").unwrap().git_name = "Alice Updated".into();
                Ok(())
            })
            .unwrap();
        assert_eq!(store.load().unwrap().profiles["work"].github_user, "alice");
        assert_eq!(
            store.load().unwrap().profiles["work"].git_name,
            "Alice Updated"
        );
    }

    #[test]
    fn v1_config_is_atomically_migrated_with_empty_roots() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("config.toml");
        fs::write(&path, "schema_version = 1\n").unwrap();
        let config = ConfigStore::at(path.clone()).load().unwrap();
        assert_eq!(config.schema_version, CURRENT_SCHEMA_VERSION);
        assert!(config.repository_roots.is_empty());
        let persisted = fs::read_to_string(path).unwrap();
        assert!(persisted.contains("schema_version = 3"));
    }

    #[test]
    fn a_v2_ssh_alias_hostname_is_split_into_a_real_host_and_an_alias() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("config.toml");
        fs::write(
            &path,
            "schema_version = 2

[profiles.work]
github_user = \"BitKiln\"
git_name = \"B\"
git_email = \"b@example.com\"
hostname = \"github.com-BitKiln\"
",
        )
        .unwrap();
        let profile = ConfigStore::at(path)
            .load()
            .unwrap()
            .profiles
            .remove("work")
            .unwrap();
        assert_eq!(profile.hostname, "github.com");
        assert_eq!(profile.ssh_host.as_deref(), Some("github.com-BitKiln"));
        assert_eq!(profile.ssh_host(), "github.com-BitKiln");
    }

    #[test]
    fn a_hostname_whose_dash_is_part_of_a_label_is_left_alone() {
        for hostname in [
            "github.com",
            "sub-domain.example.com",
            "git-host.example.com",
        ] {
            let mut profile = Profile {
                github_user: "alice".into(),
                git_name: "Alice".into(),
                git_email: "alice@example.com".into(),
                hostname: hostname.into(),
                ssh_host: None,
                ssh_key: None,
                allowed_owners: vec![],
                signing_key: None,
                signing_format: SigningFormat::Openpgp,
                require_signing: false,
            };
            migrate_ssh_alias(&mut profile);
            assert_eq!(profile.hostname, hostname);
            assert_eq!(profile.ssh_host, None);
            assert_eq!(profile.ssh_host(), hostname);
        }
    }

    #[test]
    fn an_explicit_ssh_host_survives_migration_untouched() {
        let mut profile = Profile {
            github_user: "alice".into(),
            git_name: "Alice".into(),
            git_email: "alice@example.com".into(),
            hostname: "github.com-alice".into(),
            ssh_host: Some("chosen-alias".into()),
            ssh_key: None,
            allowed_owners: vec![],
            signing_key: None,
            signing_format: SigningFormat::Openpgp,
            require_signing: false,
        };
        migrate_ssh_alias(&mut profile);
        assert_eq!(profile.hostname, "github.com-alice");
        assert_eq!(profile.ssh_host.as_deref(), Some("chosen-alias"));
    }

    #[test]
    fn future_schema_fails_closed() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("config.toml");
        fs::write(&path, "schema_version = 99\n").unwrap();
        assert!(ConfigStore::at(path).load().is_err());
    }

    #[test]
    fn rejects_control_chars_and_whitespace_in_profiles() {
        let base = Profile {
            github_user: "alice".into(),
            git_name: "Alice".into(),
            git_email: "alice@example.com".into(),
            hostname: "github.com".into(),
            ssh_host: None,
            ssh_key: None,
            allowed_owners: vec![],
            signing_key: None,
            signing_format: SigningFormat::Openpgp,
            require_signing: false,
        };

        let mut newline_user = base.clone();
        newline_user.github_user = "alice\nadmin".into();
        assert!(newline_user.validate().is_err());

        let mut space_user = base.clone();
        space_user.github_user = "alice admin".into();
        assert!(space_user.validate().is_err());

        let mut slash_user = base.clone();
        slash_user.github_user = "alice/admin".into();
        assert!(slash_user.validate().is_err());

        let mut crlf_email = base.clone();
        crlf_email.git_email = "alice@example.com\r\ncc:bad@example.com".into();
        assert!(crlf_email.validate().is_err());
    }

    #[test]
    fn accepts_literal_ssh_public_key_for_signing() {
        let profile = Profile {
            github_user: "alice".into(),
            git_name: "Alice".into(),
            git_email: "alice@example.com".into(),
            hostname: "github.com".into(),
            ssh_host: None,
            ssh_key: None,
            allowed_owners: vec![],
            signing_key: Some(
                "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI12345 alice@example.com".into(),
            ),
            signing_format: SigningFormat::Ssh,
            require_signing: true,
        };
        assert!(profile.validate().is_ok());
        assert!(profile.validate_local_resources().is_ok());
    }
}