Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/utils/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::incremental::{BaselineRef, IncrementalPlan};
use crate::log::debug;
use crate::manifest::EncodedManifest;
use crate::utils;
use corgea::vuln_api::{auth_header, source};
use corgea::vuln_api::{auth_header, default_headers, source};
use reqwest::header::HeaderMap;
use reqwest::{
blocking::multipart,
Expand Down Expand Up @@ -53,6 +53,7 @@ static SHARED_CLIENT: std::sync::LazyLock<reqwest::blocking::Client> =
std::sync::LazyLock::new(|| {
let mut builder = reqwest::blocking::Client::builder()
.timeout(REQUEST_TIMEOUT)
.default_headers(default_headers())
.cookie_provider(COOKIE_JAR.clone());

if let Ok(https_proxy) = std::env::var("https_proxy") {
Expand Down Expand Up @@ -1770,10 +1771,9 @@ pub fn exchange_code_for_token(
base_url: &str,
code: &str,
) -> Result<String, Box<dyn std::error::Error>> {
let client = reqwest::blocking::Client::new();
let exchange_url = format!("{}{}/authorize", base_url, API_BASE);

let response = client
let response = SHARED_CLIENT
.get(&exchange_url)
.header("CORGEA-SOURCE", source())
.query(&[("code", code)])
Expand Down Expand Up @@ -2807,6 +2807,19 @@ mod tests {
base
}

#[test]
fn unauthenticated_requests_send_cli_version() {
let (base, requests) = corgea::vuln_api_stub::spawn_capturing_vuln_api_stub();
let _ = exchange_code_for_token(&base, "code");

let requests = requests.lock().unwrap();
assert_eq!(
corgea::vuln_api_stub::header_value(&requests[0], corgea::vuln_api::CLI_VERSION_HEADER)
.as_deref(),
Some(env!("CARGO_PKG_VERSION"))
);
}

#[test]
fn repo_url_matches_path_compares_the_whole_path() {
// Same repo across scheme/.git/trailing-slash/port variants.
Expand Down
20 changes: 20 additions & 0 deletions src/vuln_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,21 @@ pub(crate) fn user_agent(label: &str) -> String {
format!("corgea-cli/{} ({label})", env!("CARGO_PKG_VERSION"))
}

/// Header carrying the CLI release on every request to Corgea, so the server
/// can record which version triggered a scan.
pub const CLI_VERSION_HEADER: &str = "CORGEA-CLI-VERSION";

/// Headers every Corgea client sends regardless of auth state. Installed as
/// client defaults so no call site can forget them.
pub fn default_headers() -> reqwest::header::HeaderMap {
let mut headers = reqwest::header::HeaderMap::new();
headers.insert(
CLI_VERSION_HEADER,
reqwest::header::HeaderValue::from_static(env!("CARGO_PKG_VERSION")),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical: Uppercase static header name panics during client initialization

HeaderMap::insert receives CLI_VERSION_HEADER as a &'static str, which is converted through HeaderName::from_static. Rust's http crate requires static header names to be lowercase and panics for "CORGEA-CLI-VERSION". Because default_headers() initializes both shared clients, this can prevent all affected API requests. Define the value as lowercase (HTTP header matching remains case-insensitive), preferably as a typed HeaderName.

Proof or reproduction:

#[test]
#[should_panic(expected = "invalid HTTP header name")]
fn uppercase_header_panics() {
    let mut headers = reqwest::header::HeaderMap::new();
    headers.insert("CORGEA-CLI-VERSION", reqwest::header::HeaderValue::from_static("1"));
}

);
headers
}

/// Build (once) and clone the shared vuln-api client. A blocking reqwest
/// client owns a runtime thread, and a gate run checks many packages —
/// cache it. `Client` clones share the same pool, so the clone is cheap.
Expand All @@ -164,6 +179,7 @@ pub fn http_client() -> Result<reqwest::blocking::Client, String> {
reqwest::blocking::Client::builder()
.timeout(REQUEST_TIMEOUT)
.user_agent(user_agent("vuln-api"))
.default_headers(default_headers())
.redirect(reqwest::redirect::Policy::none())
.build()
.map_err(|e| format!("failed to build vuln-api http client: {}", e))
Expand Down Expand Up @@ -797,6 +813,10 @@ mod tests {
header_value(&request, "CORGEA-SOURCE").as_deref(),
Some("cli")
);
assert_eq!(
header_value(&request, CLI_VERSION_HEADER).as_deref(),
Some(env!("CARGO_PKG_VERSION"))
);
}

#[test]
Expand Down
Loading