A request's redacted values are put back in its answer
CI / test (push) Successful in 8m49s

With restore: true on a guardrail policy's input, what redact rewrote as
placeholders comes back in the answer: the model reads "[EMAIL_1]", the
client reads the address it sent. A streamed placeholder split across
events is held until it is whole, and what is held is sent at the end.
The swap runs before the output filter, so the output rules judge the
answer as the client reads it and a placeholder they make of a value the
model invented is never taken for the request's own. The map lives in the
Verdict for the request only; nothing stores, logs or traces it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5.5 committed 2026-10-25 16:41:53 +01:00
1 parent d110968243
commit 81685ecd75
12 files changed
+383 -1

No files matched your search

+12
View File
@@ -307,6 +307,15 @@ func (p *placeholders) rewrite(text string, spans []detect.Span) string {
return b.String()
}
// inverse is each placeholder and the value it replaced.
func (p *placeholders) inverse() map[string]string {
out := make(map[string]string, len(p.byValue))
for v, ph := range p.byValue {
out[ph] = v
}
return out
}
var _ Guard = (*Gate)(nil)
// Directives are the policy in force's demands of the gateway. Not in
@@ -432,6 +441,9 @@ func (g *Gate) Input(ctx context.Context, s Subject, body []byte) (Verdict, erro
rw.seg.set(rw.text)
changed = true
}
if p.Input.Restore && len(rewrites) > 0 {
v.Restore = ph.inverse()
}
if d.pin(p.SystemPrompt, p.DropClientSystem) {
changed = true
}
+24
View File
@@ -309,3 +309,27 @@ func TestAJudgesRefusalNamesTheHazard(t *testing.T) {
t.Errorf("%q", got)
}
}
func TestRestoreSaysWhichPlaceholderWasWhichOnlyWhenAsked(t *testing.T) {
body := []byte(`{"messages":[{"role":"user","content":"mail ayse@example.com and ayse@example.com"}]}`)
for _, c := range []struct {
mode string
restore bool
want int
}{{"enforce", true, 1}, {"enforce", false, 0}, {"shadow", true, 0}} {
p := Policy{Mode: c.mode, Input: Rules{PII: map[string]string{"email": "redact"}, Restore: c.restore}}
g := gate(t, lib(map[string]Policy{"pii": p}, "pii"))
v, err := g.Input(context.Background(), Subject{}, body)
if err != nil {
t.Fatal(err)
}
if len(v.Restore) != c.want {
t.Errorf("%s restore=%v: %v", c.mode, c.restore, v.Restore)
}
for ph, val := range v.Restore {
if val != "ayse@example.com" || !strings.HasPrefix(ph, "[EMAIL_") {
t.Errorf("%s -> %s", ph, val)
}
}
}
}
+4
View File
@@ -75,6 +75,10 @@ type Verdict struct {
// Shadow says the policy only records: the action is what it would have
// done, and nothing is done.
Shadow bool
// Restore maps each placeholder the input was given to the value it
// stands for, when the policy puts them back in the answer. Held for
// the request's life only: never stored, logged or traced.
Restore map[string]string
}
// Stricter reports whether v outranks w: block over redact over flag over
+7
View File
@@ -86,6 +86,10 @@ type Rules struct {
Deny []Pattern `koanf:"deny"`
// MaxChars refuses text longer than this; zero is no limit.
MaxChars int `koanf:"max_chars"`
// Restore, on input, puts what redact rewrote as placeholders back into
// the answer: the model reads "[EMAIL_1]", the client reads its own
// address. Only the values that request sent, only into its answer.
Restore bool `koanf:"restore"`
}
// Pattern is a named regular expression and what its match does.
@@ -253,6 +257,9 @@ func mergeRules(dst *Rules, r Rules) {
}
}
dst.MaxChars = minSet(dst.MaxChars, r.MaxChars)
// Any policy may ask: what is put back is the caller's own, to the
// caller, and never reaches the model either way.
dst.Restore = dst.Restore || r.Restore
}
func stricterOf(a, b string, order []string) string {