Skip to content

Commit 5b70d39

Browse files
Allow requiring a hash algorithm for an index (#20605)
## Summary Add an index-specific `hash-algorithm` setting to require a particular advertised digest when generating lockfiles: ```toml [tool.uv] preview-features = ["index-hash-algorithm"] [[tool.uv.index]] name = "private" url = "https://example.com/simple" hash-algorithm = "sha256" ``` This lets indexes that advertise both SHA-256 and SHA-512 preserve comparable SHA-256 lockfile hashes during migrations. Existing lockfiles are re-resolved when their stored algorithm differs, and lockfile generation fails clearly when a selected artifact does not advertise the required algorithm. Tool lock generation follows the same setting. The setting is in preview and warns when used without `index-hash-algorithm` enabled.
1 parent f30c2ff commit 5b70d39

13 files changed

Lines changed: 555 additions & 35 deletions

File tree

‎crates/uv-distribution-types/src/index.rs‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use thiserror::Error;
77
use url::Url;
88

99
use uv_auth::{AuthPolicy, Credentials, CredentialsFromUrlError};
10+
use uv_pypi_types::HashAlgorithm;
1011
use uv_redacted::DisplaySafeUrl;
1112
use uv_small_str::SmallString;
1213

@@ -230,6 +231,21 @@ pub struct Index {
230231
/// ```
231232
#[serde(default)]
232233
pub cache_control: Option<IndexCacheControl>,
234+
/// The hash algorithm that must be used for distributions resolved from this index.
235+
///
236+
/// If a distribution does not advertise a hash using this algorithm, lockfile generation
237+
/// will fail.
238+
///
239+
/// This option is in preview and may change in any future release.
240+
///
241+
/// ```toml
242+
/// [[tool.uv.index]]
243+
/// name = "my-index"
244+
/// url = "https://<omitted>/simple"
245+
/// hash-algorithm = "sha256"
246+
/// ```
247+
#[serde(default, skip_serializing_if = "Option::is_none")]
248+
pub hash_algorithm: Option<IndexHashAlgorithm>,
233249
/// An index-specific `exclude-newer` cutoff.
234250
///
235251
/// Accepts the same date, timestamp, and duration values as the global `exclude-newer`
@@ -276,6 +292,7 @@ impl PartialEq for Index {
276292
authenticate,
277293
ignore_error_codes,
278294
cache_control,
295+
hash_algorithm,
279296
exclude_newer,
280297
} = self;
281298
*url == other.url
@@ -287,6 +304,7 @@ impl PartialEq for Index {
287304
&& *authenticate == other.authenticate
288305
&& *ignore_error_codes == other.ignore_error_codes
289306
&& *cache_control == other.cache_control
307+
&& *hash_algorithm == other.hash_algorithm
290308
&& *exclude_newer == other.exclude_newer
291309
}
292310
}
@@ -312,6 +330,7 @@ impl Ord for Index {
312330
authenticate,
313331
ignore_error_codes,
314332
cache_control,
333+
hash_algorithm,
315334
exclude_newer,
316335
} = self;
317336
url.cmp(&other.url)
@@ -323,6 +342,7 @@ impl Ord for Index {
323342
.then_with(|| authenticate.cmp(&other.authenticate))
324343
.then_with(|| ignore_error_codes.cmp(&other.ignore_error_codes))
325344
.then_with(|| cache_control.cmp(&other.cache_control))
345+
.then_with(|| hash_algorithm.cmp(&other.hash_algorithm))
326346
.then_with(|| exclude_newer.cmp(&other.exclude_newer))
327347
}
328348
}
@@ -340,6 +360,7 @@ impl std::hash::Hash for Index {
340360
authenticate,
341361
ignore_error_codes,
342362
cache_control,
363+
hash_algorithm,
343364
exclude_newer,
344365
} = self;
345366
url.hash(state);
@@ -351,6 +372,7 @@ impl std::hash::Hash for Index {
351372
authenticate.hash(state);
352373
ignore_error_codes.hash(state);
353374
cache_control.hash(state);
375+
hash_algorithm.hash(state);
354376
exclude_newer.hash(state);
355377
}
356378
}
@@ -378,6 +400,32 @@ pub enum IndexFormat {
378400
Flat,
379401
}
380402

