| 
									
										
										
										
											2021-12-19 11:01:50 +01:00
										 |  |  | # SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							|  |  |  | # lint: pylint | 
					
						
							|  |  |  | # pyright: basic | 
					
						
							|  |  |  | """Some bot protection / rate limitation
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-10 22:45:59 +02:00
										 |  |  | To monitor rate limits and protect privacy the IP addresses are getting stored | 
					
						
							| 
									
										
										
										
											2022-03-25 10:23:15 +01:00
										 |  |  | with a hash so the limiter plugin knows who to block.  A redis database is | 
					
						
							|  |  |  | needed to store the hash values. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-19 11:01:50 +01:00
										 |  |  | Enable the plugin in ``settings.yml``: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | - ``server.limiter: true`` | 
					
						
							|  |  |  | - ``redis.url: ...`` check the value, see :ref:`settings redis` | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import re | 
					
						
							|  |  |  | from flask import request | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-11 21:58:32 +01:00
										 |  |  | from searx import redisdb | 
					
						
							| 
									
										
										
										
											2022-04-06 23:46:11 +02:00
										 |  |  | from searx.redislib import incr_sliding_window | 
					
						
							| 
									
										
										
										
											2021-12-19 11:01:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | name = "Request limiter" | 
					
						
							|  |  |  | description = "Limit the number of request" | 
					
						
							|  |  |  | default_on = False | 
					
						
							|  |  |  | preference_section = 'service' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | re_bot = re.compile( | 
					
						
							|  |  |  |     r'(' | 
					
						
							|  |  |  |     + r'[Cc][Uu][Rr][Ll]|[wW]get|Scrapy|splash|JavaFX|FeedFetcher|python-requests|Go-http-client|Java|Jakarta|okhttp' | 
					
						
							|  |  |  |     + r'|HttpClient|Jersey|Python|libwww-perl|Ruby|SynHttpClient|UniversalFeedParser|Googlebot|GoogleImageProxy' | 
					
						
							|  |  |  |     + r'|bingbot|Baiduspider|yacybot|YandexMobileBot|YandexBot|Yahoo! Slurp|MJ12bot|AhrefsBot|archive.org_bot|msnbot' | 
					
						
							|  |  |  |     + r'|MJ12bot|SeznamBot|linkdexbot|Netvibes|SMTBot|zgrab|James BOT|Sogou|Abonti|Pixray|Spinn3r|SemrushBot|Exabot' | 
					
						
							|  |  |  |     + r'|ZmEu|BLEXBot|bitlybot' | 
					
						
							|  |  |  |     + r')' | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 23:46:11 +02:00
										 |  |  | def is_accepted_request() -> bool: | 
					
						
							| 
									
										
										
										
											2021-12-19 11:01:50 +01:00
										 |  |  |     # pylint: disable=too-many-return-statements | 
					
						
							| 
									
										
										
										
											2022-04-06 23:46:11 +02:00
										 |  |  |     redis_client = redisdb.client() | 
					
						
							| 
									
										
										
										
											2021-12-19 11:01:50 +01:00
										 |  |  |     user_agent = request.headers.get('User-Agent', '') | 
					
						
							|  |  |  |     x_forwarded_for = request.headers.get('X-Forwarded-For', '') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if request.path == '/image_proxy': | 
					
						
							|  |  |  |         if re_bot.match(user_agent): | 
					
						
							|  |  |  |             return False | 
					
						
							|  |  |  |         return True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-17 20:27:02 +01:00
										 |  |  |     if request.path == '/search': | 
					
						
							| 
									
										
										
										
											2022-04-06 23:46:11 +02:00
										 |  |  |         c_burst = incr_sliding_window(redis_client, 'IP limit, burst' + x_forwarded_for, 20) | 
					
						
							|  |  |  |         c_10min = incr_sliding_window(redis_client, 'IP limit, 10 minutes' + x_forwarded_for, 600) | 
					
						
							| 
									
										
										
										
											2022-02-17 20:27:02 +01:00
										 |  |  |         if c_burst > 15 or c_10min > 150: | 
					
						
							| 
									
										
										
										
											2022-04-06 23:46:11 +02:00
										 |  |  |             logger.debug("to many request")  # pylint: disable=undefined-variable | 
					
						
							| 
									
										
										
										
											2022-02-12 15:57:07 +01:00
										 |  |  |             return False | 
					
						
							| 
									
										
										
										
											2021-12-19 11:01:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if re_bot.match(user_agent): | 
					
						
							| 
									
										
										
										
											2022-04-06 23:46:11 +02:00
										 |  |  |             logger.debug("detected bot")  # pylint: disable=undefined-variable | 
					
						
							| 
									
										
										
										
											2021-12-19 11:01:50 +01:00
										 |  |  |             return False | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-17 20:27:02 +01:00
										 |  |  |         if len(request.headers.get('Accept-Language', '').strip()) == '': | 
					
						
							| 
									
										
										
										
											2022-04-06 23:46:11 +02:00
										 |  |  |             logger.debug("missing Accept-Language")  # pylint: disable=undefined-variable | 
					
						
							| 
									
										
										
										
											2021-12-19 11:01:50 +01:00
										 |  |  |             return False | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if request.headers.get('Connection') == 'close': | 
					
						
							| 
									
										
										
										
											2022-04-06 23:46:11 +02:00
										 |  |  |             logger.debug("got Connection=close")  # pylint: disable=undefined-variable | 
					
						
							| 
									
										
										
										
											2021-12-19 11:01:50 +01:00
										 |  |  |             return False | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         accept_encoding_list = [l.strip() for l in request.headers.get('Accept-Encoding', '').split(',')] | 
					
						
							| 
									
										
										
										
											2022-08-25 23:21:12 +02:00
										 |  |  |         if 'gzip' not in accept_encoding_list and 'deflate' not in accept_encoding_list: | 
					
						
							| 
									
										
										
										
											2022-04-06 23:46:11 +02:00
										 |  |  |             logger.debug("suspicious Accept-Encoding")  # pylint: disable=undefined-variable | 
					
						
							| 
									
										
										
										
											2021-12-19 11:01:50 +01:00
										 |  |  |             return False | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if 'text/html' not in request.accept_mimetypes: | 
					
						
							| 
									
										
										
										
											2022-04-06 23:46:11 +02:00
										 |  |  |             logger.debug("Accept-Encoding misses text/html")  # pylint: disable=undefined-variable | 
					
						
							| 
									
										
										
										
											2021-12-19 11:01:50 +01:00
										 |  |  |             return False | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if request.args.get('format', 'html') != 'html': | 
					
						
							| 
									
										
										
										
											2022-04-06 23:46:11 +02:00
										 |  |  |             c = incr_sliding_window(redis_client, 'API limit' + x_forwarded_for, 3600) | 
					
						
							| 
									
										
										
										
											2021-12-19 11:01:50 +01:00
										 |  |  |             if c > 4: | 
					
						
							| 
									
										
										
										
											2022-04-06 23:46:11 +02:00
										 |  |  |                 logger.debug("API limit exceeded")  # pylint: disable=undefined-variable | 
					
						
							| 
									
										
										
										
											2021-12-19 11:01:50 +01:00
										 |  |  |                 return False | 
					
						
							|  |  |  |     return True | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 23:46:11 +02:00
										 |  |  | def pre_request(): | 
					
						
							|  |  |  |     if not is_accepted_request(): | 
					
						
							| 
									
										
										
										
											2022-07-05 22:57:26 +02:00
										 |  |  |         return 'Too Many Requests', 429 | 
					
						
							| 
									
										
										
										
											2022-04-06 23:46:11 +02:00
										 |  |  |     return None | 
					
						
							| 
									
										
										
										
											2021-12-19 11:01:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def init(app, settings): | 
					
						
							|  |  |  |     if not settings['server']['limiter']: | 
					
						
							|  |  |  |         return False | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-15 18:38:32 +02:00
										 |  |  |     if not redisdb.client(): | 
					
						
							|  |  |  |         logger.error("The limiter requires Redis")  # pylint: disable=undefined-variable | 
					
						
							| 
									
										
										
										
											2021-12-19 11:01:50 +01:00
										 |  |  |         return False | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 23:46:11 +02:00
										 |  |  |     app.before_request(pre_request) | 
					
						
							| 
									
										
										
										
											2021-12-19 11:01:50 +01:00
										 |  |  |     return True |