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 invalid blocked CIDRs

ldm0d70a30f92cce
diff --git a/moli/src/config.rs b/moli/src/config.rs
index 31059751a..f6bc5c8d0 100644
--- a/moli/src/config.rs
+++ b/moli/src/config.rs
@@ -191,7 +191,7 @@ fn apply_common_args(config: &mut AppConfig, common: &CommonArgs) -> Result<()>
config.fetch.cookie_files = common.cookie_file.clone();
config.browser.fetch_mut().set_network_blocking(
common.block_private_networks,
- parse_block_cidrs(common.block_cidrs.as_deref()),
+ parse_block_cidrs(common.block_cidrs.as_deref())?,
);
config
.browser
@@ -256,14 +256,19 @@ fn configure_web_bot_auth(fetch: &mut FetchConfig, common: &CommonArgs) -> Resul
Ok(())
}
-fn parse_block_cidrs(raw: Option<&str>) -> Vec<AnyIpCidr> {
- raw.into_iter()
- .flat_map(|value| value.split(','))
- .filter_map(|item| {
- let trimmed = item.trim();
- (!trimmed.is_empty())
- .then(|| AnyIpCidr::from_str(trimmed).ok())
- .flatten()
+fn parse_block_cidrs(raw: Option<&str>) -> Result<Vec<AnyIpCidr>> {
+ let Some(raw) = raw else {
+ return Ok(Vec::new());
+ };
+
+ raw.split(',')
+ .map(|item| {
+ let item = item.trim();
+ if item.is_empty() {
+ bail!("invalid --block-cidrs entry: CIDR must not be empty");
+ }
+ AnyIpCidr::from_str(item)
+ .with_context(|| format!("invalid --block-cidrs entry `{item}`"))
})
.collect()
}
diff --git a/moli/tests/cli.rs b/moli/tests/cli.rs
index 9a17dd534..a8bc9166d 100644
--- a/moli/tests/cli.rs
+++ b/moli/tests/cli.rs
@@ -1393,6 +1393,53 @@ fn app_config_rejects_prefixed_http_host_resolve_entries() {
}
}
+#[test]
+fn app_config_applies_block_cidrs() {
+ let cli = Cli::try_parse_from(normalize_args_for_compat([
+ "moli",
+ "serve",
+ "--block-cidrs",
+ "198.18.0.0/15, 2001:db8::/32",
+ ]))
+ .unwrap();
+
+ let config = AppConfig::from_cli(&cli).unwrap();
+ let block_cidrs: Vec<_> = config
+ .browser
+ .fetch()
+ .block_cidrs()
+ .iter()
+ .map(ToString::to_string)
+ .collect();
+ assert_eq!(block_cidrs, ["198.18.0.0/15", "2001:db8::/32"]);
+}
+
+#[test]
+fn app_config_rejects_invalid_block_cidrs() {
+ for (value, expected) in [
+ (
+ "198.18.0.0/15,not-a-cidr,203.0.113.0/24",
+ "invalid --block-cidrs entry `not-a-cidr`",
+ ),
+ (
+ "198.18.0.0/15,,203.0.113.0/24",
+ "invalid --block-cidrs entry: CIDR must not be empty",
+ ),
+ ("", "invalid --block-cidrs entry: CIDR must not be empty"),
+ ] {
+ let option = format!("--block-cidrs={value}");
+ 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(expected), "{error:#}");
+ }
+}
+
#[test]
fn removed_cookie_cache_flag_is_rejected() {
let error = Cli::try_parse_from(normalize_args_for_compat([