Skip to content
Open
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
35 changes: 33 additions & 2 deletions src/webserver/http_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ pub fn make_http_client(config: &crate::app_config::AppConfig) -> anyhow::Result
}

pub(crate) fn default_system_root_ca_certificates_from_env() -> bool {
std::env::var("SSL_CERT_FILE").is_ok_and(|value| !value.is_empty())
|| std::env::var("SSL_CERT_DIR").is_ok_and(|value| !value.is_empty())
system_roots_selected_by(|name| std::env::var(name).ok())
}

fn system_roots_selected_by(lookup: impl Fn(&str) -> Option<String>) -> bool {
["SSL_CERT_FILE", "SSL_CERT_DIR"]
.into_iter()
.any(|name| lookup(name).is_some_and(|value| !value.is_empty()))
}

fn native_certificates() -> anyhow::Result<&'static NativeCertificates> {
Expand Down Expand Up @@ -101,3 +106,29 @@ pub(crate) fn get_http_client_from_appdata(
Err(anyhow!("HTTP client not found in app data"))
}
}

#[cfg(test)]
mod tests {
use super::system_roots_selected_by;

#[test]
fn either_ssl_cert_variable_selects_the_system_trust_store() {
for variable in ["SSL_CERT_FILE", "SSL_CERT_DIR"] {
assert!(
system_roots_selected_by(
|name| (name == variable).then(|| "/etc/ssl/certs".to_owned())
),
"{variable}"
);
}
}

#[test]
fn unset_or_empty_ssl_cert_variables_leave_the_bundled_roots() {
assert!(!system_roots_selected_by(|_| None));
assert!(!system_roots_selected_by(|_| Some(String::new())));
assert!(!system_roots_selected_by(
|name| (name == "SSL_CERT_PATH").then(|| "/etc/ssl/certs".to_owned())
));
}
}