Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer partial success to failure #126

Merged
merged 3 commits into from
Aug 28, 2024
Merged
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
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rustls-native-certs"
version = "0.7.2"
version = "0.7.3"
edition = "2021"
rust-version = "1.60"
license = "Apache-2.0 OR ISC OR MIT"
Expand Down
37 changes: 24 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,18 +157,33 @@ impl CertPaths {
return Ok(None);
}

let mut first_error = None;

let mut certs = match &self.file {
Some(cert_file) => {
load_pem_certs(cert_file).map_err(|err| Self::load_err(cert_file, err))?
}
Some(cert_file) => match load_pem_certs(cert_file)
.map_err(|err| Self::load_err(cert_file, "file", err))
{
Ok(certs) => certs,
Err(err) => {
first_error = first_error.or(Some(err));
Vec::new()
}
},
None => Vec::new(),
};

if let Some(cert_dir) = &self.dir {
certs.append(
&mut load_pem_certs_from_dir(cert_dir)
.map_err(|err| Self::load_err(cert_dir, err))?,
);
match load_pem_certs_from_dir(cert_dir)
.map_err(|err| Self::load_err(cert_dir, "dir", err))
{
Ok(mut from_dir) => certs.append(&mut from_dir),
Err(err) => first_error = first_error.or(Some(err)),
}
}

// promote first error if we have no certs to return
if let (Some(error), []) = (first_error, certs.as_slice()) {
Copy link
Member

Choose a reason for hiding this comment

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

Just to call this out: is the 0 vs 1 boundary the right one? It is different from the previous approach -- it would be "more" backwards-compatible if we just swallowed the error from load_pem_certs_from_dir(), I suppose?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not quite grasping what this comment is saying, could your reword? Is it whether we return the first or last error? Or about the preexisting behaviour where the crate can return Ok([])?

return Err(error);
}

certs.sort_unstable_by(|a, b| a.cmp(b));
Expand All @@ -177,14 +192,10 @@ impl CertPaths {
Ok(Some(certs))
}

fn load_err(path: &Path, err: Error) -> Error {
fn load_err(path: &Path, typ: &str, err: Error) -> Error {
Error::new(
err.kind(),
format!(
"could not load certs from {} {}: {err}",
if path.is_file() { "file" } else { "dir" },
path.display()
),
format!("could not load certs from {typ} {}: {err}", path.display()),
)
}
}
Expand Down
50 changes: 50 additions & 0 deletions tests/smoketests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,53 @@ fn badssl_with_dir_from_env() {

check_site("self-signed.badssl.com").unwrap();
}

#[test]
#[serial]
#[ignore]
#[cfg(target_os = "linux")]
fn google_with_dir_but_broken_file() {
unsafe {
// SAFETY: safe because of #[serial]
common::clear_env();
}

env::set_var("SSL_CERT_DIR", "/etc/ssl/certs");
env::set_var("SSL_CERT_FILE", "not-exist");
check_site("google.com").unwrap();
}

#[test]
#[serial]
#[ignore]
#[cfg(target_os = "linux")]
fn google_with_file_but_broken_dir() {
unsafe {
// SAFETY: safe because of #[serial]
common::clear_env();
}

env::set_var("SSL_CERT_DIR", "/not-exist");
env::set_var("SSL_CERT_FILE", "/etc/ssl/certs/ca-certificates.crt");
check_site("google.com").unwrap();
}

#[test]
#[serial]
#[ignore]
#[cfg(target_os = "linux")]
fn nothing_works_with_broken_file_and_dir() {
unsafe {
// SAFETY: safe because of #[serial]
common::clear_env();
}

env::set_var("SSL_CERT_DIR", "/not-exist");
env::set_var("SSL_CERT_FILE", "not-exist");
assert_eq!(
rustls_native_certs::load_native_certs()
.unwrap_err()
.to_string(),
"could not load certs from file not-exist: No such file or directory (os error 2)"
);
}