From 5bf8390f9e2d32af0b568ba51eb76a016e08cbbd Mon Sep 17 00:00:00 2001 From: Giuseppe Cassata Date: Tue, 10 Nov 2026 19:20:56 +0100 Subject: [PATCH] Add --ca-cert / PROOFLINE_CA_CERT to trust an extra PEM root Behind a TLS-inspecting corporate proxy every request failed with "error sending request", because the CLI trusts only its bundled public roots and the proxy re-signs connections with a private root. The new global --ca-cert FILE (or PROOFLINE_CA_CERT) reads a PEM bundle and adds its certificates to the bundled roots of every client the process builds, the API client and the self-updater. Verification stays on: the file adds roots, it never replaces them or disables checks. A missing, unreadable or certificate-free file is refused before any request. --- src/api.rs | 2 +- src/lib.rs | 9 ++++ src/tls.rs | 98 ++++++++++++++++++++++++++++++++++++++ src/update.rs | 2 +- tests/fixtures/ca-cert.pem | 11 +++++ 5 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 src/tls.rs create mode 100644 tests/fixtures/ca-cert.pem diff --git a/src/api.rs b/src/api.rs index f908670..d85fb22 100644 --- a/src/api.rs +++ b/src/api.rs @@ -146,7 +146,7 @@ pub struct Http { impl Http { pub fn new(origin: Origin) -> Result { - let client = reqwest::Client::builder() + let client = crate::tls::apply(reqwest::Client::builder()) .user_agent(USER_AGENT) .timeout(REQUEST_TIMEOUT) .connect_timeout(Duration::from_secs(10)) diff --git a/src/lib.rs b/src/lib.rs index 6096711..1521e54 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,6 +20,7 @@ mod result; mod sarif; mod severity; pub mod text; +mod tls; pub mod update; use std::io::Write as _; @@ -69,6 +70,11 @@ pub struct Cli { #[arg(long, global = true, env = "PROOFLINE_ORGANIZATION_ID")] org: Option, + /// PEM file of extra root certificates to trust beside the bundled public + /// roots, such as the root of a TLS-inspecting corporate proxy. + #[arg(long, global = true, env = "PROOFLINE_CA_CERT", value_name = "FILE")] + ca_cert: Option, + #[command(subcommand)] command: Command, } @@ -358,6 +364,9 @@ pub enum Verdict { /// Runs one command. `token` is `PROOFLINE_TOKEN`, read by the caller. pub async fn run(cli: Cli, token: Option) -> anyhow::Result { + if let Some(path) = &cli.ca_cert { + tls::trust(path)?; + } if let Command::Review(args) = &cli.command && args.schema { diff --git a/src/tls.rs b/src/tls.rs new file mode 100644 index 0000000..a3d7b3f --- /dev/null +++ b/src/tls.rs @@ -0,0 +1,98 @@ +//! Extra root certificates the CLI trusts beside its bundled public roots. +//! +//! A TLS-inspecting proxy re-signs every connection with a private root that +//! the bundled roots do not contain. `--ca-cert` (or `PROOFLINE_CA_CERT`) names +//! a PEM file whose certificates are added to, never substituted for, the +//! bundled roots; certificate and hostname verification stay on. + +use std::path::Path; +use std::sync::OnceLock; + +use reqwest::{Certificate, ClientBuilder}; + +static EXTRA_ROOTS: OnceLock> = OnceLock::new(); + +#[derive(Debug, thiserror::Error)] +pub enum CaCertError { + #[error("could not read the CA certificate file {path}: {reason}")] + Unreadable { path: String, reason: String }, + #[error("the CA certificate file {path} is not PEM: {reason}")] + Invalid { path: String, reason: String }, + #[error("the CA certificate file {path} contains no PEM certificate")] + Empty { path: String }, +} + +/// Reads every certificate in the PEM file at `path`. +pub fn read(path: &Path) -> Result, CaCertError> { + let shown = path.display().to_string(); + let pem = std::fs::read(path).map_err(|error| CaCertError::Unreadable { + path: shown.clone(), + reason: error.to_string(), + })?; + let roots = Certificate::from_pem_bundle(&pem).map_err(|error| CaCertError::Invalid { + path: shown.clone(), + reason: error.to_string(), + })?; + if roots.is_empty() { + return Err(CaCertError::Empty { path: shown }); + } + Ok(roots) +} + +/// Trusts the certificates in the PEM file at `path` for every client this +/// process builds afterwards. Called once, before the first request. +pub fn trust(path: &Path) -> Result<(), CaCertError> { + let roots = read(path)?; + if EXTRA_ROOTS.set(roots).is_err() { + // Set once at start-up; a second call keeps the first file. + } + Ok(()) +} + +/// Adds the trusted extra roots, if any, to `builder`. +pub fn apply(builder: ClientBuilder) -> ClientBuilder { + EXTRA_ROOTS + .get() + .into_iter() + .flatten() + .cloned() + .fold(builder, ClientBuilder::add_root_certificate) +} + +#[cfg(test)] +mod tests { + use super::{CaCertError, read}; + + const FIXTURE: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/fixtures/ca-cert.pem"); + + #[test] + fn reads_a_pem_root() { + assert_eq!(read(FIXTURE.as_ref()).unwrap().len(), 1); + } + + #[test] + fn a_built_client_accepts_the_root() { + let builder = read(FIXTURE.as_ref()).unwrap().into_iter().fold( + reqwest::Client::builder(), + reqwest::ClientBuilder::add_root_certificate, + ); + assert!(builder.build().is_ok()); + } + + #[test] + fn refuses_a_missing_file() { + let directory = tempfile::tempdir().unwrap(); + let error = read(&directory.path().join("absent.pem")).unwrap_err(); + assert!(matches!(error, CaCertError::Unreadable { .. }), "{error}"); + assert!(error.to_string().contains("absent.pem"), "{error}"); + } + + #[test] + fn refuses_a_file_without_certificates() { + let directory = tempfile::tempdir().unwrap(); + let path = directory.path().join("empty.pem"); + std::fs::write(&path, "not a certificate\n").unwrap(); + let error = read(&path).unwrap_err(); + assert!(matches!(error, CaCertError::Empty { .. }), "{error}"); + } +} diff --git a/src/update.rs b/src/update.rs index 76273ab..d630b74 100644 --- a/src/update.rs +++ b/src/update.rs @@ -226,7 +226,7 @@ fn spawn_check(mut command: Command) -> anyhow::Result<()> { } fn client(timeout: Duration) -> anyhow::Result { - Ok(reqwest::Client::builder() + Ok(crate::tls::apply(reqwest::Client::builder()) .https_only(true) .redirect(reqwest::redirect::Policy::none()) .timeout(timeout) diff --git a/tests/fixtures/ca-cert.pem b/tests/fixtures/ca-cert.pem new file mode 100644 index 0000000..e3c0408 --- /dev/null +++ b/tests/fixtures/ca-cert.pem @@ -0,0 +1,11 @@ +-----BEGIN CERTIFICATE----- +MIIBqzCCAVGgAwIBAgIUIMXhm15jk6X6kV0BjTYmX3J0TgYwCgYIKoZIzj0EAwIw +IjEgMB4GA1UEAwwXUHJvb2ZsaW5lIENMSSB0ZXN0IHJvb3QwIBcNMjYxMTEwMTgx +NzM3WhgPMjEyNjEwMTcxODE3MzdaMCIxIDAeBgNVBAMMF1Byb29mbGluZSBDTEkg +dGVzdCByb290MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE/hUZsfJruBqE3orB +C86X6/RdhhHNzozDTscOdagFeI876NNUtwHuv3fcvRX4xnerJqADKrk/besjUXn2 +zbK48KNjMGEwHQYDVR0OBBYEFDYflJ174gjZ6AHf8hzvgA+LpV/KMB8GA1UdIwQY +MBaAFDYflJ174gjZ6AHf8hzvgA+LpV/KMA8GA1UdEwEB/wQFMAMBAf8wDgYDVR0P +AQH/BAQDAgIEMAoGCCqGSM49BAMCA0gAMEUCIGbCHhXX1MK4TJa96/NpdK65y6+S +NBtxvFi2dKNC9Kw1AiEAhlusueRMnvcbdWGp+f7X3qiqjAMw3GOw/80oXVWIN6A= +-----END CERTIFICATE-----