Skip to content

Commit d7b6799

Browse files
committed
Use index-level instead of realm-level credential caching for known indexes (#12717)
The current uv behavior is to cache credentials either at the request URL or realm level. But in general, the expected behavior for indexes is to apply credentials at the index level (as implemented in #12651). This means that we also need to cache credentials at this level. Note that when uv does not detect an index URL for a request URL, it will continue to apply the old behavior. Depends on #12651.
1 parent 3fca195 commit d7b6799

File tree

8 files changed

+430
-61
lines changed

8 files changed

+430
-61
lines changed

crates/uv-auth/src/cache.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ type FxOnceMap<K, V> = OnceMap<K, V, BuildHasherDefault<FxHasher>>;
1717
pub struct CredentialsCache {
1818
/// A cache per realm and username
1919
realms: RwLock<FxHashMap<(Realm, Username), Arc<Credentials>>>,
20-
/// A cache tracking the result of fetches from external services
21-
pub(crate) fetches: FxOnceMap<(Realm, Username), Option<Arc<Credentials>>>,
20+
/// A cache tracking the result of realm or index URL fetches from external services
21+
pub(crate) fetches: FxOnceMap<(String, Username), Option<Arc<Credentials>>>,
2222
/// A cache per URL, uses a trie for efficient prefix queries.
2323
urls: RwLock<UrlTrie>,
2424
}

crates/uv-auth/src/index.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ impl Display for AuthPolicy {
5353
// could potentially make sense for a future refactor.
5454
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
5555
pub struct Index {
56+
pub url: Url,
5657
/// The root endpoint where authentication is applied.
5758
/// For PEP 503 endpoints, this excludes `/simple`.
5859
pub root_url: Url,
@@ -83,8 +84,8 @@ impl Indexes {
8384
// efficient search.
8485
self.0
8586
.iter()
86-
.find(|index| url.as_str().starts_with(index.root_url.as_str()))
87-
.map(|index| &index.root_url)
87+
.find(|index| is_url_prefix(&index.root_url, url))
88+
.map(|index| &index.url)
8889
}
8990

9091
/// Get the [`AuthPolicy`] for a URL.
@@ -93,10 +94,21 @@ impl Indexes {
9394
// but we could use a trie instead of a HashMap here for more
9495
// efficient search.
9596
for index in &self.0 {
96-
if url.as_str().starts_with(index.root_url.as_str()) {
97+
if is_url_prefix(&index.root_url, url) {
9798
return index.auth_policy;
9899
}
99100
}
100101
AuthPolicy::Auto
101102
}
102103
}
104+
105+
fn is_url_prefix(base: &Url, url: &Url) -> bool {
106+
if base.scheme() != url.scheme()
107+
|| base.host_str() != url.host_str()
108+
|| base.port_or_known_default() != url.port_or_known_default()
109+
{
110+
return false;
111+
}
112+
113+
url.path().starts_with(base.path())
114+
}

0 commit comments

Comments
 (0)