fluidattacks-core-git 0.67.0

Fluid Attacks Core 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
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
use anyhow::{Context, Result};
use base64::Engine;
use http::uri::{PathAndQuery, Uri};
use std::fmt;
use std::path::Path;
use tokio::process::Command;
use url::Url;

use super::{build_clone_args, reason, redirect_config, stall_config, CloneOpts};
use crate::errors::{GitCallError, HttpsAccessError};
use crate::types::CredentialKind;

const USER_VAR: &str = "FLUIDATTACKS_GIT_USER";
const PASSWORD_VAR: &str = "FLUIDATTACKS_GIT_PASSWORD";

// Answers only `get`, so git has nowhere to store or erase the credential.
fn env_helper() -> String {
    format!(
        "!f() {{ if [ \"$1\" = get ]; then \
         printf 'username=%s\\npassword=%s\\n' \"${USER_VAR}\" \"${PASSWORD_VAR}\"; fi; }}; f"
    )
}

// Read as git and curl read it (RFC 3986), host and path left as written: git
// normalizes the url and the helper key alike when it matches one to the other.
struct BareUrl {
    url: String,
    origin: String,
}

/// How git reaches a repo over https, with the credential kept off its command
/// line and out of the url, so that git has none of it to print back.
///
/// `git` hands the credential to the repo's own host and to no other, and
/// never lets git prompt. `secret` is what is still worth taking out of an
/// error, because a server may echo what it was sent.
pub struct HttpsAccess {
    url: String,
    secret: Option<String>,
    config: Vec<(String, String)>,
    env: Vec<(String, String)>,
}

// By hand, so that printing it, in a log or a failed unwrap, leaves the login out.
impl fmt::Debug for HttpsAccess {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("HttpsAccess")
            .field("url", &self.url)
            .finish_non_exhaustive()
    }
}

impl HttpsAccess {
    pub fn new(url: &str, credential: Option<&CredentialKind>) -> Result<Self, HttpsAccessError> {
        match credential {
            None => Ok(Self::public(url)),
            Some(CredentialKind::Https { user, password }) => Self::basic(url, user, password),
            Some(CredentialKind::Token {
                token,
                is_pat,
                oauth_type,
            }) => {
                let (user, password) = token_login(token, oauth_type, *is_pat);
                Ok(Self {
                    secret: Some(token.clone()),
                    ..Self::basic(url, user, password)?
                })
            }
            Some(CredentialKind::Ssh { .. } | CredentialKind::AwsRole { .. }) => {
                Err(HttpsAccessError::NotHttpsCredential)
            }
        }
    }

    fn public(url: &str) -> Self {
        Self {
            url: url.to_owned(),
            secret: None,
            config: Vec::new(),
            env: Vec::new(),
        }
    }

    fn basic(raw_url: &str, user: &str, password: &str) -> Result<Self, HttpsAccessError> {
        if [user, password]
            .iter()
            .any(|value| value.contains(['\n', '\r', '\0']))
        {
            return Err(HttpsAccessError::CredentialLineBreak);
        }
        let url = bare_url(raw_url)?;
        Ok(Self {
            secret: Some(password.to_owned()),
            config: vec![(format!("credential.{}.helper", url.origin), env_helper())],
            env: vec![
                (USER_VAR.to_owned(), user.to_owned()),
                (PASSWORD_VAR.to_owned(), password.to_owned()),
            ],
            url: url.url,
        })
    }

    #[must_use]
    pub fn url(&self) -> &str {
        &self.url
    }

    /// A git command for this repo, still waiting for its subcommand.
    #[must_use]
    pub fn git(&self) -> Command {
        let mut command = Command::new("git");
        // The empty helper drops whatever helpers the machine has configured,
        // so the credential is neither looked up in nor saved to them. Going
        // on the command line, it comes after any config git inherits.
        command.args(["-c", "credential.helper="]);
        for (key, value) in &self.config {
            command.arg("-c").arg(format!("{key}={value}"));
        }
        command
            .env("GIT_TERMINAL_PROMPT", "0")
            // Unset, git keeps credentials out of its own traces.
            .env_remove("GIT_TRACE_REDACT")
            // Its data dump prints what a server sends back, the Basic header included.
            .env_remove("GIT_TRACE_CURL")
            .envs(self.env.iter().cloned());
        command
    }

    #[must_use]
    pub fn secret(&self) -> Option<&str> {
        self.secret.as_deref()
    }
}

