Files
magpie/internal/gateway/redact.go
T
yetone 88ce2cdd4c gateway: a web search's query is masked before it goes to a search API, Kimi Code's or WorkBuddy's search (lc on Discord)
lc on Discord: "mcp 搜索出去的脱敏了吗?"

A searcher model is asked through serve, which masks its request, and the
image and vision MCPs go through the masked routes too. But the search
APIs (Tavily, Brave, Exa, Firecrawl, SearXNG, ...), a Kimi Code plan's
search service and WorkBuddy's search were sent the query as it came. A
model's own web_search writes its query from a request already masked, but
an agent's call to the search MCP carries the query as the agent's model
wrote it, with the values put back, so a secret or phone number in it went
out as written.

maskedQuery masks the query, as Settings say, where it leaves magpie for
each of those three (a plan the user set to go unmasked, SkipsRedaction,
keeps it), and searchMCPRun puts the values back in what it returns to
the agent. Scoped by where the value goes (the query field of a search
service's request), not by a key's name.

Run: TestSearchQueryMasked; each of the four changes removed alone turns it
red (search API, Kimi, WorkBuddy asked the phone number; the agent got the
placeholder back). -run 'Search|Redact|Kimi|WorkBuddy|MCP' over
internal/gateway (sandbox HOME); gofmt -l; go vet darwin, GOOS=linux
-tags nogui, GOOS=windows. docs/subsystems/gateway-routing.md says it.
Not tried: a real search API or a real agent's MCP call.
2026-10-14 14:47:36 +08:00

86 lines
2.8 KiB
Go

package gateway
import (
"net/http"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/redact"
"github.com/yetone/magpie/internal/settings"
)
// redacted masks a request's secrets, as the settings say, before it goes
// to a vendor, and wraps w so what the vendor answers has them back; done
// writes what the wrapper still holds. Nothing masked, nothing wrapped.
func redacted(w http.ResponseWriter, body []byte) (http.ResponseWriter, []byte, func()) {
o := redactionOptions()
if !o.Secrets && !o.Personal && len(o.Words) == 0 {
return w, body, func() {}
}
masked, n := redact.MaskJSON(body, o)
if n == 0 {
return w, body, func() {}
}
rw := redact.NewWriter(w)
return rw, masked, rw.Finish
}
// redactedPrompt masks a parsed image or video prompt before any vendor
// body is built, including multipart forms, and restores the reply's text.
func redactedPrompt(w http.ResponseWriter, prompt string) (http.ResponseWriter, string, func()) {
masked, n := redact.Mask(prompt, redactionOptions())
if n == 0 {
return w, prompt, func() {}
}
rw := redact.NewWriter(w)
return rw, masked, rw.Finish
}
func redactionOptions() redact.Options {
st := settings.Load()
return redact.Options{Secrets: st.Redact, Personal: st.RedactPersonal, Kinds: st.RedactKinds, Words: st.RedactWords, Rules: st.RedactRules}
}
// maskedQuery masks a web search's query, as the settings say, before it
// goes to a search service that isn't a model's endpoint: a search API, or
// a Kimi Code plan's or WorkBuddy's search (to p, nil for an API; one the
// user set to go unmasked keeps it as it is). A model's web_search writes
// its query from a request already masked, but an agent's call to the
// search MCP carries it as the agent's model wrote it, values put back
// (lc on Discord).
func maskedQuery(query string, p *provider.Provider) string {
if p != nil && p.SkipsRedaction() {
return query
}
masked, _ := redact.Mask(query, redactionOptions())
return masked
}
// unredactedRoute says a request resolved to p, or to the group whose
// models are ms, goes only where the user set requests to go unmasked
// (provider.SkipsRedaction): p and each of its fallbacks, or every model of
// the group (a member's own fallbacks are not the group's, see planGroup).
// One that may go on to anyone else stays masked for all of them.
func unredactedRoute(p provider.Provider, isGroup bool, ms []provider.Member) bool {
if isGroup {
if len(ms) == 0 {
return false
}
for _, m := range ms {
if !m.Provider.SkipsRedaction() {
return false
}
}
return true
}
if !p.SkipsRedaction() {
return false
}
for _, id := range p.Fallback {
// one that resolves to nothing isn't tried (plan)
if fp, _, ok := provider.Resolve(id); ok && !fp.SkipsRedaction() {
return false
}
}
return true
}