Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix: reject prefixed host resolve syntax

ldm0fbb6e06874b1
diff --git a/moli-fetch/src/blocking/mod.rs b/moli-fetch/src/blocking/mod.rs
index 72df752a5..bd26c5b29 100644
--- a/moli-fetch/src/blocking/mod.rs
+++ b/moli-fetch/src/blocking/mod.rs
@@ -648,10 +648,7 @@ pub(crate) fn configure_easy<H: Handler>(
.context("failed to disable curl HTTP no_proxy evaluation")?;
if !config.http_host_resolve().is_empty() {
let mut resolve = List::new();
- let force_permanent = config.network_address_policy().is_enforced();
- for entry in
- normalized_http_host_resolve_entries(config.http_host_resolve(), force_permanent)?
- {
+ for entry in normalized_http_host_resolve_entries(config.http_host_resolve())? {
resolve
.append(&entry)
.with_context(|| anyhow!("failed to build curl host resolve entry `{entry}`"))?;
@@ -793,106 +790,61 @@ pub(crate) fn resolve_host_resolve_override_ips(
let mut exact_match: Option<Vec<IpAddr>> = None;
let mut wildcard_match: Option<Vec<IpAddr>> = None;
for entry in entries {
- match parse_http_host_resolve_entry(entry)? {
- HttpHostResolveEntry::Add {
- host: entry_host,
- port: entry_port,
- addresses,
- ..
- } if entry_port == port => {
- if entry_host == "*" {
- wildcard_match = Some(addresses);
- } else if entry_host.eq_ignore_ascii_case(host) {
- exact_match = Some(addresses);
- }
- }
- HttpHostResolveEntry::Remove {
- host: entry_host,
- port: entry_port,
- } if entry_port == port => {
- if entry_host == "*" {
- wildcard_match = None;
- } else if entry_host.eq_ignore_ascii_case(host) {
- exact_match = None;
- }
+ let entry = parse_http_host_resolve_entry(entry)?;
+ if entry.port == port {
+ if entry.host == "*" {
+ wildcard_match = Some(entry.addresses);
+ } else if entry.host.eq_ignore_ascii_case(host) {
+ exact_match = Some(entry.addresses);
}
- _ => {}
}
}
Ok(exact_match.or(wildcard_match))
}
-enum HttpHostResolveEntry {
- Add {
- plus_prefix: bool,
- host: String,
- port: u16,
- addresses: Vec<IpAddr>,
- },
- Remove {
- host: String,
- port: u16,
- },
+
+struct HttpHostResolveEntry {
+ host: String,
+ port: u16,
+ addresses: Vec<IpAddr>,
}
impl HttpHostResolveEntry {
- fn curl_entry(&self, force_permanent: bool) -> String {
- match self {
- Self::Add {
- plus_prefix,
- host,
- port,
- addresses,
- } => {
- let prefix = if *plus_prefix && !force_permanent {
- "+"
- } else {
- ""
- };
- let addresses = addresses
- .iter()
- .map(format_http_host_resolve_ip)
- .collect::<Vec<_>>()
- .join(",");
- format!(
- "{prefix}{}:{port}:{addresses}",
- format_http_host_resolve_host(host)
- )
- }
- Self::Remove { host, port } => {
- format!("-{}:{port}", format_http_host_resolve_host(host))
- }
- }
+ fn curl_entry(&self) -> String {
+ let addresses = self
+ .addresses
+ .iter()
+ .map(format_http_host_resolve_ip)
+ .collect::<Vec<_>>()
+ .join(",");
+ format!(
+ "{}:{}:{addresses}",
+ format_http_host_resolve_host(&self.host),
+ self.port
+ )
}
}
-pub(crate) fn normalized_http_host_resolve_entries(
- entries: &[String],
- force_permanent: bool,
-) -> Result<Vec<String>> {
+pub(crate) fn normalized_http_host_resolve_entries(entries: &[String]) -> Result<Vec<String>> {
entries
.iter()
- .map(|entry| {
- parse_http_host_resolve_entry(entry).map(|entry| entry.curl_entry(force_permanent))
- })
+ .map(|entry| parse_http_host_resolve_entry(entry).map(|entry| entry.curl_entry()))
.collect()
}
+pub fn validate_http_host_resolve_entries(entries: &[String]) -> Result<()> {
+ for entry in entries {
+ parse_http_host_resolve_entry(entry)?;
+ }
+ Ok(())
+}
+
fn parse_http_host_resolve_entry(entry: &str) -> Result<HttpHostResolveEntry> {
let entry = entry.trim();
- if let Some(entry) = entry.strip_prefix('-') {
- let (host, port) = split_http_host_resolve_remove_entry(entry)?;
- return Ok(HttpHostResolveEntry::Remove {
- host: normalize_http_host_resolve_host(host),
- port: parse_http_host_resolve_port(port, entry)?,
- });
+ if entry.starts_with('+') || entry.starts_with('-') {
+ bail!("--http-host-resolve does not support `+` or `-` prefixes; use HOST:PORT:ADDR");
}
- let (plus_prefix, entry) = if let Some(entry) = entry.strip_prefix('+') {
- (true, entry)
- } else {
- (false, entry)
- };
let (host, port, address) = split_http_host_resolve_add_entry(entry)?;
let port = parse_http_host_resolve_port(port, entry)?;
let mut addresses = Vec::new();
@@ -909,8 +861,7 @@ fn parse_http_host_resolve_entry(entry: &str) -> Result<HttpHostResolveEntry> {
}
}
- Ok(HttpHostResolveEntry::Add {
- plus_prefix,
+ Ok(HttpHostResolveEntry {
host: normalize_http_host_resolve_host(host),
port,
addresses,
@@ -948,31 +899,6 @@ fn split_http_host_resolve_add_entry(entry: &str) -> Result<(&str, &str, &str)>
Ok((host, port, address))
}
-fn split_http_host_resolve_remove_entry(entry: &str) -> Result<(&str, &str)> {
- if let Some(entry) = entry.strip_prefix('[') {
- let Some(host_end) = entry.find(']') else {
- bail!("--http-host-resolve removal must be in HOST:PORT form");
- };
- let host = &entry[..host_end];
- let remainder = &entry[host_end + 1..];
- let Some(port) = remainder.strip_prefix(':') else {
- bail!("--http-host-resolve removal must be in HOST:PORT form");
- };
- if host.trim().is_empty() || port.trim().is_empty() {
- bail!("--http-host-resolve removal must be in HOST:PORT form");
- }
- return Ok((host.trim(), port.trim()));
- }
-
- let Some((host, port)) = entry.split_once(':') else {
- bail!("--http-host-resolve removal must be in HOST:PORT form");
- };
- if host.trim().is_empty() || port.trim().is_empty() {
- bail!("--http-host-resolve removal must be in HOST:PORT form");
- }
- Ok((host.trim(), port.trim()))
-}
-
fn parse_http_host_resolve_port(port: &str, entry: &str) -> Result<u16> {
port.parse::<u16>()
.with_context(|| format!("invalid --http-host-resolve port in `{entry}`"))
@@ -1550,36 +1476,36 @@ mod tests {
#[test]
fn http_host_resolve_entries_are_normalized_before_curl_configuration() {
- let entries = normalized_http_host_resolve_entries(
- &[
- " localhost:80: 1.1.1.1 , [2001:db8::1] ".to_owned(),
- " - localhost:80 ".to_owned(),
- ],
- false,
- )
+ let entries = normalized_http_host_resolve_entries(&[
+ " localhost:80: 1.1.1.1 , [2001:db8::1] ".to_owned(),
+ " [2001:db8::2]:443: 192.0.2.1 ".to_owned(),
+ ])
.unwrap();
assert_eq!(
entries,
vec![
"localhost:80:1.1.1.1,[2001:db8::1]".to_owned(),
- "-localhost:80".to_owned()
+ "[2001:db8::2]:443:192.0.2.1".to_owned()
]
);
}
#[test]
- fn address_policy_makes_temporary_host_resolve_entries_permanent() {
- let entries = vec!["+example.test:443:93.184.216.34".to_owned()];
-
- assert_eq!(
- normalized_http_host_resolve_entries(&entries, false).unwrap(),
- ["+example.test:443:93.184.216.34"]
- );
- assert_eq!(
- normalized_http_host_resolve_entries(&entries, true).unwrap(),
- ["example.test:443:93.184.216.34"]
- );
+ fn http_host_resolve_rejects_curl_prefix_syntax() {
+ for entry in [
+ "+example.test:443:93.184.216.34",
+ "-example.test:443",
+ "-example.test:443:93.184.216.34",
+ ] {
+ let error = normalized_http_host_resolve_entries(&[entry.to_owned()]).unwrap_err();
+ assert!(
+ error
+ .to_string()
+ .contains("does not support `+` or `-` prefixes"),
+ "{error:#}"
+ );
+ }
}
#[test]
diff --git a/moli-fetch/src/dns.rs b/moli-fetch/src/dns.rs
index 718cb2acb..94f00b403 100644
--- a/moli-fetch/src/dns.rs
+++ b/moli-fetch/src/dns.rs
@@ -34,10 +34,7 @@ pub(crate) fn curl_dns_resolution(
let policy = config.network_address_policy();
Ok(CurlDnsResolution::resolve_origin(
target,
- normalized_http_host_resolve_entries(
- config.http_host_resolve(),
- policy.is_enforced(),
- )?,
+ normalized_http_host_resolve_entries(config.http_host_resolve())?,
)
.with_network_address_policy(policy, url.to_string()))
}
diff --git a/moli-fetch/src/lib.rs b/moli-fetch/src/lib.rs
index af3e216da..fe12ccb19 100644
--- a/moli-fetch/src/lib.rs
+++ b/moli-fetch/src/lib.rs
@@ -34,7 +34,7 @@ pub use blocking::{
pub use blocking::{
clear_http_cache, clear_http_cache_for_origin, clear_http_cache_root,
clear_http_cache_root_for_origin, cookie_header_for_request, http_cache_stats,
- observe_cookie_access_report_for_request, trim_http_cache,
+ observe_cookie_access_report_for_request, trim_http_cache, validate_http_host_resolve_entries,
};
pub use cancellation::FetchCancelHandle;
pub use client::{FetchClient, FetchClientHandle};
diff --git a/moli/src/config.rs b/moli/src/config.rs
index b23450933..31059751a 100644
--- a/moli/src/config.rs
+++ b/moli/src/config.rs
@@ -6,7 +6,9 @@ use moli_core::{
page::{SubresourceJsonPathEquals, SubresourceJsonPathRegex, SubresourceResponseWaitCriteria},
runtime::BrowserConfig,
};
-use moli_fetch::{FetchConfig, WebBotAuthProfile, WebBotAuthSigner};
+use moli_fetch::{
+ FetchConfig, WebBotAuthProfile, WebBotAuthSigner, validate_http_host_resolve_entries,
+};
use std::path::PathBuf;
use std::str::FromStr;
@@ -254,27 +256,6 @@ fn configure_web_bot_auth(fetch: &mut FetchConfig, common: &CommonArgs) -> Resul
Ok(())
}
-fn validate_http_host_resolve_entries(entries: &[String]) -> Result<()> {
- for entry in entries {
- validate_http_host_resolve_entry(entry)?;
- }
- Ok(())
-}
-
-fn validate_http_host_resolve_entry(entry: &str) -> Result<()> {
- let mut parts = entry.splitn(3, ':');
- let host = parts.next().unwrap_or_default();
- let port = parts.next().unwrap_or_default();
- let address = parts.next().unwrap_or_default();
-
- if host.is_empty() || port.is_empty() || address.is_empty() {
- bail!("--http-host-resolve must be in HOST:PORT:ADDR form");
- }
- port.parse::<u16>()
- .with_context(|| format!("invalid --http-host-resolve port in `{entry}`"))?;
- Ok(())
-}
-
fn parse_block_cidrs(raw: Option<&str>) -> Vec<AnyIpCidr> {
raw.into_iter()
.flat_map(|value| value.split(','))
diff --git a/moli/tests/cli.rs b/moli/tests/cli.rs
index 345dedf7f..9a17dd534 100644
--- a/moli/tests/cli.rs
+++ b/moli/tests/cli.rs
@@ -1368,6 +1368,31 @@ fn app_config_rejects_invalid_http_host_resolve_entry() {
);
}
+#[test]
+fn app_config_rejects_prefixed_http_host_resolve_entries() {
+ for entry in [
+ "+web-platform.test:8443:127.0.0.1",
+ "-web-platform.test:8443",
+ "-web-platform.test:8443:127.0.0.1",
+ ] {
+ let option = format!("--http-host-resolve={entry}");
+ let cli = Cli::try_parse_from(normalize_args_for_compat([
+ "moli",
+ "serve",
+ option.as_str(),
+ ]))
+ .unwrap();
+
+ let error = AppConfig::from_cli(&cli).unwrap_err();
+ assert!(
+ error
+ .to_string()
+ .contains("does not support `+` or `-` prefixes"),
+ "{error:#}"
+ );
+ }
+}
+
#[test]
fn removed_cookie_cache_flag_is_rejected() {
let error = Cli::try_parse_from(normalize_args_for_compat([