🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
enhance: TerminalToolkit safe-mode sanitization (#3873)
Wendong-Fan0aab7cf07ac8
diff --git a/camel/toolkits/terminal_toolkit/__init__.py b/camel/toolkits/terminal_toolkit/__init__.pyindex 5b46bea14..369ece569 100644--- a/camel/toolkits/terminal_toolkit/__init__.py+++ b/camel/toolkits/terminal_toolkit/__init__.py@@ -12,7 +12,9 @@# limitations under the License.# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========from .terminal_toolkit import TerminalToolkit+from .utils import DANGEROUS_COMMANDS__all__ = [+ "DANGEROUS_COMMANDS","TerminalToolkit",]diff --git a/camel/toolkits/terminal_toolkit/terminal_toolkit.py b/camel/toolkits/terminal_toolkit/terminal_toolkit.pyindex 3453afb48..76a7fd653 100644--- a/camel/toolkits/terminal_toolkit/terminal_toolkit.py+++ b/camel/toolkits/terminal_toolkit/terminal_toolkit.py@@ -641,7 +641,10 @@ class TerminalToolkit(BaseToolkit):if self.safe_mode:is_safe, message = self._sanitize_command(command)if not is_safe:- return f"Error: {message}"+ return (+ "Error: Command rejected by TerminalToolkit safe mode. "+ f"{message}"+ )command = messageif self.use_docker_backend:diff --git a/camel/toolkits/terminal_toolkit/utils.py b/camel/toolkits/terminal_toolkit/utils.pyindex 499e528f9..7ed75df38 100644--- a/camel/toolkits/terminal_toolkit/utils.py+++ b/camel/toolkits/terminal_toolkit/utils.py@@ -12,15 +12,17 @@# limitations under the License.# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========+import itertoolsimport osimport platformimport re+import shleximport shutilimport subprocessimport sysimport tempfileimport venv-from typing import Optional, Set, Tuple+from typing import List, Optional, Set, Tuplefrom camel.logger import get_logger@@ -33,7 +35,142 @@ _CMD_PATTERN = re.compile(_QUOTED_STRING_PATTERN = re.compile(r'''["'][^"']*["']''')# Match cd command with optional quoted or unquoted path# Handles: cd path, cd "path with spaces", cd 'path with spaces'-_CD_PATTERN = re.compile(r'\bcd\s+(["\'][^"\']*["\']|[^\s;|&]+)')+_CD_PATTERN = re.compile(r'\bcd\s+(?:--\s+)?(["\'][^"\']*["\']|[^\s;|&]+)')+_PUSHD_PATTERN = re.compile(+ r'\bpushd\s+(?:--\s+)?(["\'][^"\']*["\']|[^\s;|&]+)'+)+_SHELL_SUBSTITUTION_PATTERN = re.compile(r'`|(?<!\\)\$\(')+_CONTROL_FLOW_PATTERN = re.compile(r';|&&|\|\||[|&]')+_SHELL_COMMANDS = {'bash', 'sh', 'zsh', 'dash', 'ksh', 'ash'}+_SEPARATORS = {';', '&&', '||', '|', '&'}+_CD_OUTSIDE_WORKDIR_MESSAGE_TEMPLATE = (+ "Cannot 'cd' outside of the working directory in safe mode. "+ "Allowed root: '{working_dir}'. "+ "To modify files there, copy them (for example with `cp`) "+ "into '{working_dir}' first."+)+_MULTIPLE_CD_IN_CHAIN_MESSAGE = (+ "Multiple 'cd'/'pushd' commands chained with shell operators "+ "(;, &&, ||, |, &) are not allowed in safe mode, because the "+ "runtime execution flow cannot be determined statically. "+ "Please run each directory change as a separate command. "+ "For example, instead of 'cd subdir && cd ..', run 'cd subdir' "+ "first, then run subsequent commands separately."+)++# Public list for downstream customization/import.+DANGEROUS_COMMANDS: List[str] = [+ # System administration+ 'sudo',+ 'su',+ 'reboot',+ 'shutdown',+ 'halt',+ 'poweroff',+ 'init',+ # File system manipulation+ 'rm',+ 'chown',+ 'chgrp',+ 'umount',+ 'mount',+ # Disk operations+ 'dd',+ 'mkfs',+ 'fdisk',+ 'parted',+ 'fsck',+ 'mkswap',+ 'swapon',+ 'swapoff',+ # Process management+ 'service',+ 'systemctl',+ 'systemd',+ # Network configuration+ 'iptables',+ 'ip6tables',+ 'ifconfig',+ 'route',+ 'iptables-save',+ # Cron and scheduling+ 'crontab',+ 'at',+ 'batch',+ # User management+ 'useradd',+ 'userdel',+ 'usermod',+ 'passwd',+ 'chpasswd',+ 'newgrp',+ # Kernel modules+ 'modprobe',+ 'rmmod',+ 'insmod',+ 'lsmod',+]+++def _normalize_command_name(command: str) -> str:+ r"""Normalize command names for safer matching."""+ normalized = os.path.basename(command).lower()+ if normalized.endswith('.exe'):+ normalized = normalized[:-4]+ return normalized+++def _split_command_segments(command: str) -> Optional[List[List[str]]]:+ r"""Split command by shell separators while preserving quoted content."""+ try:+ lexer = shlex.shlex(command, posix=True, punctuation_chars=';&|')+ lexer.whitespace_split = True+ tokens = list(lexer)+ except ValueError:+ return None++ segments: List[List[str]] = []+ current: List[str] = []+ for token in tokens:+ if token in _SEPARATORS:+ if current:+ segments.append(current)+ current = []+ else:+ current.append(token)+ if current:+ segments.append(current)+ return segments+++def _extract_shell_c_payloads(command: str) -> List[str]:+ r"""Extract payloads from shell wrapper commands like `bash -c "<cmd>"`."""+ segments = _split_command_segments(command)+ if segments is None:+ return []++ payloads: List[str] = []+ for segment in segments:+ if not segment:+ continue++ if _normalize_command_name(segment[0]) not in _SHELL_COMMANDS:+ continue++ args = segment[1:]+ for i, arg in enumerate(args):+ is_explicit_c = arg in {'-c', '--command'}+ is_combined_short_flag = (+ arg.startswith('-')+ and not arg.startswith('--')+ and 'c' in arg[1:]+ )+ if is_explicit_c or is_combined_short_flag:+ if i + 1 < len(args):+ payloads.append(args[i + 1])+ break++ return payloadsdef check_command_safety(@@ -53,58 +190,12 @@ def check_command_safety(if not command.strip():return False, "Empty command is not allowed."- # Dangerous commands list - including ALL rm operations- dangerous_commands = [- # System administration- 'sudo',- 'su',- 'reboot',- 'shutdown',- 'halt',- 'poweroff',- 'init',- # File system manipulation- 'rm',- 'chown',- 'chgrp',- 'umount',- 'mount',- # Disk operations- 'dd',- 'mkfs',- 'fdisk',- 'parted',- 'fsck',- 'mkswap',- 'swapon',- 'swapoff',- # Process management- 'service',- 'systemctl',- 'systemd',- # Network configuration- 'iptables',- 'ip6tables',- 'ifconfig',- 'route',- 'iptables-save',- # Cron and scheduling- 'crontab',- 'at',- 'batch',- # User management- 'useradd',- 'userdel',- 'usermod',- 'passwd',- 'chpasswd',- 'newgrp',- # Kernel modules- 'modprobe',- 'rmmod',- 'insmod',- 'lsmod',- ]+ # Recursively inspect shell-wrapper payloads to prevent bypasses such as+ # `bash -c "rm -rf /"` where the dangerous sub-command is quoted.+ for payload in _extract_shell_c_payloads(command):+ is_safe, reason = check_command_safety(payload, allowed_commands)+ if not is_safe:+ return False, reason# Remove quoted strings to avoid false positivesclean_command = _QUOTED_STRING_PATTERN.sub(' ', command)@@ -122,7 +213,7 @@ def check_command_safety(return True, ""# Check for dangerous commands- for cmd in dangerous_commands:+ for cmd in DANGEROUS_COMMANDS:pattern = rf'(?:^|;|\||&&)\s*\b{re.escape(cmd)}\b'if re.search(pattern, clean_command, re.IGNORECASE):return False, f"Command '{cmd}' is blocked for safety."@@ -157,16 +248,82 @@ def sanitize_command(if not is_safe:return False, reason- # Additional check for local backend: prevent cd outside working directory- if not use_docker_backend and working_dir and 'cd ' in command:- # Extract cd commands and check their targets- # Normalize working_dir to ensure consistent comparison+ # Additional check for local backend: prevent cd/pushd outside working+ # directory+ if (+ not use_docker_backend+ and working_dir+ and (+ 'cd ' in command+ or 'cd\t' in command+ or 'pushd ' in command+ or 'pushd\t' in command+ )+ ):normalized_working_dir = os.path.normpath(os.path.abspath(working_dir))- for match in _CD_PATTERN.finditer(command):++ quoted_ranges = [+ (m.start(), m.end())+ for m in _QUOTED_STRING_PATTERN.finditer(command)+ ]++ def _inside_quotes(pos: int) -> bool:+ return any(s <= pos < e for s, e in quoted_ranges)++ # Collect all cd/pushd matches, keeping only those whose keyword+ # is NOT inside a quoted string.+ cd_matches = [+ (name, m)+ for name, m in itertools.chain(+ (('cd', m) for m in _CD_PATTERN.finditer(command)),+ (('pushd', m) for m in _PUSHD_PATTERN.finditer(command)),+ )+ if not _inside_quotes(m.start())+ ]++ if not cd_matches:+ return True, command++ # Detect control-flow operators outside of quoted strings.+ unquoted_command = _QUOTED_STRING_PATTERN.sub(' ', command)+ has_control_flow = bool(_CONTROL_FLOW_PATTERN.search(unquoted_command))++ # If there are multiple cd/pushd AND the command contains+ # control flow operators, reject.+ if len(cd_matches) > 1 and has_control_flow:+ return False, _MULTIPLE_CD_IN_CHAIN_MESSAGE++ # Exactly one cd/pushd, or multiple without operators+ for cmd_name, match in cd_matches:target_path = match.group(1).strip('\'"')- target_dir = os.path.normpath(- os.path.abspath(os.path.join(working_dir, target_path))++ # Block 'cd -' since $OLDPWD may point outside workdir+ if target_path == '-':+ return (+ False,+ _CD_OUTSIDE_WORKDIR_MESSAGE_TEMPLATE.format(+ working_dir=normalized_working_dir+ ),+ )++ if _SHELL_SUBSTITUTION_PATTERN.search(target_path):+ return (+ False,+ "Command substitution is not allowed in "+ f"'{cmd_name}' paths.",+ )++ expanded_target = os.path.expanduser(+ os.path.expandvars(target_path))+ if os.path.isabs(expanded_target):+ resolved_target = expanded_target+ else:+ resolved_target = os.path.join(+ normalized_working_dir, expanded_target+ )++ target_dir = os.path.normpath(os.path.abspath(resolved_target))# Use os.path.commonpath for safe path comparisontry:common = os.path.commonpath(@@ -175,11 +332,18 @@ def sanitize_command(if common != normalized_working_dir:return (False,- "Cannot 'cd' outside of the working directory.",+ _CD_OUTSIDE_WORKDIR_MESSAGE_TEMPLATE.format(+ working_dir=normalized_working_dir+ ),)except ValueError:# Different drives on Windows or other path issues- return False, "Cannot 'cd' outside of the working directory."+ return (+ False,+ _CD_OUTSIDE_WORKDIR_MESSAGE_TEMPLATE.format(+ working_dir=normalized_working_dir+ ),+ )return True, commanddiff --git a/test/toolkits/test_terminal_toolkit.py b/test/toolkits/test_terminal_toolkit.pyindex 8387aa127..2e2824586 100644--- a/test/toolkits/test_terminal_toolkit.py+++ b/test/toolkits/test_terminal_toolkit.py@@ -18,6 +18,8 @@ from pathlib import Pathimport pytestfrom camel.toolkits import TerminalToolkit+from camel.toolkits.terminal_toolkit import DANGEROUS_COMMANDS+from camel.toolkits.terminal_toolkit.utils import sanitize_command@pytest.fixture@@ -158,3 +160,208 @@ def test_shell_write_content_to_file_safe_mode_blocks_path_traversal()assert "error" in result.lower()assert "outside" in result.lower() or "working directory" in result.lower()+++ "command",+ [+ 'bash -c "rm -rf /"',+ "sh -c 'rm -rf /'",+ ],+)+def test_sanitize_command_blocks_dangerous_shell_c_payloads(temp_dir, command):+ """Dangerous commands in shell-wrapper payloads must be blocked."""+ is_safe, message = sanitize_command(command, working_dir=str(temp_dir))+ assert not is_safe+ assert "blocked for safety" in message.lower()+++def test_sanitize_command_blocks_cd_expansion_outside_workdir(+ temp_dir, monkeypatch+):+ """Expanded env vars should be resolved before cd path validation."""+ outside_dir = temp_dir.parent+ monkeypatch.setenv("HOME", str(outside_dir))++ is_safe, message = sanitize_command("cd $HOME", working_dir=str(temp_dir))+ assert not is_safe+ assert "outside" in message.lower()+ assert "copy" in message.lower()+ assert "working directory" in message.lower()++ is_safe, message = sanitize_command(+ "cd ${HOME}", working_dir=str(temp_dir)+ )+ assert not is_safe+ assert "outside" in message.lower()+ assert "copy" in message.lower()+++def test_shell_exec_cd_outside_returns_copy_guidance(temp_dir, request):+ """Shell exec should provide copy guidance for blocked cd traversal."""+ toolkit = TerminalToolkit(working_directory=str(temp_dir), safe_mode=True)+ request.addfinalizer(toolkit.cleanup)++ result = toolkit.shell_exec("test_session", "cd ../")+ lowered = result.lower()+ assert "error:" in lowered+ assert "outside of the working directory" in lowered+ assert "copy" in lowered+ assert str(temp_dir) in result+++def test_shell_exec_safe_mode_rejection_prefix(temp_dir, request):+ """Blocked command errors should clearly mention safe mode rejection."""+ toolkit = TerminalToolkit(working_directory=str(temp_dir), safe_mode=True)+ request.addfinalizer(toolkit.cleanup)++ result = toolkit.shell_exec("test_session", "rm -rf /")+ lowered = result.lower()+ assert lowered.startswith("error:")+ assert "rejected by terminaltoolkit safe mode" in lowered+ assert "blocked for safety" in lowered++[email protected]("command", ["cd $(pwd)", "cd `pwd`"])+def test_sanitize_command_blocks_cd_command_substitution(temp_dir, command):+ """Dynamic command substitutions in cd targets must be rejected."""+ is_safe, message = sanitize_command(command, working_dir=str(temp_dir))+ assert not is_safe+ assert "command substitution" in message.lower()+++def test_sanitize_command_allows_expanded_cd_inside_workdir(+ temp_dir, monkeypatch+):+ """Expanded env vars pointing inside working_dir should remain allowed."""+ subdir = temp_dir / "inner"+ subdir.mkdir()+ monkeypatch.setenv("CAMEL_INSIDE_DIR", str(subdir))++ is_safe, message = sanitize_command(+ "cd $CAMEL_INSIDE_DIR", working_dir=str(temp_dir)+ )+ assert is_safe+ assert message == "cd $CAMEL_INSIDE_DIR"+++def test_sanitize_command_blocks_cd_dash(temp_dir):+ """'cd -' goes to $OLDPWD which may be outside workdir; must be blocked."""+ is_safe, message = sanitize_command("cd -", working_dir=str(temp_dir))+ assert not is_safe+ assert "outside" in message.lower()+++def test_sanitize_command_blocks_cd_double_dash_outside(temp_dir):+ """'cd -- /etc' should block since /etc is outside workdir."""+ is_safe, message = sanitize_command(+ "cd -- /etc", working_dir=str(temp_dir)+ )+ assert not is_safe+ assert "outside" in message.lower()+++def test_sanitize_command_allows_cd_double_dash_inside(temp_dir):+ """'cd -- subdir' should be allowed when subdir is inside workdir."""+ subdir = temp_dir / "inner"+ subdir.mkdir()+ is_safe, message = sanitize_command(+ "cd -- inner", working_dir=str(temp_dir)+ )+ assert is_safe+++def test_sanitize_command_blocks_pushd_outside_workdir(temp_dir):+ """pushd to a path outside workdir must be blocked like cd."""+ is_safe, message = sanitize_command(+ "pushd /etc", working_dir=str(temp_dir)+ )+ assert not is_safe+ assert "outside" in message.lower()+++def test_sanitize_command_allows_pushd_inside_workdir(temp_dir):+ """pushd to a path inside workdir should be allowed."""+ subdir = temp_dir / "inner"+ subdir.mkdir()+ is_safe, message = sanitize_command(+ "pushd inner", working_dir=str(temp_dir)+ )+ assert is_safe+++ "command",+ [+ "cd sub && cd ..",+ "cd sub; cd ..",+ "cd sub || cd ..",+ "false && cd sub; cd ..",+ "cd sub & cd ..",+ ],+)+def test_sanitize_command_blocks_multiple_cd_in_chain(temp_dir, command):+ """Multiple cd/pushd with shell operators must be rejected."""+ subdir = temp_dir / "sub"+ subdir.mkdir()+ is_safe, message = sanitize_command(command, working_dir=str(temp_dir))+ assert not is_safe+ assert "multiple" in message.lower()+ assert "separate command" in message.lower()+++ "command",+ [+ "cd sub && ls",+ "cd sub && echo hello",+ "ls && cd sub",+ "echo hello; cd sub",+ ],+)+def test_sanitize_command_allows_single_cd_in_chain(temp_dir, command):+ """A single cd combined with non-cd commands should be allowed."""+ subdir = temp_dir / "sub"+ subdir.mkdir()+ is_safe, message = sanitize_command(command, working_dir=str(temp_dir))+ assert is_safe+ assert message == command+++def test_sanitize_command_blocks_pushd_chain(temp_dir):+ """Multiple pushd in a chain must also be rejected."""+ subdir = temp_dir / "sub"+ subdir.mkdir()+ is_safe, message = sanitize_command(+ "pushd sub && pushd .", working_dir=str(temp_dir)+ )+ assert not is_safe+ assert "multiple" in message.lower()+++def test_sanitize_command_blocks_mixed_cd_pushd_chain(temp_dir):+ """cd mixed with pushd in a chain must be rejected."""+ subdir = temp_dir / "sub"+ subdir.mkdir()+ is_safe, message = sanitize_command(+ "cd sub && pushd .", working_dir=str(temp_dir)+ )+ assert not is_safe+ assert "multiple" in message.lower()+++def test_sanitize_command_respects_customized_dangerous_commands(+ temp_dir, monkeypatch+):+ r"""Module-level dangerous commands should be importable/customizable."""+ import camel.toolkits.terminal_toolkit.utils as terminal_utils++ custom_commands = DANGEROUS_COMMANDS.copy()+ custom_commands.append("echo")+ monkeypatch.setattr(terminal_utils, "DANGEROUS_COMMANDS", custom_commands)++ is_safe, message = sanitize_command(+ 'echo "should be blocked"', working_dir=str(temp_dir)+ )+ assert not is_safe+ assert "echo" in message.lower()