面向 AI 智能体安全规则的便携式开放规范
规范 · 文档 · 规则集 · JSON Schema
HushSpec 是一种面向 AI 智能体安全规则的开放策略格式。它定义了智能体在运行时可以执行哪些操作,包括文件系统访问、网络出口、工具使用、机密信息检测等,而不规定如何强制执行这些控制。这种分离使策略能够跨运行时、框架和语言进行移植。
v0.1.1-alpha — 核心规范、全部四个 SDK(Rust、TypeScript、Python、Go)以及 h2h CLI 均已发布并可用。通过它们,你可以对 10 种规则类型和 3 个扩展模块执行解析、验证、评估、合并、归一化、检测、签名和审计。API 接口面正在趋于稳定,但尚未冻结——在 v1.0 之前预计还会有调整。
hushspec: "0.1.0"
name: production-agent
rules:
forbidden_paths:
patterns:
- "**/.ssh/**"
- "**/.aws/**"
- "/etc/shadow"
egress:
allow:
- "api.openai.com"
- "*.anthropic.com"
- "api.github.com"
default: block
tool_access:
block: [shell_exec, run_command]
require_confirmation: [file_write, git_push]
default: allow
secret_patterns:
patterns:
- name: aws_key
pattern: "AKIA[0-9A-Z]{16}"
severity: critical
skip_paths: ["**/test/**"]
shell_commands:
forbidden_patterns:
- "rm\\s+-rf\\s+/"
- "curl.*\\|.*bash"
四个 SDK 均实现了完整的 HushSpec 流水线,涵盖从解析、验证到归一化和评估的全流程。
Homebrew、npm 和预编译二进制将从发布流水线构建的第一个
v0.x标签起提供,一旦发布流水线发布了工件、tap 公式和 npm 包即可使用。在此之前,请通过 Cargo 安装。
所有安装方法都会安装 h2h 命令。请参阅下方的 CLI 工具。
[dependencies]
hushspec = "0.1"
npm install @hushspec/core
pip install hushspec
go get github.com/backbay-labs/hush/packages/go@main
use hushspec::HushSpec;
let yaml_str = "hushspec: \"0.1.0\"\nname: example\n";
let spec = HushSpec::parse(yaml_str)?;
let result = hushspec::validate(&spec);
assert!(result.is_valid());
import { parseOrThrow, validate } from '@hushspec/core';
const yamlString = 'hushspec: "0.1.0"\nname: example\n';
const spec = parseOrThrow(yamlString);
const result = validate(spec);
console.log(result.valid); // true
from hushspec import parse_or_raise, validate
yaml_string = 'hushspec: "0.1.0"\nname: example\n'
spec = parse_or_raise(yaml_string)
result = validate(spec)
assert result.is_valid
import (
"fmt"
"github.com/backbay-labs/hush/packages/go/hushspec"
)
yamlString := "hushspec: \"0.1.0\"\nname: example\n"
spec, err := hushspec.Parse(yamlString)
if err != nil {
panic(err)
}
result := hushspec.Validate(spec)
fmt.Println(result.IsValid())
每个 SDK 都提供一个 evaluate() 函数,该函数接收一个已解析的规范和一个动作,然后返回决策(allow、warn 或 deny)以及匹配的规则详情。
import { parseOrThrow, evaluate } from '@hushspec/core';
const spec = parseOrThrow(policyYaml);
const result = evaluate(spec, { type: 'egress', target: 'api.openai.com' });
// result.decision === 'allow' | 'warn' | 'deny'
// result.matched_rule === 'egress'
from hushspec import parse_or_raise, evaluate
spec = parse_or_raise(policy_yaml)
result = evaluate(spec, {"type": "egress", "target": "api.openai.com"})
assert result.decision in ("allow", "warn", "deny")
HushGuard 将策略加载和评估封装在面向应用程序代码的 evaluate、check 和 enforce 简单接口之后。
import { HushGuard } from '@hushspec/core';
const guard = HushGuard.fromFile('./policy.yaml');
guard.enforce({ type: 'tool_call', target: 'bash' }); // throws HushSpecDenied if denied
from hushspec import HushGuard
guard = HushGuard.from_file("./policy.yaml")
guard.enforce({"type": "tool_call", "target": "bash"}) # raises HushSpecDenied if denied
h2h CLI 涵盖了常见的策略工作流:验证、测试、评估和解释单个动作、lint、diff、格式化、初始化、签名、验证和触发恐慌模式。
# Validate a policy against the HushSpec schema
h2h validate policy.yaml
# Run evaluation test suites
h2h test --fixtures ./tests/
# Evaluate one action and explain the decision
h2h eval policy.yaml --type egress --target api.example.com
h2h explain policy.yaml --type egress --target api.example.com
# Static analysis and linting
h2h lint policy.yaml
# Lint and auto-fix decision-neutral issues
h2h lint policy.yaml --fix
# Compare two policies and show effective decision changes
h2h diff old.yaml new.yaml
# Format policy files canonically
h2h fmt policy.yaml
# Scaffold a new policy project
h2h init --preset default
# Sign a policy with Ed25519
h2h sign policy.yaml --key h2h.key
# Verify a policy signature
h2h verify policy.yaml --key h2h.pub
# Generate a new Ed25519 keypair
h2h keygen
# Emergency override (deny-all kill switch)
h2h panic activate --sentinel /tmp/hushspec.panic
h2h panic deactivate --sentinel /tmp/hushspec.panic
有关安装选项(Homebrew、npm、Cargo 或预编译二进制),请参阅上方的 安装。
evaluate_audited() 会生成结构化的决策回执,其中包含规则追踪、策略摘要以及可选的内容脱敏。回执符合 hushspec-receipt.v0.schema.json,旨在支持 SOC 2、HIPAA、PCI-DSS 和 FedRAMP 等重审计环境。
import { parseOrThrow, evaluateAudited } from '@hushspec/core';
const spec = parseOrThrow(policyYaml);
const receipt = evaluateAudited(spec, action, {
enabled: true,
include_rule_trace: true,
redact_content: false,
});
// receipt.decision, receipt.rule_evaluations, receipt.policy_summary
回执接收器(FileReceiptSink、ConsoleReceiptSink、FilteredSink、MultiSink、CallbackSink)在四个 SDK 中均可用,用于将回执路由到存储、日志或 OTLP 端点。
检测流水线将提示注入、越狱和数据外泄检查接入评估流程。基于正则的参考检测器随所有 SDK 一同提供,自定义检测器可以通过 DetectorRegistry 注册。
import { parseOrThrow, evaluateWithDetection, DetectorRegistry } from '@hushspec/core';
const registry = DetectorRegistry.withDefaults();
const result = evaluateWithDetection(spec, action, registry, {
enabled: true,
prompt_injection_threshold: 0.5,
});
// result.detection_results contains matched patterns and confidence scores
预构建的适配器可将框架特定的工具调用转换为 HushSpec 评估动作。
EvaluationObserver 接口和 ObservableEvaluator 包装器会为每次评估、策略加载和策略重载发出结构化事件。内置观察器包括 JsonLineObserver、ConsoleObserver 和 MetricsCollector。
import { ObservableEvaluator, JsonLineObserver, MetricsCollector } from '@hushspec/core';
const evaluator = new ObservableEvaluator();
evaluator.addObserver(new JsonLineObserver(process.stderr));
evaluator.addObserver(new MetricsCollector());
const result = evaluator.evaluate(spec, action);
策略可以使用 Ed25519 密钥签名,并在加载时进行验证。CLI 提供 sign、verify 和 keygen 命令。签名格式符合 hushspec-signature.v0.schema.json。
# Generate a keypair
h2h keygen --output-dir mykeys
# Sign a policy (creates policy.yaml.sig)
h2h sign policy.yaml --key mykeys/h2h.key
# Verify the signature
h2h verify policy.yaml --key mykeys/h2h.pub
恐慌模式是一种全部拒绝的紧急熔断开关,无需重新部署策略即可立即激活。你可以通过哨兵文件、CLI 或 API 调用触发它。在恐慌模式激活期间,每次评估都会返回 deny。
# Activate panic mode
h2h panic activate --sentinel /tmp/hushspec.panic
# Deactivate
h2h panic deactivate --sentinel /tmp/hushspec.panic
import { activatePanic, deactivatePanic, isPanicActive } from '@hushspec/core';
activatePanic();
// All evaluate() calls now return deny
deactivatePanic();
策略可以从本地文件、HTTPS URL(带 ETag 缓存和 SSRF 防护)或内置规则集加载。PolicyWatcher 和 PolicyPoller 支持无需重启进程的热重载。
import { PolicyWatcher, HushGuard } from '@hushspec/core';
const guard = HushGuard.fromFile('./policy.yaml');
const watcher = new PolicyWatcher('./policy.yaml', {
onChange: (newSpec) => guard.swapPolicy(newSpec),
});
watcher.start();
HushSpec 支持可选的扩展模块,以实现更高级的策略行为:
| 扩展 | 用途 |
|---|---|
| Posture | 用于能力和预算的声明式状态机 |
| Origins | 感知来源的策略投影(Slack、GitHub、电子邮件等) |
| Detection | 提示注入、越狱、威胁情报的阈值配置 |
extensions:
posture:
initial: standard
states:
standard: { capabilities: [file_access, egress] }
restricted: { capabilities: [file_access] }
transitions:
- { from: "*", to: restricted, on: critical_violation }
detection:
prompt_injection:
block_at_or_above: high
开箱即用的策略位于 rulesets/:
HushSpec 文档可在 Clawdstrike 中原生加载:
// Auto-detects HushSpec vs Clawdstrike-native format
let policy = clawdstrike::Policy::from_yaml_auto(yaml)?;
# Convert between formats
hush policy migrate policy.yaml --to hushspec
spec/ Normative specification, including core and extension docs
schemas/ JSON Schema definitions
crates/ Rust crates
hushspec/ Core library: parse, validate, merge, resolve, evaluate, detect, sign
hushspec-cli/ CLI tool
hushspec-testkit/ Conformance test runner
packages/ Language SDKs for TypeScript, Python, and Go
rulesets/ Built-in security rulesets
fixtures/ Conformance and evaluation fixtures
docs/ mdBook documentation site
generated/ Generated shared SDK contract artifacts
scripts/ Code generation and CI tooling
规范性规范位于 spec/。用于程序化验证的 JSON Schema 定义位于 schemas/。完整文档位于 docs/。
Apache-2.0。请参阅 LICENSE。
| 能力 | Rust | TypeScript | Python | Go |
|---|
| 解析 + 验证(第 1 级) | 是 | 是 | 是 | 是 |
| 合并(第 2 级) | 是 | 是 | 是 | 是 |
| 归一化(第 2 级以上) | 是 | 是 | 是 | 是 |
| 评估(第 3 级) | 是 | 是 | 是 | 是 |
| 审计追踪(第 4 级) | 是 | 是 | 是 | 是 |
| 检测 | 是 | 是 | 是 | 是 |
| 可观测性 | 是 | 是 | 是 | 是 |
| 回执接收器 | 是 | 是 | 是 | 是 |
| 方法 | 命令 |
|---|
| Homebrew(macOS/Linux) | brew install backbay-labs/tap/h2h |
| npm | npm install -g @hushspec/cli(或 npx @hushspec/cli validate policy.yaml) |
| Cargo(从源码) | cargo install hushspec-cli |
| 预编译二进制 | GitHub Releases — h2h-<tag>-<target>.tar.gz + SHA256SUMS,附来源证明 |
| 框架 | 适配器 | SDK |
|---|
| Claude / Anthropic | mapClaudeToolToAction, createSecureToolHandler | TypeScript |
| OpenAI | mapOpenAIToolCall, createOpenAIGuard | TypeScript |
| MCP(模型上下文协议) | mapMCPToolCall, createMCPGuard | TypeScript |
import { HushGuard, mapClaudeToolToAction } from '@hushspec/core';
const guard = HushGuard.fromFile('./policy.yaml');
const action = mapClaudeToolToAction(toolUseBlock);
guard.enforce(action);
| 规则 | 用途 |
|---|
forbidden_paths | 阻止访问敏感的文件系统路径 |
path_allowlist | 基于白名单的读/写/补丁访问 |
egress | 按域名控制网络出口 |
secret_patterns | 检测文件内容中的机密信息 |
patch_integrity | 验证补丁(diff)安全性(大小限制、禁止模式) |
shell_commands | 阻止危险的 shell 命令 |
tool_access | 控制工具/MCP 调用 |
computer_use | 控制 CUA(计算机使用智能体)操作 |
remote_desktop_channels | 控制远程桌面侧信道 |
input_injection | 控制输入注入能力 |
| 规则集 | 描述 |
|---|
default | 面向 AI 智能体执行的均衡安全 |
strict | 最高安全性,最小权限 |
permissive | 适合开发,限制宽松 |
ai-agent | 针对 AI 编码助手优化 |
cicd | CI/CD 流水线安全 |
remote-desktop | 计算机使用(computer use)智能体会话 |
panic | 全部拒绝的紧急覆盖 |