Skip to content

Commit

Permalink
Limit size of auth cache keyed by HTTP password
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad-ivanov-name committed Feb 23, 2025
1 parent bb91b26 commit 94c5d3d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
27 changes: 27 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions josh-proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ version = "22.4.15"

[dependencies]
sha2 = "0.10.8"
lru = "0.13.0"
hex = { workspace = true }
base64 = { workspace = true }
clap = { workspace = true }
Expand Down
17 changes: 11 additions & 6 deletions josh-proxy/src/auth.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::num::NonZeroUsize;
use std::sync::Arc;

// Import the base64 crate Engine trait anonymously so we can
Expand Down Expand Up @@ -25,8 +26,10 @@ impl AuthTimersGroupKey {
}
}

const AUTH_LRU_CACHE_SIZE: NonZeroUsize = NonZeroUsize::new(1000).unwrap();

// Within a group, we can hold the lock for longer to verify the auth with upstream
type AuthTimersGroup = std::collections::HashMap<Handle, std::time::Instant>;
type AuthTimersGroup = lru::LruCache<Handle, std::time::Instant>;
type AuthTimers =
std::collections::HashMap<AuthTimersGroupKey, Arc<tokio::sync::Mutex<AuthTimersGroup>>>;

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

let group_key = AuthTimersGroupKey::new(url, &auth);
let auth_timers = AUTH_TIMERS
.lock()
.unwrap()
.lock()?
.entry(group_key.clone())
.or_default()
.or_insert_with(|| {
let cache = lru::LruCache::new(AUTH_LRU_CACHE_SIZE);
Arc::new(tokio::sync::Mutex::new(cache))
})
.clone();

let auth_header = AUTH.lock().unwrap().get(auth).cloned().unwrap_or_default();
let auth_header = AUTH.lock()?.get(auth).cloned().unwrap_or_default();

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

let resp = do_request().await?;
if resp.status().is_success() {
auth_timers.insert(auth.clone(), std::time::Instant::now());
auth_timers.put(auth.clone(), std::time::Instant::now());
}

resp
Expand Down

0 comments on commit 94c5d3d

Please sign in to comment.