The headless browser for AI agents and web scraping
fix(net): bound the robots.txt cache
Andrey Mnatsakanov0e17221dbb55
diff --git a/crates/obscura-net/src/robots.rs b/crates/obscura-net/src/robots.rsindex 0a13959e..a15fef43 100644--- a/crates/obscura-net/src/robots.rs+++ b/crates/obscura-net/src/robots.rs@@ -1,8 +1,21 @@-use std::collections::HashMap;+use std::collections::{HashMap, VecDeque};use std::sync::RwLock;+/// Origins whose robots.txt rules are kept per context; past this the oldest is+/// evicted and refetched if visited again.+const MAX_ROBOTS_ENTRIES: usize = 4096;+/// Bytes of a robots.txt body that are parsed (Google's documented limit).+const MAX_ROBOTS_BODY_BYTES: usize = 500 * 1024;+pub struct RobotsCache {- cache: RwLock<HashMap<String, RobotsRules>>,+ cache: RwLock<RobotsEntries>,+}++#[derive(Default)]+struct RobotsEntries {+ rules: HashMap<String, RobotsRules>,+ /// Origins in insertion order, oldest first, for eviction.+ order: VecDeque<String>,}#[derive(Debug, Clone)]@@ -14,22 +27,36 @@ struct RobotsRules {impl RobotsCache {pub fn new() -> Self {RobotsCache {- cache: RwLock::new(HashMap::new()),+ cache: RwLock::new(RobotsEntries::default()),}}pub fn parse_and_store(&self, domain: &str, body: &str, our_agent: &str) {- let rules = parse_robots_txt(body, our_agent);- self.cache.write().unwrap().insert(domain.to_string(), rules);+ let rules = parse_robots_txt(truncate_body(body), our_agent);+ let mut cache = self.cache.write().unwrap();+ if cache.rules.insert(domain.to_string(), rules).is_none() {+ cache.order.push_back(domain.to_string());+ }+ while cache.rules.len() > MAX_ROBOTS_ENTRIES {+ let Some(oldest) = cache.order.pop_front() else {+ break;+ };+ cache.rules.remove(&oldest);+ }+ }++ #[cfg(test)]+ fn len(&self) -> usize {+ self.cache.read().unwrap().rules.len()}pub fn contains(&self, origin: &str) -> bool {- self.cache.read().unwrap().contains_key(origin)+ self.cache.read().unwrap().rules.contains_key(origin)}pub fn is_allowed(&self, domain: &str, path: &str) -> bool {let cache = self.cache.read().unwrap();- let rules = match cache.get(domain) {+ let rules = match cache.rules.get(domain) {Some(r) => r,None => return true,};@@ -56,6 +83,20 @@ impl Default for RobotsCache {}}+/// The first `MAX_ROBOTS_BODY_BYTES` of `body`, cut back to the last complete+/// line so no rule is parsed from a truncated line.+fn truncate_body(body: &str) -> &str {+ if body.len() <= MAX_ROBOTS_BODY_BYTES {+ return body;+ }+ let mut end = MAX_ROBOTS_BODY_BYTES;+ while !body.is_char_boundary(end) {+ end -= 1;+ }+ let head = &body[..end];+ head.rfind('\n').map_or(head, |newline| &head[..newline])+}+fn parse_robots_txt(body: &str, our_agent: &str) -> RobotsRules {let our_agent_lower = our_agent.to_lowercase();let mut disallowed = Vec::new();@@ -169,4 +210,37 @@ mod tests {assert!(!cache.is_allowed("blocked.com", "/"));assert!(!cache.is_allowed("blocked.com", "/page"));}++ // One entry per navigated origin must not grow the cache without bound in+ // a long-lived context; the oldest origin is evicted and simply refetched.+ #[test]+ fn cache_evicts_oldest_origin_past_the_entry_cap() {+ let cache = RobotsCache::new();+ for i in 0..MAX_ROBOTS_ENTRIES + 10 {+ cache.parse_and_store(&format!("https://site{i}.test"), "", "Obscura");+ }+ assert_eq!(cache.len(), MAX_ROBOTS_ENTRIES);+ assert!(!cache.contains("https://site0.test"));+ assert!(cache.contains(&format!("https://site{}.test", MAX_ROBOTS_ENTRIES + 9)));++ // Re-storing a cached origin does not duplicate its eviction slot.+ let last = format!("https://site{}.test", MAX_ROBOTS_ENTRIES + 9);+ cache.parse_and_store(&last, "", "Obscura");+ assert_eq!(cache.len(), MAX_ROBOTS_ENTRIES);+ }++ // Like Google, only the first 500 KiB of a robots.txt body is parsed, so one+ // oversized file cannot inflate its cache entry.+ #[test]+ fn rules_past_the_body_cap_are_ignored() {+ let mut body = String::from("User-agent: *\nDisallow: /early\n");+ while body.len() <= MAX_ROBOTS_BODY_BYTES {+ body.push_str("# padding padding padding padding padding\n");+ }+ body.push_str("Disallow: /late\n");+ let cache = RobotsCache::new();+ cache.parse_and_store("big.test", &body, "Obscura");+ assert!(!cache.is_allowed("big.test", "/early"));+ assert!(cache.is_allowed("big.test", "/late"));+ }}