Skip to content

Commit b82b038

Browse files
authored
Include hashes in cyclonedx exports (#21131)
1 parent 8011778 commit b82b038

3 files changed

Lines changed: 287 additions & 34 deletions

File tree

‎crates/uv-resolver/src/lock/export/cyclonedx_json.rs‎

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ use std::path::Path;
33

44
use cyclonedx_bom::models::component::Classification;
55
use cyclonedx_bom::models::dependency::{Dependencies, Dependency};
6+
use cyclonedx_bom::models::external_reference::{
7+
ExternalReference, ExternalReferenceType, ExternalReferences, Uri as ExternalReferenceUri,
8+
};
9+
use cyclonedx_bom::models::hash::{Hash, HashAlgorithm, HashValue, Hashes};
610
use cyclonedx_bom::models::metadata::Metadata;
711
use cyclonedx_bom::models::property::{Properties, Property};
812
use cyclonedx_bom::models::tool::{Tool, Tools};
9-
use cyclonedx_bom::prelude::{Bom, Component, Components, NormalizedString};
13+
use cyclonedx_bom::prelude::{Bom, Component, Components, NormalizedString, Uri};
1014
use itertools::Itertools;
1115
use percent_encoding::{AsciiSet, CONTROLS, percent_encode};
1216
use rustc_hash::FxHashSet;
@@ -18,10 +22,11 @@ use uv_fs::PortablePath;
1822
use uv_normalize::PackageName;
1923
use uv_pep508::MarkerTree;
2024
use uv_preview::{Preview, PreviewFeature};
25+
use uv_pypi_types::{HashAlgorithm as UvHashAlgorithm, HashDigest};
2126
use uv_warnings::warn_user;
2227

2328
use crate::lock::export::{ExportableRequirement, ExportableRequirements};
24-
use crate::lock::{LockErrorKind, Package, PackageId, RegistrySource, Source};
29+
use crate::lock::{LockErrorKind, Package, PackageId, RegistrySource, Source, WheelWireSource};
2530
use crate::{Installable, LockError};
2631

2732
/// Character set for percent-encoding PURL components, copied from packageurl.rs (<https://github.com/scm-rs/packageurl.rs/blob/a725aa0ab332934c350641508017eb09ddfa0813/src/purl.rs#L18>).
@@ -52,6 +57,7 @@ const PURL_ENCODE_SET: &AsciiSet = &CONTROLS
5257
struct ComponentBuilder<'a> {
5358
id_counter: usize, // Used as prefix in bom-ref generation, to ensure uniqueness
5459
package_to_component_map: HashMap<&'a PackageId, Component>,
60+
include_hashes: bool,
5561
}
5662

5763
impl<'a> ComponentBuilder<'a> {
@@ -218,6 +224,48 @@ impl<'a> ComponentBuilder<'a> {
218224
));
219225
}
220226

227+
let external_references = if self.include_hashes {
228+
let mut external_references = Vec::new();
229+
230+
if let Some(sdist) = &package.sdist {
231+
if let (Some(url), Some(hash)) = (sdist.url(), sdist.hash()) {
232+
if let (Ok(uri), Some(cdx_hash)) =
233+
(Uri::try_from(url.to_string()), to_cyclonedx_hash(&hash.0))
234+
{
235+
external_references.push(ExternalReference {
236+
url: ExternalReferenceUri::Url(uri),
237+
comment: None,
238+
hashes: Some(Hashes(vec![cdx_hash])),
239+
external_reference_type: ExternalReferenceType::Distribution,
240+
});
241+
}
242+
}
243+
}
244+
245+
for wheel in &package.wheels {
246+
if let (WheelWireSource::Url { url }, Some(hash)) = (&wheel.url, &wheel.hash) {
247+
if let (Ok(uri), Some(cdx_hash)) =
248+
(Uri::try_from(url.to_string()), to_cyclonedx_hash(&hash.0))
249+
{
250+
external_references.push(ExternalReference {
251+
url: ExternalReferenceUri::Url(uri),
252+
comment: None,
253+
hashes: Some(Hashes(vec![cdx_hash])),
254+
external_reference_type: ExternalReferenceType::Distribution,
255+
});
256+
}
257+
}
258+
}
259+
260+
if external_references.is_empty() {
261+
None
262+
} else {
263+
Some(ExternalReferences(external_references))
264+
}
265+
} else {
266+
None
267+
};
268+
221269
Component {
222270
component_type: Classification::Library,
223271
name: NormalizedString::new(name),
@@ -238,7 +286,7 @@ impl<'a> ComponentBuilder<'a> {
238286
swid: None,
239287
modified: None,
240288
pedigree: None,
241-
external_references: None,
289+
external_references,
242290
properties: if !properties.is_empty() {
243291
Some(Properties(properties))
244292
} else {
@@ -263,6 +311,7 @@ pub fn from_lock<'lock>(
263311
extras: &ExtrasSpecificationWithDefaults,
264312
groups: &DependencyGroupsWithDefaults,
265313
annotate: bool,
314+
hashes: bool,
266315
install_options: &'lock InstallOptions,
267316
preview: Preview,
268317
all_packages: bool,
@@ -298,7 +347,10 @@ pub fn from_lock<'lock>(
298347
}
299348
.or_else(|| target.lock().root()); // Fallback to project root
300349

301-
let mut component_builder = ComponentBuilder::default();
350+
let mut component_builder = ComponentBuilder {
351+
include_hashes: hashes,
352+
..ComponentBuilder::default()
353+
};
302354

303355
let mut metadata = Metadata {
304356
component: root
@@ -443,3 +495,24 @@ enum PackageType<'a> {
443495
Workspace(&'a Path),
444496
Dependency,
445497
}
498+
499+
/// Convert an internal [`HashDigest`] into a `CycloneDX` [`Hash`].
500+
fn to_cyclonedx_hash(hash: &HashDigest) -> Option<Hash> {
501+
let alg = match hash.algorithm() {
502+
UvHashAlgorithm::Md5 => HashAlgorithm::MD5,
503+
UvHashAlgorithm::Sha256 => HashAlgorithm::SHA_256,
504+
UvHashAlgorithm::Sha384 => HashAlgorithm::SHA_384,
505+
UvHashAlgorithm::Sha512 => HashAlgorithm::SHA_512,
506+
UvHashAlgorithm::Blake2b => match hash.digest.len() {
507+
64 => HashAlgorithm::BLAKE2b_256,
508+
96 => HashAlgorithm::BLAKE2b_384,
509+
128 => HashAlgorithm::BLAKE2b_512,
510+
_ => return None,
511+
},
512+
};
513+
514+
Some(Hash {
515+
alg,
516+
content: HashValue(hash.digest.to_string()),
517+
})
518+
}

‎crates/uv/src/commands/project/export.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ pub(crate) async fn export(
474474
&extras,
475475
&groups,
476476
include_annotations,
477+
hashes,
477478
&install_options,
478479
preview,
479480
all_packages,

0 commit comments

Comments
 (0)