From 9514919d376c9aca9ff678cac3a774bf807b2d30 Mon Sep 17 00:00:00 2001 From: Josh XT <102809327+Josh-XT@users.noreply.github.com> Date: Tue, 18 Apr 2023 19:54:38 -0400 Subject: [PATCH] Option to disable working directory restrictions (#1875) Remove restriction on working directory if RESTRICT_TO_WORKSPACE != True --------- Co-authored-by: Reinier van der Leer --- .env.template | 2 ++ autogpt/commands/file_operations.py | 3 +-- autogpt/config/config.py | 3 +++ autogpt/workspace.py | 8 ++++++-- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/.env.template b/.env.template index 855cb91f..4fb09cf2 100644 --- a/.env.template +++ b/.env.template @@ -3,6 +3,8 @@ ################################################################################ # EXECUTE_LOCAL_COMMANDS - Allow local command execution (Example: False) EXECUTE_LOCAL_COMMANDS=False +# RESTRICT_TO_WORKSPACE - Restrict file operations to workspace ./auto_gpt_workspace (Default: True) +RESTRICT_TO_WORKSPACE=True # BROWSE_CHUNK_MAX_LENGTH - When browsing website, define the length of chunk stored in memory BROWSE_CHUNK_MAX_LENGTH=8192 # USER_AGENT - Define the user-agent used by the requests library to browse website (string) diff --git a/autogpt/commands/file_operations.py b/autogpt/commands/file_operations.py index 72b02b5d..ad145ec9 100644 --- a/autogpt/commands/file_operations.py +++ b/autogpt/commands/file_operations.py @@ -3,8 +3,7 @@ from __future__ import annotations import os import os.path -from pathlib import Path -from typing import Generator, List +from typing import Generator import requests from colorama import Back, Fore diff --git a/autogpt/config/config.py b/autogpt/config/config.py index 2fd300dd..1fe44edf 100644 --- a/autogpt/config/config.py +++ b/autogpt/config/config.py @@ -39,6 +39,9 @@ class Config(metaclass=Singleton): self.execute_local_commands = ( os.getenv("EXECUTE_LOCAL_COMMANDS", "False") == "True" ) + self.restrict_to_workspace = ( + os.getenv("RESTRICT_TO_WORKSPACE", "True") == "True" + ) if self.use_azure: self.load_azure_config() diff --git a/autogpt/workspace.py b/autogpt/workspace.py index 964a94d1..6fb0e311 100644 --- a/autogpt/workspace.py +++ b/autogpt/workspace.py @@ -3,6 +3,10 @@ from __future__ import annotations import os from pathlib import Path +from autogpt.config import Config + +CFG = Config() + # Set a dedicated folder for file I/O WORKSPACE_PATH = Path(os.getcwd()) / "auto_gpt_workspace" @@ -35,9 +39,9 @@ def safe_path_join(base: Path, *paths: str | Path) -> Path: """ joined_path = base.joinpath(*paths).resolve() - if not joined_path.is_relative_to(base): + if CFG.restrict_to_workspace and not joined_path.is_relative_to(base): raise ValueError( - f"Attempted to access path '{joined_path}' outside of working directory '{base}'." + f"Attempted to access path '{joined_path}' outside of workspace '{base}'." ) return joined_path