ip_limit.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. """
  2. Method ``ip_limit``
  3. -------------------
  4. The ``ip_limit`` method counts request from an IP in *sliding windows*. If
  5. there are to many requests in a sliding window, the request is evaluated as a
  6. bot request. This method requires a redis DB and needs a HTTP X-Forwarded-For_
  7. header. To take privacy only the hash value of an IP is stored in the redis DB
  8. and at least for a maximum of 10 minutes.
  9. The :py:obj:`link_token` method is used to investigate whether a request is
  10. *suspicious*. If the :py:obj:`link_token` method is activated and a request is
  11. *suspicious* the request rates are reduced:
  12. - :py:obj:`BURST_MAX` -> :py:obj:`BURST_MAX_SUSPICIOUS`
  13. - :py:obj:`LONG_MAX` -> :py:obj:`LONG_MAX_SUSPICIOUS`
  14. .. _X-Forwarded-For:
  15. https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For
  16. """
  17. from typing import Optional, Tuple
  18. import flask
  19. from searx import redisdb
  20. from searx import logger
  21. from searx.redislib import incr_sliding_window
  22. from . import link_token
  23. logger = logger.getChild('botdetection.ip_limit')
  24. BURST_WINDOW = 20
  25. """Time (sec) before sliding window for *burst* requests expires."""
  26. BURST_MAX = 15
  27. """Maximum requests from one IP in the :py:obj:`BURST_WINDOW`"""
  28. BURST_MAX_SUSPICIOUS = 2
  29. """Maximum of suspicious requests from one IP in the :py:obj:`BURST_WINDOW`"""
  30. LONG_WINDOW = 600
  31. """Time (sec) before the longer sliding window expires."""
  32. LONG_MAX = 150
  33. """Maximum requests from one IP in the :py:obj:`LONG_WINDOW`"""
  34. LONG_MAX_SUSPICIOUS = 10
  35. """Maximum suspicious requests from one IP in the :py:obj:`LONG_WINDOW`"""
  36. API_WONDOW = 3600
  37. """Time (sec) before sliding window for API requests (format != html) expires."""
  38. API_MAX = 4
  39. """Maximum requests from one IP in the :py:obj:`API_WONDOW`"""
  40. def filter_request(request: flask.Request) -> Optional[Tuple[int, str]]:
  41. redis_client = redisdb.client()
  42. x_forwarded_for = request.headers.get('X-Forwarded-For', '')
  43. if not x_forwarded_for:
  44. logger.error("missing HTTP header X-Forwarded-For")
  45. if request.args.get('format', 'html') != 'html':
  46. c = incr_sliding_window(redis_client, 'IP limit - API_WONDOW:' + x_forwarded_for, API_WONDOW)
  47. if c > API_MAX:
  48. return 429, "BLOCK %s: API limit exceeded"
  49. suspicious = link_token.is_suspicious(request)
  50. if suspicious:
  51. c = incr_sliding_window(redis_client, 'IP limit - BURST_WINDOW:' + x_forwarded_for, BURST_WINDOW)
  52. if c > BURST_MAX_SUSPICIOUS:
  53. return 429, f"bot detected, too many request from {x_forwarded_for} in BURST_MAX_SUSPICIOUS"
  54. c = incr_sliding_window(redis_client, 'IP limit - LONG_WINDOW:' + x_forwarded_for, LONG_WINDOW)
  55. if c > LONG_MAX_SUSPICIOUS:
  56. return 429, f"bot detected, too many request from {x_forwarded_for} in LONG_MAX_SUSPICIOUS"
  57. else:
  58. c = incr_sliding_window(redis_client, 'IP limit - BURST_WINDOW:' + x_forwarded_for, BURST_WINDOW)
  59. if c > BURST_MAX:
  60. return 429, f"bot detected, too many request from {x_forwarded_for} in BURST_MAX"
  61. c = incr_sliding_window(redis_client, 'IP limit - LONG_WINDOW:' + x_forwarded_for, LONG_WINDOW)
  62. if c > LONG_MAX:
  63. return 429, f"bot detected, too many request from {x_forwarded_for} in LONG_MAX"
  64. return None