ssrf_proxy.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. while retries <= max_retries:
  28. try:
  29. if dify_config.SSRF_PROXY_ALL_URL:
  30. with httpx.Client(proxy=dify_config.SSRF_PROXY_ALL_URL) as client:
  31. response = client.request(method=method, url=url, **kwargs)
  32. elif dify_config.SSRF_PROXY_HTTP_URL and dify_config.SSRF_PROXY_HTTPS_URL:
  33. proxy_mounts = {
  34. "http://": httpx.HTTPTransport(proxy=dify_config.SSRF_PROXY_HTTP_URL),
  35. "https://": httpx.HTTPTransport(proxy=dify_config.SSRF_PROXY_HTTPS_URL),
  36. }
  37. with httpx.Client(mounts=proxy_mounts) as client:
  38. response = client.request(method=method, url=url, **kwargs)
  39. else:
  40. with httpx.Client() as client:
  41. response = client.request(method=method, url=url, **kwargs)
  42. if response.status_code not in STATUS_FORCELIST:
  43. return response
  44. else:
  45. logging.warning(f"Received status code {response.status_code} for URL {url} which is in the force list")
  46. except httpx.RequestError as e:
  47. logging.warning(f"Request to URL {url} failed on attempt {retries + 1}: {e}")
  48. if max_retries == 0:
  49. raise
  50. retries += 1
  51. if retries <= max_retries:
  52. time.sleep(BACKOFF_FACTOR * (2 ** (retries - 1)))
  53. raise MaxRetriesExceededError(f"Reached maximum retries ({max_retries}) for URL {url}")
  54. def get(url, max_retries=SSRF_DEFAULT_MAX_RETRIES, **kwargs):
  55. return make_request("GET", url, max_retries=max_retries, **kwargs)
  56. def post(url, max_retries=SSRF_DEFAULT_MAX_RETRIES, **kwargs):
  57. return make_request("POST", url, max_retries=max_retries, **kwargs)
  58. def put(url, max_retries=SSRF_DEFAULT_MAX_RETRIES, **kwargs):
  59. return make_request("PUT", url, max_retries=max_retries, **kwargs)
  60. def patch(url, max_retries=SSRF_DEFAULT_MAX_RETRIES, **kwargs):
  61. return make_request("PATCH", url, max_retries=max_retries, **kwargs)
  62. def delete(url, max_retries=SSRF_DEFAULT_MAX_RETRIES, **kwargs):
  63. return make_request("DELETE", url, max_retries=max_retries, **kwargs)
  64. def head(url, max_retries=SSRF_DEFAULT_MAX_RETRIES, **kwargs):
  65. return make_request("HEAD", url, max_retries=max_retries, **kwargs)