fluidattacks-core-git 0.53.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
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
use anyhow::{bail, Context, Result};
use std::io;
use std::num::NonZeroUsize;
use std::path::{Path, PathBuf};
use std::time::Duration;
use tokio::io::{AsyncWriteExt, BufWriter};
use tracing::warn;

use crate::errors::{DownloadError, FetchError};

const TIMEOUTS: Timeouts = Timeouts {
    connect: Duration::from_secs(30),
    total: Duration::from_hours(1),
};
const DEFAULT_DOWNLOAD_BUFFER_SIZE: usize = 64 * 1024;
const MAX_DOWNLOAD_BUFFER_SIZE: usize = 16 * 1024 * 1024;
const TMP_PREFIX: &str = "fluidattacks_";
const ARCHIVE_NAME: &str = "repo.tar.gz";
const GIT_DIR_DATA: [&str; 6] = ["HEAD", "index", "objects", "packed-refs", "refs", "shallow"];
// Inside `objects/`, these point git at object stores elsewhere on disk.
const OBJECT_STORE_POINTERS: [&str; 2] = ["alternates", "http-alternates"];
const TRUSTED_CONFIG: &str = "[core]\nrepositoryformatversion = 0\nbare = false\n";

struct Timeouts {
    connect: Duration,
    total: Duration,
}

fn resolve_buffer_size(buffer_size: Option<NonZeroUsize>) -> usize {
    buffer_size.map_or(DEFAULT_DOWNLOAD_BUFFER_SIZE, |size| {
        size.get().min(MAX_DOWNLOAD_BUFFER_SIZE)
    })
}

fn parse_url(url: &str) -> Result<url::Url, FetchError> {
    let parsed = url::Url::parse(url).map_err(|error| FetchError::InvalidUrl {
        url: url.to_owned(),
        reason: error.to_string(),
    })?;
    if !matches!(parsed.scheme(), "http" | "https") {
        return Err(FetchError::NonHttpUrl(url.to_owned()));
    }
    Ok(parsed)
}

