girt 0.2.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
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
//! Pure HTTP policy selection; callers own environment capture and CA file reads.

use reqwest::Url;

use super::HttpError;
use crate::Config;
use crate::config::boolean;
use crate::remote::{Destination, Protocol};

/// Application-supplied HTTP inputs. Girt does not read Git/proxy environment variables.
///
/// Default trust uses the TLS backend's platform verifier, which may consult OS trust settings
/// and native certificate environment variables. This is not a Git/libcurl backend selector.
#[derive(Default, Clone)]
pub struct HttpEnvironment {
    /// Selected CA bundle content, read and bounded by the application (at most 1 MiB).
    ///
    /// Select `GIT_SSL_CAINFO` first, otherwise [`HttpSettings::configured_ca_info`]. A present
    /// bundle replaces platform roots for this connection. It must contain certificates.
    pub ssl_ca_info: Option<Vec<u8>>,
    /// `GIT_SSL_NO_VERIFY`; disabling verification also requires `allow_insecure_tls`.
    pub ssl_no_verify: Option<bool>,
    /// `https_proxy`, `http_proxy`, or `all_proxy` selected by the application.
    /// Configured HTTP and per-remote proxies take precedence; an empty configured proxy disables
    /// it.
    pub proxy: Option<String>,
    /// `no_proxy` selected by the application.
    pub no_proxy: Option<String>,
    /// Approved Basic credentials for the selected proxy, never parsed from a proxy URL.
    pub proxy_credentials: Option<(String, String)>,
    /// Explicit application authorization for disabling certificate and hostname checks.
    pub allow_insecure_tls: bool,
}

impl std::fmt::Debug for HttpEnvironment {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("HttpEnvironment").finish_non_exhaustive()
    }
}

/// Resolved HTTP policy for one R21 destination. Its Debug output omits URLs and secrets.
pub struct HttpSettings {
    pub(super) url: String,
    pub(super) roots: Vec<Vec<u8>>,
    pub(super) proxy: Option<String>,
    pub(super) no_proxy: Option<String>,
    pub(super) proxy_credentials: Option<(String, String)>,
    pub(super) verify_tls: bool,
    pub(super) follow_initial_redirect: bool,
}

impl std::fmt::Debug for HttpSettings {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("HttpSettings").finish_non_exhaustive()
    }
}

impl HttpSettings {
    /// Resolves HTTP policy for a destination without a named remote.
    ///
    /// Environment CA bytes and verification override configuration. Configured proxies override
    /// the environment proxy, including an empty value that disables proxying. Use
    /// [`Self::resolve_for_remote`] when a named remote supplies proxy policy.
    ///
    /// URL sections match scheme, host (including whole-label `*`), effective port and
    /// component-bounded path. More specific hosts, then longer paths, then later entries win.
    /// Only `initial` and false redirect policies are supported. The default is `initial`.
    ///
    /// # Errors
    ///
    /// Rejects unsupported HTTP settings, malformed policy, configured CA paths without supplied
    /// bytes, and disabled verification without application approval. No file or network I/O
    /// occurs.
    pub fn resolve(
        config: &Config,
        destination: &Destination,
        environment: &HttpEnvironment,
    ) -> Result<Self, HttpError> {
        Self::resolve_policy(config, destination, environment, None)
    }

    /// Resolves HTTP policy including `remote.<name>.proxy` and proxy authentication policy.
    ///
    /// The caller supplies the selected remote's exact name and its resolved destination.
    /// Remote proxy configuration overrides URL-scoped/global HTTP configuration and environment;
    /// an empty value explicitly disables proxying. Only approved Basic proxy credentials are
    /// supported, and proxy URLs must omit userinfo.
    ///
    /// # Errors
    ///
    /// Returns the errors from [`Self::resolve`], including unsupported proxy authentication.
    pub fn resolve_for_remote(
        config: &Config,
        name: &[u8],
        destination: &Destination,
        environment: &HttpEnvironment,
    ) -> Result<Self, HttpError> {
        Self::resolve_policy(config, destination, environment, Some(name))
    }

