ssrf_proxy.py 3.2 KB

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