Documentation
¶
Index ¶
- Variables
- func AddCustomPattern(pattern PIIPattern)
- type AdaptiveStrategy
- type CompositePIIDetector
- type ConditionalPIIConfig
- type ConditionalPIIMiddleware
- type HashStrategy
- type MaskStrategy
- type NoOpStrategy
- type PIIContext
- type PIIDetectionResult
- type PIIDetectionSummary
- type PIIDetector
- type PIIMatch
- type PIIMiddlewareConfig
- type PIIPattern
- type PIIRedactionMiddleware
- func (m *PIIRedactionMiddleware) ClearTracking(agentID string)
- func (m *PIIRedactionMiddleware) GetPIIMatches(agentID string) []PIIMatch
- func (m *PIIRedactionMiddleware) GetPIISummary(agentID string) *PIIDetectionSummary
- func (m *PIIRedactionMiddleware) OnAgentStop(ctx context.Context, agentID string) error
- func (m *PIIRedactionMiddleware) WrapModelCall(ctx context.Context, req *middleware.ModelRequest, ...) (*middleware.ModelResponse, error)
- type PIISensitivityLevel
- type PIIType
- type RedactionReport
- type RedactionStrategy
- type Redactor
- type RegexPIIDetector
- type ReplaceStrategy
Constants ¶
This section is empty.
Variables ¶
var PIIPatternRegistry = []PIIPattern{ { Type: PIIEmail, Description: "Email address", Regex: regexp.MustCompile(`\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b`), }, { Type: PIIChinesePhone, Description: "Chinese mobile phone number", Regex: regexp.MustCompile(`\b1[3-9]\d{9}\b`), Validator: validateChinesePhone, }, { Type: PIIPhone, Description: "US phone number", Regex: regexp.MustCompile(`\b(?:\+?1[-.\s]?)?\(?([0-9]{3})\)?[-.\s]?([0-9]{3})[-.\s]?([0-9]{4})\b`), }, { Type: PIICreditCard, Description: "Credit card number", Regex: regexp.MustCompile(`\b(?:4[0-9]{3}[-\s]?[0-9]{4}[-\s]?[0-9]{4}[-\s]?[0-9]{4}|5[1-5][0-9]{2}[-\s]?[0-9]{4}[-\s]?[0-9]{4}[-\s]?[0-9]{4}|3[47][0-9]{2}[-\s]?[0-9]{6}[-\s]?[0-9]{5})\b`), Validator: validateLuhn, }, { Type: PIISSNus, Description: "US Social Security Number", Regex: regexp.MustCompile(`\b\d{3}-?\d{2}-?\d{4}\b`), Validator: validateSSN, }, { Type: PIIChineseID, Description: "Chinese ID card number", Regex: regexp.MustCompile(`\b[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[0-9Xx]\b`), Validator: validateChineseID, }, { Type: PIIIPAddress, Description: "IPv4 address", Regex: regexp.MustCompile(`\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b`), }, { Type: PIIPassport, Description: "Passport number (generic)", Regex: regexp.MustCompile(`\b[A-Z]{1,2}[0-9]{6,9}\b`), }, { Type: PIIDateOfBirth, Description: "Date (potential date of birth)", Regex: regexp.MustCompile(`\b(19|20)\d{2}[-/](0[1-9]|1[0-2])[-/](0[1-9]|[12]\d|3[01])\b`), }, }
PIIPatternRegistry PII 模式注册表。
Functions ¶
Types ¶
type AdaptiveStrategy ¶
type AdaptiveStrategy struct {
LowStrategy RedactionStrategy
MediumStrategy RedactionStrategy
HighStrategy RedactionStrategy
}
AdaptiveStrategy 自适应策略(根据敏感度选择策略)。
func NewAdaptiveStrategy ¶
func NewAdaptiveStrategy() *AdaptiveStrategy
NewAdaptiveStrategy 创建自适应策略。
func (*AdaptiveStrategy) Redact ¶
func (s *AdaptiveStrategy) Redact(match PIIMatch) string
Redact 根据敏感度选择策略。
type CompositePIIDetector ¶
type CompositePIIDetector struct {
// contains filtered or unexported fields
}
CompositePIIDetector 组合多个检测器。
func NewCompositePIIDetector ¶
func NewCompositePIIDetector(detectors ...PIIDetector) *CompositePIIDetector
NewCompositePIIDetector 创建组合检测器。
func (*CompositePIIDetector) ContainsPII ¶
ContainsPII 检查是否包含 PII。
func (*CompositePIIDetector) DetectTypes ¶
func (d *CompositePIIDetector) DetectTypes(ctx context.Context, text string, types ...PIIType) ([]PIIMatch, error)
DetectTypes 检测指定类型的 PII。
type ConditionalPIIConfig ¶
type ConditionalPIIConfig struct {
Detector PIIDetector
Strategy RedactionStrategy
Condition func(context.Context, *middleware.ModelRequest) bool // 判断是否需要脱敏
Priority int
}
ConditionalPIIConfig 条件 PII 中间件配置。
type ConditionalPIIMiddleware ¶
type ConditionalPIIMiddleware struct {
*middleware.BaseMiddleware
// contains filtered or unexported fields
}
ConditionalPIIMiddleware 条件 PII 脱敏中间件。 根据上下文条件决定是否脱敏。
func NewConditionalPIIMiddleware ¶
func NewConditionalPIIMiddleware(cfg ConditionalPIIConfig) *ConditionalPIIMiddleware
NewConditionalPIIMiddleware 创建条件 PII 中间件。
func (*ConditionalPIIMiddleware) WrapModelCall ¶
func (m *ConditionalPIIMiddleware) WrapModelCall(ctx context.Context, req *middleware.ModelRequest, handler middleware.ModelCallHandler) (*middleware.ModelResponse, error)
WrapModelCall 包装模型调用。
type HashStrategy ¶
type HashStrategy struct {
ShowPrefix bool // 是否显示哈希前缀
PrefixLength int // 哈希前缀长度
Salt string // 盐值(用于增强安全性)
}
HashStrategy 哈希策略(单向加密)。 例如:13812345678 -> [HASH:a3f5...]
type MaskStrategy ¶
type MaskStrategy struct {
MaskChar rune // 掩码字符(默认 '*')
KeepPrefix int // 保留前缀长度
KeepSuffix int // 保留后缀长度
MinMaskLength int // 最小掩码长度
}
MaskStrategy 掩码策略(部分掩码)。 例如:13812345678 -> 138****5678
type PIIContext ¶
type PIIContext struct {
// Language 文本语言(zh/en等)
Language string
// AllowedTypes 允许的 PII 类型(白名单)
AllowedTypes []PIIType
// IgnorePatterns 忽略的模式(如公司邮箱域名)
IgnorePatterns []string
// MinConfidence 最低置信度阈值
MinConfidence float64
}
PIIContext PII 的上下文信息(用于更好的检测)。
type PIIDetectionResult ¶
type PIIDetectionResult struct {
Matches []PIIMatch
HasPII bool
PIITypes []PIIType
HighestRisk PIISensitivityLevel
TotalMatches int
}
PIIDetectionResult 检测结果汇总。
func AnalyzePII ¶
func AnalyzePII(ctx context.Context, text string, detector PIIDetector) (*PIIDetectionResult, error)
AnalyzePII 分析文本中的 PII 并返回详细报告。
type PIIDetectionSummary ¶
type PIIDetectionSummary struct {
AgentID string
HasPII bool
TotalMatches int
TypeCounts map[PIIType]int
HighestRisk PIISensitivityLevel
}
PIIDetectionSummary PII 检测摘要。
type PIIDetector ¶
type PIIDetector interface {
// Detect 检测文本中的所有 PII。
Detect(ctx context.Context, text string) ([]PIIMatch, error)
// DetectTypes 检测指定类型的 PII。
DetectTypes(ctx context.Context, text string, types ...PIIType) ([]PIIMatch, error)
// ContainsPII 快速检查文本是否包含 PII。
ContainsPII(ctx context.Context, text string) (bool, error)
}
PIIDetector PII 检测器接口。
type PIIMatch ¶
type PIIMatch struct {
Type PIIType // PII 类型
Value string // 原始值
Start int // 起始位置
End int // 结束位置
Confidence float64 // 置信度(0.0-1.0)
Severity PIISensitivityLevel // 敏感度级别
}
PIIMatch 表示一个 PII 匹配结果。
func FilterMatchesByContext ¶
func FilterMatchesByContext(matches []PIIMatch, ctx *PIIContext) []PIIMatch
FilterMatchesByContext 根据上下文过滤匹配结果。
type PIIMiddlewareConfig ¶
type PIIMiddlewareConfig struct {
Detector PIIDetector // PII 检测器
Strategy RedactionStrategy // 脱敏策略
EnableTracking bool // 是否启用 PII 追踪
Priority int // 中间件优先级(默认 200)
}
PIIMiddlewareConfig PII 中间件配置。
type PIIPattern ¶
type PIIPattern struct {
Type PIIType
Description string
Regex *regexp.Regexp
Validator func(string) bool // 可选的额外验证函数
}
PIIPattern 定义一个 PII 检测模式。
func GetPatternsByType ¶
func GetPatternsByType(types ...PIIType) []PIIPattern
GetPatternsByType 按类型获取 PII 模式。
type PIIRedactionMiddleware ¶
type PIIRedactionMiddleware struct {
*middleware.BaseMiddleware
// contains filtered or unexported fields
}
PIIRedactionMiddleware PII 脱敏中间件。 在消息发送到 LLM 前自动检测和脱敏 PII。
func NewDefaultPIIMiddleware ¶
func NewDefaultPIIMiddleware() *PIIRedactionMiddleware
NewDefaultPIIMiddleware 创建默认配置的 PII 中间件。
func NewPIIRedactionMiddleware ¶
func NewPIIRedactionMiddleware(cfg PIIMiddlewareConfig) *PIIRedactionMiddleware
NewPIIRedactionMiddleware 创建 PII 脱敏中间件。
func (*PIIRedactionMiddleware) ClearTracking ¶
func (m *PIIRedactionMiddleware) ClearTracking(agentID string)
ClearTracking 清除 Agent 的追踪信息。
func (*PIIRedactionMiddleware) GetPIIMatches ¶
func (m *PIIRedactionMiddleware) GetPIIMatches(agentID string) []PIIMatch
GetPIIMatches 获取 Agent 的 PII 匹配记录。
func (*PIIRedactionMiddleware) GetPIISummary ¶
func (m *PIIRedactionMiddleware) GetPIISummary(agentID string) *PIIDetectionSummary
GetPIISummary 获取 PII 检测摘要。
func (*PIIRedactionMiddleware) OnAgentStop ¶
func (m *PIIRedactionMiddleware) OnAgentStop(ctx context.Context, agentID string) error
OnAgentStop 在 Agent 停止时清除追踪信息。
func (*PIIRedactionMiddleware) WrapModelCall ¶
func (m *PIIRedactionMiddleware) WrapModelCall(ctx context.Context, req *middleware.ModelRequest, handler middleware.ModelCallHandler) (*middleware.ModelResponse, error)
WrapModelCall 包装模型调用,在发送前脱敏 PII。
type PIISensitivityLevel ¶
type PIISensitivityLevel int
PIISensitivityLevel PII 敏感度级别。
const ( SensitivityLow PIISensitivityLevel = 1 // 低敏感(如邮箱) SensitivityMedium PIISensitivityLevel = 2 // 中等敏感(如电话号码) SensitivityHigh PIISensitivityLevel = 3 // 高敏感(如身份证、信用卡) )
func GetSensitivityLevel ¶
func GetSensitivityLevel(piiType PIIType) PIISensitivityLevel
GetSensitivityLevel 返回 PII 类型的敏感度级别。
type PIIType ¶
type PIIType string
PIIType 定义 PII 的类型。
const ( PIIEmail PIIType = "email" PIIPhone PIIType = "phone" PIICreditCard PIIType = "credit_card" PIISSNus PIIType = "ssn_us" // 美国社会安全号 PIIChineseID PIIType = "chinese_id" // 中国身份证 PIIChinesePhone PIIType = "chinese_phone" // 中国手机号 PIIIPAddress PIIType = "ip_address" PIIPassport PIIType = "passport" PIIBankAccount PIIType = "bank_account" PIIDateOfBirth PIIType = "date_of_birth" PIIAddress PIIType = "address" PIIName PIIType = "name" // 需要 LLM 检测 PIICustom PIIType = "custom" )
type RedactionReport ¶
type RedactionReport struct {
OriginalLength int // 原始文本长度
RedactedLength int // 脱敏后文本长度
TotalMatches int // 总匹配数
RedactedCharacters int // 脱敏字符数
MatchesByType map[PIIType]int // 每种类型的匹配数
}
RedactionReport 脱敏报告。
type RedactionStrategy ¶
type RedactionStrategy interface {
// Redact 脱敏单个 PII 值。
Redact(match PIIMatch) string
// Name 返回策略名称。
Name() string
}
RedactionStrategy 脱敏策略接口。
type Redactor ¶
type Redactor struct {
// contains filtered or unexported fields
}
Redactor PII 脱敏器。
func NewRedactor ¶
func NewRedactor(detector PIIDetector, strategy RedactionStrategy) *Redactor
NewRedactor 创建脱敏器。
func (*Redactor) RedactWithReport ¶
func (r *Redactor) RedactWithReport(ctx context.Context, text string) (string, *RedactionReport, error)
RedactWithReport 脱敏文本并返回详细报告。
type RegexPIIDetector ¶
type RegexPIIDetector struct {
// contains filtered or unexported fields
}
RegexPIIDetector 基于正则表达式的 PII 检测器。
func NewRegexPIIDetector ¶
func NewRegexPIIDetector() *RegexPIIDetector
NewRegexPIIDetector 创建正则表达式 PII 检测器。
func NewRegexPIIDetectorWithTypes ¶
func NewRegexPIIDetectorWithTypes(types ...PIIType) *RegexPIIDetector
NewRegexPIIDetectorWithTypes 创建检测特定类型的 PII 检测器。
func (*RegexPIIDetector) ContainsPII ¶
ContainsPII 快速检查文本是否包含 PII。
func (*RegexPIIDetector) DetectTypes ¶
func (d *RegexPIIDetector) DetectTypes(ctx context.Context, text string, types ...PIIType) ([]PIIMatch, error)
DetectTypes 检测指定类型的 PII。
type ReplaceStrategy ¶
type ReplaceStrategy struct {
UseTypeLabel bool // 是否使用类型标签(如 [PHONE])
CustomLabels map[PIIType]string // 自定义标签
}
ReplaceStrategy 替换策略(替换为占位符)。 例如:13812345678 -> [PHONE]
func (*ReplaceStrategy) Redact ¶
func (s *ReplaceStrategy) Redact(match PIIMatch) string
Redact 执行替换脱敏。