Skip to content

Commit

Permalink
fix(agent/execute_code): Disable code execution commands when Docker …
Browse files Browse the repository at this point in the history
…is unavailable (#6888)
  • Loading branch information
kcze authored Feb 28, 2024
1 parent 5090f55 commit 30762c2
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions autogpts/autogpt/autogpt/commands/execute_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,28 @@
DENYLIST_CONTROL = "denylist"


def we_are_running_in_a_docker_container() -> bool:
"""Check if we are running in a Docker container
Returns:
bool: True if we are running in a Docker container, False otherwise
"""
return os.path.exists("/.dockerenv")


def is_docker_available() -> bool:
"""Check if Docker is available
Returns:
bool: True if Docker is available, False otherwise"""
try:
client = docker.from_env()
client.ping()
return True
except Exception:
return False


@command(
"execute_python_code",
"Executes the given Python code inside a single-use Docker container"
Expand All @@ -45,6 +67,10 @@
required=True,
),
},
disabled_reason="To execute python code agent "
"must be running in a Docker container or "
"Docker must be available on the system.",
available=we_are_running_in_a_docker_container() or is_docker_available(),
)
def execute_python_code(code: str, agent: Agent) -> str:
"""
Expand Down Expand Up @@ -92,6 +118,10 @@ def execute_python_code(code: str, agent: Agent) -> str:
items=JSONSchema(type=JSONSchema.Type.STRING),
),
},
disabled_reason="To execute python code agent "
"must be running in a Docker container or "
"Docker must be available on the system.",
available=we_are_running_in_a_docker_container() or is_docker_available(),
)
@sanitize_path_arg("filename")
def execute_python_file(
Expand Down Expand Up @@ -354,12 +384,3 @@ def execute_shell_popen(command_line: str, agent: Agent) -> str:
os.chdir(current_dir)

return f"Subprocess started with PID:'{str(process.pid)}'"


def we_are_running_in_a_docker_container() -> bool:
"""Check if we are running in a Docker container
Returns:
bool: True if we are running in a Docker container, False otherwise
"""
return os.path.exists("/.dockerenv")

0 comments on commit 30762c2

Please # to comment.