    /// Returns the selected `http.sslCAInfo` path bytes without opening the file.
    ///
    /// Call only when `GIT_SSL_CAINFO` is absent; that environment path takes precedence.
    /// The caller expands Git path syntax, resolves relative paths against its working directory,
    /// authorizes the read, and loads at most 1 MiB into [`HttpEnvironment::ssl_ca_info`].
    /// Returned bytes may contain private paths and must not be logged automatically.
    ///
    /// # Errors
    ///
    /// Rejects a non-HTTP destination or an implicit, empty, or NUL-containing CA path.
    pub fn configured_ca_info<'a>(
        config: &'a Config,
        destination: &Destination,
    ) -> Result<Option<&'a [u8]>, HttpError> {
        let target = destination_url(destination)?;
        http_value(config, &target, "sslcainfo")
            .map(|value| {
                value
                    .filter(|path| !path.is_empty() && !path.contains(&0))
                    .ok_or(HttpError::Configuration("TLS CA path"))
            })
            .transpose()
    }

    fn resolve_policy(
        config: &Config,
        destination: &Destination,
        environment: &HttpEnvironment,
        remote: Option<&[u8]>,
    ) -> Result<Self, HttpError> {
        let target = destination_url(destination)?;
        let url = std::str::from_utf8(destination.bytes())
            .expect("validated URL")
            .to_owned();
        reject_unsupported(config, &target)?;
        let configured_verify = http_value(config, &target, "sslverify")
            .map(|value| boolean(value).ok_or(HttpError::Configuration("TLS verification")))
            .transpose()?;
        let verify_tls = environment
            .ssl_no_verify
            .map(|disabled| !disabled)
            .unwrap_or(configured_verify.unwrap_or(true));
        if !verify_tls && !environment.allow_insecure_tls {
            return Err(HttpError::Configuration(
                "insecure TLS requires application approval",
            ));
        }
        let ca_configured = http_value(config, &target, "sslcainfo").is_some();
        if ca_configured && environment.ssl_ca_info.is_none() {
            return Err(HttpError::Configuration(
                "TLS CA file requires application input",
            ));
        }
        if environment
            .ssl_ca_info
            .as_ref()
            .is_some_and(|ca| ca.len() > 1024 * 1024)
        {
            return Err(HttpError::Limit("trust roots"));
        }
        let roots = environment.ssl_ca_info.iter().cloned().collect();
        let follow_initial_redirect = match http_value(config, &target, "followredirects") {
            None | Some(Some(b"initial")) => true,
            Some(value) if boolean(value) == Some(false) => false,
            _ => return Err(HttpError::Configuration("redirect policy")),
        };
        let proxy_auth = remote
            .and_then(|name| remote_value(config, name, "proxyauthmethod"))
            .or_else(|| http_value(config, &target, "proxyauthmethod"));
        if proxy_auth.is_some_and(|value| value != Some(b"basic")) {
            return Err(HttpError::Configuration("unsupported proxy authentication"));
        }
        let configured_proxy = remote
            .and_then(|name| remote_value(config, name, "proxy"))
            .or_else(|| http_value(config, &target, "proxy"));
        let proxy = match configured_proxy {
            Some(value) => Some(
                std::str::from_utf8(value.ok_or(HttpError::Configuration("proxy"))?)
                    .map_err(|_| HttpError::Configuration("proxy URL encoding"))?
                    .to_owned(),
            ),
            None => environment.proxy.clone(),
        };
        let proxy = proxy.filter(|value| !value.is_empty());
        if let Some(value) = &proxy {
            if value.len() > 8192 {
                return Err(HttpError::Limit("proxy URL"));
            }
            let parsed = Url::parse(value).map_err(|_| HttpError::Configuration("proxy URL"))?;
            if !matches!(parsed.scheme(), "http")
                || parsed.host_str().is_none()
                || !parsed.username().is_empty()
                || parsed.password().is_some()
                || parsed.query().is_some()
                || parsed.fragment().is_some()
                || parsed.path() != "/"
                || value.contains('@')
                || value
                    .bytes()
                    .any(|b| b.is_ascii_whitespace() || b.is_ascii_control())
            {
                return Err(HttpError::Configuration("proxy URL"));
            }
        }
        Ok(Self {
            url,
            roots,
            proxy,
            no_proxy: environment.no_proxy.clone(),
            proxy_credentials: environment.proxy_credentials.clone(),
            verify_tls,
            follow_initial_redirect,
        })
    }
}

