173 lines
5.4 KiB
Go
173 lines
5.4 KiB
Go
package audit
|
|
|
|
import (
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// Where flagged text already appeared in the conversation before the response.
|
|
// An empty provenance means it appeared nowhere in the context window, so the
|
|
// model produced it itself.
|
|
const (
|
|
// ProvenanceUser is text the user typed: the action was asked for.
|
|
ProvenanceUser = "user"
|
|
// ProvenanceOperator is text in the system prompt.
|
|
ProvenanceOperator = "operator"
|
|
// ProvenanceRead is text that arrived in content the agent read: a tool result,
|
|
// a file, a page, or text the harness injected into a user turn. A dangerous
|
|
// action whose text came from here is the shape of an indirect injection.
|
|
ProvenanceRead = "read"
|
|
)
|
|
|
|
// DefaultContextMessages is how many trailing request messages a finding is traced
|
|
// against when the auditor is not told otherwise.
|
|
const DefaultContextMessages = 20
|
|
|
|
// minTraceableQuote keeps short, common fragments from being traced to a message
|
|
// that merely contains the same few characters.
|
|
const minTraceableQuote = 8
|
|
|
|
var systemReminderRe = regexp.MustCompile(`(?s)<system-reminder>.*?</system-reminder>`)
|
|
|
|
type contextSource struct {
|
|
provenance string
|
|
locator string
|
|
text string
|
|
}
|
|
|
|
// contextWindow is the last n request messages, split by who authored them.
|
|
// Assistant messages are left out: the model repeating itself is not a source.
|
|
func (b *Bundle) contextWindow(n int) []contextSource {
|
|
messages := b.Request.Messages
|
|
start := 0
|
|
if n >= 0 && len(messages) > n {
|
|
start = len(messages) - n
|
|
}
|
|
var out []contextSource
|
|
for i := start; i < len(messages); i++ {
|
|
m := messages[i]
|
|
locator := locatorf("request.messages[%d].content", i)
|
|
switch m.Role {
|
|
case "user":
|
|
out = append(out, contextSource{ProvenanceUser, locator, systemReminderRe.ReplaceAllString(m.Content, " ")})
|
|
for _, injected := range systemReminderRe.FindAllString(m.Content, -1) {
|
|
out = append(out, contextSource{ProvenanceRead, locator, injected})
|
|
}
|
|
case "tool", "function":
|
|
out = append(out, contextSource{ProvenanceRead, locator, m.Content})
|
|
case "system", "developer":
|
|
out = append(out, contextSource{ProvenanceOperator, locator, m.Content})
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func normaliseForTrace(s string) string {
|
|
s = strings.Trim(strings.TrimSpace(s), "`*\"'")
|
|
return strings.ToLower(strings.Join(strings.Fields(s), " "))
|
|
}
|
|
|
|
// traceText finds the first source in the window containing quote.
|
|
func traceText(window []contextSource, quote string) (contextSource, bool) {
|
|
needle := normaliseForTrace(quote)
|
|
if len(needle) < minTraceableQuote {
|
|
return contextSource{}, false
|
|
}
|
|
for _, src := range window {
|
|
if strings.Contains(normaliseForTrace(src.text), needle) {
|
|
return src, true
|
|
}
|
|
}
|
|
return contextSource{}, false
|
|
}
|
|
|
|
// traceSecret finds the source holding the same credential. Evidence carries only
|
|
// the masked form, so the window is scanned with the same detectors and compared
|
|
// masked to masked.
|
|
func traceSecret(window []contextSource, masked string) (contextSource, bool) {
|
|
for _, src := range window {
|
|
for _, re := range secretRedactors() {
|
|
for _, raw := range re.FindAllString(src.text, -1) {
|
|
if maskSecret(raw) == masked {
|
|
return src, true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return contextSource{}, false
|
|
}
|
|
|
|
var requestLocatorRe = regexp.MustCompile(`^request\.messages\[(\d+)\]`)
|
|
|
|
// provenanceRank orders provenances by how much a finding should worry a reader,
|
|
// so a finding with several pieces of evidence reports the most worrying one.
|
|
func provenanceRank(p string) int {
|
|
switch p {
|
|
case ProvenanceRead:
|
|
return 3
|
|
case "":
|
|
return 2
|
|
case ProvenanceOperator:
|
|
return 1
|
|
default:
|
|
return 0
|
|
}
|
|
}
|
|
|
|
// traceFinding sets where the finding's evidence came from. Evidence in the
|
|
// request is attributed to the author of its message. Evidence in the response is
|
|
// traced against the context window; untraced evidence leaves the provenance
|
|
// empty, and the most worrying provenance among the evidence wins.
|
|
func (b *Bundle) traceFinding(f *Finding, window []contextSource, secret bool) {
|
|
traced := false
|
|
worst := ProvenanceUser
|
|
at := ""
|
|
for _, ev := range f.Evidence {
|
|
var src contextSource
|
|
var ok bool
|
|
if m := requestLocatorRe.FindStringSubmatch(ev.Locator); m != nil {
|
|
src, ok = b.messageSource(m[1], ev.Quote)
|
|
} else if secret {
|
|
src, ok = traceSecret(window, ev.Quote)
|
|
} else {
|
|
src, ok = traceText(window, ev.Quote)
|
|
}
|
|
p := ""
|
|
if ok {
|
|
p = src.provenance
|
|
}
|
|
if !traced || provenanceRank(p) > provenanceRank(worst) {
|
|
worst, at = p, src.locator
|
|
}
|
|
traced = true
|
|
}
|
|
if traced {
|
|
f.Provenance, f.ProvenanceAt = worst, at
|
|
}
|
|
}
|
|
|
|
// messageSource attributes request-side evidence to its message's author. Text
|
|
// inside a system-reminder block was injected by the harness, not typed.
|
|
func (b *Bundle) messageSource(index, quote string) (contextSource, bool) {
|
|
i, err := strconv.Atoi(index)
|
|
if err != nil || i < 0 || i >= len(b.Request.Messages) {
|
|
return contextSource{}, false
|
|
}
|
|
m := b.Request.Messages[i]
|
|
locator := locatorf("request.messages[%d].content", i)
|
|
switch m.Role {
|
|
case "user":
|
|
for _, injected := range systemReminderRe.FindAllString(m.Content, -1) {
|
|
if quote != "" && strings.Contains(injected, quote) {
|
|
return contextSource{ProvenanceRead, locator, ""}, true
|
|
}
|
|
}
|
|
return contextSource{ProvenanceUser, locator, ""}, true
|
|
case "system", "developer":
|
|
return contextSource{ProvenanceOperator, locator, ""}, true
|
|
default:
|
|
return contextSource{ProvenanceRead, locator, ""}, true
|
|
}
|
|
}
|