provenant-cli 0.0.38

Rust-based ScanCode-compatible scanner for licenses, package metadata, SBOMs, and provenance data.
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
// SPDX-FileCopyrightText: Provenant contributors
// SPDX-License-Identifier: Apache-2.0

use std::fs::{self, File};
use std::io::{Read, Write};
use std::path::{Component, Path, PathBuf};
use std::process::Command;
use std::time::Duration;

use anyhow::{Context, Result, anyhow, bail};
use base64::Engine;
use base64::engine::general_purpose::STANDARD;
use bzip2::read::BzDecoder;
use flate2::read::GzDecoder;
use liblzma::read::XzDecoder;
use reqwest::blocking::Client;
use reqwest::redirect::Policy;
use tar::Archive;
use tempfile::TempDir;
use url::Url;
use zip::ZipArchive;

use crate::serve_api::SyncScanInput;

const DOWNLOAD_TIMEOUT: Duration = Duration::from_secs(30);
const CONNECT_TIMEOUT: Duration = Duration::from_secs(10);
const MAX_REDIRECTS: usize = 5;
const MAX_REMOTE_INPUT_BYTES: u64 = 100 * 1024 * 1024;
const MAX_UPLOADED_INPUT_BYTES: usize = 16 * 1024 * 1024;
const MAX_ARCHIVE_ENTRY_BYTES: u64 = 50 * 1024 * 1024;
const MAX_ARCHIVE_TOTAL_BYTES: u64 = 100 * 1024 * 1024;
const MAX_ARCHIVE_ENTRY_COUNT: usize = 10_000;

#[derive(Debug)]
pub(super) struct PreparedSyncInput {
    pub(super) paths: Vec<PathBuf>,
    pub(super) staging_dir: Option<TempDir>,
    pub(super) strip_staging_root: bool,
}

pub(super) fn prepare_sync_input(input: SyncScanInput) -> Result<PreparedSyncInput> {
    match input {
        SyncScanInput::Paths { paths } => prepare_paths_input(paths),
        SyncScanInput::Repository { url, reference } => prepare_repository_input(&url, &reference),
        SyncScanInput::Url { url } => prepare_url_input(&url),
        SyncScanInput::Upload {
            filename,
            content_base64,
        } => prepare_upload_input(&filename, &content_base64),
    }
}

fn prepare_paths_input(paths: Vec<String>) -> Result<PreparedSyncInput> {
    if paths.is_empty() {
        return Err(anyhow!("input.paths must contain at least one path"));
    }

    let paths: Vec<PathBuf> = paths.into_iter().map(PathBuf::from).collect();
    for path in &paths {
        if !path.exists() {
            return Err(anyhow!("input path does not exist: {}", path.display()));
        }
    }

    Ok(PreparedSyncInput {
        paths,
        staging_dir: None,
        strip_staging_root: false,
    })
}

fn prepare_repository_input(url: &str, reference: &str) -> Result<PreparedSyncInput> {
    if url.trim().is_empty() {
        return Err(anyhow!("repository.url must not be empty"));
    }
    if reference.trim().is_empty() {
        return Err(anyhow!("repository.ref must not be empty"));
    }

    let staging_dir = TempDir::new().context("failed to create repository staging directory")?;
    let repo_dir = staging_dir.path().join("repository");

    run_git(
        Command::new("git").arg("init").arg(&repo_dir),
        "failed to initialize repository staging checkout",
    )?;
    run_git(
        Command::new("git")
            .current_dir(&repo_dir)
            .args(["remote", "add", "origin", url]),
        "failed to configure repository staging remote",
    )?;
    run_git(
        Command::new("git")
            .current_dir(&repo_dir)
            .args(["fetch", "--depth", "1", "origin", reference]),
        "failed to fetch repository ref for remote ingestion",
    )?;
    run_git(
        Command::new("git")
            .current_dir(&repo_dir)
            .args(["checkout", "--detach", "FETCH_HEAD"]),
        "failed to checkout fetched repository ref",
    )?;

    Ok(PreparedSyncInput {
        paths: vec![repo_dir],
        staging_dir: Some(staging_dir),
        strip_staging_root: true,
    })
}

