ssrf_proxy.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. BACKOFF_FACTOR = 0.5
  10. STATUS_FORCELIST = [429, 500, 502, 503, 504]
  11. class MaxRetriesExceededError(ValueError):
  12. """Raised when the maximum number of retries is exceeded."""
  13. pass
  14. def make_request(method, url, max_retries=SSRF_DEFAULT_MAX_RETRIES, **kwargs):
  15. if "allow_redirects" in kwargs:
  16. allow_redirects = kwargs.pop("allow_redirects")
  17. if "follow_redirects" not in kwargs:
  18. kwargs["follow_redirects"] = allow_redirects
  19. if "timeout" not in kwargs:
  20. kwargs["timeout"] = httpx.Timeout(
  21. timeout=dify_config.SSRF_DEFAULT_TIME_OUT,
  22. connect=dify_config.SSRF_DEFAULT_CONNECT_TIME_OUT,
  23. read=dify_config.SSRF_DEFAULT_READ_TIME_OUT,
  24. write=dify_config.SSRF_DEFAULT_WRITE_TIME_OUT,
  25. )
  26. retries = 0
  27. stream = kwargs.pop("stream", False)
  28. while retries <= max_retries:
  29. try:
  30. if dify_config.SSRF_PROXY_ALL_URL:
  31. with httpx.Client(proxy=dify_config.SSRF_PROXY_ALL_URL) as client:
  32. response = client.request(method=method, url=url, **kwargs)
  33. elif dify_config.SSRF_PROXY_HTTP_URL and dify_config.SSRF_PROXY_HTTPS_URL:
  34. proxy_mounts = {
  35. "http://": httpx.HTTPTransport(proxy=dify_config.SSRF_PROXY_HTTP_URL),
  36. "https://": httpx.HTTPTransport(proxy=dify_config.SSRF_PROXY_HTTPS_URL),
  37. }
  38. with httpx.Client(mounts=proxy_mounts) as client:
  39. response = client.request(method=method, url=url, **kwargs)
  40. else:
  41. with httpx.Client() as client:
  42. response = client.request(method=method, url=url, **kwargs)
  43. if response.status_code not in STATUS_FORCELIST:
  44. return response
  45. else:
  46. logging.warning(f"Received status code {response.status_code} for URL {url} which is in the force list")
  47. except httpx.RequestError as e:
  48. logging.warning(f"Request to URL {url} failed on attempt {retries + 1}: {e}")
  49. if max_retries == 0:
  50. raise
  51. retries += 1
  52. if retries <= max_retries:
  53. time.sleep(BACKOFF_FACTOR * (2 ** (retries - 1)))
  54. raise MaxRetriesExceededError(f"Reached maximum retries ({max_retries}) for URL {url}")
  55. def get(url, max_retries=SSRF_DEFAULT_MAX_RETRIES, **kwargs):
  56. return make_request("GET", url, max_retries=max_retries, **kwargs)
  57. def post(url, max_retries=SSRF_DEFAULT_MAX_RETRIES, **kwargs):
  58. return make_request("POST", url, max_retries=max_retries, **kwargs)
  59. def put(url, max_retries=SSRF_DEFAULT_MAX_RETRIES, **kwargs):
  60. return make_request("PUT", url, max_retries=max_retries, **kwargs)
  61. def patch(url, max_retries=SSRF_DEFAULT_MAX_RETRIES, **kwargs):
  62. return make_request("PATCH", url, max_retries=max_retries, **kwargs)
  63. def delete(url, max_retries=SSRF_DEFAULT_MAX_RETRIES, **kwargs):
  64. return make_request("DELETE", url, max_retries=max_retries, **kwargs)
  65. def head(url, max_retries=SSRF_DEFAULT_MAX_RETRIES, **kwargs):
  66. return make_request("HEAD", url, max_retries=max_retries, **kwargs)