使用说明
SapperAI
用于人工智能系统的轻量级、基于规则的安全框架。实时检测并阻止提示注入、命令注入等威胁。
具有工具调用功能的AI代理面临着严重的安全风险:
- 提示注入:用户输入中的恶意指令会覆盖系统行为
- 命令注入:通过工具执行的危险命令(rm-rf、SQL注入等)
- 数据渗漏:通过工具参数或LLM输出泄露的秘密
SapperAI提供 规则第一护栏 与:
- ✅ 50+探测器规则 用于工具调用攻击模式
- ✅ 基于政策的决策 具有允许/阻止阈值和覆盖
- ✅ 审计友好的原因 对于每一个安全决策
- ✅ 可配置的故障打开/故障关闭行为 适用于不同的环境
快速开始
npm install sapper-ai
# or
pnpm install sapper-ai
import { createGuard } from 'sapper-ai'
const guard = createGuard()
const decision = await guard.check({ toolName: 'shell', arguments: { cmd: 'ls' } })
CLI:扫描->硬化(推荐)
# 1) Scan your repo (interactive in a TTY)
npx sapper-ai scan
# 2) If you skipped prompts, you can harden explicitly:
npx sapper-ai harden --apply
# 3) To include system-level protection (writes to your home directory):
npx sapper-ai harden --apply --include-system
CI友好扫描(确定性,无提示):
npx -y sapper-ai@0.9.0 scan --policy ./sapperai.config.yaml --no-prompt --no-open --no-save
建筑
┌──────────────────────────────────────────────────────────────┐
│ SapperAI Stack │
├──────────────────────────────────────────────────────────────┤
│ │
│ @sapper-ai/types (11 types, no deps) │
│ │ │
│ └─► @sapper-ai/core (50+ rules, policy engine) │
│ │ │
│ ├─► @sapper-ai/mcp (stdio proxy + CLI) │
│ │ │
│ │
└──────────────────────────────────────────────────────────────┘
Detection Pipeline:
ToolCall → RulesDetector → DecisionEngine → Guard → Block/Allow
包裹
| 包装 | 描述 | 用例 |
|---|---|---|
| 蓝宝石ai | 单次安装包装器(createGuard+预设+CLI) | 默认入口点 |
| @蓝宝石ai/类型 | TypeScript类型定义 | 自定义检测器、集成 |
| @蓝宝石ai/core | 核心检测引擎(RulesDetector、DecisionEngine、Guard) | 直接集成 |
| @sapper ai/mcp | MCP安全代理 | 包裹任何MCP服务器 |
直接集成(高级)
import { AuditLogger, DecisionEngine, Guard, RulesDetector } from '@sapper-ai/core'
import type { Policy } from '@sapper-ai/types'
const policy: Policy = {
mode: 'enforce',
defaultAction: 'allow',
failOpen: true,
}
const detector = new RulesDetector()
const engine = new DecisionEngine([detector])
const auditLogger = new AuditLogger()
const guard = new Guard(engine, auditLogger, policy)
const decision = await guard.preTool({
toolName: 'executeCommand',
arguments: { command: 'rm -rf /' },
})
if (decision.action === 'block') {
throw new Error(`Blocked: ${decision.reasons.join(', ')}`)
}
检测能力
威胁类别(50+规则)
- 快速注射:“忽略以前”、“系统提示”、“越狱”、“绕过”
- 命令注入:
rm -rf /SQL注入(' OR '1'='1),XXE - 路径遍历:
../,/etc/passwd,/etc/shadow - 数据渗漏:API密钥、机密、,
process.env - 代码注入:
eval(),__import__(),system(),模板注入
教育背景抑制
包含安全关键字的文档/教程的误报减少。
验证
SapperAI只保留默认规则的路径依赖性,并由确定性测试覆盖。在更改检测器或策略行为之前,请运行:
pnpm test
pnpm --filter @sapper-ai/core run test:smoke
pnpm exec tsc -b --noEmit
对于本地基准数字,运行:
pnpm --filter @sapper-ai/core run bench
安装
# Full monorepo (for development)
git clone https://github.com/sapper-ai/core.git
cd core
pnpm install
pnpm build
发展
# Build all packages
pnpm build
# Run tests
pnpm test
# Run deterministic security smoke tests
pnpm --filter @sapper-ai/core run test:smoke
# Type checking
pnpm exec tsc -b --noEmit
# Benchmarks
pnpm --filter @sapper-ai/core run bench
操作文档
- Runbook索引:
docs/ops/README.md - 观察+隔离:
docs/ops/watch-quarantine.md - 威胁情报+黑名单:
docs/ops/threat-intel.md - 对抗性活动:
docs/ops/adversary.md
许可证
麻省理工学院
