|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | +""" |
| 4 | +Tool Alias Registry for Capability Canonicalization. |
| 5 | +
|
| 6 | +Maps tool name variants to canonical capability identifiers so that |
| 7 | +policy allowlists/blocklists cannot be bypassed by renaming tools. |
| 8 | +
|
| 9 | +Usage: |
| 10 | + from agent_os.integrations.tool_aliases import ToolAliasRegistry |
| 11 | +
|
| 12 | + registry = ToolAliasRegistry() |
| 13 | + registry.register_alias("bing_search", "web_search") |
| 14 | + registry.register_alias("search_web", "web_search") |
| 15 | + registry.register_alias("google_search", "web_search") |
| 16 | +
|
| 17 | + assert registry.canonicalize("bing_search") == "web_search" |
| 18 | + assert registry.canonicalize("unknown_tool") == "unknown_tool" |
| 19 | +""" |
| 20 | + |
| 21 | +from __future__ import annotations |
| 22 | + |
| 23 | +import logging |
| 24 | +import re |
| 25 | +from typing import Optional |
| 26 | + |
| 27 | +logger = logging.getLogger(__name__) |
| 28 | + |
| 29 | +# Default canonical mappings for common tool families. |
| 30 | +# Keys are alias patterns, values are canonical names. |
| 31 | +DEFAULT_ALIASES: dict[str, str] = { |
| 32 | + # Search tools |
| 33 | + "bing_search": "web_search", |
| 34 | + "google_search": "web_search", |
| 35 | + "search_web": "web_search", |
| 36 | + "internet_search": "web_search", |
| 37 | + "duckduckgo_search": "web_search", |
| 38 | + # File operations |
| 39 | + "read_file": "file_read", |
| 40 | + "file_read": "file_read", |
| 41 | + "get_file": "file_read", |
| 42 | + "load_file": "file_read", |
| 43 | + "write_file": "file_write", |
| 44 | + "file_write": "file_write", |
| 45 | + "save_file": "file_write", |
| 46 | + "create_file": "file_write", |
| 47 | + # Shell execution |
| 48 | + "shell_exec": "shell_execute", |
| 49 | + "shell_execute": "shell_execute", |
| 50 | + "run_command": "shell_execute", |
| 51 | + "exec_command": "shell_execute", |
| 52 | + "bash": "shell_execute", |
| 53 | + "terminal": "shell_execute", |
| 54 | + # Code execution |
| 55 | + "python_exec": "code_execute", |
| 56 | + "run_python": "code_execute", |
| 57 | + "execute_code": "code_execute", |
| 58 | + "eval_code": "code_execute", |
| 59 | + # Database operations |
| 60 | + "sql_query": "database_query", |
| 61 | + "run_sql": "database_query", |
| 62 | + "execute_sql": "database_query", |
| 63 | + "db_query": "database_query", |
| 64 | + # HTTP operations |
| 65 | + "http_request": "http_request", |
| 66 | + "api_call": "http_request", |
| 67 | + "fetch_url": "http_request", |
| 68 | + "curl": "http_request", |
| 69 | +} |
| 70 | + |
| 71 | + |
| 72 | +class ToolAliasRegistry: |
| 73 | + """Maps tool name variants to canonical capability identifiers. |
| 74 | +
|
| 75 | + Provides both exact-match aliases and regex pattern-based matching |
| 76 | + for tool name canonicalization. Prevents policy bypass via tool |
| 77 | + renaming. |
| 78 | +
|
| 79 | + Args: |
| 80 | + use_defaults: If True, loads the default alias mappings. |
| 81 | + """ |
| 82 | + |
| 83 | + def __init__(self, use_defaults: bool = True) -> None: |
| 84 | + self._aliases: dict[str, str] = {} |
| 85 | + self._patterns: list[tuple[re.Pattern, str]] = [] |
| 86 | + if use_defaults: |
| 87 | + self._aliases.update(DEFAULT_ALIASES) |
| 88 | + |
| 89 | + def register_alias(self, alias: str, canonical: str) -> None: |
| 90 | + """Register a tool name alias. |
| 91 | +
|
| 92 | + Args: |
| 93 | + alias: The alternative tool name (case-insensitive). |
| 94 | + canonical: The canonical capability name it maps to. |
| 95 | + """ |
| 96 | + self._aliases[alias.lower()] = canonical.lower() |
| 97 | + |
| 98 | + def register_pattern(self, pattern: str, canonical: str) -> None: |
| 99 | + """Register a regex pattern that maps matching tool names. |
| 100 | +
|
| 101 | + Args: |
| 102 | + pattern: Regex pattern to match tool names against. |
| 103 | + canonical: The canonical capability name for matches. |
| 104 | + """ |
| 105 | + self._patterns.append((re.compile(pattern, re.IGNORECASE), canonical.lower())) |
| 106 | + |
| 107 | + def canonicalize(self, tool_name: str) -> str: |
| 108 | + """Resolve a tool name to its canonical form. |
| 109 | +
|
| 110 | + Checks exact aliases first, then regex patterns. Returns the |
| 111 | + original name (lowercased) if no mapping is found. |
| 112 | +
|
| 113 | + Args: |
| 114 | + tool_name: The tool name to canonicalize. |
| 115 | +
|
| 116 | + Returns: |
| 117 | + The canonical capability name. |
| 118 | + """ |
| 119 | + lower = tool_name.lower() |
| 120 | + |
| 121 | + # Exact match first |
| 122 | + if lower in self._aliases: |
| 123 | + return self._aliases[lower] |
| 124 | + |
| 125 | + # Pattern match |
| 126 | + for pattern, canonical in self._patterns: |
| 127 | + if pattern.search(lower): |
| 128 | + return canonical |
| 129 | + |
| 130 | + return lower |
| 131 | + |
| 132 | + def is_allowed(self, tool_name: str, allowed_tools: list[str]) -> bool: |
| 133 | + """Check if a tool is in the allowed list after canonicalization. |
| 134 | +
|
| 135 | + Both the tool name and all entries in the allowed list are |
| 136 | + canonicalized before comparison. |
| 137 | +
|
| 138 | + Args: |
| 139 | + tool_name: Tool name to check. |
| 140 | + allowed_tools: List of allowed tool names/capabilities. |
| 141 | +
|
| 142 | + Returns: |
| 143 | + True if the canonicalized tool is in the canonicalized allowlist. |
| 144 | + """ |
| 145 | + if not allowed_tools: |
| 146 | + return True # Empty allowlist = all allowed |
| 147 | + canonical = self.canonicalize(tool_name) |
| 148 | + allowed_canonical = {self.canonicalize(t) for t in allowed_tools} |
| 149 | + return canonical in allowed_canonical |
| 150 | + |
| 151 | + def is_blocked(self, tool_name: str, blocked_tools: list[str]) -> bool: |
| 152 | + """Check if a tool is in a block list after canonicalization. |
| 153 | +
|
| 154 | + Args: |
| 155 | + tool_name: Tool name to check. |
| 156 | + blocked_tools: List of blocked tool names/capabilities. |
| 157 | +
|
| 158 | + Returns: |
| 159 | + True if the canonicalized tool is in the canonicalized blocklist. |
| 160 | + """ |
| 161 | + if not blocked_tools: |
| 162 | + return False |
| 163 | + canonical = self.canonicalize(tool_name) |
| 164 | + blocked_canonical = {self.canonicalize(t) for t in blocked_tools} |
| 165 | + return canonical in blocked_canonical |
| 166 | + |
| 167 | + def get_aliases(self, canonical: str) -> list[str]: |
| 168 | + """Get all known aliases for a canonical tool name. |
| 169 | +
|
| 170 | + Args: |
| 171 | + canonical: The canonical capability name. |
| 172 | +
|
| 173 | + Returns: |
| 174 | + List of alias names that map to this canonical name. |
| 175 | + """ |
| 176 | + canonical_lower = canonical.lower() |
| 177 | + return [ |
| 178 | + alias |
| 179 | + for alias, canon in self._aliases.items() |
| 180 | + if canon == canonical_lower |
| 181 | + ] |
| 182 | + |
| 183 | + def list_canonical_tools(self) -> list[str]: |
| 184 | + """List all unique canonical tool names.""" |
| 185 | + return sorted(set(self._aliases.values())) |
| 186 | + |
| 187 | + def __len__(self) -> int: |
| 188 | + return len(self._aliases) |
| 189 | + |
| 190 | + def __contains__(self, tool_name: str) -> bool: |
| 191 | + return tool_name.lower() in self._aliases |
0 commit comments