girt 0.18.0

An incremental, idiomatic Rust implementation of Git
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
//! TLS file settings compared with the Git CLI against a loopback HTTPS server that requires a
//! client certificate, and the application's certificate check, which Git has no equivalent of.
//! OpenSSL generates disposable CAs, a localhost server leaf and a client leaf; no system trust
//! store or real remote is used or modified.
#![cfg(feature = "http")]
#[allow(dead_code)]
#[path = "support/http_git.rs"]
mod http_git;
#[allow(dead_code)]
#[path = "support/pack_git.rs"]
mod pack_git;

use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::sync::{Arc, Mutex};

use girt::Repository;
use girt::transport::http::{CertificateCheck, CertificateDecision, ServerCertificate};
use girt::workflow::{Environment, NoCallbacks, TransferCallbacks};
use http_git::Server;
use pack_git::{Fixture, git};
use rstest::rstest;

fn openssl(root: &Path, args: &[&str]) {
    let output = Command::new("openssl")
        .current_dir(root)
        .args(args)
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "{}",
        String::from_utf8_lossy(&output.stderr)
    );
}

// Leaf certificate `name` signed by `ca`, with the given extended key usage.
fn leaf(root: &Path, name: &str, ca: &str, usage: &str) {
    std::fs::write(
        root.join(format!("{name}.cnf")),
        "[req]\ndistinguished_name=dn\nprompt=no\n[dn]\nCN=localhost\n",
    )
    .unwrap();
    std::fs::write(
        root.join(format!("{name}.ext")),
        format!(
            "subjectAltName=DNS:localhost\nbasicConstraints=critical,CA:FALSE\n\
             keyUsage=critical,digitalSignature,keyEncipherment\nextendedKeyUsage={usage}\n"
        ),
    )
    .unwrap();
    let [cnf, key, csr, ext, pem] =
        ["cnf", "key", "csr", "ext", "pem"].map(|e| format!("{name}.{e}"));
    openssl(
        root,
        &[
            "req", "-new", "-newkey", "rsa:2048", "-nodes", "-config", &cnf, "-keyout", &key,
            "-out", &csr,
        ],
    );
    let [ca_pem, ca_key] = ["pem", "key"].map(|e| format!("{ca}.{e}"));
    openssl(
        root,
        &[
            "x509",
            "-req",
            "-in",
            &csr,
            "-CA",
            &ca_pem,
            "-CAkey",
            &ca_key,
            "-CAcreateserial",
            "-days",
            "1",
            "-extfile",
            &ext,
            "-out",
            &pem,
        ],
    );
}

fn certificate_authority(root: &Path, name: &str) {
    std::fs::write(
        root.join(format!("{name}.cnf")),
        format!(
            "[req]\ndistinguished_name=dn\nx509_extensions=ext\nprompt=no\n[dn]\nCN=Girt {name}\n\
             [ext]\nbasicConstraints=critical,CA:TRUE\nkeyUsage=critical,keyCertSign,cRLSign\n"
        ),
    )
    .unwrap();
    let [cnf, key, pem] = ["cnf", "key", "pem"].map(|e| format!("{name}.{e}"));
    openssl(
        root,
        &[
            "req", "-x509", "-newkey", "rsa:2048", "-nodes", "-days", "1", "-config", &cnf,
            "-keyout", &key, "-out", &pem,
        ],
    );
}

// Files: ca.pem, server.{pem,key}, client.{pem,key}, combined.pem
// (client certificate and key).
fn certificates(root: &Path) {
    certificate_authority(root, "ca");
    leaf(root, "server", "ca", "serverAuth");
    leaf(root, "client", "ca", "clientAuth");
    let mut combined = std::fs::read(root.join("client.pem")).unwrap();
    combined.extend(std::fs::read(root.join("client.key")).unwrap());
    std::fs::write(root.join("combined.pem"), combined).unwrap();
}

