relay_filter/
web_crawlers.rs1use once_cell::sync::Lazy;
4use regex::Regex;
5
6use crate::{FilterConfig, FilterStatKey, Filterable};
7
8static WEB_CRAWLERS: Lazy<Regex> = Lazy::new(|| {
9 Regex::new(
10 r"(?ix)
11 Mediapartners-Google|
12 AdsBot-Google|
13 Googlebot|
14 FeedFetcher-Google|
15 BingBot| # Bing search
16 BingPreview|
17 Baiduspider| # Baidu search
18 Slurp| # Yahoo
19 Sogou| # Sogou
20 facebook| # facebook
21 meta-| # meta/facebook
22 ia_archiver| # Alexa
23 bots?[/\s\);]| # Generic bot
24 spider[/\s\);]| # Generic spider
25 Slack| # Slack - see https://api.slack.com/robots
26 Calypso\sAppCrawler| # Google indexing bot
27 pingdom| # Pingdom
28 lyticsbot| # Lytics
29 AWS\sSecurity\sScanner| # AWS Security Scanner causing DisallowedHost errors in Django, see
30 # https://forums.aws.amazon.com/thread.jspa?messageID=932404
31 # and https://github.com/getsentry/sentry-python/issues/641
32 HubSpot\sCrawler| # HubSpot web crawler (web-crawlers@hubspot.com)
33 Bytespider| # Bytedance
34 Better\sUptime| # Better Uptime
35 Cloudflare-Healthchecks| # Cloudflare Health Checks
36 GTmetrix| # GTmetrix
37 BrightEdgeOnCrawl| # BrightEdge - see https://www.brightedge.com/news/press-releases/brightedge-acquires-oncrawl-future-proof-web-30-strategies
38 ELB-HealthChecker| # AWS Elastic Load Balancing Health Checks
39 naver.me/spd| # Yeti/1.1 - naver.me
40 ClaudeBot| # Anthropic - see https://support.anthropic.com/en/articles/8896518-does-anthropic-crawl-data-from-the-web-and-how-can-site-owners-block-the-crawler
41 CCBot| # CCBot - see https://commoncrawl.org/ccbot
42 OAI-SearchBot| # OpenAI - see https://platform.openai.com/docs/bots
43 GPTBot| # OpenAI - see https://platform.openai.com/docs/bots
44 PerplexityBot| # Perplexity - see https://docs.perplexity.ai/guides/bots
45 Applebot| # Apple - see https://support.apple.com/en-us/119829
46 DuckDuckBot # DuckDuckGo - see https://duckduckgo.com/duckduckgo-help-pages/results/duckduckbot
47 "
48 )
49 .expect("Invalid web crawlers filter Regex")
50});
51
52static ALLOWED_WEB_CRAWLERS: Lazy<Regex> = Lazy::new(|| {
53 Regex::new(
54 r"(?ix)
55 Slackbot\s1\.\d+| # Slack - see https://api.slack.com/robots
56 SentryUptimeBot # Uptime Checker https://docs.sentry.io/product/alerts/uptime-monitoring/
57 ",
58 )
59 .expect("Invalid allowed web crawlers filter Regex")
60});
61
62fn matches(user_agent: Option<&str>) -> bool {
64 if let Some(user_agent) = user_agent {
65 WEB_CRAWLERS.is_match(user_agent) && !ALLOWED_WEB_CRAWLERS.is_match(user_agent)
66 } else {
67 false
68 }
69}
70
71pub fn should_filter<F: Filterable>(item: &F, config: &FilterConfig) -> Result<(), FilterStatKey> {
73 if !config.is_enabled {
74 return Ok(());
75 }
76
77 if matches(item.user_agent()) {
78 return Err(FilterStatKey::WebCrawlers);
79 }
80
81 Ok(())
82}
83
84#[cfg(test)]
85mod tests {
86 use super::*;
87 use crate::testutils;
88
89 #[test]
90 fn test_filter_when_disabled() {
91 let evt = testutils::get_event_with_user_agent("Googlebot");
92 let filter_result = should_filter(&evt, &FilterConfig { is_enabled: false });
93 assert_eq!(
94 filter_result,
95 Ok(()),
96 "Event filtered although filter should have been disabled"
97 )
98 }
99
100 #[test]
101 fn test_filter_banned_user_agents() {
102 let user_agents = [
103 "Mediapartners-Google",
104 "AdsBot-Google",
105 "Googlebot",
106 "FeedFetcher-Google",
107 "BingBot",
108 "BingPreview",
109 "Baiduspider",
110 "Slurp",
111 "Sogou",
112 "facebook",
113 "facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)",
114 "facebookcatalog/1.0",
115 "meta-externalagent/1.1 (+https://developers.facebook.com/docs/sharing/webmasters/crawler)",
116 "meta-externalfetcher/1.1",
117 "ia_archiver",
118 "bots ",
119 "bots;",
120 "bots)",
121 "spider ",
122 "spider;",
123 "spider)",
124 "Calypso AppCrawler",
125 "pingdom",
126 "lyticsbot",
127 "AWS Security Scanner",
128 "Mozilla/5.0 (Linux; Android 6.0.1; Calypso AppCrawler Build/MMB30Y; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/53.0.2785.124 Mobile Safari/537.36",
129 "Slackbot-LinkExpanding 1.0 (+https://api.slack.com/robots)",
130 "Slack-ImgProxy 0.19 (+https://api.slack.com/robots)",
131 "Twitterbot/1.0",
132 "FeedFetcher-Google; (+http://www.google.com/feedfetcher.html)",
133 "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",
134 "AdsBot-Google (+http://www.google.com/adsbot.html)",
135 "Mozilla/5.0 (compatible; HubSpot Crawler; web-crawlers@hubspot.com)",
136 "Mozilla/5.0 (Linux; Android 5.0) AppleWebKit/537.36 (KHTML, like Gecko) Mobile Safari/537.36 (compatible; Bytespider; spider-feedback@bytedance.com)",
137 "Better Uptime Bot Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36",
138 "Mozilla/5.0 (compatible;Cloudflare-Healthchecks/1.0;+https://www.cloudflare.com/; healthcheck-id: 0d1ca23e292c8c14)",
139 "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 GTmetrix",
140 "Mozilla/5.0 (compatible; BrightEdgeOnCrawl/1.0; +http://www.oncrawl.com)",
141 "ELB-HealthChecker/2.0",
142 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko; compatible; Yeti/1.1; +https://naver.me/spd) Chrome/127.0.0.0 Safari/537.36",
143 "Mozilla/5.0; ClaudeBot",
144 "Mozilla/5.0; CCBot",
145 "; OAI-SearchBot/1.0; +https://openai.com/searchbot",
146 "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko); compatible; GPTBot/1.1; +https://openai.com/gptbot",
147 "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; PerplexityBot/1.0; +https://perplexity.ai/perplexitybot)",
148 "Mozilla/5.0 (Device; OS_version) AppleWebKit/WebKit_version (KHTML, like Gecko)Version/Safari_version [Mobile/Mobile_version] Safari/WebKit_version (Applebot/Applebot_version; +http://www.apple.com/go/applebot)",
149 "DuckDuckBot/1.1; (+http://duckduckgo.com/duckduckbot.html)",
150 ];
151
152 for banned_user_agent in &user_agents {
153 let event = testutils::get_event_with_user_agent(banned_user_agent);
154 let filter_result = should_filter(&event, &FilterConfig { is_enabled: true });
155 assert_ne!(
156 filter_result,
157 Ok(()),
158 "Failed to filter web crawler with user agent '{banned_user_agent}'"
159 );
160 }
161 }
162
163 #[test]
164 fn test_dont_filter_normal_user_agents() {
165 let normal_user_agents = [
166 "some user agent",
167 "IE",
168 "ie",
169 "opera",
170 "safari",
171 "APIs-Google (+https://developers.google.com/webmasters/APIs-Google.html)",
172 "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36",
173 "Slackbot 1.0(+https://api.slack.com/robots)",
174 "SentryUptimeBot/1.0 (+http://docs.sentry.io/product/alerts/uptime-monitoring/)",
175 "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko); compatible; ChatGPT-User/1.0; +https://openai.com/bot",
176 ];
177 for user_agent in &normal_user_agents {
178 let event = testutils::get_event_with_user_agent(user_agent);
179 let filter_result = should_filter(&event, &FilterConfig { is_enabled: true });
180 assert_eq!(
181 filter_result,
182 Ok(()),
183 "Failed benign user agent '{user_agent}'"
184 );
185 }
186 }
187}