|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +import re |
| 17 | + |
| 18 | +from nemoguardrails.actions import action |
| 19 | + |
| 20 | + |
| 21 | +@action(is_system_action=True) |
| 22 | +async def check_forbidden_words(context: dict = {}): |
| 23 | + """Check if the message contains forbidden words.""" |
| 24 | + user_message = context.get("user_message", "").lower() |
| 25 | + |
| 26 | + forbidden_categories = { |
| 27 | + "security": ["password", "hack", "exploit", "vulnerability"], |
| 28 | + "inappropriate": ["violence", "illegal", "harmful"], |
| 29 | + "competitors": ["chatgpt", "openai", "claude", "anthropic"], |
| 30 | + } |
| 31 | + |
| 32 | + for category, words in forbidden_categories.items(): |
| 33 | + for word in words: |
| 34 | + if word in user_message: |
| 35 | + return {"status": "blocked", "category": category, "word": word} |
| 36 | + |
| 37 | + return {"status": "allowed"} |
| 38 | + |
| 39 | + |
| 40 | +@action(is_system_action=True) |
| 41 | +async def check_output_length(context: dict = {}): |
| 42 | + """Check if the bot message is too long.""" |
| 43 | + bot_msg = context.get("bot_message", "") |
| 44 | + return "blocked" if len(bot_msg.split()) > 100 else "allowed" |
| 45 | + |
| 46 | + |
| 47 | +@action(is_system_action=True) |
| 48 | +async def check_tool_response_safety(tool_message: str = None, context: dict = None): |
| 49 | + """Validate tool responses for sensitive data leakage.""" |
| 50 | + if tool_message is None: |
| 51 | + tool_message = context.get("tool_message", "") if context else "" |
| 52 | + |
| 53 | + if not tool_message: |
| 54 | + return "allowed" |
| 55 | + |
| 56 | + credential_patterns = { |
| 57 | + "password": r"password[:\s=]+\w+", |
| 58 | + "api_key": r"(?:api[_\s-]?key|apikey)[:\s=]+[\w-]+", |
| 59 | + "secret": r"secret[:\s=]+\w+", |
| 60 | + "token": r"(?:access[_\s]?token|bearer)[:\s=]+[\w.-]+", |
| 61 | + "private_key": r"-----BEGIN (?:RSA |EC )?PRIVATE KEY-----", |
| 62 | + } |
| 63 | + |
| 64 | + tool_message_lower = tool_message.lower() |
| 65 | + |
| 66 | + for pattern_name, pattern in credential_patterns.items(): |
| 67 | + if re.search(pattern, tool_message_lower): |
| 68 | + return "blocked" |
| 69 | + |
| 70 | + return "allowed" |
| 71 | + |
| 72 | + |
| 73 | +@action(is_system_action=True) |
| 74 | +async def check_tool_call_safety(tool_calls=None, context=None): |
| 75 | + """Validate tool calls before execution using an allow list approach.""" |
| 76 | + if tool_calls is None: |
| 77 | + tool_calls = context.get("tool_calls", []) if context else [] |
| 78 | + |
| 79 | + allowed_tools = [ |
| 80 | + "get_weather", |
| 81 | + "search_web", |
| 82 | + "read_file", |
| 83 | + "get_time", |
| 84 | + "get_stock_price", |
| 85 | + "calculate", |
| 86 | + ] |
| 87 | + |
| 88 | + dangerous_patterns = { |
| 89 | + "path_traversal": r"\.\./", |
| 90 | + "command_injection": r"[;&|`$]", |
| 91 | + "sql_injection": r"(?:DROP|DELETE|TRUNCATE)\s+(?:TABLE|DATABASE)", |
| 92 | + } |
| 93 | + |
| 94 | + for tool_call in tool_calls: |
| 95 | + tool_name = tool_call.get("name", "") |
| 96 | + |
| 97 | + if tool_name not in allowed_tools: |
| 98 | + return "blocked" |
| 99 | + |
| 100 | + args = tool_call.get("args", {}) |
| 101 | + for arg_name, arg_value in args.items(): |
| 102 | + if isinstance(arg_value, str): |
| 103 | + for pattern_name, pattern in dangerous_patterns.items(): |
| 104 | + if re.search(pattern, arg_value, re.IGNORECASE): |
| 105 | + return "blocked" |
| 106 | + |
| 107 | + return "allowed" |
0 commit comments