use std::{
collections::HashMap,
sync::Arc,
time::{Duration, Instant},
};
use html_to_markdown_rs::{ConversionOptions, OutputFormat};
use parking_lot::Mutex;
use url::Url;
use wreq::{Client, ClientBuilder};
use wreq_util::Emulation;
use super::largest_fitting;
use crate::{
agent::{MAX_TOOL_RESULT_CHARS, fits_tool_result},
datatype::Value,
tool::{ToolDesc, ToolDescBuilder, ToolFunc},
tool_func,
};
const MAX_BODY_CHARS: usize = MAX_TOOL_RESULT_CHARS;
const MAX_FIELD_CHARS: usize = 500;
const MAX_DOWNLOAD_BYTES: usize = 2 * 1024 * 1024;
const MAX_URL_CHARS: usize = 2048;
const REQUEST_TIMEOUT_SECS: u64 = 10;
const PER_HOST_MIN_INTERVAL: Duration = Duration::from_millis(1000);
const MAX_REDIRECTS: usize = 10;
#[derive(Clone)]
struct WebFetchState {
client: Client,
last_hit: Arc<Mutex<HashMap<String, Instant>>>,
}
impl WebFetchState {
fn new() -> Self {
Self::from_builder(Self::client_builder())
}
fn client_builder() -> ClientBuilder {
Client::builder()
.emulation(Emulation::Firefox135)
.dns_resolver(net_guard::PublicOnlyResolver::default())
.redirect(wreq::redirect::Policy::custom(|attempt| {
let target = attempt.uri.clone();
if let Err(e) = net_guard::check_redirect_target(target.scheme_str(), target.host())
{
log::warn!("web_fetch: refused redirect to {target}: {e}");
return attempt.error(e);
}
wreq::redirect::Policy::limited(MAX_REDIRECTS).redirect(attempt)
}))
.timeout(Duration::from_secs(REQUEST_TIMEOUT_SECS))
}
fn from_builder(builder: ClientBuilder) -> Self {
let client = builder
.build()
.expect("wreq::Client builder cannot fail with these settings");
Self {
client,
last_hit: Arc::new(Mutex::new(HashMap::new())),
}
}
}
async fn rate_limit_for(state: &WebFetchState, host: &str) {
let wait = {
let mut m = state.last_hit.lock();
let now = Instant::now();
let wait = m
.get(host)
.and_then(|when| PER_HOST_MIN_INTERVAL.checked_sub(now.duration_since(*when)))
.unwrap_or(Duration::ZERO);
m.insert(host.to_string(), now + wait);
m.retain(|_, when| now.duration_since(*when) < PER_HOST_MIN_INTERVAL);
wait
};
if !wait.is_zero() {
tokio::time::sleep(wait).await;
}
}
async fn download(
state: &WebFetchState,
url: &str,
) -> Result<(String, String, u16, String), String> {
let resp = state
.client
.get(url)
.send()
.await
.map_err(|e| {
net_guard::blocked_reason(&e).unwrap_or_else(|| format!("request failed: {e}"))
})?;
let status = resp.status().as_u16();
let final_url = resp.uri().to_string();
if resp.extensions().get::<wreq::redirect::History>().is_some()
&& final_url.len() > MAX_URL_CHARS
{
return Err(format!(
"redirected to a url over {MAX_URL_CHARS} characters"
));
}
let content_type = resp
.headers()
.get(wreq::header::CONTENT_TYPE)
.and_then(|v| v.to_str().ok())
.unwrap_or("")
.chars()
.take(MAX_FIELD_CHARS)
.collect();
use futures::StreamExt;
let mut stream = resp.bytes_stream();
let mut buf: Vec<u8> = Vec::with_capacity(64 * 1024);
while let Some(chunk) = stream.next().await {
let chunk = chunk.map_err(|e| format!("read body chunk: {e}"))?;
let remaining = MAX_DOWNLOAD_BYTES.saturating_sub(buf.len());
if remaining == 0 {
break;
}
if chunk.len() <= remaining {
buf.extend_from_slice(&chunk);
} else {
buf.extend_from_slice(&chunk[..remaining]);
break;
}
}
let body = String::from_utf8_lossy(&buf).into_owned();
Ok((body, content_type, status, final_url))
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
enum BodyFormat {
Text,
Markdown,
Html,
}
impl BodyFormat {
fn parse(s: &str) -> Option<Self> {
match s.to_ascii_lowercase().as_str() {
"text" | "plain" | "plaintext" => Some(Self::Text),
"markdown" | "md" => Some(Self::Markdown),
"html" | "raw" => Some(Self::Html),
_ => None,
}
}
}
struct Converted {
body: String,
title: String,
}
fn convert_with_crate(html: &str, output_format: OutputFormat) -> Converted {
let opts = ConversionOptions::builder()
.output_format(output_format)
.skip_images(true)
.build();
match html_to_markdown_rs::convert(html, Some(opts)) {
Ok(result) => {
let title = result
.metadata
.document
.title
.clone()
.or_else(|| result.metadata.document.open_graph.get("title").cloned())
.unwrap_or_default()
.trim()
.chars()
.take(MAX_FIELD_CHARS)
.collect();
Converted {
body: result.content.unwrap_or_default(),
title,
}
}
Err(_) => Converted {
body: html.to_string(),
title: String::new(),
},
}
}
fn convert(body: &str, content_type: &str, format: BodyFormat) -> Converted {
if matches!(format, BodyFormat::Html) {
return Converted {
body: body.to_string(),
title: String::new(),
};
}
let is_html = content_type.to_ascii_lowercase().contains("html");
if !is_html {
return Converted {
body: body.to_string(),
title: String::new(),
};
}
let output_format = match format {
BodyFormat::Text => OutputFormat::Plain,
BodyFormat::Markdown => OutputFormat::Markdown,
BodyFormat::Html => unreachable!(),
};
convert_with_crate(body, output_format)
}
fn slice_body(text: &str, offset: usize, len: usize) -> (String, usize, Option<usize>) {
let end_char = offset.saturating_add(len);
let mut start_byte: Option<usize> = None;
let mut end_byte: Option<usize> = None;
let mut total_chars: usize = 0;
for (char_idx, (byte_idx, _)) in text.char_indices().enumerate() {
if char_idx == offset {
start_byte = Some(byte_idx);
}
if char_idx == end_char {
end_byte = Some(byte_idx);
}
total_chars = char_idx + 1;
}
let Some(s) = start_byte else {
return (String::new(), total_chars, None);
};
let e = end_byte.unwrap_or(text.len());
let slice = text[s..e].to_string();
let next_offset = if e < text.len() { Some(end_char) } else { None };
(slice, total_chars, next_offset)
}
fn now_iso_utc() -> String {
let secs = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs() as i64)
.unwrap_or(0);
let days = secs.div_euclid(86_400);
let sod = secs.rem_euclid(86_400);
let (y, m, d) = civil_from_days(days);
let hh = sod / 3600;
let mm = (sod % 3600) / 60;
let ss = sod % 60;
format!("{y:04}-{m:02}-{d:02}T{hh:02}:{mm:02}:{ss:02}Z")
}
fn civil_from_days(days: i64) -> (i64, u32, u32) {
let z = days + 719_468;
let era = z.div_euclid(146_097);
let doe = z.rem_euclid(146_097) as u64;
let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365;
let y = yoe as i64 + era * 400;
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
let mp = (5 * doy + 2) / 153;
let d = (doy - (153 * mp + 2) / 5 + 1) as u32;
let m = (if mp < 10 { mp + 3 } else { mp - 9 }) as u32;
let y = if m <= 2 { y + 1 } else { y };
(y, m, d)
}
pub fn get_web_fetch_tool_desc() -> ToolDesc {
ToolDescBuilder::new("web_fetch")
.description(concat!(
"Fetch a URL and return its body. HTML responses are converted to ",
"the requested format; non-HTML responses (JSON, plain text, etc.) ",
"pass through unchanged. A long body comes in pieces; for the next, ",
"call again with `offset` set to the previous `next_offset`. ",
"Rate-limited to 1 request/second per host."
))
.parameters(crate::to_value!({
"type": "object",
"properties": {
"url": {
"type": "string",
"description": "URL to fetch."
},
"format": {
"type": "string",
"enum": ["text", "markdown", "html"],
"description": "Body format. `text` (default) is visible text only — smallest token cost, no document structure. `markdown` keeps headings, lists, tables, and link targets. `html` returns the response body unchanged.",
"default": "text"
},
"offset": {
"type": "integer",
"description": "Character offset into the body. Use 0 for the first call; on subsequent calls pass the `next_offset` returned previously.",
"default": 0,
"minimum": 0
},
"length": {
"type": "integer",
"description": "Maximum number of characters returned in this call. A piece can hold fewer, so that it reaches you whole.",
"default": MAX_BODY_CHARS as i64,
"minimum": 256,
"maximum": MAX_BODY_CHARS as i64
}
},
"required": ["url"]
}))
.build()
}
async fn fetch_one(
state: WebFetchState,
url_str: String,
offset: usize,
length: usize,
format: BodyFormat,
) -> Value {
if url_str.len() > MAX_URL_CHARS {
let msg = format!("url exceeds {MAX_URL_CHARS} characters");
return crate::to_value!({"url": url_str, "error": msg});
}
let parsed = match Url::parse(&url_str) {
Ok(u) => u,
Err(e) => {
let msg = format!("invalid url: {e}");
return crate::to_value!({"url": url_str, "error": msg});
}
};
if !matches!(parsed.scheme(), "http" | "https") {
let msg = format!("unsupported scheme: {}", parsed.scheme());
return crate::to_value!({"url": url_str, "error": msg});
}
let host = match parsed.host_str() {
Some(h) => h.to_string(),
None => return crate::to_value!({"url": url_str, "error": "url has no host"}),
};
if let Err(e) = net_guard::check_host(&host) {
log::warn!("web_fetch: refused {url_str}: {e}");
return crate::to_value!({"url": url_str, "error": e.to_string()});
}
log::debug!("web_fetch: fetching {url_str}");
rate_limit_for(&state, &host).await;
let (body, content_type, status, final_url) = match download(&state, &url_str).await {
Ok(t) => t,
Err(e) => return crate::to_value!({"url": url_str, "error": e}),
};
let Converted {
body: readable,
title,
} = convert(&body, &content_type, format);
let (slice, total_chars, slice_next_offset) = slice_body(&readable, offset, length);
let retrieved_at = now_iso_utc();
let piece = |chars: usize| {
let (body, _, rest) = slice_body(&slice, 0, chars);
let next_offset = match rest {
Some(n) => Some(offset + n),
None => slice_next_offset,
};
crate::to_value!({
"url": final_url.as_str(),
"status": status as i64,
"title": title.as_str(),
"content_type": content_type.as_str(),
"body": body,
"body_length_total": total_chars as i64,
"next_offset": next_offset.map_or(Value::Null, |n| Value::from(n as i64)),
"complete": next_offset.is_none(),
"retrieved_at": retrieved_at.as_str()
})
};
let slice_chars = slice.chars().count();
piece(largest_fitting(slice_chars, |chars| fits_tool_result(&piece(chars))).unwrap_or(0))
}
pub fn get_web_fetch_tool_factory() -> impl Fn(&ToolDesc) -> anyhow::Result<ToolFunc> {
tool_factory(WebFetchState::new())
}
fn tool_factory(state: WebFetchState) -> impl Fn(&ToolDesc) -> anyhow::Result<ToolFunc> {
move |_| {
let state = state.clone();
Ok(
tool_func!(async |args: Value| -> Value with [state = state.clone()] {
let offset = args
.pointer("/offset")
.and_then(|v| v.as_integer())
.unwrap_or(0)
.max(0) as usize;
let length = args
.pointer("/length")
.and_then(|v| v.as_integer())
.map(|n| n.max(256).min(MAX_BODY_CHARS as i64) as usize)
.unwrap_or(MAX_BODY_CHARS);
let format = args
.pointer("/format")
.and_then(|v| v.as_str())
.and_then(BodyFormat::parse)
.unwrap_or(BodyFormat::Text);
let url_str = match args.pointer("/url").and_then(|v| v.as_str()) {
Some(u) => u.to_string(),
None => {
return crate::to_value!({
"error": "missing required parameter: `url`"
});
}
};
fetch_one(state, url_str, offset, length, format).await
}),
)
}
}
mod net_guard {
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
use wreq::dns::{Addrs, GaiResolver, Name, Resolve, Resolving};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Blocked(String);
impl std::fmt::Display for Blocked {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "blocked: {}", self.0)
}
}
impl std::error::Error for Blocked {}
pub fn blocked_reason(err: &(dyn std::error::Error + 'static)) -> Option<String> {
let mut source = Some(err);
while let Some(e) = source {
if let Some(blocked) = e.downcast_ref::<Blocked>() {
return Some(blocked.to_string());
}
source = e.source();
}
None
}
pub fn check_host(host: &str) -> Result<(), Blocked> {
let bare = host
.strip_prefix('[')
.and_then(|h| h.strip_suffix(']'))
.unwrap_or(host);
match bare.parse::<IpAddr>() {
Ok(ip) if !is_public(ip) => Err(Blocked(format!("{ip} is not a public address"))),
_ => Ok(()),
}
}
pub fn check_redirect_target(scheme: Option<&str>, host: Option<&str>) -> Result<(), Blocked> {
match scheme {
Some("http") | Some("https") => {}
Some(other) => return Err(Blocked(format!("unsupported redirect scheme: {other}"))),
None => return Err(Blocked("redirect target has no scheme".to_string())),
}
match host {
Some(h) => check_host(h),
None => Err(Blocked("redirect target has no host".to_string())),
}
}
fn filter_public(
host: &str,
addrs: impl Iterator<Item = SocketAddr>,
) -> Result<Vec<SocketAddr>, Blocked> {
let (kept, dropped): (Vec<_>, Vec<_>) = addrs.partition(|addr| is_public(addr.ip()));
if !dropped.is_empty() {
let ips: Vec<IpAddr> = dropped.iter().map(|addr| addr.ip()).collect();
log::warn!("net_guard: dropped non-public address(es) for '{host}': {ips:?}");
}
if kept.is_empty() {
return Err(Blocked(format!(
"'{host}' resolves only to non-public addresses"
)));
}
Ok(kept)
}
#[derive(Clone, Default)]
pub struct PublicOnlyResolver {
inner: GaiResolver,
}
impl Resolve for PublicOnlyResolver {
fn resolve(&self, name: Name) -> Resolving {
let host = name.as_str().to_string();
let resolving = self.inner.resolve(name);
Box::pin(async move {
let addrs = resolving.await?;
let kept = filter_public(&host, addrs)?;
Ok(Box::new(kept.into_iter()) as Addrs)
})
}
}
fn is_public(ip: IpAddr) -> bool {
match ip {
IpAddr::V4(v4) => is_public_v4(v4),
IpAddr::V6(v6) => {
if let Some(v4) = v6.to_ipv4_mapped() {
return is_public_v4(v4);
}
if let Some(v4) = nat64_embedded_v4(v6) {
return is_public_v4(v4);
}
is_public_v6(v6)
}
}
}
fn is_public_v4(ip: Ipv4Addr) -> bool {
if ip.is_unspecified()
|| ip.is_loopback()
|| ip.is_private()
|| ip.is_link_local()
|| ip.is_multicast()
|| ip.is_broadcast()
|| ip.is_documentation()
{
return false;
}
let [a, b, c, _] = ip.octets();
!(a == 0
|| (a == 100 && (64..128).contains(&b))
|| (a == 192 && b == 0 && c == 0)
|| (a == 192 && b == 88 && c == 99)
|| (a == 198 && (b == 18 || b == 19))
|| a >= 240)
}
fn is_public_v6(ip: Ipv6Addr) -> bool {
if ip.is_unspecified() || ip.is_loopback() || ip.is_multicast() {
return false;
}
let s = ip.segments();
!((s[0] == 0 && s[1] == 0 && s[2] == 0 && s[3] == 0)
|| (s[0] & 0xfe00) == 0xfc00
|| (s[0] & 0xffc0) == 0xfe80
|| (s[0] & 0xffc0) == 0xfec0
|| (s[0] == 0x2001 && s[1] == 0x0db8)
|| (s[0] == 0x2001 && s[1] < 0x0200)
|| s[0] == 0x2002
|| (s[0] == 0x0064 && s[1] == 0xff9b && s[2] == 0x0001)
|| (s[0] == 0x0100 && s[1] == 0 && s[2] == 0 && s[3] == 0)
|| (s[0] == 0x3fff && (s[1] & 0xf000) == 0)
|| s[0] == 0x5f00)
}
fn nat64_embedded_v4(ip: Ipv6Addr) -> Option<Ipv4Addr> {
let s = ip.segments();
let is_nat64 =
s[0] == 0x0064 && s[1] == 0xff9b && s[2] == 0 && s[3] == 0 && s[4] == 0 && s[5] == 0;
is_nat64.then(|| Ipv4Addr::from(((s[6] as u32) << 16) | s[7] as u32))
}
#[cfg(test)]
mod tests {
use url::Url;
use super::*;
#[test]
fn check_host_rejects_non_public_literals() {
for host in [
"127.0.0.1",
"0.0.0.0",
"10.0.0.1",
"172.16.0.1",
"192.168.1.1",
"169.254.169.254", "100.64.0.1",
"192.0.2.1",
"198.18.0.1",
"224.0.0.1",
"240.0.0.1",
"255.255.255.255",
"[::1]",
"::1",
"[::ffff:127.0.0.1]",
"[64:ff9b::7f00:1]", "[fd00::1]",
"[fe80::1]",
"[fec0::1]",
"[2001:db8::1]",
"[2001::1]", "[2002:7f00:1::1]", "[::7f00:1]", "[64:ff9b:1::7f00:1]",
"[64:ff9b:1:ffff::a9fe:a9fe]",
"[100::1]", "[3fff::1]", "[3fff:fff:ffff::1]", "[5f00::1]", "[2001:20::1]", ] {
assert!(
check_host(host).is_err(),
"{host} must be refused as non-public"
);
}
}
#[test]
fn check_host_allows_public_literals() {
for host in [
"1.1.1.1",
"8.8.8.8",
"[2606:4700:4700::1111]",
"[100:0:0:1::1]", "[3fff:1000::1]", "[5f01::1]", ] {
assert!(check_host(host).is_ok(), "{host} must be allowed");
}
}
#[test]
fn check_host_defers_names_to_the_resolver() {
for host in ["localhost", "example.com", "metadata.google.internal"] {
assert!(check_host(host).is_ok(), "{host} must reach the resolver");
}
}
#[test]
fn obfuscated_loopback_urls_are_rejected_after_parsing() {
for raw in [
"http://2130706433/", "http://0177.0.0.1/", "http://0x7f.0.0.1/", "http://127.1/", "http://127.0.0.1.:8080/",
] {
let bare = raw.trim_start_matches("http://").trim_end_matches('/');
assert!(
bare.parse::<IpAddr>().is_err(),
"{bare} parses as an address on its own, so this case does not \
exercise the normalization path it exists to cover"
);
let url = Url::parse(raw).unwrap_or_else(|e| panic!("parse {raw}: {e}"));
let host = url
.host_str()
.unwrap_or_else(|| panic!("{raw} has no host"));
assert!(
check_host(host).is_err(),
"{raw} normalized to host {host}, which must be refused"
);
}
}
#[test]
fn redirect_target_requires_http_scheme_and_public_host() {
assert!(check_redirect_target(Some("https"), Some("example.com")).is_ok());
assert!(check_redirect_target(Some("http"), Some("1.1.1.1")).is_ok());
assert!(check_redirect_target(Some("http"), Some("127.0.0.1")).is_err());
assert!(check_redirect_target(Some("file"), Some("example.com")).is_err());
assert!(check_redirect_target(None, Some("example.com")).is_err());
assert!(check_redirect_target(Some("https"), None).is_err());
}
#[test]
fn filter_public_keeps_public_and_drops_internal() {
let addrs = vec![
"1.1.1.1:443".parse::<SocketAddr>().unwrap(),
"127.0.0.1:443".parse::<SocketAddr>().unwrap(),
"169.254.169.254:80".parse::<SocketAddr>().unwrap(),
];
let kept =
filter_public("split.example", addrs.into_iter()).expect("public addr survives");
assert_eq!(kept, vec!["1.1.1.1:443".parse::<SocketAddr>().unwrap()]);
}
#[test]
fn filter_public_errors_when_every_answer_is_internal() {
let addrs = vec![
"127.0.0.1:80".parse::<SocketAddr>().unwrap(),
"10.1.2.3:80".parse::<SocketAddr>().unwrap(),
];
let err = filter_public("rebind.example", addrs.into_iter())
.expect_err("all-internal answer must be refused");
let text = err.to_string();
assert!(text.contains("rebind.example"), "error text: {text}");
}
#[test]
fn blocked_reason_digs_a_refusal_out_of_a_source_chain() {
#[derive(Debug)]
struct Opaque(Box<dyn std::error::Error + Send + Sync>);
impl std::fmt::Display for Opaque {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "client error (Connect)")
}
}
impl std::error::Error for Opaque {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(self.0.as_ref())
}
}
let refusal = check_host("127.0.0.1").expect_err("loopback must be refused");
let wrapped = Opaque(Box::new(Opaque(Box::new(refusal))));
assert_eq!(
blocked_reason(&wrapped).as_deref(),
Some("blocked: 127.0.0.1 is not a public address")
);
assert_eq!(wrapped.to_string(), "client error (Connect)");
}
#[test]
fn blocked_reason_ignores_an_ordinary_error() {
let err = std::io::Error::other("connection reset");
assert_eq!(blocked_reason(&err), None);
}
#[test]
fn nat64_prefix_unwraps_to_its_ipv4() {
let ip: Ipv6Addr = "64:ff9b::7f00:1".parse().unwrap();
assert_eq!(nat64_embedded_v4(ip), Some(Ipv4Addr::new(127, 0, 0, 1)));
let ip: Ipv6Addr = "64:ff9b::808:808".parse().unwrap();
assert_eq!(nat64_embedded_v4(ip), Some(Ipv4Addr::new(8, 8, 8, 8)));
let ip: Ipv6Addr = "64:ff9c::1".parse().unwrap();
assert_eq!(nat64_embedded_v4(ip), None);
}
#[test]
fn nat64_wrapping_a_public_address_stays_allowed() {
assert!(check_host("[64:ff9b::808:808]").is_ok());
assert!(check_host("[64:ff9c::1]").is_ok());
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn slice_body_basic() {
let s = "abcdefghij";
let (out, total, next) = slice_body(s, 0, 5);
assert_eq!(out, "abcde");
assert_eq!(total, 10);
assert_eq!(next, Some(5));
let (out2, _, next2) = slice_body(s, 5, 5);
assert_eq!(out2, "fghij");
assert_eq!(next2, None);
let (out3, _, next3) = slice_body(s, 100, 5);
assert_eq!(out3, "");
assert_eq!(next3, None);
}
#[test]
fn slice_body_unicode_is_char_based() {
let s = "한국어테스트";
let (out, total, next) = slice_body(s, 0, 3);
assert_eq!(out, "한국어");
assert_eq!(total, 6);
assert_eq!(next, Some(3));
let (out2, _, next2) = slice_body(s, 3, 3);
assert_eq!(out2, "테스트");
assert_eq!(next2, None);
}
#[test]
fn slice_body_next_offset_matches_chars_count() {
let s = "한국어테스트0123456789";
let (slice, _, next) = slice_body(s, 2, 4);
assert_eq!(next, Some(2 + slice.chars().count()));
}
#[test]
fn body_format_parse_accepts_known_values() {
assert_eq!(BodyFormat::parse("text"), Some(BodyFormat::Text));
assert_eq!(BodyFormat::parse("plain"), Some(BodyFormat::Text));
assert_eq!(BodyFormat::parse("Markdown"), Some(BodyFormat::Markdown));
assert_eq!(BodyFormat::parse("md"), Some(BodyFormat::Markdown));
assert_eq!(BodyFormat::parse("HTML"), Some(BodyFormat::Html));
assert_eq!(BodyFormat::parse("raw"), Some(BodyFormat::Html));
assert_eq!(BodyFormat::parse("xml"), None);
}
#[test]
fn last_hit_retain_keeps_fresh_and_future_drops_stale() {
let now = Instant::now();
let mut m: HashMap<String, Instant> = HashMap::new();
m.insert("fresh".into(), now);
m.insert("stale".into(), now - PER_HOST_MIN_INTERVAL * 2);
m.insert("future".into(), now + PER_HOST_MIN_INTERVAL);
m.retain(|_, when| now.duration_since(*when) < PER_HOST_MIN_INTERVAL);
assert!(m.contains_key("fresh"));
assert!(m.contains_key("future"));
assert!(!m.contains_key("stale"));
}
#[test]
fn convert_text_strips_script_and_style() {
let html = "<html><head><title>T</title></head><body>\
<script>var x = 1;</script>\
<style>body { color: red; }</style>\
<h1>Hello</h1>\
<p>World</p>\
</body></html>";
let out = convert(html, "text/html", BodyFormat::Text);
assert!(!out.body.contains("var x"), "{}", out.body);
assert!(!out.body.contains("color: red"), "{}", out.body);
assert!(out.body.contains("Hello"), "{}", out.body);
assert!(out.body.contains("World"), "{}", out.body);
}
#[test]
fn convert_markdown_preserves_structure() {
let html = "<html><body><h1>Title</h1><p>See <a href=\"https://example.com/x\">docs</a></p></body></html>";
let out = convert(html, "text/html", BodyFormat::Markdown);
assert!(out.body.contains("# Title"), "{}", out.body);
assert!(out.body.contains("(https://example.com/x)"), "{}", out.body);
}
#[test]
fn convert_html_returns_body_verbatim() {
let html = "<html><body><script>var x = 1;</script>\
<a href=\"https://example.com\">link</a></body></html>";
let out = convert(html, "text/html", BodyFormat::Html);
assert_eq!(out.body, html);
assert_eq!(out.title, "");
}
#[test]
fn convert_non_html_passes_through() {
let body = "{\"key\":\"value\"}";
let out_text = convert(body, "application/json", BodyFormat::Text);
assert_eq!(out_text.body, body);
assert_eq!(out_text.title, "");
let out_md = convert(body, "application/json", BodyFormat::Markdown);
assert_eq!(out_md.body, body);
}
#[test]
fn convert_extracts_title_from_title_tag_up_to_max_field_chars() {
for (title, extracted) in [
("Hello World".to_string(), "Hello World".to_string()),
("가".repeat(40_000), "가".repeat(MAX_FIELD_CHARS)),
] {
let html =
format!("<html><head><title>{title}</title></head><body><p>x</p></body></html>");
let out = convert(&html, "text/html", BodyFormat::Text);
assert_eq!(out.title, extracted);
}
}
#[test]
fn convert_falls_back_to_og_title() {
let html = "<html><head><meta property=\"og:title\" content=\"OG Title\"></head>\
<body><p>x</p></body></html>";
let out = convert(html, "text/html", BodyFormat::Text);
assert_eq!(out.title, "OG Title");
}
#[test]
fn descriptor_shape() {
let desc = get_web_fetch_tool_desc();
assert_eq!(desc.name, "web_fetch");
assert!(desc.description.is_some());
}
#[tokio::test]
async fn fetch_one_refuses_loopback_before_connecting() {
use std::sync::{Arc, Mutex};
use axum::{Router, routing::get};
let hits = Arc::new(Mutex::new(0u32));
let count = hits.clone();
let app = Router::new().route(
"/admin",
get(move || {
let count = count.clone();
async move {
*count.lock().unwrap() += 1;
"INTERNAL-ONLY db_password=hunter2"
}
}),
);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
tokio::spawn(async move { axum::serve(listener, app).await.ok() });
let result = fetch_one(
WebFetchState::new(),
format!("http://{addr}/admin"),
0,
MAX_BODY_CHARS,
BodyFormat::Text,
)
.await;
let error = result
.pointer("/error")
.and_then(|v| v.as_str())
.unwrap_or_default();
assert!(
error.contains("blocked"),
"expected a blocked error, got {result:?}"
);
assert!(
result.pointer("/body").is_none(),
"no body may reach the model: {result:?}"
);
assert_eq!(
*hits.lock().unwrap(),
0,
"the request must never reach the service"
);
}
#[tokio::test]
async fn fetch_one_refuses_a_name_resolving_inward_with_the_same_error() {
use std::sync::{Arc, Mutex};
use axum::{Router, routing::get};
let hits = Arc::new(Mutex::new(0u32));
let count = hits.clone();
let app = Router::new().route(
"/admin",
get(move || {
let count = count.clone();
async move {
*count.lock().unwrap() += 1;
"INTERNAL-ONLY db_password=hunter2"
}
}),
);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let port = listener.local_addr().unwrap().port();
tokio::spawn(async move { axum::serve(listener, app).await.ok() });
let result = fetch_one(
WebFetchState::new(),
format!("http://localhost:{port}/admin"),
0,
MAX_BODY_CHARS,
BodyFormat::Text,
)
.await;
let error = result
.pointer("/error")
.and_then(|v| v.as_str())
.unwrap_or_default();
assert!(
error.starts_with("blocked: "),
"a name refused by the resolver must report like a refused literal, \
got {result:?}"
);
assert_eq!(
*hits.lock().unwrap(),
0,
"the request must never reach the service"
);
}
#[tokio::test]
async fn fetch_one_refuses_redirect_into_loopback() {
use std::sync::{Arc, Mutex};
use axum::{Router, response::Redirect, routing::get};
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let go_hits = Arc::new(Mutex::new(0u32));
let meta_hits = Arc::new(Mutex::new(0u32));
let count = meta_hits.clone();
let started = go_hits.clone();
let app = Router::new()
.route(
"/go",
get(move || {
let started = started.clone();
async move {
*started.lock().unwrap() += 1;
Redirect::temporary(&format!("http://127.0.0.1:{}/meta", addr.port()))
}
}),
)
.route(
"/meta",
get(move || {
let count = count.clone();
async move {
*count.lock().unwrap() += 1;
"METADATA iam-role-cred=AKIA_EXAMPLE"
}
}),
);
tokio::spawn(async move { axum::serve(listener, app).await.ok() });
let state = WebFetchState::from_builder(
WebFetchState::client_builder()
.no_proxy()
.resolve_to_addrs("first-hop.test", [addr]),
);
let result = fetch_one(
state,
format!("http://first-hop.test:{}/go", addr.port()),
0,
MAX_BODY_CHARS,
BodyFormat::Text,
)
.await;
assert_eq!(
*go_hits.lock().unwrap(),
1,
"the first hop must be served, otherwise this test proves nothing"
);
let error = result
.pointer("/error")
.and_then(|v| v.as_str())
.unwrap_or_default();
assert!(
error.starts_with("blocked: "),
"following the hop must fail as a refusal, not as a bare transport \
error, got {result:?}"
);
assert_eq!(
*meta_hits.lock().unwrap(),
0,
"the redirect target must never be fetched"
);
}
async fn serve_page(
content_type: String,
body: String,
) -> (String, impl Fn() -> WebFetchState) {
use axum::{Router, http::header::CONTENT_TYPE, routing::get};
let app = Router::new().route(
"/page",
get(move || {
let (content_type, body) = (content_type.clone(), body.clone());
async move { ([(CONTENT_TYPE, content_type)], body) }
}),
);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
tokio::spawn(async move { axum::serve(listener, app).await.ok() });
let state = move || {
WebFetchState::from_builder(
WebFetchState::client_builder()
.no_proxy()
.resolve_to_addrs("page.test", [addr]),
)
};
(format!("http://page.test:{}/page", addr.port()), state)
}
async fn call(state: WebFetchState, args: Value) -> Value {
use futures::StreamExt;
let func = tool_factory(state)(&get_web_fetch_tool_desc()).unwrap();
let output = func.call_pure(args, "f").unwrap().next().await.unwrap();
output.message.contents[0].as_value().unwrap().clone()
}
#[tokio::test]
async fn web_fetch_pages_through_a_long_body_in_pieces_that_fit() {
let plain = || "text/plain; charset=utf-8".to_string();
let said = |n: usize| "He said \"stop\".\n".repeat(n);
let long_type = format!("text/html; p={}", "a".repeat(40_000));
let long_page = format!(
"<html><head><title>{}</title></head><body><p>{}</p></body></html>",
"t".repeat(40_000),
"He said \"stop\". ".repeat(5_000)
);
for (content_type, body, length, size) in [
(plain(), said(5_000), None, None),
(plain(), said(500), Some(1_000), Some(1_000)),
(plain(), said(100), Some(10), Some(256)),
(plain(), "\u{1}a".repeat(10_000), None, None),
(
plain(),
"가나다라마바사아자차카타파하 ".repeat(2_000),
None,
None,
),
(long_type, long_page, None, None),
] {
let (url, state) = serve_page(content_type.clone(), body.clone()).await;
let readable = convert(&body, &content_type, BodyFormat::Text).body;
let content_type: String = content_type.chars().take(MAX_FIELD_CHARS).collect();
let chars: Vec<char> = readable.chars().collect();
let mut read = String::new();
let mut offset = 0;
loop {
let mut args = crate::to_value!({"url": url.as_str(), "offset": offset as i64});
if let Some(length) = length {
let args = args.as_object_mut().unwrap();
args.insert("length".into(), Value::from(length as i64));
}
let piece = call(state(), args).await;
assert!(fits_tool_result(&piece), "offset {offset}: {piece:?}");
assert_eq!(
piece.pointer("/content_type").and_then(|v| v.as_str()),
Some(content_type.as_str())
);
let text = piece.pointer("/body").and_then(|v| v.as_str()).unwrap();
assert!(!text.is_empty(), "offset {offset}");
read.push_str(text);
let next = piece.pointer("/next_offset").and_then(|v| v.as_integer());
assert_eq!(
piece.pointer("/complete").and_then(|v| v.as_bool()),
Some(next.is_none()),
"offset {offset}"
);
let Some(next) = next else {
break;
};
let next = next as usize;
assert_eq!(next, offset + text.chars().count(), "offset {offset}");
match size {
None => {
let mut longer = piece.clone();
longer
.pointer_mut("/body")
.and_then(Value::as_string_mut)
.unwrap()
.push(chars[next]);
*longer.pointer_mut("/next_offset").unwrap() = Value::from(next as i64 + 1);
assert!(!fits_tool_result(&longer), "offset {offset}");
}
Some(size) => assert_eq!(next - offset, size, "offset {offset}"),
}
offset = next;
}
assert_eq!(read, readable);
}
}
#[tokio::test]
async fn web_fetch_refuses_a_redirect_to_a_url_over_max_url_chars() {
use axum::{Router, extract::Path, response::Redirect, routing::get};
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let page = format!("http://page.test:{}/page", addr.port());
let target = page.clone();
let app = Router::new()
.route("/page", get(|| async { "body" }))
.route(
"/to/{len}",
get(move |Path(len): Path<usize>| {
let to = format!("{target}?{}", "a".repeat(len - target.len() - 1));
async move { Redirect::temporary(&to) }
}),
);
tokio::spawn(async move { axum::serve(listener, app).await.ok() });
let to = |len: usize| format!("http://page.test:{}/to/{len}", addr.port());
let refused = format!("redirected to a url over {MAX_URL_CHARS} characters");
for (row, url, error) in [
("redirect, at the limit", to(MAX_URL_CHARS), None),
(
"redirect, over it",
to(MAX_URL_CHARS + 1),
Some(refused.as_str()),
),
(
"percent-encoded",
format!("{page}?q={}", "\"".repeat(1_000)),
None,
),
] {
let state = WebFetchState::from_builder(
WebFetchState::client_builder()
.no_proxy()
.resolve_to_addrs("page.test", [addr]),
);
let result = call(state, crate::to_value!({ "url": url.as_str() })).await;
let error_of = result.pointer("/error").and_then(|v| v.as_str());
assert_eq!(error_of, error, "{row}: {result:?}");
let body = error.is_none().then_some("body");
assert_eq!(
result.pointer("/body").and_then(|v| v.as_str()),
body,
"{row}"
);
}
}
#[tokio::test]
#[ignore = "requires network"]
async fn network_single_fetch_returns_200_and_body() {
let state = WebFetchState::new();
let result = fetch_one(
state,
"https://www.accuweather.com/ko/kr/bundang-gu/2330398/current-weather/2330398"
.to_string(),
0,
MAX_BODY_CHARS,
BodyFormat::Text,
)
.await;
let status = result
.pointer("/status")
.and_then(|v| v.as_integer())
.unwrap_or(0);
let body = result
.pointer("/body")
.and_then(|v| v.as_str())
.unwrap_or("");
let retrieved_at = result
.pointer("/retrieved_at")
.and_then(|v| v.as_str())
.unwrap_or("");
assert_eq!(status, 200, "result: {result:?}");
assert!(
body.to_ascii_lowercase().contains("accuweather") || body.contains("분당구"),
"body should mention AccuWeather or 분당구, got first 200 chars: {:?}",
body.chars().take(200).collect::<String>()
);
assert!(retrieved_at.ends_with('Z'), "retrieved_at: {retrieved_at}");
}
#[tokio::test]
#[ignore = "requires network"]
async fn network_single_fetch_html_returns_raw_markup() {
let state = WebFetchState::new();
let result = fetch_one(
state,
"https://example.com/".to_string(),
0,
MAX_BODY_CHARS,
BodyFormat::Html,
)
.await;
let body = result
.pointer("/body")
.and_then(|v| v.as_str())
.unwrap_or("");
assert!(
body.contains("<html") || body.contains("<HTML"),
"html format should preserve markup, got first 200 chars: {:?}",
body.chars().take(200).collect::<String>()
);
}
}