2023-05-28 18:58:31 +02:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
# lint: pylint
|
2023-05-26 17:24:43 +02:00
|
|
|
""".. _botdetection.ip_limit:
|
|
|
|
|
2023-05-23 18:16:37 +02:00
|
|
|
Method ``ip_limit``
|
|
|
|
-------------------
|
|
|
|
|
|
|
|
The ``ip_limit`` method counts request from an IP in *sliding windows*. If
|
|
|
|
there are to many requests in a sliding window, the request is evaluated as a
|
|
|
|
bot request. This method requires a redis DB and needs a HTTP X-Forwarded-For_
|
|
|
|
header. To take privacy only the hash value of an IP is stored in the redis DB
|
|
|
|
and at least for a maximum of 10 minutes.
|
|
|
|
|
2023-05-27 18:58:06 +02:00
|
|
|
The :py:obj:`.link_token` method can be used to investigate whether a request is
|
|
|
|
*suspicious*. To activate the :py:obj:`.link_token` method in the
|
2023-10-02 16:36:07 +02:00
|
|
|
:py:obj:`.ip_limit` method add the following configuration:
|
2023-05-27 18:58:06 +02:00
|
|
|
|
|
|
|
.. code:: toml
|
|
|
|
|
|
|
|
[botdetection.ip_limit]
|
|
|
|
link_token = true
|
|
|
|
|
|
|
|
If the :py:obj:`.link_token` method is activated and a request is *suspicious*
|
|
|
|
the request rates are reduced:
|
2023-05-23 18:16:37 +02:00
|
|
|
|
|
|
|
- :py:obj:`BURST_MAX` -> :py:obj:`BURST_MAX_SUSPICIOUS`
|
|
|
|
- :py:obj:`LONG_MAX` -> :py:obj:`LONG_MAX_SUSPICIOUS`
|
|
|
|
|
2023-05-27 21:36:34 +02:00
|
|
|
To intercept bots that get their IPs from a range of IPs, there is a
|
|
|
|
:py:obj:`SUSPICIOUS_IP_WINDOW`. In this window the suspicious IPs are stored
|
|
|
|
for a longer time. IPs stored in this sliding window have a maximum of
|
|
|
|
:py:obj:`SUSPICIOUS_IP_MAX` accesses before they are blocked. As soon as the IP
|
|
|
|
makes a request that is not suspicious, the sliding window for this IP is
|
2023-09-15 09:53:03 +02:00
|
|
|
dropped.
|
2023-05-27 21:36:34 +02:00
|
|
|
|
2023-05-23 18:16:37 +02:00
|
|
|
.. _X-Forwarded-For:
|
|
|
|
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For
|
|
|
|
|
|
|
|
"""
|
2023-06-01 15:41:48 +02:00
|
|
|
from __future__ import annotations
|
|
|
|
from ipaddress import (
|
|
|
|
IPv4Network,
|
|
|
|
IPv6Network,
|
|
|
|
)
|
2023-05-23 18:16:37 +02:00
|
|
|
|
|
|
|
import flask
|
2023-05-28 18:58:31 +02:00
|
|
|
import werkzeug
|
2023-05-26 17:24:43 +02:00
|
|
|
|
2023-05-23 18:16:37 +02:00
|
|
|
from searx import redisdb
|
2023-05-27 21:36:34 +02:00
|
|
|
from searx.redislib import incr_sliding_window, drop_counter
|
2023-05-23 18:16:37 +02:00
|
|
|
|
|
|
|
from . import link_token
|
2023-10-02 16:36:07 +02:00
|
|
|
from . import config
|
2023-06-03 13:43:34 +02:00
|
|
|
from ._helpers import (
|
|
|
|
too_many_requests,
|
|
|
|
logger,
|
|
|
|
)
|
2023-05-28 18:58:31 +02:00
|
|
|
|
2023-05-23 18:16:37 +02:00
|
|
|
|
2023-06-03 13:43:34 +02:00
|
|
|
logger = logger.getChild('ip_limit')
|
2023-05-23 18:16:37 +02:00
|
|
|
|
|
|
|
BURST_WINDOW = 20
|
|
|
|
"""Time (sec) before sliding window for *burst* requests expires."""
|
|
|
|
|
|
|
|
BURST_MAX = 15
|
|
|
|
"""Maximum requests from one IP in the :py:obj:`BURST_WINDOW`"""
|
|
|
|
|
|
|
|
BURST_MAX_SUSPICIOUS = 2
|
|
|
|
"""Maximum of suspicious requests from one IP in the :py:obj:`BURST_WINDOW`"""
|
|
|
|
|
|
|
|
LONG_WINDOW = 600
|
|
|
|
"""Time (sec) before the longer sliding window expires."""
|
|
|
|
|
|
|
|
LONG_MAX = 150
|
|
|
|
"""Maximum requests from one IP in the :py:obj:`LONG_WINDOW`"""
|
|
|
|
|
|
|
|
LONG_MAX_SUSPICIOUS = 10
|
|
|
|
"""Maximum suspicious requests from one IP in the :py:obj:`LONG_WINDOW`"""
|
|
|
|
|
|
|
|
API_WONDOW = 3600
|
|
|
|
"""Time (sec) before sliding window for API requests (format != html) expires."""
|
|
|
|
|
|
|
|
API_MAX = 4
|
|
|
|
"""Maximum requests from one IP in the :py:obj:`API_WONDOW`"""
|
|
|
|
|
2023-06-01 16:00:49 +02:00
|
|
|
SUSPICIOUS_IP_WINDOW = 3600 * 24 * 30
|
2023-05-27 21:36:34 +02:00
|
|
|
"""Time (sec) before sliding window for one suspicious IP expires."""
|
|
|
|
|
|
|
|
SUSPICIOUS_IP_MAX = 3
|
|
|
|
"""Maximum requests from one suspicious IP in the :py:obj:`SUSPICIOUS_IP_WINDOW`."""
|
|
|
|
|
2023-05-23 18:16:37 +02:00
|
|
|
|
2023-06-01 15:41:48 +02:00
|
|
|
def filter_request(
|
|
|
|
network: IPv4Network | IPv6Network,
|
|
|
|
request: flask.Request,
|
|
|
|
cfg: config.Config,
|
|
|
|
) -> werkzeug.Response | None:
|
|
|
|
|
2023-05-28 18:58:31 +02:00
|
|
|
# pylint: disable=too-many-return-statements
|
2023-05-23 18:16:37 +02:00
|
|
|
redis_client = redisdb.client()
|
|
|
|
|
2023-06-01 15:41:48 +02:00
|
|
|
if network.is_link_local and not cfg['botdetection.ip_limit.filter_link_local']:
|
|
|
|
logger.debug("network %s is link-local -> not monitored by ip_limit method", network.compressed)
|
|
|
|
return None
|
2023-05-23 18:16:37 +02:00
|
|
|
|
|
|
|
if request.args.get('format', 'html') != 'html':
|
2023-06-01 15:41:48 +02:00
|
|
|
c = incr_sliding_window(redis_client, 'ip_limit.API_WONDOW:' + network.compressed, API_WONDOW)
|
2023-05-23 18:16:37 +02:00
|
|
|
if c > API_MAX:
|
2023-06-01 15:41:48 +02:00
|
|
|
return too_many_requests(network, "too many request in API_WINDOW")
|
2023-05-27 21:36:34 +02:00
|
|
|
|
2023-10-02 18:29:58 +02:00
|
|
|
if cfg['botdetection.ip_limit.link_token']:
|
2023-05-23 18:16:37 +02:00
|
|
|
|
2023-06-01 15:41:48 +02:00
|
|
|
suspicious = link_token.is_suspicious(network, request, True)
|
2023-05-28 18:58:31 +02:00
|
|
|
|
|
|
|
if not suspicious:
|
|
|
|
# this IP is no longer suspicious: release ip again / delete the counter of this IP
|
2023-06-01 15:41:48 +02:00
|
|
|
drop_counter(redis_client, 'ip_limit.SUSPICIOUS_IP_WINDOW' + network.compressed)
|
2023-05-28 18:58:31 +02:00
|
|
|
return None
|
2023-05-27 21:36:34 +02:00
|
|
|
|
|
|
|
# this IP is suspicious: count requests from this IP
|
2023-06-01 15:41:48 +02:00
|
|
|
c = incr_sliding_window(
|
|
|
|
redis_client, 'ip_limit.SUSPICIOUS_IP_WINDOW' + network.compressed, SUSPICIOUS_IP_WINDOW
|
|
|
|
)
|
2023-05-27 21:36:34 +02:00
|
|
|
if c > SUSPICIOUS_IP_MAX:
|
2023-06-01 15:41:48 +02:00
|
|
|
logger.error("BLOCK: too many request from %s in SUSPICIOUS_IP_WINDOW (redirect to /)", network)
|
2023-05-28 18:58:31 +02:00
|
|
|
return flask.redirect(flask.url_for('index'), code=302)
|
2023-05-27 21:36:34 +02:00
|
|
|
|
2023-06-01 15:41:48 +02:00
|
|
|
c = incr_sliding_window(redis_client, 'ip_limit.BURST_WINDOW' + network.compressed, BURST_WINDOW)
|
2023-05-23 18:16:37 +02:00
|
|
|
if c > BURST_MAX_SUSPICIOUS:
|
2023-06-01 15:41:48 +02:00
|
|
|
return too_many_requests(network, "too many request in BURST_WINDOW (BURST_MAX_SUSPICIOUS)")
|
2023-05-23 18:16:37 +02:00
|
|
|
|
2023-06-01 15:41:48 +02:00
|
|
|
c = incr_sliding_window(redis_client, 'ip_limit.LONG_WINDOW' + network.compressed, LONG_WINDOW)
|
2023-05-23 18:16:37 +02:00
|
|
|
if c > LONG_MAX_SUSPICIOUS:
|
2023-06-01 15:41:48 +02:00
|
|
|
return too_many_requests(network, "too many request in LONG_WINDOW (LONG_MAX_SUSPICIOUS)")
|
2023-05-23 18:16:37 +02:00
|
|
|
|
2023-05-28 18:58:31 +02:00
|
|
|
return None
|
2023-05-27 21:36:34 +02:00
|
|
|
|
2023-05-28 18:58:31 +02:00
|
|
|
# vanilla limiter without extensions counts BURST_MAX and LONG_MAX
|
2023-06-01 15:41:48 +02:00
|
|
|
c = incr_sliding_window(redis_client, 'ip_limit.BURST_WINDOW' + network.compressed, BURST_WINDOW)
|
2023-05-28 18:58:31 +02:00
|
|
|
if c > BURST_MAX:
|
2023-06-01 15:41:48 +02:00
|
|
|
return too_many_requests(network, "too many request in BURST_WINDOW (BURST_MAX)")
|
2023-05-27 21:36:34 +02:00
|
|
|
|
2023-06-01 15:41:48 +02:00
|
|
|
c = incr_sliding_window(redis_client, 'ip_limit.LONG_WINDOW' + network.compressed, LONG_WINDOW)
|
2023-05-28 18:58:31 +02:00
|
|
|
if c > LONG_MAX:
|
2023-06-01 15:41:48 +02:00
|
|
|
return too_many_requests(network, "too many request in LONG_WINDOW (LONG_MAX)")
|
2023-05-23 18:16:37 +02:00
|
|
|
|
|
|
|
return None
|