Documentation
¶
Overview ¶
Package guard 提供 Hexagon AI Agent 框架的安全守卫能力
Guard 用于在 Agent 执行前后进行安全检查,包括:
- Prompt 注入检测:检测恶意的 prompt 注入攻击
- PII 检测:检测和脱敏个人身份信息
- 内容过滤:过滤有害或敏感内容
- 输出验证:验证 Agent 输出是否符合要求
主要类型:
- Guard: 守卫接口,执行安全检查
- GuardChain: 守卫链,按顺序执行多个守卫
- CheckResult: 检查结果,包含通过状态、风险分数和发现的问题
守卫链模式:
- ChainModeAll: 所有守卫都必须通过
- ChainModeAny: 任一守卫通过即可
- ChainModeFirst: 第一个失败就停止
使用示例:
guard := NewGuardChain(ChainModeAll,
NewPromptInjectionGuard(),
NewPIIGuard(),
)
result, err := guard.Check(ctx, userInput)
Index ¶
- func RedactPII(text string) string
- func RedactPIISelective(text string, types ...string) string
- type ChainMode
- type CheckResult
- type Finding
- type Guard
- type GuardAction
- type GuardChain
- type GuardConfig
- type InputGuard
- type Middleware
- type OutputGuard
- type PIIGuard
- type PIIOption
- type Position
- type PromptInjectionGuard
- type PromptInjectionOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RedactPIISelective ¶
RedactPIISelective 选择性脱敏 只脱敏指定类型的 PII
Types ¶
type CheckResult ¶
type CheckResult struct {
// Passed 是否通过
Passed bool `json:"passed"`
// Score 风险分数 (0-1,越高风险越大)
Score float64 `json:"score"`
// Category 风险类别
Category string `json:"category,omitempty"`
// Reason 原因
Reason string `json:"reason,omitempty"`
// Findings 发现的问题
Findings []Finding `json:"findings,omitempty"`
// Metadata 额外元数据
Metadata map[string]any `json:"metadata,omitempty"`
}
CheckResult 检查结果
type Finding ¶
type Finding struct {
// Type 问题类型
Type string `json:"type"`
// Text 问题文本
Text string `json:"text"`
// Position 位置
Position Position `json:"position,omitempty"`
// Severity 严重程度: low, medium, high, critical
Severity string `json:"severity"`
// Suggestion 建议
Suggestion string `json:"suggestion,omitempty"`
}
Finding 发现的问题
func DetectPIIWithError ¶
DetectPIIWithError 检测文本中的 PII,返回错误信息 这是 DetectPII 的安全版本,提供完整的错误处理
type Guard ¶
type Guard interface {
// Name 返回守卫名称
Name() string
// Check 执行检查
Check(ctx context.Context, input string) (*CheckResult, error)
// Enabled 是否启用
Enabled() bool
}
Guard 安全守卫接口
type GuardAction ¶
type GuardAction string
GuardAction 守卫动作
const ( // ActionBlock 阻止 ActionBlock GuardAction = "block" // ActionWarn 警告 ActionWarn GuardAction = "warn" // ActionLog 仅记录 ActionLog GuardAction = "log" // ActionRedact 脱敏 ActionRedact GuardAction = "redact" )
type GuardChain ¶
type GuardChain struct {
// contains filtered or unexported fields
}
GuardChain 守卫链 按顺序执行多个守卫
func NewGuardChain ¶
func NewGuardChain(mode ChainMode, guards ...Guard) *GuardChain
NewGuardChain 创建守卫链
func (*GuardChain) Check ¶
func (c *GuardChain) Check(ctx context.Context, input string) (*CheckResult, error)
Check 执行检查
根据链模式执行安全检查:
- ChainModeAll: 所有启用的守卫都必须通过
- ChainModeAny: 任一启用的守卫通过即可
- ChainModeFirst: 遇到第一个失败的守卫就停止
线程安全:在迭代前创建守卫列表的副本
func (*GuardChain) Enabled ¶
func (c *GuardChain) Enabled() bool
Enabled 返回守卫链是否启用
链的"启用"语义应与 Check 的 enabledCount 一致:当且仅当存在至少一个 已启用的子守卫时才视为启用。仅判断 len(guards)>0 会把"全部子守卫被禁用" 误报为启用,与 Check 默认放行的行为不一致(回归: B12)。
type GuardConfig ¶
type GuardConfig struct {
// Enabled 是否启用
Enabled bool `json:"enabled" yaml:"enabled"`
// Threshold 风险阈值(超过则拒绝)
Threshold float64 `json:"threshold" yaml:"threshold"`
// Action 触发后的动作
Action GuardAction `json:"action" yaml:"action"`
// Categories 要检查的类别
Categories []string `json:"categories" yaml:"categories"`
// Allowlist 允许列表
Allowlist []string `json:"allowlist" yaml:"allowlist"`
// Blocklist 阻止列表
Blocklist []string `json:"blocklist" yaml:"blocklist"`
}
GuardConfig 守卫配置
type InputGuard ¶
type InputGuard interface {
Guard
// IsInputGuard 标记为输入守卫
IsInputGuard()
}
InputGuard 输入守卫(在 Agent 执行前检查)
type Middleware ¶
type Middleware func(ctx context.Context, input string, next func(context.Context, string) (string, error)) (string, error)
Middleware 守卫中间件
func ToMiddleware ¶
func ToMiddleware(g Guard, action GuardAction) Middleware
ToMiddleware 将守卫转换为中间件
type OutputGuard ¶
type OutputGuard interface {
Guard
// IsOutputGuard 标记为输出守卫
IsOutputGuard()
}
OutputGuard 输出守卫(在 Agent 执行后检查)
type PIIGuard ¶
type PIIGuard struct {
// contains filtered or unexported fields
}
PIIGuard PII 检测守卫
type PromptInjectionGuard ¶
type PromptInjectionGuard struct {
// contains filtered or unexported fields
}
PromptInjectionGuard Prompt 注入检测守卫
func NewPromptInjectionGuard ¶
func NewPromptInjectionGuard(opts ...PromptInjectionOption) *PromptInjectionGuard
NewPromptInjectionGuard 创建 Prompt 注入守卫
func (*PromptInjectionGuard) Check ¶
func (g *PromptInjectionGuard) Check(ctx context.Context, input string) (*CheckResult, error)
Check 执行检查
func (*PromptInjectionGuard) IsInputGuard ¶
func (g *PromptInjectionGuard) IsInputGuard()
IsInputGuard 标记为输入守卫
type PromptInjectionOption ¶
type PromptInjectionOption func(*PromptInjectionGuard)
PromptInjectionOption 配置选项
func WithCustomPatterns ¶
func WithCustomPatterns(patterns map[string]string) PromptInjectionOption
WithCustomPatterns 添加自定义模式
func WithInjectionConfig ¶
func WithInjectionConfig(cfg *GuardConfig) PromptInjectionOption
WithInjectionConfig 设置配置