Skip to content

Commit 9052d3c

Browse files
Limit size of auth cache keyed by HTTP password (#1412)
1 parent 92d228b commit 9052d3c

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

Cargo.lock

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

josh-proxy/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ version = "22.4.15"
1111

1212
[dependencies]
1313
sha2 = "0.10.8"
14+
lru = "0.13.0"
1415
hex = { workspace = true }
1516
base64 = { workspace = true }
1617
clap = { workspace = true }

josh-proxy/src/auth.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::num::NonZeroUsize;
12
use std::sync::Arc;
23

34
// Import the base64 crate Engine trait anonymously so we can
@@ -25,8 +26,10 @@ impl AuthTimersGroupKey {
2526
}
2627
}
2728

29+
const AUTH_LRU_CACHE_SIZE: NonZeroUsize = NonZeroUsize::new(1000).unwrap();
30+
2831
// Within a group, we can hold the lock for longer to verify the auth with upstream
29-
type AuthTimersGroup = std::collections::HashMap<Handle, std::time::Instant>;
32+
type AuthTimersGroup = lru::LruCache<Handle, std::time::Instant>;
3033
type AuthTimers =
3134
std::collections::HashMap<AuthTimersGroupKey, Arc<tokio::sync::Mutex<AuthTimersGroup>>>;
3235

@@ -129,13 +132,15 @@ pub async fn check_http_auth(url: &str, auth: &Handle, required: bool) -> josh::
129132

130133
let group_key = AuthTimersGroupKey::new(url, &auth);
131134
let auth_timers = AUTH_TIMERS
132-
.lock()
133-
.unwrap()
135+
.lock()?
134136
.entry(group_key.clone())
135-
.or_default()
137+
.or_insert_with(|| {
138+
let cache = lru::LruCache::new(AUTH_LRU_CACHE_SIZE);
139+
Arc::new(tokio::sync::Mutex::new(cache))
140+
})
136141
.clone();
137142

138-
let auth_header = AUTH.lock().unwrap().get(auth).cloned().unwrap_or_default();
143+
let auth_header = AUTH.lock()?.get(auth).cloned().unwrap_or_default();
139144

140145
let refs_url = format!("{}/info/refs?service=git-upload-pack", url);
141146
let do_request = || {
@@ -195,7 +200,7 @@ pub async fn check_http_auth(url: &str, auth: &Handle, required: bool) -> josh::
195200

196201
let resp = do_request().await?;
197202
if resp.status().is_success() {
198-
auth_timers.insert(auth.clone(), std::time::Instant::now());
203+
auth_timers.put(auth.clone(), std::time::Instant::now());
199204
}
200205

201206
resp

0 commit comments

Comments
 (0)