Skip to content

Commit 0f58cf5

Browse files
committed
fix: do not create a new client with invalid certificate
1 parent edb4d32 commit 0f58cf5

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

compiler-cli/src/http.rs

+17-20
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl gleam_core::io::HttpClient for HttpClient {
2828
.try_into()
2929
.expect("Unable to convert HTTP request for use by reqwest library");
3030
let mut response = REQWEST_CLIENT
31-
.get_or_init(init_client)
31+
.get_or_init(|| init_client().expect("Unable to create reqwest client"))
3232
.execute(request)
3333
.await
3434
.map_err(Error::http)?;
@@ -44,24 +44,21 @@ impl gleam_core::io::HttpClient for HttpClient {
4444
}
4545
}
4646

47-
fn init_client() -> Client {
48-
match get_certificate() {
49-
Ok(cert) => Client::builder()
50-
.add_root_certificate(cert)
51-
.build()
52-
.expect("Unable to build reqwest client with certificate"),
53-
_ => Client::new(),
54-
}
55-
}
56-
57-
fn get_certificate() -> Result<Certificate, Error> {
58-
let certificate_path = std::env::var("GLEAM_CACERTS_PATH")?;
59-
let certificate_bytes = std::fs::read(&certificate_path)?;
60-
61-
match Certificate::from_pem(&certificate_bytes) {
62-
Ok(certificate) => Ok(certificate),
63-
Err(e) => Error::CannotReadCertificate {
47+
fn init_client() -> Result<Client, Error> {
48+
let certificate_path = std::env::var("GLEAM_CACERTS_PATH")
49+
.map_err(|_| Error::CannotReadCertificate { path: "".into() })?;
50+
let certificate_bytes =
51+
std::fs::read(&certificate_path).map_err(|_| Error::CannotReadCertificate {
52+
path: certificate_path.clone(),
53+
})?;
54+
let certificate =
55+
Certificate::from_pem(&certificate_bytes).map_err(|_| Error::CannotReadCertificate {
56+
path: certificate_path.clone(),
57+
})?;
58+
Client::builder()
59+
.add_root_certificate(certificate)
60+
.build()
61+
.map_err(|_| Error::CannotReadCertificate {
6462
path: certificate_path,
65-
},
66-
}
63+
})
6764
}

compiler-core/src/error.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1401,6 +1401,17 @@ https://learn.microsoft.com/en-us/windows/apps/get-started/enable-your-device-fo
14011401
}]
14021402
}
14031403

1404+
Error::CannotReadCertificate { path } => {
1405+
let text = wrap_format!("An error occurred while trying to read the certificate file at: {path}");
1406+
1407+
vec![Diagnostic {
1408+
title: "Failed to read certificate".into(),
1409+
text,
1410+
hint: None,
1411+
level: Level::Error,
1412+
location: None,
1413+
}]
1414+
}
14041415

14051416
Error::FailedToEncrypt { detail } => {
14061417
let text = wrap_format!("A problem was encountered encrypting data.

0 commit comments

Comments
 (0)