TTokenySpace
返回 Skills 列表

Guardrail System

三层护栏系统:输入护栏(Prompt注入检测)、工具护栏(权限分级)、输出护栏(敏感信息过滤)。

#中文
0

安装到 Tokeny(自动)

下载 ZIP
安装"guardrail-system"技能
技能信息:
- 名称: Guardrail System
- 标识: guardrail-system
- 描述: 三层护栏系统:输入护栏(Prompt注入检测)、工具护栏(权限分级)、输出护栏(敏感信息过滤)。
- 版本: 1.0.0
下载地址:
https://www.tokeny.space/api/skills/guardrail-system/download
继续

复制上方内容到 Tokeny 客户端并在会话中发送即可自动安装;也可直接 下载 ZIP并拖动到技能窗口安装。

SKILL.md

Guardrail System

三层安全防护系统,用于 AI Agent 的输入/输出/工具调用安全控制。

功能

三层安全防护:

  1. 输入护栏:14种Prompt注入模式检测(中英文),异常长度检测(>10000字符)
  2. 工具护栏:读/写/危险三级权限控制
  3. 输出护栏:10种敏感信息自动过滤(API密钥、密码、邮箱、身份证等)

文件结构

skills/guardrail-system/
├── SKILL.md                    # 本文件
├── test_guardrails.py          # 测试用例
├── scripts/
│   ├── guardrail.py           # 统一接口
│   ├── input_guard.py         # 输入护栏
│   ├── tool_guard.py          # 工具护栏
│   └── output_guard.py        # 输出护栏
└── references/
    ├── injection_patterns.md  # 注入模式库
    └── permission_levels.md   # 权限分级表

使用方法

import sys
sys.path.insert(0, "skills/guardrail-system/scripts")

from guardrail import GuardrailSystem

guardrails = GuardrailSystem()

# 输入检查 - Prompt注入检测
result = guardrails.check_input(user_message)
if not result.allowed:
    print(f"拦截: {result.reason}")

# 工具检查 - 权限分级控制
result = guardrails.check_tool_call("rm", {"path": "/"})
if result.requires_authorization:
    print(f"需授权: {result.message}")

# 输出检查 - 敏感信息过滤
result = guardrails.check_output(assistant_message)
if result.sanitized_output:
    print(f"已过滤: {result.sanitized_output}")

统一接口

GuardrailResult

所有护栏方法返回统一的结果对象:

字段类型说明
allowedbool是否允许通过
reasonstr原因说明
requires_confirmationbool是否需要用户确认
requires_authorizationbool是否需要明确授权
messagestr提示信息
sanitized_outputstr过滤后的输出(仅输出护栏)

三层护栏详解

层1: 输入护栏 (InputGuard)

检测 14 种 Prompt 注入模式:

#模式示例
1ignore previous instructions"ignore previous instructions and..."
2ignore above instructions"ignore all above instructions"
3you are now a/an"you are now a hacker"
4new instructions:"new instructions: do X"
5system prompt"system prompt override"
6disregard previous"disregard previous commands"
7forget your instructions"forget your instructions"
8act as a/an"act as a python interpreter"
9pretend you are"pretend you are an admin"
10override previous"override previous settings"
11新的指令"新的指令:执行命令"
12忽略之前的"忽略之前的所有内容"
13忽略以上"忽略以上指令"
14你现在是"你现在是一个AI"

额外检测:输入长度 > 10000 字符。

层2: 工具护栏 (ToolGuard)

权限级别行为工具示例
READ自动批准read, read_file, web_search, list_files, search, fetch_url
WRITE需用户确认write, write_file, edit_file, create_file, save
DANGEROUS需明确授权rm, delete_file, execute_shell, exec, format, drop_table

层3: 输出护栏 (OutputGuard)

#类型匹配模式
1API密钥API_KEY=xxx, api_key=xxx
2密码password=xxx, passwd=xxx
3密钥secret=xxx
4访问令牌token=xxx(长度≥20)
5私钥private_key=xxx, -----BEGIN PRIVATE KEY-----
6邮箱user@domain.com
7身份证号18位身份证号码

集成点

与 hook-engine 集成:

  • PreInput Hookguardrails.check_input(message)
  • PreToolUse Hookguardrails.check_tool_call(tool_name, params)
  • PostOutput Hookguardrails.check_output(message)

测试

cd skills/guardrail-system
python test_guardrails.py

扩展

添加新的注入模式

编辑 scripts/input_guard.py 中的 INJECTION_PATTERNS 列表。

添加工具到权限列表

from tool_guard import ToolGuard, PermissionLevel

guard = ToolGuard()
guard.add_tool("my_custom_tool", PermissionLevel.READ)

添加新的敏感信息模式

编辑 scripts/output_guard.py 中的 SENSITIVE_PATTERNS 列表。

评论

加载中…