Skip to content

Commit

Permalink
Add more checks
Browse files Browse the repository at this point in the history
  • Loading branch information
harshdoesdev committed Oct 6, 2023
1 parent d2da8c1 commit 20031d0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/commands/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,29 @@ pub async fn download(

let output_get_res = output_get_req.get().await?;

println!("build artifacts downloaded");
println!("Build artifacts downloaded");

output_dir_archive.write_all(&output_get_res.bytes().await?.to_vec())?;
let buf = &output_get_res.bytes().await?.to_vec();

if !vercel_cache_helper::utils::is_zstd_compressed(&buf) {
return Err(vercel_cache_helper::Error::InvalidInput(
format!(
"Downloaded archive (Size: {}) is not zstd compressed",
&buf.len()
)
));
}

output_dir_archive.write_all(buf)?;

output_dir_archive
.seek(std::io::SeekFrom::Start(0))
.unwrap();

vercel_cache_helper::utils::extract_tar_zst(output_dir_archive, &output_dir.path().to_path_buf())?;
vercel_cache_helper::utils::extract_tar_zst(
output_dir_archive,
&output_dir.path().to_path_buf(),
)?;

let temp_build_dir = output_dir.path().join(".build");
let temp_cache_dir = output_dir.path().join("cache");
Expand Down
10 changes: 10 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ pub fn create_tar_zst_archive(
}
}

pub fn is_zstd_compressed(data: &[u8]) -> bool {
if data.len() >= 4 {
// Check if the first 4 bytes match the Zstd magic number
if &data[0..4] == &[0x28, 0xB5, 0x2F, 0xFD] {
return true;
}
}
false
}

pub fn extract_tar_zst(file: std::fs::File, dest_path: &std::path::PathBuf) -> std::io::Result<()> {
println!(
"Preparing to extract archive in {}...",
Expand Down

0 comments on commit 20031d0

Please sign in to comment.