fn prepare_url_input(url: &str) -> Result<PreparedSyncInput> {
    if url.trim().is_empty() {
        return Err(anyhow!("url.url must not be empty"));
    }

    let parsed_url = Url::parse(url).context("url.url must be a valid URL")?;
    if !matches!(parsed_url.scheme(), "http" | "https") {
        return Err(anyhow!("url.url must use http or https"));
    }

    let staging_dir = TempDir::new().context("failed to create URL staging directory")?;
    let download_dir = staging_dir.path().join("download");
    fs::create_dir_all(&download_dir)
        .with_context(|| format!("failed to create {}", download_dir.display()))?;

    let artifact_path = download_remote_input(url, &download_dir)?;
    materialize_downloaded_artifact(staging_dir, artifact_path)
}

fn prepare_upload_input(filename: &str, content_base64: &str) -> Result<PreparedSyncInput> {
    let normalized_filename = validate_upload_filename(filename)?;
    let decoded = STANDARD
        .decode(content_base64)
        .context("upload.content_base64 must be valid base64")?;

    if decoded.is_empty() {
        return Err(anyhow!(
            "upload.content_base64 must not decode to an empty payload"
        ));
    }

    if decoded.len() > MAX_UPLOADED_INPUT_BYTES {
        return Err(anyhow!(
            "upload payload exceeds max size of {} bytes",
            MAX_UPLOADED_INPUT_BYTES
        ));
    }

    let staging_dir = TempDir::new().context("failed to create upload staging directory")?;
    let upload_dir = staging_dir.path().join("upload");
    fs::create_dir_all(&upload_dir)
        .with_context(|| format!("failed to create {}", upload_dir.display()))?;

    let artifact_path = upload_dir.join(normalized_filename);
    fs::write(&artifact_path, decoded)
        .with_context(|| format!("failed to write {}", artifact_path.display()))?;

    materialize_downloaded_artifact(staging_dir, artifact_path)
}

fn materialize_downloaded_artifact(
    staging_dir: TempDir,
    artifact_path: PathBuf,
) -> Result<PreparedSyncInput> {
    let artifact_name = artifact_path
        .file_name()
        .and_then(|name| name.to_str())
        .unwrap_or("downloaded");

    if looks_like_supported_archive(artifact_name) {
        let extract_dir = staging_dir.path().join("extracted");
        fs::create_dir_all(&extract_dir)
            .with_context(|| format!("failed to create {}", extract_dir.display()))?;
        extract_archive(&artifact_path, &extract_dir)?;
        return Ok(PreparedSyncInput {
            paths: vec![extract_dir],
            staging_dir: Some(staging_dir),
            strip_staging_root: true,
        });
    }

    Ok(PreparedSyncInput {
        paths: vec![artifact_path],
        staging_dir: Some(staging_dir),
        strip_staging_root: true,
    })
}

fn download_remote_input(url: &str, output_dir: &Path) -> Result<PathBuf> {
    let client = Client::builder()
        .connect_timeout(CONNECT_TIMEOUT)
        .timeout(DOWNLOAD_TIMEOUT)
        .redirect(Policy::limited(MAX_REDIRECTS))
        .build()
        .context("failed to build remote-ingestion HTTP client")?;

    let mut response = client
        .get(url)
        .header("User-Agent", "provenant-serve/remote-ingestion")
        .send()
        .with_context(|| format!("failed to fetch remote input from {url}"))?
        .error_for_status()
        .with_context(|| format!("remote input fetch returned an error status for {url}"))?;

    if response
        .content_length()
        .is_some_and(|content_length| content_length > MAX_REMOTE_INPUT_BYTES)
    {
        return Err(anyhow!(
            "remote input exceeds max size of {} bytes",
            MAX_REMOTE_INPUT_BYTES
        ));
    }

    let filename = derive_download_filename(response.url());
    let output_path = output_dir.join(filename);
    let mut output_file = File::create(&output_path)
        .with_context(|| format!("failed to create {}", output_path.display()))?;

    let mut total_bytes = 0u64;
    let mut buffer = [0u8; 8192];
    loop {
        let read_bytes = response
            .read(&mut buffer)
            .with_context(|| format!("failed to read remote input from {url}"))?;
        if read_bytes == 0 {
            break;
        }
        total_bytes += read_bytes as u64;
        if total_bytes > MAX_REMOTE_INPUT_BYTES {
            return Err(anyhow!(
                "remote input exceeds max size of {} bytes",
                MAX_REMOTE_INPUT_BYTES
            ));
        }
        output_file
            .write_all(&buffer[..read_bytes])
            .with_context(|| format!("failed to write {}", output_path.display()))?;
    }

    Ok(output_path)
}

