|
24 | 24 | import typing |
25 | 25 | from typing import Any |
26 | 26 | from typing import List |
| 27 | +from typing import NoReturn |
27 | 28 | from typing import Optional |
28 | 29 | from typing import TYPE_CHECKING |
29 | 30 | import warnings |
@@ -824,20 +825,99 @@ def _set_enforce_yaml_key_denylist(value: bool) -> None: |
824 | 825 | _ENFORCE_YAML_KEY_DENYLIST = value |
825 | 826 |
|
826 | 827 |
|
827 | | -def _check_config_for_blocked_keys(node: Any, filename: str) -> None: |
828 | | - """Recursively check if the configuration contains any blocked keys.""" |
| 828 | +def _validate_mcp_toolset_args(args: Any) -> None: |
| 829 | + """Validates McpToolset args without resolving a code reference.""" |
| 830 | + from ..tools.mcp_tool.mcp_toolset import McpToolsetConfig |
| 831 | + |
| 832 | + McpToolsetConfig.model_validate(args) |
| 833 | + |
| 834 | + |
| 835 | +# Entries in this set must be built-in tools whose validator treats the |
| 836 | +# YAML input as data only. The name is matched literally; it is never |
| 837 | +# imported or otherwise resolved from the configuration. |
| 838 | +_MCP_TOOLSET_NAMES = frozenset({ |
| 839 | + "MCPToolset", |
| 840 | + "McpToolset", |
| 841 | + "google.adk.tools.MCPToolset", |
| 842 | + "google.adk.tools.McpToolset", |
| 843 | + "google.adk.tools.mcp_tool.MCPToolset", |
| 844 | + "google.adk.tools.mcp_tool.McpToolset", |
| 845 | + "google.adk.tools.mcp_tool.mcp_toolset.MCPToolset", |
| 846 | + "google.adk.tools.mcp_tool.mcp_toolset.McpToolset", |
| 847 | +}) |
| 848 | +_BUILTIN_LLM_AGENT_NAMES = frozenset({ |
| 849 | + "Agent", |
| 850 | + "LlmAgent", |
| 851 | + "google.adk.agents.Agent", |
| 852 | + "google.adk.agents.LlmAgent", |
| 853 | + "google.adk.agents.llm_agent.Agent", |
| 854 | + "google.adk.agents.llm_agent.LlmAgent", |
| 855 | +}) |
| 856 | + |
| 857 | + |
| 858 | +def _raise_blocked_key(key: str, filename: str) -> NoReturn: |
| 859 | + raise ValueError( |
| 860 | + f"Blocked key {key!r} found in {filename!r}. " |
| 861 | + f"The '{key}' field is not allowed in agent configurations " |
| 862 | + "because it can execute arbitrary code." |
| 863 | + ) |
| 864 | + |
| 865 | + |
| 866 | +def _check_node_for_blocked_keys(node: Any, filename: str) -> None: |
| 867 | + """Recursively checks a non-tool configuration node for blocked keys.""" |
829 | 868 | if isinstance(node, dict): |
830 | 869 | for key, value in node.items(): |
831 | 870 | if key in _BLOCKED_YAML_KEYS: |
832 | | - raise ValueError( |
833 | | - f"Blocked key {key!r} found in {filename!r}. " |
834 | | - f"The '{key}' field is not allowed in agent configurations " |
835 | | - "because it can execute arbitrary code." |
836 | | - ) |
837 | | - _check_config_for_blocked_keys(value, filename) |
| 871 | + _raise_blocked_key(key, filename) |
| 872 | + _check_node_for_blocked_keys(value, filename) |
838 | 873 | elif isinstance(node, list): |
839 | 874 | for item in node: |
840 | | - _check_config_for_blocked_keys(item, filename) |
| 875 | + _check_node_for_blocked_keys(item, filename) |
| 876 | + |
| 877 | + |
| 878 | +def _check_tool_configs_for_blocked_keys( |
| 879 | + tools: list[Any], filename: str |
| 880 | +) -> None: |
| 881 | + """Checks tool configs, allowing args only for registered built-ins.""" |
| 882 | + for tool in tools: |
| 883 | + if not isinstance(tool, dict): |
| 884 | + _check_node_for_blocked_keys(tool, filename) |
| 885 | + continue |
| 886 | + |
| 887 | + tool_name = tool.get("name") |
| 888 | + is_mcp_toolset = ( |
| 889 | + isinstance(tool_name, str) and tool_name in _MCP_TOOLSET_NAMES |
| 890 | + ) |
| 891 | + for key, value in tool.items(): |
| 892 | + if key not in _BLOCKED_YAML_KEYS: |
| 893 | + _check_node_for_blocked_keys(value, filename) |
| 894 | + continue |
| 895 | + if not is_mcp_toolset: |
| 896 | + _raise_blocked_key(key, filename) |
| 897 | + try: |
| 898 | + _validate_mcp_toolset_args(value) |
| 899 | + except (TypeError, ValueError) as e: |
| 900 | + raise ValueError( |
| 901 | + f"Invalid {key!r} for safe built-in tool {tool_name!r} " |
| 902 | + f"in {filename!r}." |
| 903 | + ) from e |
| 904 | + |
| 905 | + |
| 906 | +def _check_config_for_blocked_keys(node: Any, filename: str) -> None: |
| 907 | + """Checks blocked keys with a narrow exception for safe built-in tools.""" |
| 908 | + if not isinstance(node, dict): |
| 909 | + _check_node_for_blocked_keys(node, filename) |
| 910 | + return |
| 911 | + |
| 912 | + agent_class = node.get("agent_class", "LlmAgent") |
| 913 | + is_builtin_llm_agent = ( |
| 914 | + isinstance(agent_class, str) and agent_class in _BUILTIN_LLM_AGENT_NAMES |
| 915 | + ) |
| 916 | + for key, value in node.items(): |
| 917 | + if key == "tools" and is_builtin_llm_agent and isinstance(value, list): |
| 918 | + _check_tool_configs_for_blocked_keys(value, filename) |
| 919 | + else: |
| 920 | + _check_node_for_blocked_keys({key: value}, filename) |
841 | 921 |
|
842 | 922 |
|
843 | 923 | _ENFORCE_DENYLIST = True |
|
0 commit comments