403+
/// A hash algorithm that can be required for distributions resolved from an index.
404+
#[derive(
405+
Debug, Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd, serde::Serialize, serde::Deserialize,
406+
)]
407+
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
408+
#[serde(rename_all = "lowercase")]
409+
pub enum IndexHashAlgorithm {
410+
Md5,
411+
Sha256,
412+
Sha384,
413+
Sha512,
414+
Blake2b,
415+
}
416+
417+
impl From<IndexHashAlgorithm> for HashAlgorithm {
418+
fn from(value: IndexHashAlgorithm) -> Self {
419+
match value {
420+
IndexHashAlgorithm::Md5 => Self::Md5,
421+
IndexHashAlgorithm::Sha256 => Self::Sha256,
422+
IndexHashAlgorithm::Sha384 => Self::Sha384,
423+
IndexHashAlgorithm::Sha512 => Self::Sha512,
424+
IndexHashAlgorithm::Blake2b => Self::Blake2b,
425+
}
426+
}
427+
}
428+
381429
impl Index {
382430
/// Initialize an [`Index`] from a pip-style `--index-url`.
383431
pub fn from_index_url(url: IndexUrl) -> Self {
@@ -392,6 +440,7 @@ impl Index {
392440
authenticate: AuthPolicy::default(),
393441
ignore_error_codes: None,
394442
cache_control: None,
443+
hash_algorithm: None,
395444
exclude_newer: None,
396445
}
397446
}
@@ -409,6 +458,7 @@ impl Index {
409458
authenticate: AuthPolicy::default(),
410459
ignore_error_codes: None,
411460
cache_control: None,
461+
hash_algorithm: None,
412462
exclude_newer: None,
413463
}
414464
}
@@ -426,6 +476,7 @@ impl Index {
426476
authenticate: AuthPolicy::default(),
427477
ignore_error_codes: None,
428478
cache_control: None,
479+
hash_algorithm: None,
429480
exclude_newer: None,
430481
}
431482
}
@@ -564,6 +615,7 @@ impl From<IndexUrl> for Index {
564615
authenticate: AuthPolicy::default(),
565616
ignore_error_codes: None,
566617
cache_control: None,
618+
hash_algorithm: None,
567619
exclude_newer: None,
568620
}
569621
}
@@ -590,6 +642,7 @@ impl FromStr for Index {
590642
authenticate: AuthPolicy::default(),
591643
ignore_error_codes: None,
592644
cache_control: None,
645+
hash_algorithm: None,
593646
exclude_newer: None,
594647
});
595648
}
@@ -607,6 +660,7 @@ impl FromStr for Index {
607660
authenticate: AuthPolicy::default(),
608661
ignore_error_codes: None,
609662
cache_control: None,
663+
hash_algorithm: None,
610664
exclude_newer: None,
611665
})
612666
}
@@ -700,6 +754,8 @@ struct IndexWire {
700754
#[serde(default)]
701755
cache_control: Option<IndexCacheControl>,
702756
#[serde(default)]
757+
hash_algorithm: Option<IndexHashAlgorithm>,
758+
#[serde(default)]
703759
exclude_newer: Option<ExcludeNewerOverride>,
704760
}
705761

@@ -728,6 +784,7 @@ impl<'de> Deserialize<'de> for Index {
728784
authenticate: wire.authenticate,
729785
ignore_error_codes: wire.ignore_error_codes,
730786
cache_control: wire.cache_control,
787+
hash_algorithm: wire.hash_algorithm,
731788
exclude_newer: wire.exclude_newer,
732789
})
733790
}

