AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
feat(backend): Enable safe URL redirect on web requests for blocks (#9555)
Zamil Majdye7888ddb9f08
diff --git a/autogpt_platform/backend/backend/util/request.py b/autogpt_platform/backend/backend/util/request.pyindex 9a3fa0c5e3..a867d2ac29 100644--- a/autogpt_platform/backend/backend/util/request.py+++ b/autogpt_platform/backend/backend/util/request.py@@ -2,7 +2,7 @@ import ipaddressimport reimport socketfrom typing import Callable-from urllib.parse import urlparse, urlunparse+from urllib.parse import urljoin, urlparse, urlunparseimport idnaimport requests as req@@ -128,7 +128,14 @@ class Requests:self.extra_headers = extra_headersdef request(- self, method, url, headers=None, allow_redirects=False, *args, **kwargs+ self,+ method,+ url,+ headers=None,+ allow_redirects=True,+ max_redirects=10,+ *args,+ **kwargs,) -> req.Response:# Merge any extra headersif self.extra_headers is not None:@@ -139,18 +146,41 @@ class Requests:if self.extra_url_validator is not None:url = self.extra_url_validator(url)- # Perform the request+ # Perform the request with redirects disabled for manual handlingresponse = req.request(method,url,headers=headers,- allow_redirects=allow_redirects,+ allow_redirects=False,*args,**kwargs,)if self.raise_for_status:response.raise_for_status()+ # If allowed and a redirect is received, follow the redirect+ if allow_redirects and response.is_redirect:+ if max_redirects <= 0:+ raise Exception("Too many redirects.")++ location = response.headers.get("Location")+ if not location:+ return response++ new_url = validate_url(urljoin(url, location), self.trusted_origins)+ if self.extra_url_validator is not None:+ new_url = self.extra_url_validator(new_url)++ return self.request(+ method,+ new_url,+ headers=headers,+ allow_redirects=allow_redirects,+ max_redirects=max_redirects - 1,+ *args,+ **kwargs,+ )+return responsedef get(self, url, *args, **kwargs) -> req.Response: