2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2022-08-10 15:23:58 -04:00
|
|
|
|
|
|
|
use std::collections::HashSet;
|
|
|
|
use std::fs;
|
|
|
|
use std::path::Path;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
use deno_core::anyhow::bail;
|
|
|
|
use deno_core::error::AnyError;
|
2023-02-22 14:15:25 -05:00
|
|
|
use deno_graph::npm::NpmPackageNv;
|
2022-08-10 15:23:58 -04:00
|
|
|
use flate2::read::GzDecoder;
|
|
|
|
use tar::Archive;
|
|
|
|
use tar::EntryType;
|
|
|
|
|
2022-11-08 14:17:24 -05:00
|
|
|
use super::cache::with_folder_sync_lock;
|
2022-08-10 15:23:58 -04:00
|
|
|
use super::registry::NpmPackageVersionDistInfo;
|
|
|
|
|
|
|
|
pub fn verify_and_extract_tarball(
|
2023-02-22 14:15:25 -05:00
|
|
|
package: &NpmPackageNv,
|
2022-08-10 15:23:58 -04:00
|
|
|
data: &[u8],
|
|
|
|
dist_info: &NpmPackageVersionDistInfo,
|
|
|
|
output_folder: &Path,
|
|
|
|
) -> Result<(), AnyError> {
|
2023-01-06 11:36:12 -05:00
|
|
|
verify_tarball_integrity(package, data, &dist_info.integrity())?;
|
2022-08-10 15:23:58 -04:00
|
|
|
|
2022-11-08 14:17:24 -05:00
|
|
|
with_folder_sync_lock(package, output_folder, || {
|
|
|
|
extract_tarball(data, output_folder)
|
|
|
|
})
|
2022-08-10 15:23:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn verify_tarball_integrity(
|
2023-02-22 14:15:25 -05:00
|
|
|
package: &NpmPackageNv,
|
2022-08-10 15:23:58 -04:00
|
|
|
data: &[u8],
|
|
|
|
npm_integrity: &str,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
use ring::digest::Context;
|
|
|
|
let (algo, expected_checksum) = match npm_integrity.split_once('-') {
|
|
|
|
Some((hash_kind, checksum)) => {
|
|
|
|
let algo = match hash_kind {
|
2023-01-06 11:36:12 -05:00
|
|
|
"sha512" => &ring::digest::SHA512,
|
|
|
|
"sha1" => &ring::digest::SHA1_FOR_LEGACY_USE_ONLY,
|
2022-08-10 15:23:58 -04:00
|
|
|
hash_kind => bail!(
|
2023-02-22 14:15:25 -05:00
|
|
|
"Not implemented hash function for {}: {}",
|
|
|
|
package,
|
2022-08-10 15:23:58 -04:00
|
|
|
hash_kind
|
|
|
|
),
|
|
|
|
};
|
|
|
|
(algo, checksum.to_lowercase())
|
|
|
|
}
|
|
|
|
None => bail!(
|
2023-02-22 14:15:25 -05:00
|
|
|
"Not implemented integrity kind for {}: {}",
|
|
|
|
package,
|
2022-08-10 15:23:58 -04:00
|
|
|
npm_integrity
|
|
|
|
),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut hash_ctx = Context::new(algo);
|
|
|
|
hash_ctx.update(data);
|
|
|
|
let digest = hash_ctx.finish();
|
|
|
|
let tarball_checksum = base64::encode(digest.as_ref()).to_lowercase();
|
|
|
|
if tarball_checksum != expected_checksum {
|
|
|
|
bail!(
|
2023-02-22 14:15:25 -05:00
|
|
|
"Tarball checksum did not match what was provided by npm registry for {}.\n\nExpected: {}\nActual: {}",
|
|
|
|
package,
|
2022-08-10 15:23:58 -04:00
|
|
|
expected_checksum,
|
|
|
|
tarball_checksum,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn extract_tarball(data: &[u8], output_folder: &Path) -> Result<(), AnyError> {
|
|
|
|
fs::create_dir_all(output_folder)?;
|
|
|
|
let output_folder = fs::canonicalize(output_folder)?;
|
|
|
|
let tar = GzDecoder::new(data);
|
|
|
|
let mut archive = Archive::new(tar);
|
|
|
|
archive.set_overwrite(true);
|
|
|
|
archive.set_preserve_permissions(true);
|
|
|
|
let mut created_dirs = HashSet::new();
|
|
|
|
|
|
|
|
for entry in archive.entries()? {
|
|
|
|
let mut entry = entry?;
|
|
|
|
let path = entry.path()?;
|
|
|
|
let entry_type = entry.header().entry_type();
|
2022-08-30 11:15:41 -04:00
|
|
|
|
|
|
|
// Some package tarballs contain "pax_global_header", these entries
|
|
|
|
// should be skipped.
|
|
|
|
if entry_type == EntryType::XGlobalHeader {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-08-10 15:23:58 -04:00
|
|
|
// skip the first component which will be either "package" or the name of the package
|
|
|
|
let relative_path = path.components().skip(1).collect::<PathBuf>();
|
|
|
|
let absolute_path = output_folder.join(relative_path);
|
|
|
|
let dir_path = if entry_type == EntryType::Directory {
|
|
|
|
absolute_path.as_path()
|
|
|
|
} else {
|
|
|
|
absolute_path.parent().unwrap()
|
|
|
|
};
|
|
|
|
if created_dirs.insert(dir_path.to_path_buf()) {
|
2022-11-17 20:59:10 -05:00
|
|
|
fs::create_dir_all(dir_path)?;
|
|
|
|
let canonicalized_dir = fs::canonicalize(dir_path)?;
|
2022-08-10 15:23:58 -04:00
|
|
|
if !canonicalized_dir.starts_with(&output_folder) {
|
|
|
|
bail!(
|
|
|
|
"Extracted directory '{}' of npm tarball was not in output directory.",
|
|
|
|
canonicalized_dir.display()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if entry.header().entry_type() == EntryType::Regular {
|
|
|
|
entry.unpack(&absolute_path)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2023-02-22 14:15:25 -05:00
|
|
|
use deno_graph::semver::Version;
|
|
|
|
|
2022-08-10 15:23:58 -04:00
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn test_verify_tarball() {
|
2023-02-22 14:15:25 -05:00
|
|
|
let package = NpmPackageNv {
|
|
|
|
name: "package".to_string(),
|
|
|
|
version: Version::parse_from_npm("1.0.0").unwrap(),
|
|
|
|
};
|
2022-08-10 15:23:58 -04:00
|
|
|
let actual_checksum =
|
|
|
|
"z4phnx7vul3xvchq1m2ab9yg5aulvxxcg/spidns6c5h0ne8xyxysp+dgnkhfuwvy7kxvudbeoglodj6+sfapg==";
|
|
|
|
assert_eq!(
|
2023-02-22 14:15:25 -05:00
|
|
|
verify_tarball_integrity(&package, &Vec::new(), "test")
|
2022-08-10 15:23:58 -04:00
|
|
|
.unwrap_err()
|
|
|
|
.to_string(),
|
|
|
|
"Not implemented integrity kind for package@1.0.0: test",
|
|
|
|
);
|
2023-01-06 11:36:12 -05:00
|
|
|
assert_eq!(
|
2023-02-22 14:15:25 -05:00
|
|
|
verify_tarball_integrity(&package, &Vec::new(), "notimplemented-test")
|
2023-01-06 11:36:12 -05:00
|
|
|
.unwrap_err()
|
|
|
|
.to_string(),
|
|
|
|
"Not implemented hash function for package@1.0.0: notimplemented",
|
|
|
|
);
|
2022-08-10 15:23:58 -04:00
|
|
|
assert_eq!(
|
2023-02-22 14:15:25 -05:00
|
|
|
verify_tarball_integrity(&package, &Vec::new(), "sha1-test")
|
2022-08-10 15:23:58 -04:00
|
|
|
.unwrap_err()
|
|
|
|
.to_string(),
|
2023-01-06 11:36:12 -05:00
|
|
|
concat!(
|
|
|
|
"Tarball checksum did not match what was provided by npm ",
|
|
|
|
"registry for package@1.0.0.\n\nExpected: test\nActual: 2jmj7l5rsw0yvb/vlwaykk/ybwk=",
|
|
|
|
),
|
2022-08-10 15:23:58 -04:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-02-22 14:15:25 -05:00
|
|
|
verify_tarball_integrity(&package, &Vec::new(), "sha512-test")
|
2022-08-10 15:23:58 -04:00
|
|
|
.unwrap_err()
|
|
|
|
.to_string(),
|
2023-01-27 10:43:16 -05:00
|
|
|
format!("Tarball checksum did not match what was provided by npm registry for package@1.0.0.\n\nExpected: test\nActual: {actual_checksum}"),
|
2022-08-10 15:23:58 -04:00
|
|
|
);
|
|
|
|
assert!(verify_tarball_integrity(
|
2023-02-22 14:15:25 -05:00
|
|
|
&package,
|
2022-08-10 15:23:58 -04:00
|
|
|
&Vec::new(),
|
2023-01-27 10:43:16 -05:00
|
|
|
&format!("sha512-{actual_checksum}")
|
2022-08-10 15:23:58 -04:00
|
|
|
)
|
|
|
|
.is_ok());
|
|
|
|
}
|
|
|
|
}
|