fn bare_url(raw_url: &str) -> Result<BareUrl, HttpsAccessError> {
    let uri: Uri = raw_url.parse().map_err(|_| HttpsAccessError::Unparsable)?;
    let scheme = uri
        .scheme_str()
        .filter(|scheme| matches!(*scheme, "http" | "https"))
        .ok_or(HttpsAccessError::NotHttp)?;
    let authority = uri.authority().ok_or(HttpsAccessError::Unparsable)?;
    let port = authority
        .port()
        .map_or_else(String::new, |port| format!(":{port}"));
    let host = format!("{}{port}", authority.host());
    // An empty host, or a port the parser could not read and left out.
    if authority.host().is_empty() || !authority.as_str().ends_with(&host) {
        return Err(HttpsAccessError::Unparsable);
    }
    let origin = format!("{scheme}://{host}");
    let path = uri.path_and_query().map_or("", PathAndQuery::as_str);
    Ok(BareUrl {
        url: format!("{origin}{path}"),
        origin,
    })
}

// A token with no provider is the user itself, with no password. A PAT takes
// any user but an empty one, since git sends no credential without a user.
const fn token_login<'a>(token: &'a str, provider: &str, is_pat: bool) -> (&'a str, &'a str) {
    if is_pat {
        ("pat", token)
    } else if provider.eq_ignore_ascii_case("BITBUCKET") {
        ("x-token-auth", token)
    } else if provider.is_empty() {
        (token, "")
    } else {
        ("oauth2", token)
    }
}

pub async fn clone_https(opts: &CloneOpts, dest: &Path) -> Result<()> {
    let kind = &opts
        .credentials
        .as_ref()
        .context("missing credentials")?
        .kind;
    if !matches!(kind, CredentialKind::Https { .. }) {
        anyhow::bail!("expected HTTPS credentials");
    }
    Ok(run_git_clone(opts, dest, &HttpsAccess::new(&opts.repo_url, Some(kind))?).await?)
}

pub async fn clone_https_token(opts: &CloneOpts, dest: &Path) -> Result<()> {
    let kind = &opts
        .credentials
        .as_ref()
        .context("missing credentials")?
        .kind;
    if !matches!(kind, CredentialKind::Token { .. }) {
        anyhow::bail!("expected Token credentials");
    }
    Ok(run_git_clone(opts, dest, &HttpsAccess::new(&opts.repo_url, Some(kind))?).await?)
}

pub async fn clone_https_public(opts: &CloneOpts, dest: &Path) -> Result<()> {
    Ok(run_git_clone(opts, dest, &HttpsAccess::public(&opts.repo_url)).await?)
}

#[must_use]
pub fn build_https_config_args(
    follow_redirects: bool,
    is_pat: bool,
    token: Option<&str>,
    disable_credential_prompt: bool,
) -> Vec<String> {
    let mut args = vec![
        "-c".to_owned(),
        "http.sslVerify=false".to_owned(),
        "-c".to_owned(),
        format!("http.followRedirects={follow_redirects}"),
    ];
    if is_pat {
        if let Some(t) = token {
            let encoded = base64::engine::general_purpose::STANDARD.encode(format!(":{t}"));
            args.extend([
                "-c".to_owned(),
                format!("http.extraHeader=Authorization: Basic {encoded}"),
            ]);
        }
    }
    if disable_credential_prompt {
        args.extend(["-c".to_owned(), "credential.helper=".to_owned()]);
    }
    args
}

#[allow(clippy::too_many_arguments)]
pub fn format_https_url(
    repo_url: &str,
    user: Option<&str>,
    password: Option<&str>,
    token: Option<&str>,
    provider: Option<&str>,
    is_pat: bool,
) -> Result<String> {
    let mut parsed = Url::parse(repo_url).context("parsing URL")?;
    // Re-quote the path
    let path = parsed.path().to_owned();
    parsed.set_path(&path);

    if is_pat {
        return Ok(parsed.to_string());
    }

    if let Some(t) = token {
        let user_part = match provider {
            Some(p) if p.eq_ignore_ascii_case("BITBUCKET") => "x-token-auth",
            Some("") | None => "",
            Some(_) => "oauth2",
        };
        if user_part.is_empty() {
            // No provider: use token as username, no password
            parsed
                .set_username(t)
                .map_err(|()| anyhow::anyhow!("set username"))?;
            parsed
                .set_password(None)
                .map_err(|()| anyhow::anyhow!("set password"))?;
        } else {
            // Use username/password separately to avoid percent-encoding the colon
            parsed
                .set_username(user_part)
                .map_err(|()| anyhow::anyhow!("set username"))?;
            parsed
                .set_password(Some(t))
                .map_err(|()| anyhow::anyhow!("set password"))?;
        }
        return Ok(parsed.to_string());
    }

    if let (Some(u), Some(p)) = (user, password) {
        parsed
            .set_username(u)
            .map_err(|()| anyhow::anyhow!("set username"))?;
        parsed
            .set_password(Some(p))
            .map_err(|()| anyhow::anyhow!("set password"))?;
        return Ok(parsed.to_string());
    }

    Ok(parsed.to_string())
}

