guard

package
v0.5.12 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 13, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func RedactPII

func RedactPII(text string) string

RedactPII 脱敏文本中的所有 PII

func RedactPIISelective

func RedactPIISelective(text string, types ...string) string

RedactPIISelective 选择性脱敏 只脱敏指定类型的 PII

Types

type ChainMode

type ChainMode int

ChainMode 链模式

const (
	// ChainModeAll 所有守卫都必须通过
	ChainModeAll ChainMode = iota
	// ChainModeAny 任一守卫通过即可
	ChainModeAny
	// ChainModeFirst 第一个失败就停止
	ChainModeFirst
)

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 DetectPII

func DetectPII(text string) []Finding

DetectPII 检测文本中的 PII 如果检测过程出错,返回空列表

func DetectPIIWithError

func DetectPIIWithError(text string) ([]Finding, error)

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) Add

func (c *GuardChain) Add(g Guard)

Add 添加守卫

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)。

func (*GuardChain) Name

func (c *GuardChain) Name() string

Name 返回名称

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 守卫配置

func DefaultConfig

func DefaultConfig() *GuardConfig

DefaultConfig 默认配置

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 检测守卫

func NewPIIGuard

func NewPIIGuard(opts ...PIIOption) *PIIGuard

NewPIIGuard 创建 PII 守卫

func (*PIIGuard) Check

func (g *PIIGuard) Check(ctx context.Context, input string) (*CheckResult, error)

Check 执行检查

func (*PIIGuard) Enabled

func (g *PIIGuard) Enabled() bool

Enabled 返回是否启用

func (*PIIGuard) IsInputGuard

func (g *PIIGuard) IsInputGuard()

IsInputGuard 标记为输入守卫

func (*PIIGuard) Name

func (g *PIIGuard) Name() string

Name 返回名称

func (*PIIGuard) Redact

func (g *PIIGuard) Redact(input string) string

Redact 脱敏处理

采用"先收集互斥区间、再一次性替换"的方式,替代过去对每个模式独立做 ReplaceAllStringFunc 的多遍替换。多遍替换会因模式相互遮蔽(如 phone_cn 先把身份证中间切碎)导致输出错乱(回归: B9/B10)。 卡号类模式的 Luhn 校验在区间收集阶段完成,与 Check 共用同一判定, 确保"被判为 PII 的串脱敏后绝不原样残留"(回归: B11)。

type PIIOption

type PIIOption func(*PIIGuard)

PIIOption 配置选项

func WithPIIConfig

func WithPIIConfig(cfg *GuardConfig) PIIOption

WithPIIConfig 设置配置

type Position

type Position struct {
	Start int `json:"start"`
	End   int `json:"end"`
}

Position 文本位置

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) Enabled

func (g *PromptInjectionGuard) Enabled() bool

Enabled 返回是否启用

func (*PromptInjectionGuard) IsInputGuard

func (g *PromptInjectionGuard) IsInputGuard()

IsInputGuard 标记为输入守卫

func (*PromptInjectionGuard) Name

func (g *PromptInjectionGuard) Name() string

Name 返回名称

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 设置配置

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL