ssrf_proxy.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. """
  2. Proxy requests to avoid SSRF
  3. """
  4. import logging
  5. import time
  6. import httpx
  7. from configs import dify_config
  8. SSRF_DEFAULT_MAX_RETRIES = dify_config.SSRF_DEFAULT_MAX_RETRIES
  9. HTTP_REQUEST_NODE_SSL_VERIFY = True # Default value for HTTP_REQUEST_NODE_SSL_VERIFY is True
  10. try:
  11. HTTP_REQUEST_NODE_SSL_VERIFY = dify_config.HTTP_REQUEST_NODE_SSL_VERIFY
  12. http_request_node_ssl_verify_lower = str(HTTP_REQUEST_NODE_SSL_VERIFY).lower()
  13. if http_request_node_ssl_verify_lower == "true":
  14. HTTP_REQUEST_NODE_SSL_VERIFY = True
  15. elif http_request_node_ssl_verify_lower == "false":
  16. HTTP_REQUEST_NODE_SSL_VERIFY = False
  17. else:
  18. raise ValueError("Invalid value. HTTP_REQUEST_NODE_SSL_VERIFY should be 'True' or 'False'")
  19. except NameError:
  20. HTTP_REQUEST_NODE_SSL_VERIFY = True
  21. BACKOFF_FACTOR = 0.5
  22. STATUS_FORCELIST = [429, 500, 502, 503, 504]
  23. class MaxRetriesExceededError(ValueError):
  24. """Raised when the maximum number of retries is exceeded."""
  25. pass
  26. def make_request(method, url, max_retries=SSRF_DEFAULT_MAX_RETRIES, **kwargs):
  27. if "allow_redirects" in kwargs:
  28. allow_redirects = kwargs.pop("allow_redirects")
  29. if "follow_redirects" not in kwargs:
  30. kwargs["follow_redirects"] = allow_redirects
  31. if "timeout" not in kwargs:
  32. kwargs["timeout"] = httpx.Timeout(
  33. timeout=dify_config.SSRF_DEFAULT_TIME_OUT,
  34. connect=dify_config.SSRF_DEFAULT_CONNECT_TIME_OUT,
  35. read=dify_config.SSRF_DEFAULT_READ_TIME_OUT,
  36. write=dify_config.SSRF_DEFAULT_WRITE_TIME_OUT,
  37. )
  38. retries = 0
  39. while retries <= max_retries:
  40. try:
  41. if dify_config.SSRF_PROXY_ALL_URL:
  42. with httpx.Client(proxy=dify_config.SSRF_PROXY_ALL_URL, verify=HTTP_REQUEST_NODE_SSL_VERIFY) as client:
  43. response = client.request(method=method, url=url, **kwargs)
  44. elif dify_config.SSRF_PROXY_HTTP_URL and dify_config.SSRF_PROXY_HTTPS_URL:
  45. proxy_mounts = {
  46. "http://": httpx.HTTPTransport(proxy=dify_config.SSRF_PROXY_HTTP_URL),
  47. "https://": httpx.HTTPTransport(proxy=dify_config.SSRF_PROXY_HTTPS_URL),
  48. }
  49. with httpx.Client(mounts=proxy_mounts, verify=HTTP_REQUEST_NODE_SSL_VERIFY) as client:
  50. response = client.request(method=method, url=url, **kwargs)
  51. else:
  52. with httpx.Client(verify=HTTP_REQUEST_NODE_SSL_VERIFY) as client:
  53. response = client.request(method=method, url=url, **kwargs)
  54. if response.status_code not in STATUS_FORCELIST:
  55. return response
  56. else:
  57. logging.warning(f"Received status code {response.status_code} for URL {url} which is in the force list")
  58. except httpx.RequestError as e:
  59. logging.warning(f"Request to URL {url} failed on attempt {retries + 1}: {e}")
  60. if max_retries == 0:
  61. raise
  62. retries += 1
  63. if retries <= max_retries:
  64. time.sleep(BACKOFF_FACTOR * (2 ** (retries - 1)))
  65. raise MaxRetriesExceededError(f"Reached maximum retries ({max_retries}) for URL {url}")
  66. def get(url, max_retries=SSRF_DEFAULT_MAX_RETRIES, **kwargs):
  67. return make_request("GET", url, max_retries=max_retries, **kwargs)
  68. def post(url, max_retries=SSRF_DEFAULT_MAX_RETRIES, **kwargs):
  69. return make_request("POST", url, max_retries=max_retries, **kwargs)
  70. def put(url, max_retries=SSRF_DEFAULT_MAX_RETRIES, **kwargs):
  71. return make_request("PUT", url, max_retries=max_retries, **kwargs)
  72. def patch(url, max_retries=SSRF_DEFAULT_MAX_RETRIES, **kwargs):
  73. return make_request("PATCH", url, max_retries=max_retries, **kwargs)
  74. def delete(url, max_retries=SSRF_DEFAULT_MAX_RETRIES, **kwargs):
  75. return make_request("DELETE", url, max_retries=max_retries, **kwargs)
  76. def head(url, max_retries=SSRF_DEFAULT_MAX_RETRIES, **kwargs):
  77. return make_request("HEAD", url, max_retries=max_retries, **kwargs)