Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

refactor(curl): share TLS certificate configuration

ldm06832d179d945
diff --git a/moli-curl/src/lib.rs b/moli-curl/src/lib.rs
index c9eb8ed2b..40bda9474 100644
--- a/moli-curl/src/lib.rs
+++ b/moli-curl/src/lib.rs
@@ -2,6 +2,7 @@
mod dns_adapter;
mod runtime;
+mod tls;
pub mod websocket;
pub use dns_adapter::CurlDnsResolution;
@@ -9,3 +10,4 @@ pub use runtime::{
CurlMultiCompletion, CurlMultiJob, CurlMultiRuntime, CurlMultiRuntimeConfig, CurlOriginKey,
CurlSubmitError, CurlTransferId,
};
+pub use tls::CurlTlsConfig;
diff --git a/moli-curl/src/tls.rs b/moli-curl/src/tls.rs
new file mode 100644
index 000000000..ca17241c1
--- /dev/null
+++ b/moli-curl/src/tls.rs
@@ -0,0 +1,121 @@
+use std::{fmt, path::PathBuf};
+
+use anyhow::{Context, Result};
+use curl::easy::{Easy2, Handler};
+
+/// Owned TLS settings shared by HTTP and WebSocket connections.
+#[derive(Clone, PartialEq, Eq)]
+pub struct CurlTlsConfig {
+ /// Verify both the server certificate chain and hostname.
+ pub verify: bool,
+ pub ca_cert: Option<PathBuf>,
+ pub client_cert: Option<PathBuf>,
+ pub client_key: Option<PathBuf>,
+ pub client_cert_password: Option<String>,
+}
+
+impl Default for CurlTlsConfig {
+ fn default() -> Self {
+ Self {
+ verify: true,
+ ca_cert: None,
+ client_cert: None,
+ client_key: None,
+ client_cert_password: None,
+ }
+ }
+}
+
+impl fmt::Debug for CurlTlsConfig {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("CurlTlsConfig")
+ .field("verify", &self.verify)
+ .field("ca_cert", &self.ca_cert)
+ .field("client_cert", &self.client_cert)
+ .field("client_key", &self.client_key)
+ .field(
+ "client_cert_password",
+ &self.client_cert_password.as_ref().map(|_| "[REDACTED]"),
+ )
+ .finish()
+ }
+}
+
+impl CurlTlsConfig {
+ /// Install TLS settings on a fresh handle, before its first transfer.
+ ///
+ /// The caller decides whether credentials are allowed for the current URL.
+ /// CA trust applies even when the client identity is omitted. Using a fresh
+ /// handle lets curl isolate reusable connections by their TLS settings.
+ pub fn configure<H: Handler>(
+ &self,
+ easy: &mut Easy2<H>,
+ include_client_identity: bool,
+ ) -> Result<()> {
+ easy.ssl_verify_peer(self.verify)
+ .context("failed to configure curl TLS peer verification")?;
+ easy.ssl_verify_host(self.verify)
+ .context("failed to configure curl TLS host verification")?;
+ if let Some(ca_cert) = &self.ca_cert {
+ easy.cainfo(ca_cert).with_context(|| {
+ format!(
+ "failed to configure curl CA certificate `{}`",
+ ca_cert.display()
+ )
+ })?;
+ }
+ if include_client_identity {
+ if let Some(client_cert) = &self.client_cert {
+ easy.ssl_cert(client_cert).with_context(|| {
+ format!(
+ "failed to configure curl client certificate `{}`",
+ client_cert.display()
+ )
+ })?;
+ let cert_type = match client_cert
+ .extension()
+ .and_then(|extension| extension.to_str())
+ .map(str::to_ascii_lowercase)
+ .as_deref()
+ {
+ Some("p12" | "pfx") => "P12",
+ Some("cer" | "der") => "DER",
+ _ => "PEM",
+ };
+ easy.ssl_cert_type(cert_type)
+ .context("failed to configure curl client certificate type")?;
+ }
+ if let Some(client_key) = &self.client_key {
+ easy.ssl_key(client_key).with_context(|| {
+ format!(
+ "failed to configure curl client private key `{}`",
+ client_key.display()
+ )
+ })?;
+ easy.ssl_key_type("PEM")
+ .context("failed to configure curl client private key type")?;
+ }
+ if let Some(password) = &self.client_cert_password {
+ easy.key_password(password)
+ .context("failed to configure curl client certificate password")?;
+ }
+ }
+ Ok(())
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn debug_omits_client_certificate_password() {
+ let config = CurlTlsConfig {
+ client_cert_password: Some("private-certificate-password".to_owned()),
+ ..CurlTlsConfig::default()
+ };
+ let debug = format!("{config:?}");
+ assert!(!debug.contains("private-certificate-password"));
+ assert!(debug.contains("[REDACTED]"));
+ }
+}
diff --git a/moli-fetch/src/blocking/mod.rs b/moli-fetch/src/blocking/mod.rs
index d60bbd931..2c70179eb 100644
--- a/moli-fetch/src/blocking/mod.rs
+++ b/moli-fetch/src/blocking/mod.rs
@@ -652,57 +652,12 @@ pub(crate) fn configure_easy<H: Handler>(
easy.accept_encoding("")
.context("failed to enable curl response decompression")?;
- easy.ssl_verify_peer(config.tls_verify_host())
- .context("failed to configure curl TLS peer verification")?;
- easy.ssl_verify_host(config.tls_verify_host())
- .context("failed to configure curl TLS host verification")?;
- if let Some(ca_cert) = config.ca_cert() {
- easy.cainfo(ca_cert).with_context(|| {
- format!(
- "failed to configure curl CA certificate `{}`",
- ca_cert.display()
- )
- })?;
- }
// TLS client identities are Fetch credentials. Check each redirect target;
// curl also uses the client certificate configuration to match reusable
// connections, keeping authenticated and anonymous requests separate.
- if request.allows_credentials_for_url(request_url) {
- if let Some(client_cert) = config.client_cert() {
- easy.ssl_cert(client_cert).with_context(|| {
- format!(
- "failed to configure curl client certificate `{}`",
- client_cert.display()
- )
- })?;
- let cert_type = match client_cert
- .extension()
- .and_then(|extension| extension.to_str())
- .map(str::to_ascii_lowercase)
- .as_deref()
- {
- Some("p12" | "pfx") => "P12",
- Some("cer" | "der") => "DER",
- _ => "PEM",
- };
- easy.ssl_cert_type(cert_type)
- .context("failed to configure curl client certificate type")?;
- }
- if let Some(client_key) = config.client_key() {
- easy.ssl_key(client_key).with_context(|| {
- format!(
- "failed to configure curl client private key `{}`",
- client_key.display()
- )
- })?;
- easy.ssl_key_type("PEM")
- .context("failed to configure curl client private key type")?;
- }
- if let Some(password) = config.client_cert_password() {
- easy.key_password(password)
- .context("failed to configure curl client certificate password")?;
- }
- }
+ config
+ .tls_config()
+ .configure(easy, request.allows_credentials_for_url(request_url))?;
easy.useragent(config.user_agent())
.context("failed to set curl user-agent")?;
easy.url(request_url.as_str())
diff --git a/moli-fetch/src/config.rs b/moli-fetch/src/config.rs
index 83d7f396d..108db34c1 100644
--- a/moli-fetch/src/config.rs
+++ b/moli-fetch/src/config.rs
@@ -2,6 +2,7 @@ use std::{num::NonZeroU32, path::PathBuf};
use cidr::AnyIpCidr;
use moli_browser_profile::{BrowserIdentityProfile, DEFAULT_ACCEPT_LANGUAGE};
+use moli_curl::CurlTlsConfig;
use crate::WebBotAuthSigner;
@@ -35,11 +36,7 @@ pub struct FetchConfig {
http_max_response_size: Option<usize>,
block_private_networks: bool,
block_cidrs: Vec<AnyIpCidr>,
- tls_verify_host: bool,
- ca_cert: Option<PathBuf>,
- client_cert: Option<PathBuf>,
- client_key: Option<PathBuf>,
- client_cert_password: Option<String>,
+ tls: CurlTlsConfig,
web_bot_auth: Option<WebBotAuthSigner>,
}
@@ -144,11 +141,15 @@ impl FetchConfig {
}
pub fn tls_verify_host(&self) -> bool {
- self.tls_verify_host
+ self.tls.verify
}
pub fn set_tls_verify_host(&mut self, tls_verify_host: bool) {
- self.tls_verify_host = tls_verify_host;
+ self.tls.verify = tls_verify_host;
+ }
+
+ pub fn tls_config(&self) -> &CurlTlsConfig {
+ &self.tls
}
/// Configure TLS trust and mutual-TLS credentials for this fetch runtime,
@@ -162,26 +163,26 @@ impl FetchConfig {
client_key: Option<PathBuf>,
client_cert_password: Option<String>,
) {
- self.ca_cert = ca_cert;
- self.client_cert = client_cert;
- self.client_key = client_key;
- self.client_cert_password = client_cert_password;
+ self.tls.ca_cert = ca_cert;
+ self.tls.client_cert = client_cert;
+ self.tls.client_key = client_key;
+ self.tls.client_cert_password = client_cert_password;
}
pub fn ca_cert(&self) -> Option<&std::path::Path> {
- self.ca_cert.as_deref()
+ self.tls.ca_cert.as_deref()
}
pub fn client_cert(&self) -> Option<&std::path::Path> {
- self.client_cert.as_deref()
+ self.tls.client_cert.as_deref()
}
pub fn client_key(&self) -> Option<&std::path::Path> {
- self.client_key.as_deref()
+ self.tls.client_key.as_deref()
}
pub fn client_cert_password(&self) -> Option<&str> {
- self.client_cert_password.as_deref()
+ self.tls.client_cert_password.as_deref()
}
pub fn web_bot_auth(&self) -> Option<&WebBotAuthSigner> {
@@ -321,11 +322,7 @@ impl Default for FetchConfig {
http_max_response_size: None,
block_private_networks: false,
block_cidrs: Vec::new(),
- tls_verify_host: true,
- ca_cert: None,
- client_cert: None,
- client_key: None,
- client_cert_password: None,
+ tls: CurlTlsConfig::default(),
web_bot_auth: None,
}
}