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.
Merge branch 'master' into browse_scrape_links_test_and_validate
Itamar Friedman7c0c896600be
diff --cc scripts/browse.pyindex 2edc156b24,c3fc066274..046ff6ae9f--- a/scripts/browse.py+++ b/scripts/browse.py@@@ -5,42 -6,57 +6,81 @@@ from urllib.parse import urlparse, urljcfg = Config()+++ # Function to check if the URL is valid+ def is_valid_url(url):+ try:+ result = urlparse(url)+ return all([result.scheme, result.netloc])+ except ValueError:+ return False++ # Function to sanitize the URL+ def sanitize_url(url):+ return urljoin(url, urlparse(url).path)++ # Function to make a request with a specified timeout and handle exceptions+ def make_request(url, timeout=10):+ try:+ response = requests.get(url, headers=cfg.user_agent_header, timeout=timeout)+ response.raise_for_status()+ return response+ except requests.exceptions.RequestException as e:+ return "Error: " + str(e)++ # Define and check for local file address prefixesdef check_local_file_access(url):+ # Define and check for local file address prefixeslocal_prefixes = ['file:///', 'file://localhost', 'http://localhost', 'https://localhost']return any(url.startswith(prefix) for prefix in local_prefixes)+def get_validated_response(url, headers=cfg.user_agent_header):+ try:+ # Restrict access to local files+ if check_local_file_access(url):+ raise ValueError('Access to local files is restricted')++ # Most basic check if the URL is valid:+ if not url.startswith('http://') and not url.startswith('https://'):+ raise ValueError('Invalid URL format')++ # Make the HTTP request and return the response+ response = requests.get(url, headers=headers)+ response.raise_for_status() # Raise an exception if the response contains an HTTP error status code+ return response, None+ except ValueError as ve:+ # Handle invalid URL format+ return None, "Error: " + str(ve)++ except requests.exceptions.RequestException as re:+ # Handle exceptions related to the HTTP request (e.g., connection errors, timeouts, etc.)+ return None, "Error: " + str(re)+def scrape_text(url):"""Scrape text from a webpage"""- response, error_message = get_validated_response(url)- if error_message:- return error_message-- # Check if the response contains an HTTP error- if response.status_code >= 400:- return "Error: HTTP " + str(response.status_code) + " error"+ # Basic check if the URL is valid+ if not url.startswith('http'):+ return "Error: Invalid URL"++ # Restrict access to local files+ if check_local_file_access(url):+ return "Error: Access to local files is restricted"++ # Validate the input URL+ if not is_valid_url(url):+ # Sanitize the input URL+ sanitized_url = sanitize_url(url)++ # Make the request with a timeout and handle exceptions+ response = make_request(sanitized_url)++ if isinstance(response, str):+ return response+ else:+ # Sanitize the input URL+ sanitized_url = sanitize_url(url)++ response = requests.get(sanitized_url, headers=cfg.user_agent_header)soup = BeautifulSoup(response.text, "html.parser")