fn derive_download_filename(url: &Url) -> String {
    let candidate = url
        .path_segments()
        .and_then(|mut segments| segments.rfind(|segment| !segment.is_empty()))
        .filter(|segment| !segment.trim().is_empty())
        .unwrap_or("downloaded");
    validate_upload_filename(candidate).unwrap_or_else(|_| "downloaded".to_string())
}

fn validate_upload_filename(filename: &str) -> Result<String> {
    let path = Path::new(filename);
    let mut components = path.components();
    let Some(Component::Normal(component)) = components.next() else {
        return Err(anyhow!("upload.filename must be a simple file name"));
    };
    if components.next().is_some() {
        return Err(anyhow!("upload.filename must be a simple file name"));
    }
    let normalized = component
        .to_str()
        .ok_or_else(|| anyhow!("upload.filename must be valid UTF-8"))?;
    if normalized.trim().is_empty() {
        return Err(anyhow!("upload.filename must not be empty"));
    }
    Ok(normalized.to_string())
}

fn looks_like_supported_archive(filename: &str) -> bool {
    let filename = filename.to_ascii_lowercase();
    [".zip", ".tar", ".tar.gz", ".tgz", ".tar.bz2", ".tar.xz"]
        .iter()
        .any(|suffix| filename.ends_with(suffix))
}

fn extract_archive(archive_path: &Path, output_dir: &Path) -> Result<()> {
    let filename = archive_path
        .file_name()
        .and_then(|name| name.to_str())
        .unwrap_or_default()
        .to_ascii_lowercase();

    if filename.ends_with(".zip") {
        return extract_zip_archive(archive_path, output_dir);
    }
    if filename.ends_with(".tar.gz") || filename.ends_with(".tgz") {
        return extract_tar_archive(
            archive_path,
            output_dir,
            GzDecoder::new(
                File::open(archive_path)
                    .with_context(|| format!("failed to open {}", archive_path.display()))?,
            ),
        );
    }
    if filename.ends_with(".tar.bz2") {
        return extract_tar_archive(
            archive_path,
            output_dir,
            BzDecoder::new(
                File::open(archive_path)
                    .with_context(|| format!("failed to open {}", archive_path.display()))?,
            ),
        );
    }
    if filename.ends_with(".tar.xz") {
        return extract_tar_archive(
            archive_path,
            output_dir,
            XzDecoder::new(
                File::open(archive_path)
                    .with_context(|| format!("failed to open {}", archive_path.display()))?,
            ),
        );
    }
    if filename.ends_with(".tar") {
        return extract_tar_archive(
            archive_path,
            output_dir,
            File::open(archive_path)
                .with_context(|| format!("failed to open {}", archive_path.display()))?,
        );
    }

    Err(anyhow!(
        "unsupported archive format for remote ingestion: {}",
        archive_path.display()
    ))
}

fn extract_zip_archive(archive_path: &Path, output_dir: &Path) -> Result<()> {
    let file = File::open(archive_path)
        .with_context(|| format!("failed to open {}", archive_path.display()))?;
    let mut archive = ZipArchive::new(file)
        .with_context(|| format!("failed to read zip archive {}", archive_path.display()))?;

    let mut extracted_files = 0usize;
    let mut extracted_bytes = 0u64;

    for index in 0..archive.len() {
        let mut entry = archive
            .by_index(index)
            .with_context(|| format!("failed to read zip entry {index}"))?;
        let Some(relative_path) = normalize_archive_path(Path::new(entry.name())) else {
            continue;
        };
        if entry.is_dir() {
            fs::create_dir_all(output_dir.join(relative_path)).with_context(|| {
                format!(
                    "failed to create archive directory in {}",
                    output_dir.display()
                )
            })?;
            continue;
        }

        enforce_archive_limits(&mut extracted_files, &mut extracted_bytes, entry.size())?;
        let destination = output_dir.join(relative_path);
        if let Some(parent) = destination.parent() {
            fs::create_dir_all(parent)
                .with_context(|| format!("failed to create {}", parent.display()))?;
        }
        let mut output = File::create(&destination)
            .with_context(|| format!("failed to create {}", destination.display()))?;
        std::io::copy(&mut entry, &mut output)
            .with_context(|| format!("failed to extract {}", destination.display()))?;
    }

    if extracted_files == 0 {
        return Err(anyhow!("archive did not contain any safe files to scan"));
    }

    Ok(())
}

