@@ -15,130 +15,133 @@ use uv_pep440::Version;
1515use uv_pypi_types:: { HashAlgorithm , HashDigest , HashDigests , HashError , ResolverMarkerEnvironment } ;
1616use uv_redacted:: DisplaySafeUrl ;
1717
18+ /// Hash generation and verification policies for a resolution.
19+ ///
20+ /// Verification takes precedence for distributions with trusted hashes. The generation policy
21+ /// applies to the remaining distributions.
22+ #[ derive( Debug , Default , Clone ) ]
23+ pub struct HashStrategy {
24+ generation : Option < HashGeneration > ,
25+ verification : HashVerification ,
26+ }
27+
28+ /// The trusted hashes to enforce when retrieving distributions.
1829#[ derive( Debug , Default , Clone ) ]
19- pub enum HashStrategy {
20- /// No hash policy is specified .
30+ pub enum HashVerification {
31+ /// Hashes do not need to be validated .
2132 #[ default]
2233 None ,
23- /// Hashes should be generated (specifically, a SHA-256 hash), but not validated.
24- Generate ( HashGeneration ) ,
25- /// Hashes should be validated, if present, but ignored if absent.
26- ///
27- /// If necessary, hashes should be generated to ensure that the archive is valid.
28- Verify ( Arc < FxHashMap < VersionId , Vec < HashDigest > > > ) ,
29- /// Hashes should be validated against a pre-defined list of hashes.
30- ///
31- /// If necessary, hashes should be generated to ensure that the archive is valid.
32- Require ( Arc < FxHashMap < VersionId , Vec < HashDigest > > > ) ,
34+ /// Validate known hashes, without requiring hashes for other distributions.
35+ IfPresent ( Arc < FxHashMap < VersionId , Vec < HashDigest > > > ) ,
36+ /// Every distribution must have a matching trusted hash.
37+ Required ( Arc < FxHashMap < VersionId , Vec < HashDigest > > > ) ,
3338}
3439
3540impl HashStrategy {
41+ /// Generate hashes according to the given policy.
42+ pub fn generate ( generation : HashGeneration ) -> Self {
43+ Self {
44+ generation : Some ( generation) ,
45+ ..Self :: default ( )
46+ }
47+ }
48+
49+ /// Validate hashes when present.
50+ pub fn verify ( hashes : Arc < FxHashMap < VersionId , Vec < HashDigest > > > ) -> Self {
51+ Self :: default ( ) . with_verification ( HashVerification :: IfPresent ( hashes) )
52+ }
53+
54+ /// Require a matching trusted hash for every distribution.
55+ fn require ( hashes : Arc < FxHashMap < VersionId , Vec < HashDigest > > > ) -> Self {
56+ Self :: default ( ) . with_verification ( HashVerification :: Required ( hashes) )
57+ }
58+
59+ /// Set verification independently of hash generation.
60+ #[ must_use]
61+ fn with_verification ( mut self , verification : HashVerification ) -> Self {
62+ self . verification = verification;
63+ self
64+ }
65+
66+ /// Return the hash generation policy.
67+ pub fn generation ( & self ) -> Option < HashGeneration > {
68+ self . generation
69+ }
70+
71+ /// Return the hash verification policy.
72+ pub fn verification ( & self ) -> & HashVerification {
73+ & self . verification
74+ }
75+
3676 /// Return the [`HashPolicy`] for the given distribution.
3777 pub fn get < T : DistributionMetadata > ( & self , distribution : & T ) -> HashPolicy < ' _ > {
38- match self {
39- Self :: None => HashPolicy :: None ,
40- Self :: Generate ( mode) => HashPolicy :: Generate ( * mode) ,
41- Self :: Verify ( hashes) => {
42- let id = distribution. version_id ( ) ;
43- if let Some ( hashes) = hashes. get ( & id) {
44- hash_policy ( & id, hashes. as_slice ( ) )
45- } else {
46- HashPolicy :: None
47- }
48- }
49- Self :: Require ( hashes) => {
50- let id = distribution. version_id ( ) ;
51- hash_policy ( & id, hashes. get ( & id) . map ( Vec :: as_slice) . unwrap_or_default ( ) )
52- }
53- }
78+ self . get_id ( || distribution. version_id ( ) )
5479 }
5580
5681 /// Return the [`HashPolicy`] for the given registry-based package.
5782 pub fn get_package ( & self , name : & PackageName , version : & Version ) -> HashPolicy < ' _ > {
58- let id = VersionId :: from_registry ( name. clone ( ) , version. clone ( ) ) ;
59- match self {
60- Self :: None => HashPolicy :: None ,
61- Self :: Generate ( mode) => HashPolicy :: Generate ( * mode) ,
62- Self :: Verify ( hashes) => {
63- if let Some ( hashes) = hashes. get ( & id) {
64- HashPolicy :: Any ( hashes. as_slice ( ) )
65- } else {
66- HashPolicy :: None
67- }
68- }
69- Self :: Require ( hashes) => {
70- HashPolicy :: Any ( hashes. get ( & id) . map ( Vec :: as_slice) . unwrap_or_default ( ) )
71- }
72- }
83+ self . get_id ( || VersionId :: from_registry ( name. clone ( ) , version. clone ( ) ) )
7384 }
7485
7586 /// Return the [`HashPolicy`] for the given direct URL package.
7687 ///
7788 /// A direct URL identifies a single concrete artifact, so every provided digest must match.
7889 pub fn get_url ( & self , url : & DisplaySafeUrl ) -> HashPolicy < ' _ > {
79- let id = VersionId :: from_url ( url) ;
80- match self {
81- Self :: None => HashPolicy :: None ,
82- Self :: Generate ( mode) => HashPolicy :: Generate ( * mode) ,
83- Self :: Verify ( hashes) => {
90+ self . get_id ( || VersionId :: from_url ( url) )
91+ }
92+
93+ /// Construct an identity only when verification requires a lookup.
94+ fn get_id ( & self , id : impl FnOnce ( ) -> VersionId ) -> HashPolicy < ' _ > {
95+ match & self . verification {
96+ HashVerification :: IfPresent ( hashes) => {
97+ let id = id ( ) ;
8498 if let Some ( hashes) = hashes. get ( & id) {
85- HashPolicy :: All ( hashes. as_slice ( ) )
86- } else {
87- HashPolicy :: None
99+ return hash_policy ( & id, hashes) ;
88100 }
89101 }
90- Self :: Require ( hashes) => {
91- HashPolicy :: All ( hashes. get ( & id) . map ( Vec :: as_slice) . unwrap_or_default ( ) )
102+ HashVerification :: Required ( hashes) => {
103+ let id = id ( ) ;
104+ return hash_policy ( & id, hashes. get ( & id) . map ( Vec :: as_slice) . unwrap_or_default ( ) ) ;
92105 }
106+ HashVerification :: None => { }
93107 }
108+ self . generation
109+ . map_or ( HashPolicy :: None , HashPolicy :: Generate )
94110 }
95111
96112 /// Returns `true` if the given registry-based package is allowed.
97113 pub fn allows_package ( & self , name : & PackageName , version : & Version ) -> bool {
98- match self {
99- Self :: None => true ,
100- Self :: Generate ( _) => true ,
101- Self :: Verify ( _) => true ,
102- Self :: Require ( hashes) => {
114+ match & self . verification {
115+ HashVerification :: Required ( hashes) => {
103116 hashes. contains_key ( & VersionId :: from_registry ( name. clone ( ) , version. clone ( ) ) )
104117 }
118+ HashVerification :: None | HashVerification :: IfPresent ( _) => true ,
105119 }
106120 }
107121
108122 /// Returns `true` if the given direct URL package is allowed.
109123 pub fn allows_url ( & self , url : & DisplaySafeUrl ) -> bool {
110- match self {
111- Self :: None => true ,
112- Self :: Generate ( _) => true ,
113- Self :: Verify ( _) => true ,
114- Self :: Require ( hashes) => hashes. contains_key ( & VersionId :: from_url ( url) ) ,
124+ match & self . verification {
125+ HashVerification :: Required ( hashes) => hashes. contains_key ( & VersionId :: from_url ( url) ) ,
126+ HashVerification :: None | HashVerification :: IfPresent ( _) => true ,
115127 }
116128 }
117129
118130 /// Return a [`HashStrategy`] augmented with archive URL hashes discovered in additional
119131 /// requirements after the initial command-line parse.
120132 pub fn augment_with_requirements < ' a > (
121- self ,
133+ mut self ,
122134 requirements : impl Iterator < Item = & ' a Requirement > ,
123135 ) -> Result < Self , HashStrategyError > {
124- Ok ( match self {
125- Self :: None => Self :: None ,
126- Self :: Generate ( mode) => Self :: Generate ( mode) ,
127- Self :: Verify ( existing) => {
128- if let Some ( hashes) = Self :: augment_hashes ( existing. as_ref ( ) , requirements) ? {
129- Self :: Verify ( Arc :: new ( hashes) )
130- } else {
131- Self :: Verify ( existing)
136+ match & mut self . verification {
137+ HashVerification :: None => { }
138+ HashVerification :: IfPresent ( existing) | HashVerification :: Required ( existing) => {
139+ if let Some ( hashes) = Self :: augment_hashes ( existing, requirements) ? {
140+ * existing = Arc :: new ( hashes) ;
132141 }
133142 }
134- Self :: Require ( existing) => {
135- if let Some ( hashes) = Self :: augment_hashes ( existing. as_ref ( ) , requirements) ? {
136- Self :: Require ( Arc :: new ( hashes) )
137- } else {
138- Self :: Require ( existing)
139- }
140- }
141- } )
143+ }
144+ Ok ( self )
142145 }
143146
144147 /// Generate the required hashes from a set of [`UnresolvedRequirement`] entries.
@@ -297,8 +300,8 @@ impl HashStrategy {
297300 . chain ( requirement_hashes)
298301 . collect ( ) ;
299302 match mode {
300- HashCheckingMode :: Verify => Ok ( Self :: Verify ( Arc :: new ( hashes) ) ) ,
301- HashCheckingMode :: Require => Ok ( Self :: Require ( Arc :: new ( hashes) ) ) ,
303+ HashCheckingMode :: Verify => Ok ( Self :: verify ( Arc :: new ( hashes) ) ) ,
304+ HashCheckingMode :: Require => Ok ( Self :: require ( Arc :: new ( hashes) ) ) ,
302305 }
303306 }
304307
@@ -324,8 +327,8 @@ impl HashStrategy {
324327 }
325328
326329 match mode {
327- HashCheckingMode :: Verify => Ok ( Self :: Verify ( Arc :: new ( hashes) ) ) ,
328- HashCheckingMode :: Require => Ok ( Self :: Require ( Arc :: new ( hashes) ) ) ,
330+ HashCheckingMode :: Verify => Ok ( Self :: verify ( Arc :: new ( hashes) ) ) ,
331+ HashCheckingMode :: Require => Ok ( Self :: require ( Arc :: new ( hashes) ) ) ,
329332 }
330333 }
331334
@@ -510,15 +513,23 @@ pub enum HashStrategyError {
510513
511514#[ cfg( test) ]
512515mod tests {
516+ use std:: slice;
513517 use std:: str:: FromStr ;
518+ use std:: sync:: Arc ;
519+
520+ use rustc_hash:: FxHashMap ;
514521 use uv_configuration:: HashCheckingMode ;
515522 use uv_distribution_filename:: DistExtension ;
516523 use uv_distribution_types:: {
517- HashPolicy , Requirement , RequirementSource , UnresolvedRequirement ,
524+ HashGeneration , HashPolicy , Requirement , RequirementSource , UnresolvedRequirement ,
525+ VersionId ,
518526 } ;
527+ use uv_normalize:: PackageName ;
528+ use uv_pep440:: Version ;
519529 use uv_pypi_types:: HashDigest ;
530+ use uv_redacted:: DisplaySafeUrl ;
520531
521- use super :: HashStrategy ;
532+ use super :: { HashStrategy , HashVerification } ;
522533
523534 fn requirement ( url : & str ) -> Requirement {
524535 Requirement {
@@ -577,4 +588,60 @@ mod tests {
577588 assert_eq ! ( hasher. get_url( url) , HashPolicy :: All ( expected. as_slice( ) ) ) ;
578589 }
579590 }
591+
592+ #[ test]
593+ fn generate_and_verify_validates_known_hashes_and_generates_unknown_hashes ( )
594+ -> Result < ( ) , Box < dyn std:: error:: Error > > {
595+ let url: DisplaySafeUrl = "https://example.com/anyio-4.0.0.tar.gz" . parse ( ) ?;
596+ let unknown_url: DisplaySafeUrl = "https://example.com/anyio-4.1.0.tar.gz" . parse ( ) ?;
597+ let name: PackageName = "anyio" . parse ( ) ?;
598+ let version: Version = "4.0.0" . parse ( ) ?;
599+ let unknown_version: Version = "4.1.0" . parse ( ) ?;
600+ let digest = HashDigest :: from_str (
601+ "sha256:cfdb2b588b9fc25ede96d8db56ed50848b0b649dca3dd1df0b11f683bb9e0b5f" ,
602+ ) ?;
603+ let hashes = FxHashMap :: from_iter ( [
604+ ( VersionId :: from_url ( & url) , vec ! [ digest. clone( ) ] ) ,
605+ (
606+ VersionId :: from_registry ( name. clone ( ) , version. clone ( ) ) ,
607+ vec ! [ digest. clone( ) ] ,
608+ ) ,
609+ ] ) ;
610+ let strategy = HashStrategy :: generate ( HashGeneration :: All )
611+ . with_verification ( HashVerification :: IfPresent ( Arc :: new ( hashes) ) ) ;
612+
613+ assert_eq ! (
614+ strategy. get_url( & url) ,
615+ HashPolicy :: All ( slice:: from_ref( & digest) )
616+ ) ;
617+ assert_eq ! (
618+ strategy. get_url( & unknown_url) ,
619+ HashPolicy :: Generate ( HashGeneration :: All )
620+ ) ;
621+ assert_eq ! (
622+ strategy. get_package( & name, & version) ,
623+ HashPolicy :: Any ( slice:: from_ref( & digest) )
624+ ) ;
625+ assert_eq ! (
626+ strategy. get_package( & name, & unknown_version) ,
627+ HashPolicy :: Generate ( HashGeneration :: All )
628+ ) ;
629+
630+ Ok ( ( ) )
631+ }
632+
633+ #[ test]
634+ fn required_hashes_take_precedence_over_generation ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
635+ let url: DisplaySafeUrl = "https://example.com/anyio-4.0.0.tar.gz" . parse ( ) ?;
636+ let name: PackageName = "anyio" . parse ( ) ?;
637+ let version: Version = "4.0.0" . parse ( ) ?;
638+ let strategy = HashStrategy :: generate ( HashGeneration :: All )
639+ . with_verification ( HashVerification :: Required ( Arc :: default ( ) ) ) ;
640+
641+ assert_eq ! ( strategy. get_url( & url) , HashPolicy :: All ( & [ ] ) ) ;
642+ assert_eq ! ( strategy. get_package( & name, & version) , HashPolicy :: Any ( & [ ] ) ) ;
643+ assert ! ( !strategy. allows_url( & url) ) ;
644+ assert ! ( !strategy. allows_package( & name, & version) ) ;
645+ Ok ( ( ) )
646+ }
580647}
0 commit comments