Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.
Rust ai-agentsai-assistantai-botautomationbotbots-teamchatbotclaude-duetgrokgrok-botgrok-bot-alternativeharnessllmmulti-agentopenai-dotsopenbotpersonal-agentrustself-hostedselfhosted-ai
WebFetch asks for any URL that reaches this computer or the local network, in every spelling
Lin (kwant bot)80b7675dd164
diff --git a/src/mcp/builtin_tools.rs b/src/mcp/builtin_tools.rsindex a3c2bd23..76e3e01e 100644--- a/src/mcp/builtin_tools.rs+++ b/src/mcp/builtin_tools.rs@@ -134,19 +134,24 @@ fn malformed_read(tool: &str, input: &Value) -> Option<String> {}}-/// Whether a WebFetch could reach kwnt's own API on this computer (see-/// [`super::shell_risk::kwnt_url`]), or names no URL kwnt can read.+/// Whether a WebFetch could reach this computer or the local network (see+/// [`super::fetch_risk`]), or names no URL kwnt can read. May resolve the host name, so call+/// it off the async workers.fn fetches_kwnt(input: &Value) -> bool {+ fetch_reason(input).is_some()+}++fn fetch_reason(input: &Value) -> Option<String> {match input.get("url").and_then(Value::as_str) {- Some(url) => super::shell_risk::kwnt_url(url.trim()),- None => true,+ Some(url) => super::fetch_risk::fetch_risk(url),+ None => Some("kwnt can't tell which page this fetches (no `url`)".into()),}}/// What "Ask only when risky" makes of a call [`decide`] would ask about.pub fn smart_verdict(tool: &str, input: &Value, folder: Option<&Path>) -> SmartVerdict {if tool == "WebFetch" {- return SmartVerdict::Ask("it fetches from kwnt itself, whose settings only the person may change".into());+ return SmartVerdict::Ask(fetch_reason(input).unwrap_or_else(|| "it fetches from this computer or the local network".into()));}if tool == "Bash" {let Some(folder) = folder else { return SmartVerdict::Ask("the bot has no working folder this turn".into()) };@@ -464,7 +469,7 @@ mod tests {#[test]fn lookups_run_without_asking() {for tool in ["WebSearch", "WebFetch", "TodoWrite", "Task"] {- assert_eq!(decide(tool, &json!({"url": "https://x"}), None), BuiltinDecision::Allow, "{tool}");+ assert_eq!(decide(tool, &json!({"url": "https://93.184.216.34/"}), None), BuiltinDecision::Allow, "{tool}");}}@@ -598,13 +603,17 @@ mod tests {"http://[::1]:8800/","http://0.0.0.0:8799/api/spaces","http://localhost:3000/api/spaces",+ "http://2130706433:8799/api/passwords/store",+ "http://169.254.169.254/latest/meta-data/",+ "http://192.168.1.1/",] {let input = json!({"url": url, "prompt": "read it"});assert_eq!(decide("WebFetch", &input, None), BuiltinDecision::Ask { outside_folder: false }, "{url}");assert!(!smart_verdict("WebFetch", &input, None).is_allowed(), "{url}");}assert_eq!(decide("WebFetch", &json!({"prompt": "x"}), None), BuiltinDecision::Ask { outside_folder: false });- for url in ["https://example.com/api/x", "http://localhost:3000/"] {+ // (a public IP, so the test needs no DNS; names are covered in fetch_risk's tests)+ for url in ["https://93.184.216.34/api/x", "http://localhost:3000/"] {assert_eq!(decide("WebFetch", &json!({"url": url, "prompt": "x"}), None), BuiltinDecision::Allow, "{url}");}}diff --git a/src/mcp/fetch_risk.rs b/src/mcp/fetch_risk.rsnew file mode 100644index 00000000..1413da01--- /dev/null+++ b/src/mcp/fetch_risk.rs@@ -0,0 +1,245 @@+//! Whether a bot's WebFetch needs a card: anything that can reach this computer or the local+//! network. The URL is read the way a browser (WHATWG URL parser) reads it, so `2130706433`,+//! `0x7f000001`, `0177.0.0.1`, `127.1`, `%6cocalhost` and `localhost.` all count as what they+//! really are. A name that isn't an IP is resolved, and one that points at this computer or a+//! private network asks too (a name that doesn't resolve asks as well).+//!+//! The local dev server case stays free: `localhost`, `127.0.0.1` or `[::1]` on a port that+//! isn't kwnt's and a path outside `/api/` (see [`super::shell_risk::kwnt_url`]).+//!+//! Not a sandbox: the fetch resolves the name again, so a name whose answer changes between the+//! check and the fetch (DNS rebinding) isn't caught here.++use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, ToSocketAddrs};+use std::time::Duration;++/// How long the check waits for a name to resolve before asking instead.+const RESOLVE_TIMEOUT: Duration = Duration::from_secs(2);++/// Where an address points.+#[derive(Debug, Clone, Copy, PartialEq, Eq)]+enum Reach {+ /// This computer (loopback, or "any address").+ Local,+ /// A private, link-local or carrier-grade NAT network (routers, NAS, cloud metadata).+ Private,+ Public,+}++/// Why a WebFetch of `url` needs a card, or `None` when it may run without one.+pub fn fetch_risk(url: &str) -> Option<String> {+ fetch_risk_with(url, &system_resolve)+}++/// [`fetch_risk`] with the name lookup supplied (tests).+pub fn fetch_risk_with(url: &str, resolve: &dyn Fn(&str, u16) -> Option<Vec<IpAddr>>) -> Option<String> {+ let url = url.trim();+ let with_scheme = if url.contains("://") { url.to_string() } else { format!("http://{url}") };+ let Ok(parsed) = url::Url::parse(&with_scheme) else {+ return Some("kwnt can't read this URL".into());+ };+ if !matches!(parsed.scheme(), "http" | "https") {+ return Some(format!("it fetches a `{}:` URL", parsed.scheme()));+ }+ let port = parsed.port_or_known_default().unwrap_or(80);+ let local_and_kwnt = || {+ // kwnt's own port or its API path, from any local spelling (the dev server stays free)+ super::shell_risk::kwnt_url(&format!("http://127.0.0.1:{port}{}", parsed.path()))+ .then(|| "it fetches from kwnt itself, whose settings only the person may change".to_string())+ };+ match parsed.host() {+ None => Some("kwnt can't tell where this URL goes".into()),+ Some(url::Host::Ipv4(ip)) => by_reach(reach(IpAddr::V4(ip)), &ip.to_string(), local_and_kwnt),+ Some(url::Host::Ipv6(ip)) => by_reach(reach(IpAddr::V6(ip)), &ip.to_string(), local_and_kwnt),+ Some(url::Host::Domain(name)) => {+ let name = name.trim_end_matches('.').to_ascii_lowercase();+ if name.is_empty() {+ return Some("kwnt can't tell where this URL goes".into());+ }+ if name == "localhost" || name.ends_with(".localhost") || name == "localhost.localdomain" {+ return local_and_kwnt();+ }+ let Some(addrs) = resolve(&name, port).filter(|a| !a.is_empty()) else {+ return Some(format!("kwnt couldn't look up `{name}`, so it can't tell whether it is on this computer"));+ };+ // A public name that points here: no dev server exemption, it's a way around the check+ match addrs.iter().map(|ip| reach(*ip)).find(|r| *r != Reach::Public) {+ Some(Reach::Local) => Some(format!("`{name}` points at this computer")),+ Some(_) => Some(format!("`{name}` points into a private network")),+ None => None,+ }+ }+ }+}++fn by_reach(reach: Reach, shown: &str, local_and_kwnt: impl FnOnce() -> Option<String>) -> Option<String> {+ match reach {+ Reach::Local => local_and_kwnt(),+ Reach::Private => Some(format!("{shown} is on a private or link-local network")),+ Reach::Public => None,+ }+}++fn reach(ip: IpAddr) -> Reach {+ match ip {+ IpAddr::V4(v4) => reach_v4(v4),+ IpAddr::V6(v6) => {+ if v6.is_loopback() || v6.is_unspecified() {+ return Reach::Local;+ }+ if let Some(v4) = embedded_v4(v6) {+ return reach_v4(v4);+ }+ let first = v6.segments()[0];+ // fc00::/7 unique local, fe80::/10 link-local, fec0::/10 old site-local+ if (first & 0xfe00) == 0xfc00 || (first & 0xffc0) == 0xfe80 || (first & 0xffc0) == 0xfec0 {+ Reach::Private+ } else {+ Reach::Public+ }+ }+ }+}++fn reach_v4(ip: Ipv4Addr) -> Reach {+ let [a, b, ..] = ip.octets();+ if ip.is_loopback() || ip.is_unspecified() || a == 0 {+ Reach::Local+ } else if ip.is_private() || ip.is_link_local() || ip.is_broadcast() || (a == 100 && (64..128).contains(&b)) {+ Reach::Private+ } else {+ Reach::Public+ }+}++/// The IPv4 address inside an IPv4-mapped (`::ffff:a.b.c.d`), IPv4-compatible (`::a.b.c.d`)+/// or NAT64 (`64:ff9b::a.b.c.d`) address.+fn embedded_v4(ip: Ipv6Addr) -> Option<Ipv4Addr> {+ if let Some(v4) = ip.to_ipv4_mapped() {+ return Some(v4);+ }+ let s = ip.segments();+ let tail = Ipv4Addr::new((s[6] >> 8) as u8, s[6] as u8, (s[7] >> 8) as u8, s[7] as u8);+ match s {+ [0, 0, 0, 0, 0, 0, _, _] => Some(tail),+ [0x64, 0xff9b, 0, 0, 0, 0, _, _] => Some(tail),+ _ => None,+ }+}++/// The system's answer for `name`, or `None` when it fails or takes longer than+/// [`RESOLVE_TIMEOUT`].+fn system_resolve(name: &str, port: u16) -> Option<Vec<IpAddr>> {+ let (tx, rx) = std::sync::mpsc::channel();+ let target = format!("{name}:{port}");+ std::thread::spawn(move || {+ let found = target.to_socket_addrs().map(|a| a.map(|s| s.ip()).collect::<Vec<_>>()).ok();+ let _ = tx.send(found);+ });+ rx.recv_timeout(RESOLVE_TIMEOUT).ok().flatten()+}++#[cfg(test)]+mod tests {+ use super::*;++ /// A fake DNS: `localtest.me` and `*.nip.io` point at 127.0.0.1, `nas.example` at+ /// 192.168.1.20, `example.com` at a public address, anything else doesn't resolve.+ fn fake(name: &str, _port: u16) -> Option<Vec<IpAddr>> {+ match name {+ "localtest.me" => Some(vec!["127.0.0.1".parse().unwrap()]),+ n if n.ends_with(".nip.io") => Some(vec!["127.0.0.1".parse().unwrap()]),+ "nas.example" => Some(vec!["192.168.1.20".parse().unwrap()]),+ "dual.example" => Some(vec!["93.184.216.34".parse().unwrap(), "::1".parse().unwrap()]),+ "example.com" | "docs.rs" => Some(vec!["93.184.216.34".parse().unwrap()]),+ _ => None,+ }+ }++ fn asks(url: &str) -> bool {+ fetch_risk_with(url, &fake).is_some()+ }++ /// Rigel's S1 list (scripts-rigel/probe4-webfetch.log), with kwnt on 8799.+ #[test]+ fn every_local_or_private_spelling_asks() {+ super::super::shell_risk::set_serve_port(8799);+ for url in [+ "http://127.1:8799/",+ "http://2130706433:8799/",+ "http://2130706433:8799/api/passwords/store",+ "http://0x7f000001:8799/",+ "http://0177.0.0.1:8799/",+ "http://0x7f.1:8799/",+ "http://localhost.:8799/",+ "http://[::ffff:7f00:1]:8799/",+ "http://[::ffff:127.0.0.1]:8799/api/x",+ "http://127.0.0.1.nip.io:8799/api/passwords/store",+ "http://localtest.me:8799/api/x",+ "http://localtest.me:3000/",+ "http://foo.localhost:8799/api/x",+ "http://localhost.localdomain:8799/",+ "http://0:8799/",+ "http://0.0.0.0:8799/",+ "http://127.0.0.1:8799",+ "http://127.5.5.5:8799/",+ "http://[0:0:0:0:0:0:0:1]:8799/",+ "http://LOCALHOST:8799/",+ "http://%6cocalhost:8799/api/x",+ "http://localhost%2e:8799/",+ "http://localhost:8799%2f",+ "http://user@localhost:8799",+ "http://[email protected]:8799/",+ "http://localhost:3000/api/spaces",+ "http://localhost:3000/%61pi/x",+ "localhost:8799",+ "http://169.254.169.254/latest/meta-data/",+ "http://metadata.google.internal/",+ "http://10.0.0.1:8799/",+ "http://192.168.1.1/",+ "http://172.16.5.4/",+ "http://100.64.0.1/",+ "http://[fe80::1]/",+ "http://[fd00::1]/",+ "http://[::127.0.0.1]:8799/",+ "http://[64:ff9b::a00:1]/",+ "http://my-nas.local/",+ "http://nas.example/",+ "http://dual.example/",+ "ftp://example.com/",+ "file:///etc/passwd",+ "http://",+ ] {+ assert!(asks(url), "{url} should ask");+ }+ }++ #[test]+ fn public_hosts_and_the_local_dev_server_stay_free() {+ super::super::shell_risk::set_serve_port(8799);+ for url in [+ "https://example.com/",+ "https://example.com/api/x",+ "http://127.0.0.1%[email protected]:8799/",+ "https://docs.rs/url",+ "http://93.184.216.34/",+ "http://[2606:2800:220:1:248:1893:25c8:1946]/",+ "http://localhost:3000/",+ "http://127.0.0.1:3000/index.html",+ "http://[::1]:3000/",+ "http://127.0.0.1/",+ "https://localhost/",+ ] {+ assert_eq!(fetch_risk_with(url, &fake), None, "{url} should run");+ }+ }++ #[test]+ fn the_reason_names_what_was_risky() {+ super::super::shell_risk::set_serve_port(8799);+ assert!(fetch_risk_with("http://localtest.me/", &fake).unwrap().contains("points at this computer"));+ assert!(fetch_risk_with("http://10.1.2.3/", &fake).unwrap().contains("private"));+ assert!(fetch_risk_with("http://nowhere.invalid/", &fake).unwrap().contains("couldn't look up"));+ assert!(fetch_risk_with("http://2130706433:8799/", &fake).unwrap().contains("kwnt itself"));+ }+}diff --git a/src/mcp/mod.rs b/src/mcp/mod.rsindex c7039185..272064f7 100644--- a/src/mcp/mod.rs+++ b/src/mcp/mod.rs@@ -5,6 +5,7 @@ pub mod builtin_tools;pub mod cli_config;pub mod client;pub mod echo_server;+pub mod fetch_risk;pub mod oauth;pub mod oauth_mock;pub mod registry;diff --git a/src/server/mcp_runtime.rs b/src/server/mcp_runtime.rsindex dc523754..5f579b9c 100644--- a/src/server/mcp_runtime.rs+++ b/src/server/mcp_runtime.rs@@ -408,13 +408,25 @@ async fn decide(session: &PermissionSession, args: &Value, smart: bool) -> Value}_ => {let folder = mcp::builtin_tools::canonical_folder(session.cwd.as_deref());- match mcp::builtin_tools::decide(tool_name, &input, folder.as_deref()) {+ // A WebFetch check may resolve a host name: keep that off the async workers+ let (decision, smart_verdict) = {+ let (tool, input, folder) = (tool_name.to_string(), input.clone(), folder.clone());+ tokio::task::spawn_blocking(move || {+ let decision = mcp::builtin_tools::decide(&tool, &input, folder.as_deref());+ let ask = matches!(decision, mcp::builtin_tools::BuiltinDecision::Ask { .. });+ let verdict = (smart && ask).then(|| mcp::builtin_tools::smart_verdict(&tool, &input, folder.as_deref()));+ (decision, verdict)+ })+ .await+ .unwrap_or((mcp::builtin_tools::BuiltinDecision::Ask { outside_folder: false }, None))+ };+ match decision {mcp::builtin_tools::BuiltinDecision::Allow => {return json!({"behavior": "allow", "updatedInput": input});}mcp::builtin_tools::BuiltinDecision::Ask { outside_folder } => {if smart {- verdict = Some(mcp::builtin_tools::smart_verdict(tool_name, &input, folder.as_deref()));+ verdict = Some(smart_verdict.unwrap_or_else(|| mcp::SmartVerdict::Ask("kwnt couldn't finish checking this call".into())));}let cwd = folder.or_else(|| session.cwd.clone()).map(|f| f.display().to_string());("", tool_name, Some(mcp::BuiltinCall { cwd, outside_folder }))diff --git a/tests/browser_tools_test.rs b/tests/browser_tools_test.rsindex 912b7f4c..670fc940 100644--- a/tests/browser_tools_test.rs+++ b/tests/browser_tools_test.rs@@ -99,7 +99,7 @@ async fn claude_asks(app: &Router, token: &str, tool_name: &str) -> String {app,&format!("/api/mcp/permission/{token}"),None,- rpc("tools/call", json!({"name": PERMISSION_TOOL, "arguments": {"tool_name": tool_name, "input": {"url": "x"}, "tool_use_id": "u1"}})),+ rpc("tools/call", json!({"name": PERMISSION_TOOL, "arguments": {"tool_name": tool_name, "input": {"url": "http://93.184.216.34/"}, "tool_use_id": "u1"}})),).await;assert_eq!(status, StatusCode::OK, "{v}");diff --git a/tests/builtin_tools_test.rs b/tests/builtin_tools_test.rsindex dce1988d..81305655 100644--- a/tests/builtin_tools_test.rs+++ b/tests/builtin_tools_test.rs@@ -92,7 +92,8 @@ async fn built_in_tools_follow_the_folder_policy() {// Looking things up and writing inside the folder: no cardfor (tool, input) in [("WebSearch", json!({"query": "sma crossover"})),- ("WebFetch", json!({"url": "https://example.com", "prompt": "summary"})),+ // A public IP, so the check needs no DNS (a name that doesn't resolve asks)+ ("WebFetch", json!({"url": "https://93.184.216.34/", "prompt": "summary"})),("Write", json!({"file_path": real.join("strategy.go").display().to_string(), "content": "package x"})),("Edit", json!({"file_path": "strategy.go", "old_string": "x", "new_string": "y"})),("Read", json!({"file_path": "strategy.go"})),