fn extract_tar_archive<R: Read>(archive_path: &Path, output_dir: &Path, reader: R) -> Result<()> {
    let mut archive = Archive::new(reader);
    let mut extracted_files = 0usize;
    let mut extracted_bytes = 0u64;

    for entry in archive
        .entries()
        .with_context(|| format!("failed to enumerate tar archive {}", archive_path.display()))?
    {
        let mut entry = entry
            .with_context(|| format!("failed to read tar entry in {}", archive_path.display()))?;
        let entry_path = entry
            .path()
            .with_context(|| format!("failed to read tar path in {}", archive_path.display()))?;
        let Some(relative_path) = normalize_archive_path(&entry_path) else {
            continue;
        };

        if entry.header().entry_type().is_dir() {
            fs::create_dir_all(output_dir.join(relative_path)).with_context(|| {
                format!(
                    "failed to create archive directory in {}",
                    output_dir.display()
                )
            })?;
            continue;
        }

        if !entry.header().entry_type().is_file() {
            continue;
        }

        enforce_archive_limits(
            &mut extracted_files,
            &mut extracted_bytes,
            entry.header().size().with_context(|| {
                format!("failed to read tar size in {}", archive_path.display())
            })?,
        )?;
        let destination = output_dir.join(relative_path);
        if let Some(parent) = destination.parent() {
            fs::create_dir_all(parent)
                .with_context(|| format!("failed to create {}", parent.display()))?;
        }
        let mut output = File::create(&destination)
            .with_context(|| format!("failed to create {}", destination.display()))?;
        std::io::copy(&mut entry, &mut output)
            .with_context(|| format!("failed to extract {}", destination.display()))?;
    }

    if extracted_files == 0 {
        return Err(anyhow!("archive did not contain any safe files to scan"));
    }

    Ok(())
}

fn enforce_archive_limits(
    extracted_files: &mut usize,
    extracted_bytes: &mut u64,
    entry_size: u64,
) -> Result<()> {
    if entry_size > MAX_ARCHIVE_ENTRY_BYTES {
        bail!(
            "archive entry exceeds max size of {} bytes",
            MAX_ARCHIVE_ENTRY_BYTES
        );
    }

    *extracted_files += 1;
    if *extracted_files > MAX_ARCHIVE_ENTRY_COUNT {
        bail!(
            "archive exceeds max entry count of {}",
            MAX_ARCHIVE_ENTRY_COUNT
        );
    }

    *extracted_bytes += entry_size;
    if *extracted_bytes > MAX_ARCHIVE_TOTAL_BYTES {
        bail!(
            "archive exceeds max extracted size of {} bytes",
            MAX_ARCHIVE_TOTAL_BYTES
        );
    }

    Ok(())
}

fn normalize_archive_path(path: &Path) -> Option<PathBuf> {
    let mut normalized = PathBuf::new();
    for component in path.components() {
        match component {
            Component::Normal(part) => normalized.push(part),
            Component::CurDir => continue,
            Component::ParentDir | Component::RootDir | Component::Prefix(_) => return None,
        }
    }

    (!normalized.as_os_str().is_empty()).then_some(normalized)
}

fn run_git(command: &mut Command, context_message: &str) -> Result<()> {
    let output = command
        .output()
        .with_context(|| context_message.to_string())?;
    if output.status.success() {
        Ok(())
    } else {
        bail!(
            "{}: {}",
            context_message,
            String::from_utf8_lossy(&output.stderr).trim()
        )
    }
}

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

    #[test]
    fn upload_filename_must_be_simple() {
        let error = validate_upload_filename("nested/file.txt")
            .expect_err("nested upload filename should fail");
        assert!(error.to_string().contains("simple file name"));
    }

    #[test]
    fn url_input_rejects_non_http_scheme() {
        let error =
            prepare_url_input("file:///tmp/input.txt").expect_err("non-http URL input should fail");
        assert!(error.to_string().contains("http or https"));
    }

    #[test]
    fn upload_input_rejects_invalid_base64() {
        let error = prepare_upload_input("input.txt", "%%%%")
            .expect_err("invalid base64 upload should fail");
        assert!(error.to_string().contains("valid base64"));
    }

    #[test]
    fn normalize_archive_path_rejects_parent_dirs() {
        assert!(normalize_archive_path(Path::new("../escape.txt")).is_none());
    }
}