Skip to content

Commit 04acf05

Browse files
authored
fix(security): enforce HTTPS for all HTTP requests (#4)
1 parent 2b96b55 commit 04acf05

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

crates/deps-core/src/cache.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,26 @@ use std::time::Instant;
77
/// Maximum number of cached entries to prevent unbounded memory growth.
88
const MAX_CACHE_ENTRIES: usize = 1000;
99

10+
/// Validates that a URL uses HTTPS protocol.
11+
///
12+
/// Returns an error if the URL doesn't start with "https://".
13+
/// This ensures all network requests are encrypted.
14+
///
15+
/// In test mode, HTTP URLs are allowed for mockito compatibility.
16+
#[inline]
17+
fn ensure_https(url: &str) -> Result<()> {
18+
#[cfg(not(test))]
19+
if !url.starts_with("https://") {
20+
return Err(DepsError::CacheError(format!(
21+
"URL must use HTTPS: {}",
22+
url
23+
)));
24+
}
25+
#[cfg(test)]
26+
let _ = url; // Silence unused warning in tests
27+
Ok(())
28+
}
29+
1030
/// Cached HTTP response with validation headers.
1131
///
1232
/// Stores response body and cache validation headers (ETag, Last-Modified)
@@ -168,6 +188,7 @@ impl HttpCache {
168188
url: &str,
169189
cached: &CachedResponse,
170190
) -> Result<Option<Arc<Vec<u8>>>> {
191+
ensure_https(url)?;
171192
let mut request = self.client.get(url);
172193

173194
if let Some(etag) = &cached.etag {
@@ -235,6 +256,7 @@ impl HttpCache {
235256
/// Returns `DepsError::CacheError` if the server returns a non-2xx status code,
236257
/// or `DepsError::RegistryError` if the network request fails.
237258
pub(crate) async fn fetch_and_store(&self, url: &str) -> Result<Arc<Vec<u8>>> {
259+
ensure_https(url)?;
238260
tracing::debug!("fetching fresh: {}", url);
239261

240262
let response = self

0 commit comments

Comments
 (0)