fn http_value<'a>(config: &'a Config, target: &Url, name: &str) -> Option<Option<&'a [u8]>> {
    let mut selected = None;
    let mut best = (0, 0);
    let mut scoped = false;
    for entry in config.entries() {
        if !entry.section.eq_ignore_ascii_case(b"http")
            || !entry.name.eq_ignore_ascii_case(name.as_bytes())
        {
            continue;
        }
        let Some(scope) = entry.subsection.as_deref() else {
            if !scoped {
                selected = Some(entry.value.as_deref());
            }
            continue;
        };
        let Ok(scope) = std::str::from_utf8(scope) else {
            continue;
        };
        let Ok(scope) = Url::parse(scope) else {
            continue;
        };
        let Some(host_score) = host_match(scope.host_str(), target.host_str()) else {
            continue;
        };
        let scope_path = normalized_path(scope.path());
        let target_path = normalized_path(target.path());
        let path = scope_path.strip_suffix('/').unwrap_or(&scope_path);
        let score = (host_score, path.len());
        if scope.scheme() == target.scheme()
            && scope.port_or_known_default() == target.port_or_known_default()
            && scope.username().is_empty()
            && scope.query().is_none()
            && scope.fragment().is_none()
            && target_path.starts_with(path)
            && (target_path.len() == path.len()
                || target_path.as_bytes().get(path.len()) == Some(&b'/'))
            && score >= best
        {
            selected = Some(entry.value.as_deref());
            best = score;
            scoped = true;
        }
    }
    selected
}

fn destination_url(destination: &Destination) -> Result<Url, HttpError> {
    if !matches!(destination.protocol(), Protocol::Http | Protocol::Https) {
        return Err(HttpError::Configuration("HTTP destination"));
    }
    let url = std::str::from_utf8(destination.bytes())
        .map_err(|_| HttpError::Configuration("repository URL encoding"))?;
    Url::parse(url).map_err(|_| HttpError::Configuration("repository URL"))
}

fn remote_value<'a>(config: &'a Config, remote: &[u8], name: &str) -> Option<Option<&'a [u8]>> {
    config
        .entries()
        .iter()
        .rev()
        .find(|entry| {
            entry.section.eq_ignore_ascii_case(b"remote")
                && entry.subsection.as_deref() == Some(remote)
                && entry.name.eq_ignore_ascii_case(name.as_bytes())
        })
        .map(|entry| entry.value.as_deref())
}

// Fail closed: a configured pin, client certificate, alternate backend or CA directory must not
// silently disappear when a caller switches from Git. Unrelated URL scopes remain irrelevant.
fn reject_unsupported(config: &Config, target: &Url) -> Result<(), HttpError> {
    for entry in config.entries() {
        if !entry.section.eq_ignore_ascii_case(b"http") {
            continue;
        }
        let name = std::str::from_utf8(&entry.name)
            .map_err(|_| HttpError::Configuration("HTTP setting name"))?;
        if ![
            "sslverify",
            "sslcainfo",
            "proxy",
            "proxyauthmethod",
            "followredirects",
        ]
        .iter()
        .any(|supported| name.eq_ignore_ascii_case(supported))
            && http_value(config, target, name).is_some()
        {
            return Err(HttpError::Configuration("unsupported HTTP setting"));
        }
    }
    Ok(())
}

