mirror of
https://github.com/typst/typst
synced 2025-05-14 17:15:28 +08:00
Raise error when failing to load & parse the specified certificate (#4554)
This commit is contained in:
parent
b037c19ccb
commit
684efa2e0e
@ -8,7 +8,7 @@ use std::sync::Arc;
|
|||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
use native_tls::{Certificate, TlsConnector};
|
use native_tls::{Certificate, TlsConnector};
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::OnceCell;
|
||||||
use ureq::Response;
|
use ureq::Response;
|
||||||
|
|
||||||
use crate::terminal;
|
use crate::terminal;
|
||||||
@ -16,13 +16,22 @@ use crate::terminal;
|
|||||||
/// Keep track of this many download speed samples.
|
/// Keep track of this many download speed samples.
|
||||||
const SPEED_SAMPLES: usize = 5;
|
const SPEED_SAMPLES: usize = 5;
|
||||||
|
|
||||||
/// Lazily loads a custom CA certificate if present, but if there's an error
|
/// Load a certificate from the file system if the `--cert` argument or
|
||||||
/// loading certificate, it just uses the default configuration.
|
/// `TYPST_CERT` environment variable is present. The certificate is cached for
|
||||||
static CERT: Lazy<Option<Certificate>> = Lazy::new(|| {
|
/// efficiency.
|
||||||
let path = crate::ARGS.cert.as_ref()?;
|
///
|
||||||
let pem = std::fs::read(path).ok()?;
|
/// - Returns `None` if `--cert` and `TYPST_CERT` are not set.
|
||||||
Certificate::from_pem(&pem).ok()
|
/// - Returns `Some(Ok(cert))` if the certificate was loaded successfully.
|
||||||
});
|
/// - Returns `Some(Err(err))` if an error occurred while loading the certificate.
|
||||||
|
fn cert() -> Option<Result<&'static Certificate, io::Error>> {
|
||||||
|
static CERT: OnceCell<Certificate> = OnceCell::new();
|
||||||
|
crate::ARGS.cert.as_ref().map(|path| {
|
||||||
|
CERT.get_or_try_init(|| {
|
||||||
|
let pem = std::fs::read(path)?;
|
||||||
|
Certificate::from_pem(&pem).map_err(io::Error::other)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/// Download binary data and display its progress.
|
/// Download binary data and display its progress.
|
||||||
#[allow(clippy::result_large_err)]
|
#[allow(clippy::result_large_err)]
|
||||||
@ -49,8 +58,8 @@ pub fn download(url: &str) -> Result<ureq::Response, ureq::Error> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Apply a custom CA certificate if present.
|
// Apply a custom CA certificate if present.
|
||||||
if let Some(cert) = &*CERT {
|
if let Some(cert) = cert() {
|
||||||
tls.add_root_certificate(cert.clone());
|
tls.add_root_certificate(cert?.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Configure native TLS.
|
// Configure native TLS.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user