fn request_error(error: reqwest::Error) -> FetchError {
    if error.is_connect() {
        return if error.is_timeout() {
            FetchError::ConnectTimeout(error)
        } else {
            FetchError::Connect(error)
        };
    }
    if error.is_timeout() {
        return FetchError::Timeout(error);
    }
    let mut cause: Option<&(dyn std::error::Error + 'static)> = Some(&error);
    let mut socket = None;
    while let Some(current) = cause {
        if let Some(hyper) = current.downcast_ref::<hyper::Error>() {
            if hyper.is_incomplete_message() {
                return FetchError::Disconnected(error);
            }
            if hyper.is_parse() {
                return FetchError::Malformed(error);
            }
        }
        if let Some(io) = current.downcast_ref::<io::Error>() {
            socket = Some(io);
        }
        cause = current.source();
    }
    match socket {
        Some(io) => FetchError::Socket {
            reset: io.kind() == io::ErrorKind::ConnectionReset,
            errno: io.raw_os_error(),
            source: error,
        },
        None => FetchError::Other(error),
    }
}

fn body_error(error: reqwest::Error) -> FetchError {
    if error.is_timeout() {
        FetchError::BodyTimeout(error)
    } else {
        FetchError::BodyCut(error)
    }
}

async fn fetch_to(
    url: &str,
    path: &Path,
    buffer_size: usize,
    timeouts: Timeouts,
) -> Result<(), DownloadError> {
    let url = parse_url(url)?;
    // As the python's aiohttp, the environment's proxy is ignored.
    let client = reqwest::Client::builder()
        .no_proxy()
        .timeout(timeouts.total)
        .connect_timeout(timeouts.connect)
        .build()
        .map_err(FetchError::Other)?;
    let mut response = client.get(url).send().await.map_err(request_error)?;
    let status = response.status();
    if status != reqwest::StatusCode::OK {
        return Err(FetchError::Status(status).into());
    }

    let file = tokio::fs::File::create(path)
        .await
        .map_err(DownloadError::Write)?;
    let mut writer = BufWriter::with_capacity(buffer_size, file);
    while let Some(chunk) = response.chunk().await.map_err(body_error)? {
        for slice in chunk.chunks(buffer_size) {
            writer
                .write_all(slice)
                .await
                .map_err(DownloadError::Write)?;
        }
    }
    writer.flush().await.map_err(DownloadError::Write)?;
    Ok(())
}

fn archive_parent(dest: &Path) -> Option<&Path> {
    match dest.parent() {
        Some(parent) if parent.as_os_str().is_empty() => Some(Path::new(".")),
        parent => parent,
    }
}

fn extracted_dir(tmp_path: &Path) -> Result<PathBuf> {
    let mut dirs: Vec<PathBuf> = std::fs::read_dir(tmp_path)
        .with_context(|| format!("reading {}", tmp_path.display()))?
        .filter_map(std::result::Result::ok)
        .filter(|entry| entry.file_type().is_ok_and(|kind| kind.is_dir()))
        .map(|entry| entry.path())
        .collect();
    dirs.sort();

    if dirs.len() > 1 {
        warn!(
            count = dirs.len(),
            "multiple directories found in archive, using the first one"
        );
    }

    dirs.into_iter()
        .next()
        .context("no directory found in the extracted archive")
}

fn move_or_copy(from: &Path, to: &Path) -> Result<()> {
    if let Err(rename_error) = std::fs::rename(from, to) {
        super::fs::copy_dir_all(from, to).with_context(|| {
            format!(
                "moving {} to {} failed with {rename_error}, and so did copying it",
                from.display(),
                to.display()
            )
        })?;
    }
    Ok(())
}

// A `.git` file or link would redirect git to a repository outside the archive.
fn keep_only_history(repo: &Path) -> Result<()> {
    let git_dir = repo.join(".git");
    let is_dir = std::fs::symlink_metadata(&git_dir).is_ok_and(|meta| meta.is_dir());
    if !is_dir {
        bail!("no .git directory found in the extracted archive");
    }

    for entry in
        std::fs::read_dir(&git_dir).with_context(|| format!("reading {}", git_dir.display()))?
    {
        let entry = entry.with_context(|| format!("reading {}", git_dir.display()))?;
        if GIT_DIR_DATA.iter().any(|kept| entry.file_name() == *kept) {
            continue;
        }
        let path = entry.path();
        let removed = if entry.file_type().is_ok_and(|kind| kind.is_dir()) {
            std::fs::remove_dir_all(&path)
        } else {
            std::fs::remove_file(&path)
        };
        removed.with_context(|| format!("removing {}", path.display()))?;
    }
    for pointer in OBJECT_STORE_POINTERS {
        let path = git_dir.join("objects/info").join(pointer);
        match std::fs::remove_file(&path) {
            Err(error) if error.kind() != std::io::ErrorKind::NotFound => {
                return Err(error).with_context(|| format!("removing {}", path.display()));
            }
            _ => {}
        }
    }

    let config = git_dir.join("config");
    std::fs::write(&config, TRUSTED_CONFIG).with_context(|| format!("writing {}", config.display()))
}

async fn extract_and_replace(archive_path: &Path, tmp_path: &Path, dest: &Path) -> Result<()> {
    let archive = tokio::fs::File::open(archive_path)
        .await
        .with_context(|| format!("opening {}", archive_path.display()))?;
    super::tar::extract(archive, tmp_path).await?;
    let _ = tokio::fs::remove_file(archive_path).await;

    let (tmp_path, dest) = (tmp_path.to_path_buf(), dest.to_path_buf());
    tokio::task::spawn_blocking(move || replace_destination(&tmp_path, &dest)).await?
}

fn replace_destination(tmp_path: &Path, dest: &Path) -> Result<()> {
    let extracted = extracted_dir(tmp_path)?;
    // Still inside the temporary directory, so a failure leaves nothing behind.
    keep_only_history(&extracted)?;

    if dest.exists() {
        std::fs::remove_dir_all(dest).with_context(|| format!("removing {}", dest.display()))?;
    }

    move_or_copy(&extracted, dest)
}

async fn place_repo(url: &str, dest: &Path, buffer_size: Option<NonZeroUsize>) -> bool {
    let Some(parent) = archive_parent(dest) else {
        warn!("the destination has no parent directory");
        return false;
    };
    if let Err(e) = std::fs::create_dir_all(parent) {
        warn!(error = %e, "failed to create parent dir");
        return false;
    }

    let tmp_dir = match tempfile::Builder::new().prefix(TMP_PREFIX).tempdir() {
        Ok(d) => d,
        Err(e) => {
            warn!(error = %e, "failed to create temp dir");
            return false;
        }
    };
    let tmp_path = tmp_dir.path().to_path_buf();
    let archive_path = tmp_path.join(ARCHIVE_NAME);

    let buffer_size = resolve_buffer_size(buffer_size);
    if let Err(e) = fetch_to(url, &archive_path, buffer_size, TIMEOUTS).await {
        warn!(error = %e, "failed to download");
        return false;
    }

    if let Err(e) = extract_and_replace(&archive_path, &tmp_path, dest).await {
        warn!(error = %e, "failed to extract archive");
        return false;
    }

    true
}

/// Leaves the repository behind `url` at `dest`.
///
/// The archive holds only `.git`, so the working tree is restored with a
/// `git reset --hard`. Pass `checkout = false` to keep just the history: peak
/// disk stays at the size of the archive plus `.git`, which is what a consumer
/// that walks commits needs. `git_ignore` prunes working-tree files and has no
/// effect on a history-only download. `buffer_size` bounds a single write of
/// the archive to disk; `None` takes 64 KiB, and anything above 16 MiB is
/// taken as 16 MiB. Anything already at `dest` is replaced.
/// The archive is untrusted, so only its history is kept and its config is replaced.
pub async fn download_and_extract_repo(
    url: &str,
    dest: &Path,
    git_ignore: &[String],
    buffer_size: Option<NonZeroUsize>,
    checkout: bool,
) -> bool {
    if !place_repo(url, dest, buffer_size).await {
        return false;
    }

    if !checkout {
        return true;
    }

    if super::cli::reset_hard(dest).await.is_err() {
        let _ = std::fs::remove_dir_all(dest);
        return false;
    }

    let dest_path = dest.to_path_buf();
    let patterns = git_ignore.to_vec();
    let pruned = tokio::task::spawn_blocking(move || {
        super::fs::remove_symlinks(&dest_path)?;
        super::fs::delete_gitignored_files(&dest_path, &patterns)
    })
    .await;
    match pruned {
        Ok(Ok(())) => true,
        Ok(Err(error)) => {
            warn!(error = %error, "failed to prune the working tree");
            false
        }
        Err(error) => {
            warn!(error = %error, "failed to prune the working tree");
            false
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{
        extract_and_replace, extracted_dir, fetch_to, resolve_buffer_size, Timeouts,
        DEFAULT_DOWNLOAD_BUFFER_SIZE, MAX_DOWNLOAD_BUFFER_SIZE, TRUSTED_CONFIG,
    };
    use crate::errors::{DownloadError, FetchError};
    use flate2::write::GzEncoder;
    use flate2::Compression;
    use std::num::NonZeroUsize;
    use std::path::{Path, PathBuf};
    use std::time::Duration;
    use tokio::io::{AsyncReadExt, AsyncWriteExt};
    use tokio::net::TcpListener;

    fn write_tar_gz(path: &Path, name: &str, contents: &[u8]) {
        let mut builder = tar::Builder::new(GzEncoder::new(Vec::new(), Compression::default()));
        let mut header = tar::Header::new_gnu();
        header.set_size(u64::try_from(contents.len()).unwrap());
        header.set_mode(0o644);
        builder.append_data(&mut header, name, contents).unwrap();
        let archive = builder.into_inner().unwrap().finish().unwrap();
        std::fs::write(path, archive).unwrap();
    }

    #[tokio::test]
    async fn the_archive_replaces_an_existing_destination_and_is_deleted() {
        let tmp = tempfile::tempdir().unwrap();
        let work = tmp.path().join("work");
        std::fs::create_dir_all(&work).unwrap();
        let archive_path = work.join("repo.tar.gz");
        write_tar_gz(&archive_path, "repo/.git/HEAD", b"ref: refs/heads/trunk\n");

        let dest = tmp.path().join("dest");
        std::fs::create_dir_all(&dest).unwrap();
        std::fs::write(dest.join("stale"), b"old").unwrap();

        extract_and_replace(&archive_path, &work, &dest)
            .await
            .unwrap();

        assert_eq!(
            std::fs::read_to_string(dest.join(".git/HEAD")).unwrap(),
            "ref: refs/heads/trunk\n"
        );
        assert!(!dest.join("stale").exists());
        assert!(!archive_path.exists());
    }

    #[tokio::test]
    async fn an_archive_without_a_directory_is_rejected() {
        let tmp = tempfile::tempdir().unwrap();
        let work = tmp.path().join("work");
        std::fs::create_dir_all(&work).unwrap();
        let archive_path = work.join("repo.tar.gz");
        write_tar_gz(&archive_path, "HEAD", b"ref: refs/heads/trunk\n");

        let dest = tmp.path().join("dest");

        assert!(extract_and_replace(&archive_path, &work, &dest)
            .await
            .is_err());
        assert!(!dest.exists());
    }

    const SHORT: Duration = Duration::from_secs(1);
    const LONG: Duration = Duration::from_secs(30);

    async fn answer_and_stall(mut stream: tokio::net::TcpStream, raw: &'static [u8]) {
        let mut request = [0u8; 1024];
        let _ = stream.read(&mut request).await;
        let _ = stream.write_all(raw).await;
        while stream.read(&mut request).await.is_ok_and(|read| read > 0) {}
    }

    // Answers every connection with `raw` and keeps it open, so only a
    // timeout of the client ends the request.
    async fn serve_and_stall(raw: &'static [u8]) -> String {
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let address = listener.local_addr().unwrap();
        tokio::spawn(async move {
            while let Ok((stream, _)) = listener.accept().await {
                tokio::spawn(answer_and_stall(stream, raw));
            }
        });
        format!("http://{address}/repo.tar.gz")
    }

    async fn fetch_with(url: &str, timeouts: Timeouts) -> Result<(), DownloadError> {
        let dir = tempfile::tempdir().unwrap();
        fetch_to(
            url,
            &dir.path().join("repo.tar.gz"),
            DEFAULT_DOWNLOAD_BUFFER_SIZE,
            timeouts,
        )
        .await
    }

    #[tokio::test]
    async fn a_connect_that_never_completes_is_a_connect_timeout() {
        // The connect covers the TLS handshake, which this server never answers.
        let url = serve_and_stall(b"")
            .await
            .replacen("http://", "https://", 1);

        let outcome = fetch_with(
            &url,
            Timeouts {
                connect: SHORT,
                total: LONG,
            },
        )
        .await;

        assert!(
            matches!(
                outcome,
                Err(DownloadError::Fetch(FetchError::ConnectTimeout(_)))
            ),
            "{outcome:?}"
        );
    }

    #[tokio::test]
    async fn a_response_that_never_starts_is_a_timeout_before_the_headers() {
        let url = serve_and_stall(b"").await;

        let outcome = fetch_with(
            &url,
            Timeouts {
                connect: LONG,
                total: SHORT,
            },
        )
        .await;

        assert!(
            matches!(outcome, Err(DownloadError::Fetch(FetchError::Timeout(_)))),
            "{outcome:?}"
        );
    }

    #[tokio::test]
    async fn a_body_that_stops_arriving_is_a_timeout_reading_the_body() {
        let url =
            serve_and_stall(b"HTTP/1.1 200 OK\r\nContent-Length: 1000\r\n\r\n\x1f\x8b\x08\x00")
                .await;

        let outcome = fetch_with(
            &url,
            Timeouts {
                connect: LONG,
                total: SHORT,
            },
        )
        .await;

        assert!(
            matches!(
                outcome,
                Err(DownloadError::Fetch(FetchError::BodyTimeout(_)))
            ),
            "{outcome:?}"
        );
    }

    #[test]
    fn an_absent_buffer_size_takes_the_default_of_the_python() {
        assert_eq!(resolve_buffer_size(None), DEFAULT_DOWNLOAD_BUFFER_SIZE);
        assert_eq!(resolve_buffer_size(NonZeroUsize::new(8 * 1024)), 8 * 1024);
    }

    #[test]
    fn a_buffer_size_above_the_maximum_is_clamped() {
        assert_eq!(
            resolve_buffer_size(NonZeroUsize::new(MAX_DOWNLOAD_BUFFER_SIZE + 1)),
            MAX_DOWNLOAD_BUFFER_SIZE
        );
    }

    fn git(repo: &Path, args: &[&str]) {
        let status = std::process::Command::new("git")
            .arg("-C")
            .arg(repo)
            .args([
                "-c",
                "user.email=dev@fluidattacks.com",
                "-c",
                "user.name=dev",
            ])
            .args(args)
            .status()
            .unwrap();
        assert!(status.success());
    }

    fn hostile_archive(dir: &Path, key: &str) -> (PathBuf, PathBuf) {
        let marker = dir.join("pwned");
        let payload = dir.join("payload.sh");
        std::fs::write(
            &payload,
            format!("#!/bin/sh\ntouch {}\ncat\n", marker.display()),
        )
        .unwrap();
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            std::fs::set_permissions(&payload, std::fs::Permissions::from_mode(0o755)).unwrap();
        }

        let source = dir.join("source");
        std::fs::create_dir_all(&source).unwrap();
        git(&source, &["init", "-q"]);
        std::fs::write(source.join(".gitattributes"), b"* filter=pwn\n").unwrap();
        std::fs::write(source.join("tracked.txt"), b"content\n").unwrap();
        git(&source, &["add", "-A"]);
        git(&source, &["commit", "-q", "-m", "add tracked file"]);
        git(&source, &["config", key, &payload.to_string_lossy()]);
        std::fs::create_dir_all(source.join(".git/objects/info")).unwrap();
        std::fs::write(
            source.join(".git/objects/info/alternates"),
            format!("{}\n", dir.display()),
        )
        .unwrap();

        let archive = dir.join("work/repo.tar.gz");
        std::fs::create_dir_all(archive.parent().unwrap()).unwrap();
        let mut builder = tar::Builder::new(GzEncoder::new(Vec::new(), Compression::default()));
        builder
            .append_dir_all("source/.git", source.join(".git"))
            .unwrap();
        std::fs::write(&archive, builder.into_inner().unwrap().finish().unwrap()).unwrap();
        std::fs::remove_dir_all(&source).unwrap();
        (archive, marker)
    }

    #[tokio::test]
    async fn a_restored_archive_runs_nothing_it_configures() {
        for key in ["filter.pwn.smudge", "filter.pwn.process", "core.fsmonitor"] {
            let tmp = tempfile::tempdir().unwrap();
            let (archive, marker) = hostile_archive(tmp.path(), key);
            let dest = tmp.path().join("dest");

            extract_and_replace(&archive, archive.parent().unwrap(), &dest)
                .await
                .unwrap();
            crate::cli::reset_hard(&dest).await.unwrap();

            assert!(!marker.exists(), "{key} ran");
            assert_eq!(
                std::fs::read_to_string(dest.join("tracked.txt")).unwrap(),
                "content\n"
            );
        }
    }

    #[tokio::test]
    async fn only_the_history_of_the_archive_is_kept() {
        let tmp = tempfile::tempdir().unwrap();
        let (archive, _) = hostile_archive(tmp.path(), "core.fsmonitor");
        let dest = tmp.path().join("dest");

        extract_and_replace(&archive, archive.parent().unwrap(), &dest)
            .await
            .unwrap();

        let git_dir = dest.join(".git");
        assert_eq!(
            std::fs::read_to_string(git_dir.join("config")).unwrap(),
            TRUSTED_CONFIG
        );
        assert!(!git_dir.join("hooks").exists());
        assert!(!git_dir.join("objects/info/alternates").exists());
        assert!(git_dir.join("HEAD").is_file());
    }

    #[tokio::test]
    async fn a_git_file_in_the_archive_is_rejected() {
        let tmp = tempfile::tempdir().unwrap();
        let work = tmp.path().join("work");
        std::fs::create_dir_all(&work).unwrap();
        let archive_path = work.join("repo.tar.gz");
        write_tar_gz(&archive_path, "repo/.git", b"gitdir: /elsewhere/.git\n");
        let dest = tmp.path().join("dest");

        assert!(extract_and_replace(&archive_path, &work, &dest)
            .await
            .is_err());
        assert!(!dest.exists());
    }

    #[test]
    fn the_first_directory_is_taken_in_a_stable_order() {
        let tmp = tempfile::tempdir().unwrap();
        std::fs::write(tmp.path().join("repo.tar.gz"), b"").unwrap();
        std::fs::create_dir_all(tmp.path().join("b_repo")).unwrap();
        std::fs::create_dir_all(tmp.path().join("a_repo")).unwrap();

        assert_eq!(
            extracted_dir(tmp.path()).unwrap(),
            tmp.path().join("a_repo")
        );
    }
}