.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
ip_limit.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2""".. _botdetection.ip_limit:
3
4Method ``ip_limit``
5-------------------
6
7The ``ip_limit`` method counts request from an IP in *sliding windows*. If
8there are to many requests in a sliding window, the request is evaluated as a
9bot request. This method requires a redis DB and needs a HTTP X-Forwarded-For_
10header. To take privacy only the hash value of an IP is stored in the redis DB
11and at least for a maximum of 10 minutes.
12
13The :py:obj:`.link_token` method can be used to investigate whether a request is
14*suspicious*. To activate the :py:obj:`.link_token` method in the
15:py:obj:`.ip_limit` method add the following configuration:
16
17.. code:: toml
18
19 [botdetection.ip_limit]
20 link_token = true
21
22If the :py:obj:`.link_token` method is activated and a request is *suspicious*
23the request rates are reduced:
24
25- :py:obj:`BURST_MAX` -> :py:obj:`BURST_MAX_SUSPICIOUS`
26- :py:obj:`LONG_MAX` -> :py:obj:`LONG_MAX_SUSPICIOUS`
27
28To intercept bots that get their IPs from a range of IPs, there is a
29:py:obj:`SUSPICIOUS_IP_WINDOW`. In this window the suspicious IPs are stored
30for a longer time. IPs stored in this sliding window have a maximum of
31:py:obj:`SUSPICIOUS_IP_MAX` accesses before they are blocked. As soon as the IP
32makes a request that is not suspicious, the sliding window for this IP is
33dropped.
34
35.. _X-Forwarded-For:
36 https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For
37
38"""
39from __future__ import annotations
40from ipaddress import (
41 IPv4Network,
42 IPv6Network,
43)
44
45import flask
46import werkzeug
47
48from searx import redisdb
49from searx.redislib import incr_sliding_window, drop_counter
50
51from . import link_token
52from . import config
53from ._helpers import (
54 too_many_requests,
55 logger,
56)
57
58
59logger = logger.getChild('ip_limit')
60
61BURST_WINDOW = 20
62"""Time (sec) before sliding window for *burst* requests expires."""
63
64BURST_MAX = 15
65"""Maximum requests from one IP in the :py:obj:`BURST_WINDOW`"""
66
67BURST_MAX_SUSPICIOUS = 2
68"""Maximum of suspicious requests from one IP in the :py:obj:`BURST_WINDOW`"""
69
70LONG_WINDOW = 600
71"""Time (sec) before the longer sliding window expires."""
72
73LONG_MAX = 150
74"""Maximum requests from one IP in the :py:obj:`LONG_WINDOW`"""
75
76LONG_MAX_SUSPICIOUS = 10
77"""Maximum suspicious requests from one IP in the :py:obj:`LONG_WINDOW`"""
78
79API_WONDOW = 3600
80"""Time (sec) before sliding window for API requests (format != html) expires."""
81
82API_MAX = 4
83"""Maximum requests from one IP in the :py:obj:`API_WONDOW`"""
84
85SUSPICIOUS_IP_WINDOW = 3600 * 24 * 30
86"""Time (sec) before sliding window for one suspicious IP expires."""
87
88SUSPICIOUS_IP_MAX = 3
89"""Maximum requests from one suspicious IP in the :py:obj:`SUSPICIOUS_IP_WINDOW`."""
90
91
93 network: IPv4Network | IPv6Network,
94 request: flask.Request,
95 cfg: config.Config,
96) -> werkzeug.Response | None:
97
98 # pylint: disable=too-many-return-statements
99 redis_client = redisdb.client()
100
101 if network.is_link_local and not cfg['botdetection.ip_limit.filter_link_local']:
102 logger.debug("network %s is link-local -> not monitored by ip_limit method", network.compressed)
103 return None
104
105 if request.args.get('format', 'html') != 'html':
106 c = incr_sliding_window(redis_client, 'ip_limit.API_WONDOW:' + network.compressed, API_WONDOW)
107 if c > API_MAX:
108 return too_many_requests(network, "too many request in API_WINDOW")
109
110 if cfg['botdetection.ip_limit.link_token']:
111
112 suspicious = link_token.is_suspicious(network, request, True)
113
114 if not suspicious:
115 # this IP is no longer suspicious: release ip again / delete the counter of this IP
116 drop_counter(redis_client, 'ip_limit.SUSPICIOUS_IP_WINDOW' + network.compressed)
117 return None
118
119 # this IP is suspicious: count requests from this IP
120 c = incr_sliding_window(
121 redis_client, 'ip_limit.SUSPICIOUS_IP_WINDOW' + network.compressed, SUSPICIOUS_IP_WINDOW
122 )
123 if c > SUSPICIOUS_IP_MAX:
124 logger.error("BLOCK: too many request from %s in SUSPICIOUS_IP_WINDOW (redirect to /)", network)
125 return flask.redirect(flask.url_for('index'), code=302)
126
127 c = incr_sliding_window(redis_client, 'ip_limit.BURST_WINDOW' + network.compressed, BURST_WINDOW)
128 if c > BURST_MAX_SUSPICIOUS:
129 return too_many_requests(network, "too many request in BURST_WINDOW (BURST_MAX_SUSPICIOUS)")
130
131 c = incr_sliding_window(redis_client, 'ip_limit.LONG_WINDOW' + network.compressed, LONG_WINDOW)
132 if c > LONG_MAX_SUSPICIOUS:
133 return too_many_requests(network, "too many request in LONG_WINDOW (LONG_MAX_SUSPICIOUS)")
134
135 return None
136
137 # vanilla limiter without extensions counts BURST_MAX and LONG_MAX
138 c = incr_sliding_window(redis_client, 'ip_limit.BURST_WINDOW' + network.compressed, BURST_WINDOW)
139 if c > BURST_MAX:
140 return too_many_requests(network, "too many request in BURST_WINDOW (BURST_MAX)")
141
142 c = incr_sliding_window(redis_client, 'ip_limit.LONG_WINDOW' + network.compressed, LONG_WINDOW)
143 if c > LONG_MAX:
144 return too_many_requests(network, "too many request in LONG_WINDOW (LONG_MAX)")
145
146 return None
werkzeug.Response|None filter_request(IPv4Network|IPv6Network network, flask.Request request, config.Config cfg)
Definition ip_limit.py:96