pub(super) async fn run_git_clone(
    opts: &CloneOpts,
    dest: &Path,
    access: &HttpsAccess,
) -> Result<(), GitCallError> {
    let mut command = access.git();
    command.args(clone_args(opts, dest, access));
    reason::run(&mut command, access.secret().as_slice()).await
}

fn clone_args(opts: &CloneOpts, dest: &Path, access: &HttpsAccess) -> Vec<std::ffi::OsString> {
    let mut extra_config = vec!["-c".to_owned(), "http.sslVerify=false".to_owned()];
    extra_config.extend(redirect_config(opts.follow_redirects));
    extra_config.extend(stall_config());
    build_clone_args(opts, access.url(), dest, &extra_config)
}

#[cfg(test)]
mod tests {
    use super::{clone_args, format_https_url, token_login, HttpsAccess};
    use crate::clone::tests::opts;
    use crate::clone::{CloneOpts, CloneShape};
    use crate::errors::HttpsAccessError;
    use crate::types::CredentialKind;
    use std::ffi::OsString;

    fn args(access: &HttpsAccess) -> Vec<String> {
        access
            .git()
            .as_std()
            .get_args()
            .map(|arg| arg.to_string_lossy().into_owned())
            .collect()
    }

    fn token(token: &str, provider: &str) -> CredentialKind {
        CredentialKind::Token {
            token: token.to_owned(),
            is_pat: false,
            oauth_type: provider.to_owned(),
        }
    }

    #[test]
    fn a_token_with_no_provider_logs_in_as_the_user_with_no_password() {
        assert_eq!(token_login("tok", "", false), ("tok", ""));
    }

    #[test]
    fn the_provider_names_the_user_even_off_its_own_domain() {
        assert_eq!(
            token_login("tok", "BITBUCKET", false),
            ("x-token-auth", "tok")
        );
        assert_eq!(token_login("tok", "GITLAB", false), ("oauth2", "tok"));
    }

    #[test]
    fn the_provider_is_matched_ignoring_the_case() {
        let url = "https://host/a/b.git";

        assert_eq!(
            format_https_url(url, None, None, Some("tok"), Some("Bitbucket"), false).unwrap(),
            format_https_url(url, None, None, Some("tok"), Some("BITBUCKET"), false).unwrap()
        );
        assert_eq!(
            token_login("tok", "Bitbucket", false),
            token_login("tok", "BITBUCKET", false)
        );
    }

    fn basic(user: &str, password: &str) -> CredentialKind {
        CredentialKind::Https {
            user: user.to_owned(),
            password: password.to_owned(),
        }
    }

    #[test]
    fn the_url_drops_whatever_credential_it_carried() {
        let access = HttpsAccess::new(
            "https://olduser:oldpass@host/a/b.git",
            Some(&token("tok", "")),
        )
        .unwrap();
        assert_eq!(access.url(), "https://host/a/b.git");
    }

    #[test]
    fn a_pat_logs_in_as_a_placeholder_user_whatever_the_provider() {
        assert_eq!(token_login("tok", "", true), ("pat", "tok"));
        assert_eq!(token_login("tok", "BITBUCKET", true), ("pat", "tok"));
    }

    #[test]
    fn the_host_is_left_as_written_for_git_to_read() {
        for (url, expected) in [
            ("https://Host:443/a.git", "https://Host:443/a.git"),
            ("https://[::1]:8443/a.git", "https://[::1]:8443/a.git"),
            ("https://u:p@w@host/a.git", "https://host/a.git"),
        ] {
            let access = HttpsAccess::new(url, Some(&token("tok", ""))).unwrap();
            let origin = expected.trim_end_matches("/a.git");
            assert_eq!(access.url(), expected, "{url}");
            let helper = format!("credential.{origin}.helper=");
            let host = args(&access).get(3).cloned().unwrap_or_default();
            assert!(host.starts_with(&helper), "{url}");
        }
    }

    #[test]
    fn each_failure_says_what_the_python_said_and_never_the_url() {
        for (url, expected) in [
            ("ftp://u:hunter2@host/a.git", HttpsAccessError::NotHttp),
            ("host/a.git", HttpsAccessError::Unparsable),
            ("https:u:hunter2@host/a.git", HttpsAccessError::Unparsable),
            ("https:///host/a.git", HttpsAccessError::Unparsable),
            ("https://u:hunter2@/a.git", HttpsAccessError::Unparsable),
            ("https://u:hunter2@:443/a.git", HttpsAccessError::Unparsable),
            (
                "https://u:hunter2@host:99999/a.git",
                HttpsAccessError::Unparsable,
            ),
            (
                "https://u:hunter2@faß.de/a.git",
                HttpsAccessError::Unparsable,
            ),
            (
                "https://tok\\hunter2@host/a.git",
                HttpsAccessError::Unparsable,
            ),
        ] {
            let error = HttpsAccess::new(url, Some(&token("tok", ""))).unwrap_err();
            assert_eq!(error, expected, "{url}");
            assert!(!error.to_string().contains("hunter2"), "{url}");
        }
        assert_eq!(
            HttpsAccessError::NotHttp.to_string(),
            "expected an http or https url"
        );
        assert_eq!(
            HttpsAccessError::Unparsable.to_string(),
            "Invalid url: its host or port could not be parsed"
        );
    }

