Repository navigation
security(install): verify lockfile tarball URLs #842
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -787,6 +787,10 @@ where | |
| let url = tarball_url_override | ||
| .clone() | ||
| .unwrap_or_else(|| client.tarball_url(®istry_name, &version)); | ||
| if let Some(lockfile_url) = tarball_url_override.as_deref() { | ||
| verify_lockfile_tarball_url(&client, ®istry_name, &version, lockfile_url) | ||
| .await?; | ||
| } | ||
|
|
||
| let dl_start = std::time::Instant::now(); | ||
|
|
||
|
|
@@ -973,6 +977,69 @@ where | |
| Ok((indices, cached_count, fetch_count)) | ||
| } | ||
|
|
||
| async fn verify_lockfile_tarball_url( | ||
| client: &aube_registry::client::RegistryClient, | ||
| registry_name: &str, | ||
| version: &str, | ||
| lockfile_url: &str, | ||
| ) -> miette::Result<()> { | ||
| let meta = client | ||
| .fetch_single_version_metadata(registry_name, version) | ||
| .await | ||
| .map_err(|e| { | ||
| miette!( | ||
| code = aube_codes::errors::ERR_AUBE_TARBALL_URL_MISMATCH, | ||
| "{}@{}: failed to fetch registry metadata to verify lockfile tarball URL: {}", | ||
| registry_name, | ||
| version, | ||
| e | ||
| ) | ||
| })?; | ||
|
cursor[bot] marked this conversation as resolved.
Resolved
|
||
| let Some(expected_url) = meta.dist.as_ref().map(|dist| dist.tarball.as_str()) else { | ||
| return Err(miette!( | ||
|
Comment on lines
+990
to
+999
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When |
||
| code = aube_codes::errors::ERR_AUBE_TARBALL_URL_MISMATCH, | ||
| "{}@{}: registry metadata did not include dist.tarball while lockfile pinned {}", | ||
| registry_name, | ||
| version, | ||
| aube_util::url::redact_url(lockfile_url) | ||
| )); | ||
| }; | ||
| if !lockfile_tarball_url_matches_metadata(lockfile_url, expected_url) { | ||
| return Err(miette!( | ||
| code = aube_codes::errors::ERR_AUBE_TARBALL_URL_MISMATCH, | ||
| "{}@{}: lockfile tarball URL {} does not match registry metadata {}", | ||
| registry_name, | ||
| version, | ||
| aube_util::url::redact_url(lockfile_url), | ||
| aube_util::url::redact_url(expected_url) | ||
| )); | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn lockfile_tarball_url_matches_metadata(lockfile_url: &str, expected_url: &str) -> bool { | ||
| lockfile_url == expected_url | ||
| || (is_public_npm_registry_tarball(lockfile_url) | ||
| && tarball_url_path(lockfile_url) | ||
| .zip(tarball_url_path(expected_url)) | ||
| .is_some_and(|(lockfile_path, expected_path)| lockfile_path == expected_path)) | ||
| } | ||
|
|
||
| fn is_public_npm_registry_tarball(url: &str) -> bool { | ||
| reqwest::Url::parse(url) | ||
| .ok() | ||
| .and_then(|url| url.host_str().map(str::to_ascii_lowercase)) | ||
| .as_deref() | ||
| == Some("registry.npmjs.org") | ||
| } | ||
|
|
||
| fn tarball_url_path(url: &str) -> Option<String> { | ||
| let url = reqwest::Url::parse(url).ok()?; | ||
| let path = url.path(); | ||
| path.contains("/-/") | ||
| .then_some(path.trim_start_matches('/').to_string()) | ||
| } | ||
|
|
||
| /// Pull the canonical version off a dep_path for display purposes. The | ||
| /// dep_path looks like `name@1.2.3(peer@x)` — we strip the `name@` prefix | ||
| /// and any peer suffix so the warning shows `1.2.3` not `1.2.3(peer@x)`. | ||
|
|
@@ -1004,3 +1071,47 @@ pub(super) fn remap_indices_to_contextualized( | |
| } | ||
| out | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::lockfile_tarball_url_matches_metadata; | ||
|
|
||
| #[test] | ||
| fn tarball_url_match_accepts_exact_url() { | ||
| let url = "https://private.example.com/is-odd/-/is-odd-3.0.1.tgz"; | ||
|
|
||
| assert!(lockfile_tarball_url_matches_metadata(url, url)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn tarball_url_match_accepts_public_npm_lockfile_against_mirror() { | ||
| assert!(lockfile_tarball_url_matches_metadata( | ||
| "https://registry.npmjs.org/is-odd/-/is-odd-3.0.1.tgz", | ||
| "http://localhost:4873/is-odd/-/is-odd-3.0.1.tgz", | ||
| )); | ||
| } | ||
|
|
||
| #[test] | ||
| fn tarball_url_match_accepts_scoped_public_npm_lockfile_against_mirror() { | ||
| assert!(lockfile_tarball_url_matches_metadata( | ||
| "https://registry.npmjs.org/@isaacs/fs-minipass/-/fs-minipass-4.0.1.tgz", | ||
| "http://localhost:4873/@isaacs/fs-minipass/-/fs-minipass-4.0.1.tgz", | ||
| )); | ||
| } | ||
|
|
||
| #[test] | ||
| fn tarball_url_match_rejects_tampered_path() { | ||
| assert!(!lockfile_tarball_url_matches_metadata( | ||
| "https://registry.npmjs.org/not-is-odd/-/not-is-odd-3.0.1.tgz", | ||
| "http://localhost:4873/is-odd/-/is-odd-3.0.1.tgz", | ||
| )); | ||
| } | ||
|
|
||
| #[test] | ||
| fn tarball_url_match_rejects_mirror_match_from_arbitrary_host() { | ||
| assert!(!lockfile_tarball_url_matches_metadata( | ||
| "https://example.com/is-odd/-/is-odd-3.0.1.tgz", | ||
| "http://localhost:4873/is-odd/-/is-odd-3.0.1.tgz", | ||
| )); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.