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
// Auditing the authorship of a revision range.
//
// Every other check in GitBound asks "is this working copy configured
// correctly *right now*". That is the right question on a developer's machine
// and the wrong one in a pipeline, where the working copy is a fresh clone with
// no bindings and the interesting evidence is the commits themselves.
//
// So this module asks the complementary question: over a range of commits, was
// each one authored by somebody this repository accepts, by the same person who
// committed it, and signed where a signature is required.
//
// It produces a `CheckReport` like everything else, so `src/report.rs` renders
// it into SARIF, JUnit, or GitHub annotations for free.

use crate::{
    check::{CheckItem, CheckReport, CheckStatus},
    config::Profile,
    error::GitBoundError,
    policy::Policy,
    process::Runner,
};
use std::{ffi::OsString, path::Path, time::Duration};

const TIMEOUT: Duration = Duration::from_secs(30);

/// Field and record separators. Unit separator and record separator are chosen
/// because Git will not emit them from a name, an address, or a subject line,
/// so the output cannot be confused by an author who puts a comma or a newline
/// in their display name.
const FIELD: char = '\u{1f}';
const RECORD: char = '\u{1e}';

const FORMAT: &str = "--format=%H%x1f%an%x1f%ae%x1f%cn%x1f%ce%x1f%G?%x1f%s%x1e";

/// Upper bound on how many commits are examined, so an accidental `--range
/// HEAD` on a large repository cannot turn into an unbounded report.
pub const DEFAULT_MAX_COMMITS: usize = 1000;

pub struct AuditOptions<'a> {
    /// A Git revision range, e.g. `origin/main..HEAD`.
    pub range: &'a str,
    pub max_commits: usize,
    /// Fail when a commit carries no valid signature. Independent of policy so
    /// that `--enforce-signing` can turn it on for one run.
    pub require_signature: bool,
}

