feat(dispatcher): 输出护栏 —— 发射层脱敏疑似密钥/令牌
补齐 Harness 输出侧:harness.RedactSecrets 识别并脱敏 sk-/AKIA/JWT/Bearer 等 疑似密钥令牌(纯逻辑 + 单测)。runAgent 在每个 token 分片发射前调用(流式无法回收 已发,故逐片脱敏),脱敏会累计进 b.answer(写回历史也是脱敏版);有命中则在 运行·观测打一条'已脱敏 N 处'轨迹。 注:跨分片的密钥可能漏(流式现实),逐片为最佳努力;生产可加滑窗缓冲增强。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
package harness
|
||||
|
||||
import "regexp"
|
||||
|
||||
// secretPatterns 是输出里疑似密钥/令牌的特征(命中即脱敏,防模型把密钥回吐给用户)。
|
||||
var secretPatterns = []*regexp.Regexp{
|
||||
regexp.MustCompile(`sk-[A-Za-z0-9_-]{16,}`), // OpenAI/DeepSeek 风格 key
|
||||
regexp.MustCompile(`AKIA[0-9A-Z]{16}`), // AWS Access Key ID
|
||||
regexp.MustCompile(`eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{4,}`), // JWT
|
||||
regexp.MustCompile(`(?i)bearer\s+[A-Za-z0-9._-]{16,}`), // Bearer 令牌
|
||||
}
|
||||
|
||||
const redactMark = "[已脱敏]"
|
||||
|
||||
// RedactSecrets 把文本里疑似密钥/令牌替换为脱敏标记,返回脱敏后文本与命中次数。
|
||||
// 输出护栏:在 token 发射前对每个分片调用(流式无法回收已发,故逐片脱敏 + 最终标记)。
|
||||
func RedactSecrets(s string) (string, int) {
|
||||
n := 0
|
||||
for _, re := range secretPatterns {
|
||||
s = re.ReplaceAllStringFunc(s, func(string) string {
|
||||
n++
|
||||
return redactMark
|
||||
})
|
||||
}
|
||||
return s, n
|
||||
}
|
||||
Reference in New Issue
Block a user