Skip to content

Commit c2100d1

Browse files
Make cache control lookups robust to username (#16088)
## Summary We serialize the index to the lockfile without the username, so if we compare based on `==` and the user _includes_ the username in their `pyproject.toml`, the check will always fail. Closes #16076.
1 parent d483b02 commit c2100d1

3 files changed

Lines changed: 15 additions & 8 deletions

File tree

crates/uv-auth/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub use middleware::AuthMiddleware;
1414
pub use pyx::{
1515
DEFAULT_TOLERANCE_SECS, PyxJwt, PyxOAuthTokens, PyxTokenStore, PyxTokens, TokenStoreError,
1616
};
17-
pub use realm::Realm;
17+
pub use realm::{Realm, RealmRef};
1818
pub use service::{Service, ServiceParseError};
1919
pub use store::{AuthBackend, AuthScheme, TextCredentialStore, TomlCredentialError};
2020

crates/uv-auth/src/realm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl Hash for Realm {
7575

7676
/// A reference to a [`Realm`] that can be used for zero-allocation comparisons.
7777
#[derive(Debug, Copy, Clone)]
78-
pub(crate) struct RealmRef<'a> {
78+
pub struct RealmRef<'a> {
7979
scheme: &'a str,
8080
host: Option<&'a str>,
8181
port: Option<u16>,

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ use rustc_hash::{FxHashMap, FxHashSet};
1010
use thiserror::Error;
1111
use tracing::trace;
1212
use url::{ParseError, Url};
13-
13+
use uv_auth::RealmRef;
14+
use uv_cache_key::CanonicalUrl;
1415
use uv_pep508::{Scheme, VerbatimUrl, VerbatimUrlError, split_scheme};
1516
use uv_redacted::DisplaySafeUrl;
1617
use uv_warnings::warn_user;
@@ -292,6 +293,12 @@ impl IndexLocations {
292293
}
293294
}
294295

296+
/// Returns `true` if two [`IndexUrl`]s refer to the same index.
297+
fn is_same_index(a: &IndexUrl, b: &IndexUrl) -> bool {
298+
RealmRef::from(&**b.url()) == RealmRef::from(&**a.url())
299+
&& CanonicalUrl::new(a.url()) == CanonicalUrl::new(b.url())
300+
}
301+
295302
impl<'a> IndexLocations {
296303
/// Return the default [`Index`] entry.
297304
///
@@ -456,7 +463,7 @@ impl<'a> IndexLocations {
456463
/// Return the Simple API cache control header for an [`IndexUrl`], if configured.
457464
pub fn simple_api_cache_control_for(&self, url: &IndexUrl) -> Option<&str> {
458465
for index in &self.indexes {
459-
if index.url() == url {
466+
if is_same_index(index.url(), url) {
460467
return index.simple_api_cache_control();
461468
}
462469
}
@@ -466,7 +473,7 @@ impl<'a> IndexLocations {
466473
/// Return the artifact cache control header for an [`IndexUrl`], if configured.
467474
pub fn artifact_cache_control_for(&self, url: &IndexUrl) -> Option<&str> {
468475
for index in &self.indexes {
469-
if index.url() == url {
476+
if is_same_index(index.url(), url) {
470477
return index.artifact_cache_control();
471478
}
472479
}
@@ -599,7 +606,7 @@ impl<'a> IndexUrls {
599606
/// Return the [`IndexStatusCodeStrategy`] for an [`IndexUrl`].
600607
pub fn status_code_strategy_for(&self, url: &IndexUrl) -> IndexStatusCodeStrategy {
601608
for index in &self.indexes {
602-
if index.url() == url {
609+
if is_same_index(index.url(), url) {
603610
return index.status_code_strategy();
604611
}
605612
}
@@ -609,7 +616,7 @@ impl<'a> IndexUrls {
609616
/// Return the Simple API cache control header for an [`IndexUrl`], if configured.
610617
pub fn simple_api_cache_control_for(&self, url: &IndexUrl) -> Option<&str> {
611618
for index in &self.indexes {
612-
if index.url() == url {
619+
if is_same_index(index.url(), url) {
613620
return index.simple_api_cache_control();
614621
}
615622
}
@@ -619,7 +626,7 @@ impl<'a> IndexUrls {
619626
/// Return the artifact cache control header for an [`IndexUrl`], if configured.
620627
pub fn artifact_cache_control_for(&self, url: &IndexUrl) -> Option<&str> {
621628
for index in &self.indexes {
622-
if index.url() == url {
629+
if is_same_index(index.url(), url) {
623630
return index.artifact_cache_control();
624631
}
625632
}

0 commit comments

Comments
 (0)