link_token.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. """
  4. Method ``link_token``
  5. ---------------------
  6. The ``link_token`` method evaluates a request as :py:obj:`suspicious
  7. <is_suspicious>` if the URL ``/client<token>.css`` is not requested by the
  8. client. By adding a random component (the token) in the URL, a bot can not send
  9. a ping by request a static URL.
  10. .. note::
  11. This method requires a redis DB and needs a HTTP X-Forwarded-For_ header.
  12. To get in use of this method a flask URL route needs to be added:
  13. .. code:: python
  14. @app.route('/client<token>.css', methods=['GET', 'POST'])
  15. def client_token(token=None):
  16. link_token.ping(request, token)
  17. return Response('', mimetype='text/css')
  18. And in the HTML template from flask a stylesheet link is needed (the value of
  19. ``link_token`` comes from :py:obj:`get_token`):
  20. .. code:: html
  21. <link rel="stylesheet"
  22. href="{{ url_for('client_token', token=link_token) }}"
  23. type="text/css" />
  24. .. _X-Forwarded-For:
  25. https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For
  26. """
  27. from __future__ import annotations
  28. from ipaddress import (
  29. IPv4Network,
  30. IPv6Network,
  31. ip_address,
  32. )
  33. import string
  34. import random
  35. import flask
  36. from searx import logger
  37. from searx import redisdb
  38. from searx.redislib import secret_hash
  39. from ._helpers import (
  40. get_network,
  41. get_real_ip,
  42. )
  43. TOKEN_LIVE_TIME = 600
  44. """Livetime (sec) of limiter's CSS token."""
  45. PING_LIVE_TIME = 3600
  46. """Livetime (sec) of the ping-key from a client (request)"""
  47. PING_KEY = 'SearXNG_limiter.ping'
  48. """Prefix of all ping-keys generated by :py:obj:`get_ping_key`"""
  49. TOKEN_KEY = 'SearXNG_limiter.token'
  50. """Key for which the current token is stored in the DB"""
  51. logger = logger.getChild('botdetection.link_token')
  52. def is_suspicious(network: IPv4Network | IPv6Network, request: flask.Request, renew: bool = False):
  53. """Checks whether a valid ping is exists for this (client) network, if not
  54. this request is rated as *suspicious*. If a valid ping exists and argument
  55. ``renew`` is ``True`` the expire time of this ping is reset to
  56. :py:obj:`PING_LIVE_TIME`.
  57. """
  58. redis_client = redisdb.client()
  59. if not redis_client:
  60. return False
  61. ping_key = get_ping_key(network, request)
  62. if not redis_client.get(ping_key):
  63. logger.info("missing ping (IP: %s) / request: %s", network.compressed, ping_key)
  64. return True
  65. if renew:
  66. redis_client.set(ping_key, 1, ex=PING_LIVE_TIME)
  67. logger.debug("found ping for (client) network %s -> %s", network.compressed, ping_key)
  68. return False
  69. def ping(request: flask.Request, token: str):
  70. """This function is called by a request to URL ``/client<token>.css``. If
  71. ``token`` is valid a :py:obj:`PING_KEY` for the client is stored in the DB.
  72. The expire time of this ping-key is :py:obj:`PING_LIVE_TIME`.
  73. """
  74. from . import limiter # pylint: disable=import-outside-toplevel, cyclic-import
  75. redis_client = redisdb.client()
  76. if not redis_client:
  77. return
  78. if not token_is_valid(token):
  79. return
  80. cfg = limiter.get_cfg()
  81. real_ip = ip_address(get_real_ip(request))
  82. network = get_network(real_ip, cfg)
  83. ping_key = get_ping_key(network, request)
  84. logger.debug("store ping_key for (client) network %s (IP %s) -> %s", network.compressed, real_ip, ping_key)
  85. redis_client.set(ping_key, 1, ex=PING_LIVE_TIME)
  86. def get_ping_key(network: IPv4Network | IPv6Network, request: flask.Request) -> str:
  87. """Generates a hashed key that fits (more or less) to a *WEB-browser
  88. session* in a network."""
  89. return (
  90. PING_KEY
  91. + "["
  92. + secret_hash(
  93. network.compressed + request.headers.get('Accept-Language', '') + request.headers.get('User-Agent', '')
  94. )
  95. + "]"
  96. )
  97. def token_is_valid(token) -> bool:
  98. valid = token == get_token()
  99. logger.debug("token is valid --> %s", valid)
  100. return valid
  101. def get_token() -> str:
  102. """Returns current token. If there is no currently active token a new token
  103. is generated randomly and stored in the redis DB.
  104. - :py:obj:`TOKEN_LIVE_TIME`
  105. - :py:obj:`TOKEN_KEY`
  106. """
  107. redis_client = redisdb.client()
  108. if not redis_client:
  109. # This function is also called when limiter is inactive / no redis DB
  110. # (see render function in webapp.py)
  111. return '12345678'
  112. token = redis_client.get(TOKEN_KEY)
  113. if token:
  114. token = token.decode('UTF-8')
  115. else:
  116. token = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(16))
  117. redis_client.set(TOKEN_KEY, token, ex=TOKEN_LIVE_TIME)
  118. return token