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-04-06 18:46:44 -04:00
|
|
|
use deno_npm::registry::NpmPackageVersionDistInfo;
|
2023-09-18 16:40:41 -04:00
|
|
|
use deno_npm::registry::NpmPackageVersionDistInfoIntegrity;
|
2023-08-21 05:53:52 -04:00
|
|
|
use deno_semver::package::PackageNv;
|
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
|
|
|
|
|
|
|
pub fn verify_and_extract_tarball(
|
2023-08-21 05:53:52 -04:00
|
|
|
package: &PackageNv,
|
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-08-21 05:53:52 -04:00
|
|
|
package: &PackageNv,
|
2022-08-10 15:23:58 -04:00
|
|
|
data: &[u8],
|
2023-09-18 16:40:41 -04:00
|
|
|
npm_integrity: &NpmPackageVersionDistInfoIntegrity,
|
2022-08-10 15:23:58 -04:00
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
use ring::digest::Context;
|
2023-09-18 16:40:41 -04:00
|
|
|
let (tarball_checksum, expected_checksum) = match npm_integrity {
|
|
|
|
NpmPackageVersionDistInfoIntegrity::Integrity {
|
|
|
|
algorithm,
|
|
|
|
base64_hash,
|
|
|
|
} => {
|
|
|
|
let algo = match *algorithm {
|
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
|
|
|
|
),
|
|
|
|
};
|
2023-09-18 16:40:41 -04:00
|
|
|
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();
|
|
|
|
(tarball_checksum, base64_hash.to_lowercase())
|
|
|
|
}
|
|
|
|
NpmPackageVersionDistInfoIntegrity::LegacySha1Hex(hex) => {
|
|
|
|
let mut hash_ctx = Context::new(&ring::digest::SHA1_FOR_LEGACY_USE_ONLY);
|
|
|
|
hash_ctx.update(data);
|
|
|
|
let digest = hash_ctx.finish();
|
|
|
|
let tarball_checksum = hex::encode(digest.as_ref()).to_lowercase();
|
|
|
|
(tarball_checksum, hex.to_lowercase())
|
|
|
|
}
|
|
|
|
NpmPackageVersionDistInfoIntegrity::UnknownIntegrity(integrity) => {
|
|
|
|
bail!(
|
|
|
|
"Not implemented integrity kind for {}: {}",
|
|
|
|
package,
|
|
|
|
integrity
|
|
|
|
)
|
2022-08-10 15:23:58 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2023-06-13 09:48:13 -04:00
|
|
|
|
|
|
|
let entry_type = entry.header().entry_type();
|
|
|
|
match entry_type {
|
|
|
|
EntryType::Regular => {
|
|
|
|
entry.unpack(&absolute_path)?;
|
|
|
|
}
|
|
|
|
EntryType::Symlink | EntryType::Link => {
|
|
|
|
// At the moment, npm doesn't seem to support uploading hardlinks or
|
|
|
|
// symlinks to the npm registry. If ever adding symlink or hardlink
|
|
|
|
// support, we will need to validate that the hardlink and symlink
|
|
|
|
// target are within the package directory.
|
|
|
|
log::warn!(
|
|
|
|
"Ignoring npm tarball entry type {:?} for '{}'",
|
|
|
|
entry_type,
|
|
|
|
absolute_path.display()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// ignore
|
|
|
|
}
|
2022-08-10 15:23:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2023-04-06 18:46:44 -04:00
|
|
|
use deno_semver::Version;
|
2023-02-22 14:15:25 -05:00
|
|
|
|
2022-08-10 15:23:58 -04:00
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn test_verify_tarball() {
|
2023-08-21 05:53:52 -04:00
|
|
|
let package = PackageNv {
|
2023-02-22 14:15:25 -05:00
|
|
|
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-09-18 16:40:41 -04:00
|
|
|
verify_tarball_integrity(
|
|
|
|
&package,
|
|
|
|
&Vec::new(),
|
|
|
|
&NpmPackageVersionDistInfoIntegrity::UnknownIntegrity("test")
|
|
|
|
)
|
|
|
|
.unwrap_err()
|
|
|
|
.to_string(),
|
2022-08-10 15:23:58 -04:00
|
|
|
"Not implemented integrity kind for package@1.0.0: test",
|
|
|
|
);
|
2023-01-06 11:36:12 -05:00
|
|
|
assert_eq!(
|
2023-09-18 16:40:41 -04:00
|
|
|
verify_tarball_integrity(
|
|
|
|
&package,
|
|
|
|
&Vec::new(),
|
|
|
|
&NpmPackageVersionDistInfoIntegrity::Integrity {
|
|
|
|
algorithm: "notimplemented",
|
|
|
|
base64_hash: "test"
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.unwrap_err()
|
|
|
|
.to_string(),
|
2023-01-06 11:36:12 -05:00
|
|
|
"Not implemented hash function for package@1.0.0: notimplemented",
|
|
|
|
);
|
2022-08-10 15:23:58 -04:00
|
|
|
assert_eq!(
|
2023-09-18 16:40:41 -04:00
|
|
|
verify_tarball_integrity(
|
|
|
|
&package,
|
|
|
|
&Vec::new(),
|
|
|
|
&NpmPackageVersionDistInfoIntegrity::Integrity {
|
|
|
|
algorithm: "sha1",
|
|
|
|
base64_hash: "test"
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.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-09-18 16:40:41 -04:00
|
|
|
verify_tarball_integrity(
|
|
|
|
&package,
|
|
|
|
&Vec::new(),
|
|
|
|
&NpmPackageVersionDistInfoIntegrity::Integrity {
|
|
|
|
algorithm: "sha512",
|
|
|
|
base64_hash: "test"
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.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-09-18 16:40:41 -04:00
|
|
|
&NpmPackageVersionDistInfoIntegrity::Integrity {
|
|
|
|
algorithm: "sha512",
|
|
|
|
base64_hash: actual_checksum,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.is_ok());
|
|
|
|
let actual_hex = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
|
|
|
|
assert_eq!(
|
|
|
|
verify_tarball_integrity(
|
|
|
|
&package,
|
|
|
|
&Vec::new(),
|
|
|
|
&NpmPackageVersionDistInfoIntegrity::LegacySha1Hex("test"),
|
|
|
|
)
|
|
|
|
.unwrap_err()
|
|
|
|
.to_string(),
|
|
|
|
format!("Tarball checksum did not match what was provided by npm registry for package@1.0.0.\n\nExpected: test\nActual: {actual_hex}"),
|
|
|
|
);
|
|
|
|
assert!(verify_tarball_integrity(
|
|
|
|
&package,
|
|
|
|
&Vec::new(),
|
|
|
|
&NpmPackageVersionDistInfoIntegrity::LegacySha1Hex(actual_hex),
|
2022-08-10 15:23:58 -04:00
|
|
|
)
|
|
|
|
.is_ok());
|
|
|
|
}
|
|
|
|
}
|