impl Default for AuditOptions<'_> {
    fn default() -> Self {
        Self {
            range: "HEAD",
            max_commits: DEFAULT_MAX_COMMITS,
            require_signature: false,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CommitRecord {
    pub sha: String,
    pub author_name: String,
    pub author_email: String,
    pub committer_name: String,
    pub committer_email: String,
    /// Git's `%G?` verification code.
    pub signature: char,
    pub subject: String,
}

impl CommitRecord {
    pub fn short(&self) -> &str {
        let end = self.sha.len().min(8);
        &self.sha[..end]
    }

    /// `G` good, `U` good but the key is untrusted — both mean a signature was
    /// present and verified. Everything else (`N` none, `B` bad, `X`/`Y`
    /// expired, `R` revoked, `E` unverifiable) is treated as unsigned, because
    /// a signature that cannot be trusted is not evidence of anything.
    pub fn is_signed(&self) -> bool {
        matches!(self.signature, 'G' | 'U')
    }

    fn signature_reason(&self) -> &'static str {
        match self.signature {
            'N' => "is not signed",
            'B' => "has a bad signature",
            'X' => "has a signature made by an expired key",
            'Y' => "has a signature made by a key that expired",
            'R' => "has a signature made by a revoked key",
            'E' => "has a signature that could not be verified",
            _ => "has no usable signature",
        }
    }
}

/// Read the commits in `range`. The range is validated before it reaches Git:
/// anything that looks like an option is refused, so a range cannot smuggle in
/// `--output` or `--exec`.
pub fn commits(
    runner: &dyn Runner,
    repository: &Path,
    options: &AuditOptions<'_>,
) -> Result<Vec<CommitRecord>, GitBoundError> {
    let range = options.range.trim();
    if range.is_empty() {
        return Err(GitBoundError::usage("a revision range is required"));
    }
    if range.starts_with('-') {
        return Err(GitBoundError::usage(format!(
            "'{range}' is not a revision range"
        )));
    }

    let args = vec![
        OsString::from("log"),
        OsString::from("--no-color"),
        OsString::from(format!("--max-count={}", options.max_commits)),
        OsString::from(FORMAT),
        OsString::from(range),
        OsString::from("--"),
    ];
    let output = runner.run_git_in(&args, repository, TIMEOUT)?;
    if !output.success() {
        return Err(GitBoundError::usage(format!(
            "cannot read the range '{range}': {}",
            output.stderr.trim()
        )));
    }
    Ok(parse(&output.stdout))
}

fn parse(stdout: &str) -> Vec<CommitRecord> {
    stdout
        .split(RECORD)
        // `%x1e` leaves the newline that terminated the previous record
        // attached to the front of the next one.
        .map(|record| record.trim_start_matches(['\r', '\n']))
        .filter(|record| !record.trim().is_empty())
        .filter_map(|record| {
            let mut fields = record.split(FIELD);
            Some(CommitRecord {
                sha: fields.next()?.trim().to_string(),
                author_name: fields.next()?.to_string(),
                author_email: fields.next()?.to_string(),
                committer_name: fields.next()?.to_string(),
                committer_email: fields.next()?.to_string(),
                signature: fields.next()?.chars().next().unwrap_or('N'),
                subject: fields.next().unwrap_or_default().to_string(),
            })
        })
        .collect()
}

/// Audit a range and return a report.
///
/// `policy` is the repository's committed policy, when it has one. `profile` is
/// the locally bound profile, when there is one; its address is treated as
/// implicitly allowed, so a developer running this on their own machine does
/// not have to also list themselves in the policy file.
pub fn audit(
    runner: &dyn Runner,
    repository: &Path,
    policy: Option<&Policy>,
    profile: Option<&Profile>,
    options: &AuditOptions<'_>,
) -> Result<CheckReport, GitBoundError> {
    let commits = commits(runner, repository, options)?;
    let empty = Policy::default();
    let policy = policy.unwrap_or(&empty);
    let mut checks = Vec::new();

    if commits.is_empty() {
        checks.push(CheckItem {
            id: "commit_range".into(),
            status: CheckStatus::Ok,
            expected: Some(options.range.to_string()),
            actual: Some("0 commits".into()),
            message: format!("no commits in '{}'; nothing to audit", options.range),
        });
        return Ok(report(repository, profile, checks));
    }

    let mut offenders = 0usize;
    for commit in &commits {
        let permitted = policy.allows_email(&commit.author_email).is_ok()
            || profile.is_some_and(|profile| {
                profile
                    .git_email
                    .eq_ignore_ascii_case(commit.author_email.trim())
            });
        if !permitted {
            offenders += 1;
            checks.push(CheckItem {
                id: "commit_author".into(),
                status: CheckStatus::Failure,
                expected: expectation(policy, profile),
                actual: Some(format!("{} <{}>", commit.short(), commit.author_email)),
                message: format!(
                    "{} \"{}\" was authored by {} <{}>, which this repository does not accept",
                    commit.short(),
                    commit.subject,
                    commit.author_name,
                    commit.author_email
                ),
            });
        }

        if policy.identity.require_author_matches_committer
            && !commit
                .author_email
                .eq_ignore_ascii_case(&commit.committer_email)
        {
            offenders += 1;
            checks.push(CheckItem {
                id: "commit_committer".into(),
                status: CheckStatus::Failure,
                expected: Some(commit.author_email.clone()),
                actual: Some(commit.committer_email.clone()),
                message: format!(
                    "{} was authored by {} but committed by {}",
                    commit.short(),
                    commit.author_email,
                    commit.committer_email
                ),
            });
        }

        if (options.require_signature || policy.signing.require) && !commit.is_signed() {
            offenders += 1;
            checks.push(CheckItem {
                id: "commit_signature".into(),
                status: CheckStatus::Failure,
                expected: Some("a verified signature".into()),
                actual: Some(commit.signature.to_string()),
                message: format!(
                    "{} \"{}\" {}",
                    commit.short(),
                    commit.subject,
                    commit.signature_reason()
                ),
            });
        }
    }

    if offenders == 0 {
        checks.push(CheckItem {
            id: "commit_range".into(),
            status: CheckStatus::Ok,
            expected: Some(options.range.to_string()),
            actual: Some(format!("{} commits", commits.len())),
            message: format!(
                "all {} commits in '{}' are within policy",
                commits.len(),
                options.range
            ),
        });
    }

    if commits.len() == options.max_commits {
        checks.push(CheckItem {
            id: "commit_range".into(),
            status: CheckStatus::Warning,
            expected: Some(format!("at most {} commits", options.max_commits)),
            actual: Some(format!("{} commits", commits.len())),
            message: format!(
                "the range was truncated at {} commits; narrow it or raise --max-commits to audit the rest",
                options.max_commits
            ),
        });
    }

    Ok(report(repository, profile, checks))
}

fn expectation(policy: &Policy, profile: Option<&Profile>) -> Option<String> {
    let mut parts: Vec<String> = Vec::new();
    parts.extend(policy.identity.allowed_emails.iter().cloned());
    parts.extend(
        policy
            .identity
            .allowed_email_domains
            .iter()
            .map(|domain| format!("*@{domain}")),
    );
    if let Some(profile) = profile {
        parts.push(profile.git_email.clone());
    }
    if parts.is_empty() {
        None
    } else {
        parts.sort();
        parts.dedup();
        Some(parts.join(", "))
    }
}

fn report(repository: &Path, profile: Option<&Profile>, checks: Vec<CheckItem>) -> CheckReport {
    CheckReport {
        repository: repository.display().to_string(),
        profile: profile.map(|profile| profile.github_user.clone()),
        remote: None,
        overall: CheckReport::overall_of(&checks),
        checks,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{policy::IdentityPolicy, process::ProcessOutput};
    use std::sync::Mutex;

    struct FakeRunner {
        stdout: String,
        code: i32,
        seen: Mutex<Vec<Vec<OsString>>>,
    }

    impl FakeRunner {
        fn ok(stdout: &str) -> Self {
            Self {
                stdout: stdout.into(),
                code: 0,
                seen: Mutex::new(Vec::new()),
            }
        }
    }

    impl Runner for FakeRunner {
        fn run(
            &self,
            _program: &str,
            args: &[OsString],
            _timeout: Duration,
        ) -> Result<ProcessOutput, GitBoundError> {
            self.seen.lock().expect("lock").push(args.to_vec());
            Ok(ProcessOutput {
                code: Some(self.code),
                stdout: self.stdout.clone(),
                stderr: String::new(),
            })
        }

        fn run_in(
            &self,
            program: &str,
            args: &[OsString],
            _cwd: &Path,
            timeout: Duration,
        ) -> Result<ProcessOutput, GitBoundError> {
            self.run(program, args, timeout)
        }
    }

    fn line(sha: &str, email: &str, signature: char) -> String {
        format!(
            "{sha}\u{1f}Alice\u{1f}{email}\u{1f}Alice\u{1f}{email}\u{1f}{signature}\u{1f}a change\u{1e}\n"
        )
    }

    fn company_policy() -> Policy {
        Policy {
            identity: IdentityPolicy {
                allowed_email_domains: vec!["company.example".into()],
                ..IdentityPolicy::default()
            },
            ..Policy::default()
        }
    }

    #[test]
    fn parses_every_field_of_every_record() {
        let stdout = format!(
            "{}{}",
            line("aaaaaaaaaaaa", "alice@company.example", 'G'),
            line("bbbbbbbbbbbb", "bob@personal.example", 'N')
        );
        let parsed = parse(&stdout);
        assert_eq!(parsed.len(), 2);
        assert_eq!(parsed[0].short(), "aaaaaaaa");
        assert_eq!(parsed[0].author_email, "alice@company.example");
        assert!(parsed[0].is_signed());
        assert_eq!(parsed[1].author_email, "bob@personal.example");
        assert!(!parsed[1].is_signed());
    }

    #[test]
    fn a_disallowed_author_fails_the_range() {
        let runner = FakeRunner::ok(&format!(
            "{}{}",
            line("aaaaaaaaaaaa", "alice@company.example", 'G'),
            line("bbbbbbbbbbbb", "bob@personal.example", 'G')
        ));
        let policy = company_policy();
        let report = audit(
            &runner,
            Path::new("/tmp/project"),
            Some(&policy),
            None,
            &AuditOptions {
                range: "origin/main..HEAD",
                ..AuditOptions::default()
            },
        )
        .expect("audit");
        assert!(!report.enforceable());
        let offenders: Vec<_> = report
            .checks
            .iter()
            .filter(|check| check.id == "commit_author")
            .collect();
        assert_eq!(offenders.len(), 1);
        assert!(offenders[0].actual.as_deref().unwrap().contains("bbbbbbbb"));
    }

    #[test]
    fn a_clean_range_reports_one_summary_check() {
        let runner = FakeRunner::ok(&line("aaaaaaaaaaaa", "alice@company.example", 'G'));
        let policy = company_policy();
        let report = audit(
            &runner,
            Path::new("/tmp/project"),
            Some(&policy),
            None,
            &AuditOptions::default(),
        )
        .expect("audit");
        assert!(report.enforceable());
        assert_eq!(report.checks.len(), 1);
        assert_eq!(report.checks[0].id, "commit_range");
    }

    #[test]
    fn the_bound_profile_is_implicitly_allowed() {
        let runner = FakeRunner::ok(&line("aaaaaaaaaaaa", "alice@personal.example", 'G'));
        let policy = company_policy();
        let profile = Profile {
            github_user: "alice".into(),
            git_name: "Alice".into(),
            git_email: "alice@personal.example".into(),
            hostname: "github.com".into(),
            ssh_host: None,
            ssh_key: None,
            allowed_owners: Vec::new(),
            signing_key: None,
            signing_format: crate::config::SigningFormat::default(),
            require_signing: false,
        };
        let report = audit(
            &runner,
            Path::new("/tmp/project"),
            Some(&policy),
            Some(&profile),
            &AuditOptions::default(),
        )
        .expect("audit");
        assert!(report.enforceable(), "{:?}", report.checks);
    }

    #[test]
    fn required_signatures_are_enforced_per_commit() {
        let runner = FakeRunner::ok(&line("aaaaaaaaaaaa", "alice@company.example", 'N'));
        let policy = company_policy();
        let report = audit(
            &runner,
            Path::new("/tmp/project"),
            Some(&policy),
            None,
            &AuditOptions {
                require_signature: true,
                ..AuditOptions::default()
            },
        )
        .expect("audit");
        assert!(!report.enforceable());
        assert!(report.checks.iter().any(|c| c.id == "commit_signature"));
    }

    #[test]
    fn an_option_shaped_range_is_refused_before_reaching_git() {
        let runner = FakeRunner::ok("");
        let error = commits(
            &runner,
            Path::new("/tmp/project"),
            &AuditOptions {
                range: "--output=/tmp/pwned",
                ..AuditOptions::default()
            },
        )
        .expect_err("must refuse");
        assert!(error.to_string().contains("not a revision range"));
        assert!(
            runner.seen.lock().expect("lock").is_empty(),
            "git must not have been invoked"
        );
    }

    #[test]
    fn an_empty_range_is_reported_rather_than_passing_silently() {
        let runner = FakeRunner::ok("");
        let report = audit(
            &runner,
            Path::new("/tmp/project"),
            None,
            None,
            &AuditOptions::default(),
        )
        .expect("audit");
        assert!(report.enforceable());
        assert!(report.checks[0].message.contains("nothing to audit"));
    }
}