    #[test]
    fn no_credential_leaves_the_url_unchecked() {
        let access = HttpsAccess::new("ftp://host:99999/a.git", None).unwrap();
        assert_eq!(access.url(), "ftp://host:99999/a.git");
        assert_eq!(args(&access), ["-c", "credential.helper="]);
    }

    #[test]
    fn the_debug_shows_neither_the_login_nor_the_environment() {
        let access =
            HttpsAccess::new("https://host/a.git", Some(&basic("someone", "s3cr3t"))).unwrap();
        let shown = format!("{access:?}");
        assert!(shown.contains("https://host/a.git"), "{shown}");
        assert!(!shown.contains("s3cr3t"), "{shown}");
        assert!(!shown.contains("someone"), "{shown}");
    }

    #[test]
    fn the_machine_helpers_are_dropped_before_the_host_helper() {
        let access = HttpsAccess::new("https://host/a.git", Some(&token("tok", ""))).unwrap();
        let args = args(&access);
        let [flag, machine, host_flag, host] = args.as_slice() else {
            panic!("{args:?}");
        };
        assert_eq!(
            [flag, machine, host_flag],
            ["-c", "credential.helper=", "-c"]
        );
        assert!(
            host.starts_with("credential.https://host.helper=!f()"),
            "{host}"
        );
    }

    #[test]
    fn the_login_goes_through_the_environment() {
        let access = HttpsAccess::new("https://host/a.git", Some(&basic("u", "p"))).unwrap();
        let envs: Vec<_> = access
            .git()
            .as_std()
            .get_envs()
            .map(|(name, value)| (name.to_owned(), value.map(ToOwned::to_owned)))
            .collect();
        let set = |value: &str| Some(OsString::from(value));
        assert_eq!(
            envs,
            [
                ("FLUIDATTACKS_GIT_PASSWORD".into(), set("p")),
                ("FLUIDATTACKS_GIT_USER".into(), set("u")),
                ("GIT_TERMINAL_PROMPT".into(), set("0")),
                ("GIT_TRACE_CURL".into(), None),
                ("GIT_TRACE_REDACT".into(), None),
            ]
        );
    }

    #[test]
    fn a_credential_with_a_line_break_or_a_nul_is_refused() {
        for credential in [
            token("tok\nhost=x", ""),
            token("tok\0x", "GITLAB"),
            basic("u\rx", "p"),
            basic("u", "p\nx"),
        ] {
            let error = HttpsAccess::new("https://host/a/b.git", Some(&credential)).unwrap_err();
            assert_eq!(error, HttpsAccessError::CredentialLineBreak);
            assert_eq!(
                error.to_string(),
                "Invalid credentials: a line break or a nul byte"
            );
        }
    }

    #[test]
    fn an_empty_provider_formats_the_url_like_no_provider_at_all() {
        let url = "https://gitlab.com/a/b.git";
        let no_provider = format_https_url(url, None, None, Some("tok"), None, false).unwrap();
        assert_eq!(no_provider, "https://tok@gitlab.com/a/b.git");
        assert_eq!(
            format_https_url(url, None, None, Some("tok"), Some(""), false).unwrap(),
            no_provider
        );
    }

    #[test]
    fn a_redirect_is_followed_only_when_the_root_asks_for_one() {
        for follow_redirects in [false, true] {
            let opts = CloneOpts {
                follow_redirects,
                ..opts(CloneShape::SingleBranch)
            };
            let access = HttpsAccess::new(&opts.repo_url, None).unwrap();

            let args = clone_args(&opts, std::path::Path::new("/tmp/a_root"), &access);

            let config = format!("http.followRedirects={follow_redirects}");
            assert!(args.windows(2).any(|pair| pair == ["-c", config.as_str()]));
        }
    }

    #[test]
    fn a_clone_that_stops_receiving_data_is_cut_after_ten_minutes() {
        let opts = opts(CloneShape::SingleBranch);
        let access = HttpsAccess::new(&opts.repo_url, None).unwrap();

        let args = clone_args(&opts, std::path::Path::new("/tmp/a_root"), &access);

        assert!(args
            .windows(2)
            .any(|pair| pair == ["-c", "http.lowSpeedLimit=1000"]));
        assert!(args
            .windows(2)
            .any(|pair| pair == ["-c", "http.lowSpeedTime=600"]));
    }
}