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.
Validate URLs in web commands before execution (#2616)
Eddie Cohen40a75c804ccf
diff --git a/autogpt/app.py b/autogpt/app.pyindex 237feae2e2..590f3d5099 100644--- a/autogpt/app.py+++ b/autogpt/app.py@@ -10,6 +10,7 @@ from autogpt.memory import get_memoryfrom autogpt.processing.text import summarize_textfrom autogpt.prompts.generator import PromptGeneratorfrom autogpt.speech import say_text+from autogpt.url_utils.validators import validate_urlCFG = Config()AGENT_MANAGER = AgentManager()@@ -141,6 +142,7 @@ def execute_command(@command("get_text_summary", "Get text summary", '"url": "<url>", "question": "<question>"')+@validate_urldef get_text_summary(url: str, question: str) -> str:"""Return the results of a Google search@@ -157,6 +159,7 @@ def get_text_summary(url: str, question: str) -> str:@command("get_hyperlinks", "Get text summary", '"url": "<url>"')+@validate_urldef get_hyperlinks(url: str) -> Union[str, List[str]]:"""Return the results of a Google searchdiff --git a/autogpt/commands/git_operations.py b/autogpt/commands/git_operations.pyindex c373b8c0a8..fb74374cb7 100644--- a/autogpt/commands/git_operations.py+++ b/autogpt/commands/git_operations.py@@ -3,6 +3,7 @@ from git.repo import Repofrom autogpt.commands.command import commandfrom autogpt.config import Config+from autogpt.url_utils.validators import validate_urlCFG = Config()@@ -14,6 +15,7 @@ CFG = Config()CFG.github_username and CFG.github_api_key,"Configure github_username and github_api_key.",)+@validate_urldef clone_repository(repository_url: str, clone_path: str) -> str:"""Clone a GitHub repository locally.diff --git a/autogpt/commands/web_requests.py b/autogpt/commands/web_requests.pyindex f3ad019763..60e54252d4 100644--- a/autogpt/commands/web_requests.py+++ b/autogpt/commands/web_requests.py@@ -1,15 +1,13 @@"""Browse a webpage and summarize it using the LLM model"""from __future__ import annotations-from urllib.parse import urljoin, urlparse-import requestsfrom bs4 import BeautifulSoupfrom requests import Response-from requests.compat import urljoinfrom autogpt.config import Configfrom autogpt.processing.html import extract_hyperlinks, format_hyperlinks+from autogpt.url_utils.validators import validate_urlCFG = Config()@@ -17,71 +15,7 @@ session = requests.Session()session.headers.update({"User-Agent": CFG.user_agent})-def is_valid_url(url: str) -> bool:- """Check if the URL is valid-- Args:- url (str): The URL to check-- Returns:- bool: True if the URL is valid, False otherwise- """- try:- result = urlparse(url)- return all([result.scheme, result.netloc])- except ValueError:- return False---def sanitize_url(url: str) -> str:- """Sanitize the URL-- Args:- url (str): The URL to sanitize-- Returns:- str: The sanitized URL- """- return urljoin(url, urlparse(url).path)---def check_local_file_access(url: str) -> bool:- """Check if the URL is a local file-- Args:- url (str): The URL to check-- Returns:- bool: True if the URL is a local file, False otherwise- """- local_prefixes = [- "file:///",- "file://localhost/",- "file://localhost",- "http://localhost",- "http://localhost/",- "https://localhost",- "https://localhost/",- "http://2130706433",- "http://2130706433/",- "https://2130706433",- "https://2130706433/",- "http://127.0.0.1/",- "http://127.0.0.1",- "https://127.0.0.1/",- "https://127.0.0.1",- "https://0.0.0.0/",- "https://0.0.0.0",- "http://0.0.0.0/",- "http://0.0.0.0",- "http://0000",- "http://0000/",- "https://0000",- "https://0000/",- ]- return any(url.startswith(prefix) for prefix in local_prefixes)--+@validate_urldef get_response(url: str, timeout: int = 10) -> tuple[None, str] | tuple[Response, None]:@@ -99,17 +33,7 @@ def get_response(requests.exceptions.RequestException: If the HTTP request fails"""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")-- sanitized_url = sanitize_url(url)-- response = session.get(sanitized_url, timeout=timeout)+ response = session.get(url, timeout=timeout)# Check if the response contains an HTTP errorif response.status_code >= 400:diff --git a/autogpt/commands/web_selenium.py b/autogpt/commands/web_selenium.pyindex e0e0d70ae8..8f55c103e9 100644--- a/autogpt/commands/web_selenium.py+++ b/autogpt/commands/web_selenium.py@@ -21,6 +21,7 @@ import autogpt.processing.text as summaryfrom autogpt.commands.command import commandfrom autogpt.config import Configfrom autogpt.processing.html import extract_hyperlinks, format_hyperlinks+from autogpt.url_utils.validators import validate_urlFILE_DIR = Path(__file__).parent.parentCFG = Config()@@ -31,6 +32,7 @@ CFG = Config()"Browse Website",'"url": "<url>", "question": "<what_you_want_to_find_on_website>"',)+@validate_urldef browse_website(url: str, question: str) -> tuple[str, WebDriver]:"""Browse a website and return the answer and links to the userdiff --git a/autogpt/url_utils/__init__.py b/autogpt/url_utils/__init__.pynew file mode 100644index 0000000000..e69de29bb2diff --git a/autogpt/url_utils/validators.py b/autogpt/url_utils/validators.pynew file mode 100644index 0000000000..8754667c31--- /dev/null+++ b/autogpt/url_utils/validators.py@@ -0,0 +1,101 @@+import functools+from typing import Any, Callable+from urllib.parse import urljoin, urlparse++from requests.compat import urljoin+++def validate_url(func: Callable[..., Any]) -> Any:+ """The method decorator validate_url is used to validate urls for any command that requires+ a url as an arugment"""++ @functools.wraps(func)+ def wrapper(url: str, *args, **kwargs) -> Any:+ """Check if the URL is valid using a basic check, urllib check, and local file check++ Args:+ url (str): The URL to check++ Returns:+ the result of the wrapped function++ Raises:+ ValueError if the url fails any of the validation tests+ """+ # Most basic check if the URL is valid:+ if not url.startswith("http://") and not url.startswith("https://"):+ raise ValueError("Invalid URL format")+ if not is_valid_url(url):+ raise ValueError("Missing Scheme or Network location")+ # Restrict access to local files+ if check_local_file_access(url):+ raise ValueError("Access to local files is restricted")++ return func(sanitize_url(url), *args, **kwargs)++ return wrapper+++def is_valid_url(url: str) -> bool:+ """Check if the URL is valid++ Args:+ url (str): The URL to check++ Returns:+ bool: True if the URL is valid, False otherwise+ """+ try:+ result = urlparse(url)+ return all([result.scheme, result.netloc])+ except ValueError:+ return False+++def sanitize_url(url: str) -> str:+ """Sanitize the URL++ Args:+ url (str): The URL to sanitize++ Returns:+ str: The sanitized URL+ """+ return urljoin(url, urlparse(url).path)+++def check_local_file_access(url: str) -> bool:+ """Check if the URL is a local file++ Args:+ url (str): The URL to check++ Returns:+ bool: True if the URL is a local file, False otherwise+ """+ local_prefixes = [+ "file:///",+ "file://localhost/",+ "file://localhost",+ "http://localhost",+ "http://localhost/",+ "https://localhost",+ "https://localhost/",+ "http://2130706433",+ "http://2130706433/",+ "https://2130706433",+ "https://2130706433/",+ "http://127.0.0.1/",+ "http://127.0.0.1",+ "https://127.0.0.1/",+ "https://127.0.0.1",+ "https://0.0.0.0/",+ "https://0.0.0.0",+ "http://0.0.0.0/",+ "http://0.0.0.0",+ "http://0000",+ "http://0000/",+ "https://0000",+ "https://0000/",+ ]+ return any(url.startswith(prefix) for prefix in local_prefixes)diff --git a/tests/unit/test_browse_scrape_text.py b/tests/unit/test_browse_scrape_text.pyindex 1a36e19b5d..c6ce4d39bf 100644--- a/tests/unit/test_browse_scrape_text.py+++ b/tests/unit/test_browse_scrape_text.py@@ -1,5 +1,6 @@# Generated by CodiumAI+import pytestimport requestsfrom autogpt.commands.web_requests import scrape_text@@ -58,9 +59,14 @@ class TestScrapeText:url = "http://www.example.com"assert scrape_text(url) == expected_text- # Tests that the function returns an error message when an invalid or unreachable+ # Tests that an error is raised when an invalid url is provided.+ def test_invalid_url(self):+ url = "invalidurl.com"+ pytest.raises(ValueError, scrape_text, url)++ # Tests that the function returns an error message when an unreachable# url is provided.- def test_invalid_url(self, mocker):+ def test_unreachable_url(self, mocker):# Mock the requests.get() method to raise an exceptionmocker.patch("requests.Session.get", side_effect=requests.exceptions.RequestException@@ -68,7 +74,7 @@ class TestScrapeText:# Call the function with an invalid URL and assert that it returns an error# message- url = "http://www.invalidurl.com"+ url = "http://thiswebsitedoesnotexist.net/"error_message = scrape_text(url)assert "Error:" in error_message