fn host_match(scope: Option<&str>, target: Option<&str>) -> Option<usize> {
    let (scope, target) = (scope?, target?);
    let scope_parts: Vec<_> = scope.split('.').collect();
    let target_parts: Vec<_> = target.split('.').collect();
    (scope_parts.len() == target_parts.len()
        && scope_parts
            .iter()
            .zip(target_parts)
            .all(|(s, t)| *s == "*" || *s == t))
    .then(|| scope.bytes().filter(|byte| *byte != b'*').count())
}

// Git URL matching normalizes unreserved escapes while preserving escaped separators.
fn normalized_path(path: &str) -> String {
    let mut result = String::new();
    let mut bytes = path.bytes();
    while let Some(byte) = bytes.next() {
        if byte == b'%' {
            let high = bytes.next();
            let low = bytes.next();
            if let (Some(high), Some(low)) = (high, low) {
                let value = (high as char)
                    .to_digit(16)
                    .zip((low as char).to_digit(16))
                    .map(|(h, l)| (h * 16 + l) as u8);
                if let Some(value) =
                    value.filter(|b| b.is_ascii_alphanumeric() || b"-._~".contains(b))
                {
                    result.push(value as char);
                } else {
                    result.push('%');
                    result.push(high.to_ascii_uppercase() as char);
                    result.push(low.to_ascii_uppercase() as char);
                }
            } else {
                result.push('%');
                result.extend(high.map(char::from));
            }
        } else {
            result.push(byte as char);
        }
    }
    result
}

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

    use super::*;
    use crate::remote::{ProtocolEnvironment, Remote};

    fn configuration(settings: &str) -> (Config, Destination) {
        let config = Config::parse(
            format!("[remote \"r\"]\nurl=https://example.test/repo/sub\n{settings}").as_bytes(),
        )
        .unwrap();
        let destination = Remote::find(&config, b"r")
            .unwrap()
            .unwrap()
            .fetch_destination(&config, &ProtocolEnvironment::default())
            .unwrap();
        (config, destination)
    }

    #[rstest]
    #[case::default("", None, true)]
    #[case::config_false("[http]\nsslVerify=false\n", None, false)]
    #[case::environment_true("[http]\nsslVerify=true\n", Some(true), false)]
    #[case::environment_false("[http]\nsslVerify=false\n", Some(false), true)]
    fn verification_precedence(
        #[case] config: &str,
        #[case] disabled: Option<bool>,
        #[case] expected: bool,
    ) {
        let (config, destination) = configuration(config);
        let environment = HttpEnvironment {
            ssl_no_verify: disabled,
            allow_insecure_tls: true,
            ..Default::default()
        };
        let settings = HttpSettings::resolve(&config, &destination, &environment).unwrap();
        assert_eq!(settings.verify_tls, expected);
    }

    #[rstest]
    #[case::environment("", Some("http://environment.test"))]
    #[case::global("[http]\nproxy=http://global.test\n", Some("http://global.test"))]
    #[case::disabled("[http]\nproxy=\n", None)]
    #[case::remote(
        "[http]\nproxy=http://global.test\n[remote \"r\"]\nproxy=http://remote.test\n",
        Some("http://remote.test")
    )]
    #[case::remote_disabled("[http]\nproxy=http://global.test\n[remote \"r\"]\nproxy=\n", None)]
    #[case::unrelated_remote(
        "[remote \"other\"]\nproxy=http://other.test\n",
        Some("http://environment.test")
    )]
    fn proxy_precedence(#[case] config: &str, #[case] expected: Option<&str>) {
        let (config, destination) = configuration(config);
        let environment = HttpEnvironment {
            proxy: Some("http://environment.test".into()),
            ..Default::default()
        };
        let settings =
            HttpSettings::resolve_for_remote(&config, b"r", &destination, &environment).unwrap();
        assert_eq!(settings.proxy.as_deref(), expected);
    }

    #[rstest]
    #[case::pin("pinnedPubkey=secret")]
    #[case::ca_directory("sslCAPath=secret")]
    #[case::client_certificate("sslCert=secret")]
    #[case::backend("sslBackend=secret")]
    #[case::proxy_ca("proxySSLCAInfo=secret")]
    #[case::headers("extraHeader=secret")]
    #[case::auth("proxyAuthMethod=negotiate")]
    fn unsupported_settings_fail_without_values(#[case] setting: &str) {
        let (config, destination) = configuration(&format!("[http]\n{setting}\n"));
        let error =
            HttpSettings::resolve(&config, &destination, &HttpEnvironment::default()).unwrap_err();
        assert!(matches!(error, HttpError::Configuration(_)));
        assert!(!format!("{error:?} {error}").contains("secret"));
    }

    #[test]
    fn unsupported_policy_for_another_origin_does_not_apply() {
        let (config, destination) =
            configuration("[http \"https://other.test\"]\npinnedPubkey=secret\n");
        HttpSettings::resolve(&config, &destination, &HttpEnvironment::default()).unwrap();
    }

    #[rstest]
    #[case::userinfo("http://user:secret@proxy.test")]
    #[case::empty_userinfo("http://@proxy.test")]
    #[case::socks("socks5://proxy.test")]
    #[case::https_proxy("https://proxy.test")]
    #[case::path("http://proxy.test/secret")]
    #[case::query("http://proxy.test/?secret")]
    fn unsupported_proxy_urls_fail(#[case] proxy: &str) {
        let (config, destination) = configuration("");
        let environment = HttpEnvironment {
            proxy: Some(proxy.into()),
            ..Default::default()
        };
        let error = HttpSettings::resolve(&config, &destination, &environment).unwrap_err();
        assert!(matches!(error, HttpError::Configuration("proxy URL")));
        assert!(!format!("{error:?}").contains("secret"));
    }

    #[test]
    fn ca_path_selection_does_not_open_files() {
        let (config, destination) = configuration(
            "[http]\nsslCAInfo=global\n[http \"https://example.test/repo\"]\nsslCAInfo=selected\n",
        );
        assert_eq!(
            HttpSettings::configured_ca_info(&config, &destination).unwrap(),
            Some(b"selected".as_slice())
        );
        assert!(HttpSettings::resolve(&config, &destination, &HttpEnvironment::default()).is_err());
    }

    #[rstest]
    #[case::implicit("sslCAInfo")]
    #[case::empty("sslCAInfo=")]
    fn invalid_ca_paths_are_refused(#[case] setting: &str) {
        let (config, destination) = configuration(&format!("[http]\n{setting}\n"));
        assert!(HttpSettings::configured_ca_info(&config, &destination).is_err());
    }

    #[test]
    fn explicit_verification_needs_no_insecure_approval() {
        let (config, destination) = configuration("[http]\nsslVerify=false\n");
        let environment = HttpEnvironment {
            ssl_no_verify: Some(false),
            ..Default::default()
        };
        let settings = HttpSettings::resolve(&config, &destination, &environment).unwrap();
        assert!(settings.verify_tls);
    }

    #[test]
    fn oversized_ca_input_is_refused_during_resolution() {
        let (config, destination) = configuration("");
        let environment = HttpEnvironment {
            ssl_ca_info: Some(vec![0; 1024 * 1024 + 1]),
            ..Default::default()
        };
        assert!(matches!(
            HttpSettings::resolve(&config, &destination, &environment),
            Err(HttpError::Limit("trust roots"))
        ));
    }

    #[test]
    fn remote_proxy_authentication_overrides_global_policy() {
        let (config, destination) = configuration(
            "[http]\nproxyAuthMethod=negotiate\n[remote \"r\"]\nproxyAuthMethod=basic\n",
        );
        HttpSettings::resolve_for_remote(&config, b"r", &destination, &HttpEnvironment::default())
            .unwrap();
        assert!(HttpSettings::resolve(&config, &destination, &HttpEnvironment::default()).is_err());
    }
}