Skip to content

The headless browser for AI agents and web scraping

Merge pull request #1095 from mnaza/fix/net-hardening-batch

SGavrl18eef9b88ff9
diff --cc crates/obscura-net/src/robots.rs
index a15fef43,30df7e4e..e0cdd149
--- a/crates/obscura-net/src/robots.rs
+++ b/crates/obscura-net/src/robots.rs
@@@ -83,22 -56,17 +83,31 @@@ 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();
+ // RFC 9309 2.2.1: a group applies when its product token equals one of
+ // ours, case-insensitively. Our identification string is a full
+ // User-Agent, so split it into its product tokens. Substring matching in
+ // either direction let "User-agent: Moz" govern a Mozilla/5.0 UA and
+ // "User-agent: ObscuraBot" govern a UA of "Obscura".
+ let our_tokens: Vec<String> = our_agent
+ .split(|c: char| c.is_whitespace() || matches!(c, '/' | '(' | ')' | ';' | ','))
+ .filter(|token| !token.is_empty())
+ .map(str::to_lowercase)
+ .collect();
let mut disallowed = Vec::new();
let mut allowed = Vec::new();
let mut in_matching_section = false;