// `{certs}` in settings and environment values becomes the certificate directory.
#[rstest]
#[case::no_client_certificate(&["sslCAInfo={certs}/ca.pem"], &[], false)]
#[case::certificate_and_key(&["sslCAInfo={certs}/ca.pem", "sslCert={certs}/client.pem", "sslKey={certs}/client.key"], &[], true)]
#[case::combined_file(&["sslCAInfo={certs}/ca.pem", "sslCert={certs}/combined.pem"], &[], true)]
#[case::certificate_without_key(&["sslCAInfo={certs}/ca.pem", "sslCert={certs}/client.pem"], &[], false)]
#[case::environment_certificate(&["sslCAInfo={certs}/ca.pem"], &[("GIT_SSL_CERT", "{certs}/combined.pem")], true)]
#[case::environment_beats_setting(&["sslCAInfo={certs}/ca.pem", "sslCert={certs}/combined.pem"], &[("GIT_SSL_CERT", "{certs}/client.pem")], false)]
#[case::environment_replaces_empty_setting(&["sslCAInfo={certs}/ca.pem", "sslCert="], &[("GIT_SSL_CERT", "{certs}/combined.pem")], true)]
#[case::environment_key(&["sslCAInfo={certs}/ca.pem", "sslCert={certs}/client.pem"], &[("GIT_SSL_KEY", "{certs}/client.key")], true)]
fn tls_files_match_git(
    #[case] settings: &[&str],
    #[case] variables: &[(&str, &str)],
    #[case] succeeds: bool,
) {
    let certs = tempfile::tempdir().unwrap();
    certificates(certs.path());
    let source = Fixture::new(girt::ObjectFormat::Sha1, true, 2);
    let server = Server::requiring_client_certificate(
        source.root.path(),
        (
            &certs.path().join("server.pem"),
            &certs.path().join("server.key"),
        ),
        &certs.path().join("ca.pem"),
    );
    let url = server.url.replace("127.0.0.1", "localhost");
    let expand = |value: &str| value.replace("{certs}", certs.path().to_str().unwrap());

    let local = tempfile::tempdir().unwrap();
    git(local.path(), &["init", "--quiet", "."], b"");
    git(local.path(), &["remote", "add", "origin", &url], b"");
    for setting in settings {
        let (name, value) = setting.split_once('=').unwrap();
        let value = &expand(value);
        git(
            local.path(),
            &["config", &format!("http.{name}"), value],
            b"",
        );
    }

    let mut command = Command::new("git");
    for (key, _) in std::env::vars_os() {
        let key_text = key.to_string_lossy().to_ascii_lowercase();
        if key_text.starts_with("git_") || key_text.ends_with("_proxy") {
            command.env_remove(key);
        }
    }
    command
        .current_dir(local.path())
        .env("HOME", local.path())
        .env("GIT_CONFIG_NOSYSTEM", "1")
        .env("GIT_CONFIG_GLOBAL", local.path().join("absent-config"))
        .env("GIT_TERMINAL_PROMPT", "0")
        .env_remove("SSH_AUTH_SOCK")
        .args(["-c", "credential.helper=", "-c", "core.askPass="])
        .args(["ls-remote", "origin"])
        .stdin(Stdio::null());
    let mut environment = Environment::default().with("HOME", local.path());
    for (name, value) in variables {
        command.env(name, expand(value));
        environment = environment.with(*name, PathBuf::from(expand(value)));
    }
    let output = command.output().unwrap();
    assert_eq!(
        output.status.success(),
        succeeds,
        "git: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let repository = Repository::open(local.path()).unwrap();
    let result = girt::workflow::remote_head(&repository, "origin", &environment, &mut NoCallbacks);
    assert_eq!(result.is_ok(), succeeds, "girt: {:?}", result.err());
}

#[test]
fn password_protected_certificates_are_refused() {
    let config = girt::Config::parse(
        b"[remote \"r\"]\nurl = https://localhost/repo\n[http]\nsslCertPasswordProtected = true\n",
    )
    .unwrap();
    let destination = girt::remote::Remote::find(&config, b"r")
        .unwrap()
        .unwrap()
        .fetch_destination(&config, &girt::remote::ProtocolEnvironment::default())
        .unwrap();
    let resolved = girt::transport::http::HttpSettings::resolve(
        &config,
        &destination,
        &girt::transport::http::HttpEnvironment::default(),
    );
    assert!(matches!(
        resolved,
        Err(girt::transport::http::HttpError::Configuration(_))
    ));
}

// Records what the check saw and answers with a fixed decision.
struct Checking {
    decision: fn(&ServerCertificate<'_>, &[u8]) -> CertificateDecision,
    pinned: Vec<u8>,
    seen: Arc<Mutex<Vec<(String, usize, bool)>>>,
}

impl TransferCallbacks for Checking {
    fn certificate_check(&self) -> Option<Arc<dyn CertificateCheck>> {
        let decision = self.decision;
        let pinned = self.pinned.clone();
        let seen = self.seen.clone();
        Some(Arc::new(move |server: &ServerCertificate<'_>| {
            seen.lock().unwrap().push((
                server.host().to_owned(),
                server.intermediates().len(),
                server.verification().is_ok(),
            ));
            decision(server, &pinned)
        }))
    }
}

fn accept_pinned(server: &ServerCertificate<'_>, pinned: &[u8]) -> CertificateDecision {
    if server.end_entity() == pinned {
        CertificateDecision::Accept
    } else {
        CertificateDecision::UseDefault
    }
}

// `trusted` configures the fixture CA with `http.sslCAInfo`; otherwise girt's own verification
// fails with an unknown issuer.
#[rstest]
#[case::pinned_untrusted_certificate(false, accept_pinned, true, true)]
#[case::pin_mismatch_keeps_failure(false, |_: &ServerCertificate<'_>, _: &[u8]| CertificateDecision::UseDefault, false, true)]
#[case::reject_trusted_certificate(true, |_: &ServerCertificate<'_>, _: &[u8]| CertificateDecision::Reject, false, true)]
#[case::default_keeps_trust(true, |_: &ServerCertificate<'_>, _: &[u8]| CertificateDecision::UseDefault, true, true)]
#[case::accept_overrides_unknown_issuer(false, |_: &ServerCertificate<'_>, _: &[u8]| CertificateDecision::Accept, true, true)]
fn certificate_check_decides_server_trust(
    #[case] trusted: bool,
    #[case] decision: fn(&ServerCertificate<'_>, &[u8]) -> CertificateDecision,
    #[case] succeeds: bool,
    #[case] checked: bool,
) {
    let certs = tempfile::tempdir().unwrap();
    certificates(certs.path());
    openssl(
        certs.path(),
        &[
            "x509",
            "-in",
            "server.pem",
            "-outform",
            "der",
            "-out",
            "server.der",
        ],
    );
    let pinned = if trusted {
        Vec::new()
    } else {
        std::fs::read(certs.path().join("server.der")).unwrap()
    };
    let source = Fixture::new(girt::ObjectFormat::Sha1, true, 2);
    let server = Server::new(
        source.root.path(),
        "",
        "",
        Some((
            &certs.path().join("server.pem"),
            &certs.path().join("server.key"),
        )),
    );
    let local = tempfile::tempdir().unwrap();
    git(local.path(), &["init", "--quiet", "."], b"");
    let url = server.url.replace("127.0.0.1", "localhost");
    git(local.path(), &["remote", "add", "origin", &url], b"");
    if trusted {
        let ca = certs.path().join("ca.pem");
        git(
            local.path(),
            &["config", "http.sslCAInfo", ca.to_str().unwrap()],
            b"",
        );
    }
    let seen = Arc::new(Mutex::new(Vec::new()));
    let mut callbacks = Checking {
        decision,
        pinned,
        seen: seen.clone(),
    };
    let repository = Repository::open(local.path()).unwrap();
    let environment = Environment::default().with("HOME", local.path());
    let result = girt::workflow::remote_head(&repository, "origin", &environment, &mut callbacks);
    assert_eq!(result.is_ok(), succeeds, "{:?}", result.err());
    let seen = seen.lock().unwrap();
    assert_eq!(!seen.is_empty(), checked);
    for (host, intermediates, verified) in seen.iter() {
        assert_eq!(host, "localhost");
        assert_eq!(*intermediates, 0);
        assert_eq!(*verified, trusted);
    }
}

// With a check installed girt builds the TLS configuration itself, so client certificates and
// disabled verification must behave as without one.
#[rstest]
#[case::client_certificate(true, false, |_: &ServerCertificate<'_>, _: &[u8]| CertificateDecision::UseDefault, true)]
#[case::verification_off(false, true, |_: &ServerCertificate<'_>, _: &[u8]| CertificateDecision::UseDefault, true)]
#[case::verification_off_rejected(false, true, |_: &ServerCertificate<'_>, _: &[u8]| CertificateDecision::Reject, false)]
fn certificate_check_keeps_tls_settings(
    #[case] client_certificate: bool,
    #[case] no_verify: bool,
    #[case] decision: fn(&ServerCertificate<'_>, &[u8]) -> CertificateDecision,
    #[case] succeeds: bool,
) {
    let certs = tempfile::tempdir().unwrap();
    certificates(certs.path());
    let source = Fixture::new(girt::ObjectFormat::Sha1, true, 2);
    let tls = (
        &*certs.path().join("server.pem"),
        &*certs.path().join("server.key"),
    );
    let ca = certs.path().join("ca.pem");
    let server = if client_certificate {
        Server::requiring_client_certificate(source.root.path(), tls, &ca)
    } else {
        Server::new(source.root.path(), "", "", Some(tls))
    };
    let local = tempfile::tempdir().unwrap();
    git(local.path(), &["init", "--quiet", "."], b"");
    let url = server.url.replace("127.0.0.1", "localhost");
    git(local.path(), &["remote", "add", "origin", &url], b"");
    let mut environment = Environment::default().with("HOME", local.path());
    if client_certificate {
        let combined = certs.path().join("combined.pem");
        for (name, value) in [("http.sslCAInfo", &ca), ("http.sslCert", &combined)] {
            git(
                local.path(),
                &["config", name, value.to_str().unwrap()],
                b"",
            );
        }
    }
    if no_verify {
        environment = environment.with("GIT_SSL_NO_VERIFY", "1");
    }
    let mut callbacks = Checking {
        decision,
        pinned: Vec::new(),
        seen: Arc::new(Mutex::new(Vec::new())),
    };
    let repository = Repository::open(local.path()).unwrap();
    let result = girt::workflow::remote_head(&repository, "origin", &environment, &mut callbacks);
    assert_eq!(result.is_ok(), succeeds, "{:?}", result.err());
}

// Fetching opens one connection per request; a resumed session would skip the check on the later
// ones.
#[test]
fn certificate_check_runs_for_every_connection() {
    let certs = tempfile::tempdir().unwrap();
    certificates(certs.path());
    let source = Fixture::new(girt::ObjectFormat::Sha1, true, 2);
    let server = Server::new(
        source.root.path(),
        "",
        "",
        Some((
            &certs.path().join("server.pem"),
            &certs.path().join("server.key"),
        )),
    );
    let local = tempfile::tempdir().unwrap();
    git(local.path(), &["init", "--quiet", "."], b"");
    let url = server.url.replace("127.0.0.1", "localhost");
    git(local.path(), &["remote", "add", "origin", &url], b"");
    let ca = certs.path().join("ca.pem");
    git(
        local.path(),
        &["config", "http.sslCAInfo", ca.to_str().unwrap()],
        b"",
    );
    let seen = Arc::new(Mutex::new(Vec::new()));
    let mut callbacks = Checking {
        decision: |_, _| CertificateDecision::UseDefault,
        pinned: Vec::new(),
        seen: seen.clone(),
    };
    let repository = Repository::open(local.path()).unwrap();
    let environment = Environment::default().with("HOME", local.path());
    girt::workflow::fetch(
        &repository,
        "origin",
        &girt::workflow::FetchOptions::new(["refs/heads/*:refs/remotes/origin/*"]),
        &environment,
        &mut callbacks,
    )
    .unwrap();
    let requests = server.requests().len();
    assert!(requests >= 2, "{requests}");
    assert_eq!(seen.lock().unwrap().len(), requests);
}