‎crates/uv-distribution-types/src/index_url.rs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use url::{ParseError, Url};
1313
use uv_auth::RealmRef;
1414
use uv_cache_key::CanonicalUrl;
1515
use uv_pep508::{Scheme, VerbatimUrl, VerbatimUrlError, split_scheme};
16+
use uv_pypi_types::HashAlgorithm;
1617
use uv_redacted::DisplaySafeUrl;
1718
use uv_warnings::warn_user;
1819

@@ -525,6 +526,12 @@ impl<'a> IndexLocations {
525526
.and_then(Index::artifact_cache_control)
526527
}
527528

529+
/// Return the hash algorithm required for distributions resolved from a given index.
530+
pub fn hash_algorithm_for(&self, url: &IndexUrl) -> Option<HashAlgorithm> {
531+
self.index_for_url(url)
532+
.and_then(|index| index.hash_algorithm.map(HashAlgorithm::from))
533+
}
534+
528535
/// Return the `exclude-newer` setting for a given index, if the index is configured.
529536
pub fn exclude_newer_for(&self, url: &IndexUrl) -> Option<&ExcludeNewerOverride> {
530537
self.index_for_url(url).and_then(Index::exclude_newer)
@@ -704,6 +711,7 @@ mod tests {
704711
publish_url: None,
705712
authenticate: uv_auth::AuthPolicy::default(),
706713
ignore_error_codes: None,
714+
hash_algorithm: None,
707715
exclude_newer: None,
708716
},
709717
Index {
@@ -717,6 +725,7 @@ mod tests {
717725
publish_url: None,
718726
authenticate: uv_auth::AuthPolicy::default(),
719727
ignore_error_codes: None,
728+
hash_algorithm: None,
720729
exclude_newer: None,
721730
},
722731
];
@@ -756,6 +765,7 @@ mod tests {
756765
publish_url: None,
757766
authenticate: uv_auth::AuthPolicy::default(),
758767
ignore_error_codes: None,
768+
hash_algorithm: None,
759769
exclude_newer: None,
760770
}];
761771

@@ -792,6 +802,7 @@ mod tests {
792802
publish_url: None,
793803
authenticate: uv_auth::AuthPolicy::default(),
794804
ignore_error_codes: None,
805+
hash_algorithm: None,
795806
exclude_newer: None,
796807
}];
797808

@@ -823,6 +834,7 @@ mod tests {
823834
publish_url: None,
824835
authenticate: uv_auth::AuthPolicy::default(),
825836
ignore_error_codes: None,
837+
hash_algorithm: None,
826838
exclude_newer: None,
827839
}];
828840

‎crates/uv-preview/src/lib.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ pub enum PreviewFeature {
263263
ToolInstallLocks = 1 << 36,
264264
WorkspaceListScripts = 1 << 37,
265265
NoDistutilsPatch = 1 << 38,
266+
IndexHashAlgorithm = 1 << 39,
266267
}
267268

268269
impl PreviewFeature {
@@ -308,6 +309,7 @@ impl PreviewFeature {
308309
Self::ToolInstallLocks => "tool-install-locks",
309310
Self::WorkspaceListScripts => "workspace-list-scripts",
310311
Self::NoDistutilsPatch => "no-distutils-patch",
312+
Self::IndexHashAlgorithm => "index-hash-algorithm",
311313
}
312314
}
313315
}
@@ -366,6 +368,7 @@ impl FromStr for PreviewFeature {
366368
"tool-install-locks" => Self::ToolInstallLocks,
367369
"workspace-list-scripts" => Self::WorkspaceListScripts,
368370
"no-distutils-patch" => Self::NoDistutilsPatch,
371+
"index-hash-algorithm" => Self::IndexHashAlgorithm,
369372
_ => return Err(PreviewFeatureParseError),
370373
})
371374
}
@@ -677,6 +680,10 @@ mod tests {
677680
PreviewFeature::NoDistutilsPatch.as_str(),
678681
"no-distutils-patch"
679682
);
683+
assert_eq!(
684+
PreviewFeature::IndexHashAlgorithm.as_str(),
685+
"index-hash-algorithm"
686+
);
680687
}
681688

682689
#[test]

0 commit comments

Comments
 (0)