Streamline / clarify shell command control configuration (#4628)

* Streamline / clarify shell command control configuration

* Fix lint
This commit is contained in:
Erik Peterson
2023-06-09 11:48:20 -07:00
committed by GitHub
parent cce50bef50
commit ff4e53d0e6
4 changed files with 71 additions and 40 deletions

View File

@@ -13,6 +13,9 @@ from autogpt.logs import logger
from autogpt.setup import CFG
from autogpt.workspace.workspace import Workspace
ALLOWLIST_CONTROL = "allowlist"
DENYLIST_CONTROL = "denylist"
@command(
"execute_python_code",
@@ -152,21 +155,15 @@ def validate_command(command: str, config: Config) -> bool:
Returns:
bool: True if the command is allowed, False otherwise
"""
tokens = command.split()
if not tokens:
if not command:
return False
if config.deny_commands and tokens[0] in config.deny_commands:
return False
command_name = command.split()[0]
for keyword in config.allow_commands:
if keyword in tokens:
return True
if config.allow_commands:
return False
return True
if config.shell_command_control == ALLOWLIST_CONTROL:
return command_name in config.shell_allowlist
else:
return command_name not in config.shell_denylist
@command(

View File

@@ -38,17 +38,21 @@ class Config(metaclass=Singleton):
else:
self.disabled_command_categories = []
deny_commands = os.getenv("DENY_COMMANDS")
if deny_commands:
self.deny_commands = deny_commands.split(",")
else:
self.deny_commands = []
self.shell_command_control = os.getenv("SHELL_COMMAND_CONTROL", "denylist")
allow_commands = os.getenv("ALLOW_COMMANDS")
if allow_commands:
self.allow_commands = allow_commands.split(",")
# DENY_COMMANDS is deprecated and included for backwards-compatibility
shell_denylist = os.getenv("SHELL_DENYLIST", os.getenv("DENY_COMMANDS"))
if shell_denylist:
self.shell_denylist = shell_denylist.split(",")
else:
self.allow_commands = []
self.shell_denylist = ["sudo", "su"]
# ALLOW_COMMANDS is deprecated and included for backwards-compatibility
shell_allowlist = os.getenv("SHELL_ALLOWLIST", os.getenv("ALLOW_COMMANDS"))
if shell_allowlist:
self.shell_allowlist = shell_allowlist.split(",")
else:
self.shell_allowlist = []
self.ai_settings_file = os.getenv("AI_SETTINGS_FILE", "ai_settings.yaml")
self.prompt_settings_file = os.getenv(