What changed (one per gap the reporter reproduced on main): - API_KEY gains the ark- value shape (#2946): Volcano Ark and the relays in front of it issue keys with an ark- prefix, and no value rule knew one. ARK_API_KEY=<ark key> was masked already (its name has api_key in it); ARK_KEY=<ark key>, a key in prose and one in a request body went as they were. - A new SECRET rule takes an upper-case env var whose name only ends in KEY or PASS: SERVICE_X_KEY=…, FOO_KEY=…, DB_PASS=… — quoted or not. Upper case is the boundary: in the lower case a name that ends in key is code's (a sort_key:, a cache_key:, a public key =), so nothing of TestMaskGatewayKey's rows moves; notAPlainKey turns the upper-case SORT_KEY/PUBLIC_KEY/PROMPT_CACHE_KEY/CACHE_KEY away too. - A new SECRET rule takes the value of an Authorization: or Cookie: header line, whatever it looks like — the opaque bearer tokens and session values the shape rules had no eye for (#2946). - MaskJSON masks the value of a field named for a secret by its name, before the ids and names that would keep it: {"api_key": "…"} with a value no rule knows, {"db_pass": "…"}, an AKIA under access_key_id (its _id kept it from the value's own AKIA rule), {"Authorization": "Bearer …"} and {"Cookie": "…"} (#2946). signature, encrypted_content and data: values still go as written (#1911), and the tools' definitions are untouched. SecretName now says a name ending in pass is a secret's name too, so the archive (ScrubJSON) and MaskJSON judge a field by the same names. Why: the reporter's five examples each leak a credential whose value has no key's shape; the rules judged values by shape alone and field names only ever kept a value. The boundary the reporter warned about — sort_key, cache_key, prompt_cache_key, public_key must stay unmasked — is held by case (lower-case names never enter the new rules) and by notAPlainKey for the upper-case ones. Run (rebased onto9ae79122, upstream/main 10-27): the 18 upstream redact commits since9b0f4861(714ab8e8CJK password rule ..25341b4caddress narrowing) add CJK-named secret rules, made-up key forms and narrow emails and addresses — none covers the five families here, so the rules take them side by side; the shared secretNames rule carries upstream's new notCode boundary (511c4a76). The _KEY/_PASS rule's boundary is notPlainKeyOrCode (plain key names and code naming a secret both stay), the same resolution 2ea09ec3 on codex/verify-2946-redact carried and the issue thread endorsed: Refs #2946 Review: the ark- key, _KEY/_PASS assignment and Authorization/Cookie rules land as written here. Two parts were narrowed in this batch's redact follow-up ("a secret's field name masks only in a tool's arguments"): a secret's JSON field name now masks only where the user's data sits (a tool call's arguments and a tool's result, with secrets on), never across the whole body by name, and an Authorization or Cookie line masks only when what follows has a credential's shape. None of the redact commits on main since 10-27 covered these five families. Landing record (yetone): Run at the pushed batch head (tree a6b084d0b, on origin/main616405061), go1.26.8: - go test -count=1 -timeout 45m on Linux (wsl-run) of internal/gateway, internal/redact, internal/provider and internal/plugin (this batch's packages and those main changed since the full run): all ok. gofmt -l clean; go vet ./... (darwin), GOOS=linux and GOOS=windows go vet -tags nogui ./... clean. internal/gui on macOS (mote-run): TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and TestMainWindowCorners (#3949), both red at main too. - The full go test ./... -timeout 45m -count=1 ran one rebase earlier, at tree 66a7a4c17 on origin/main3a3bdb9f2(main then gained 5bfa9f076's group stand-in,b19d08593,f856d381aand616405061). It ran on Linux (wsl-run): 82 packages ok. internal/agent has 4 reds, all red at origin/main on that box as well: TestInstalledVersion, TestGrokInstalled, TestOmpDetectedWithPiDir and TestCopilotFolderFromAnIDEPluginIsNotTheCLI (#4033, #3548). The root package and internal/cli need gtk4 to build, so they ran on macOS (mote-run): ok. TestColdTurnsTaken (#4510) was red there once. An earlier full run at based307aa648was red once in TestUnroutedHeldReportSaved (#4300), which passes 5 of 5 alone at both the head and main. - Each landed patch's diff matches the head ggbdpq posted last on its issue (patch-id). - No GUI file is touched, so no GUI test file was run. Broken by hand: gua's ablations, in the thread. The narrowing is broken by hand in that follow-up. Not tried: a real vendor request carrying these values through the gateway; the rules were tested in internal/redact only. Review: the ark- key, _KEY/_PASS assignment and Authorization/Cookie rules land as written here. Two parts were narrowed in this batch's redact follow-up ("a secret's field name masks only in a tool's arguments"): a secret's JSON field name now masks only where the user's data sits (a tool call's arguments and a tool's result, with secrets on), never across the whole body by name, and an Authorization or Cookie line masks only when what follows has a credential's shape. None of the redact commits on main since 10-27 covered these five families. Landing record (yetone): Run at tree 7d7955eb9 (this batch on origin/main3cfba2e37), go1.26.8: - go test -count=1 -timeout 45m on Linux (wsl-run) of internal/gateway, internal/redact, internal/provider, internal/agent and internal/usage (this batch's packages, plus those main changed since the full run): all ok except internal/agent's TestInstalledVersion, TestGrokInstalled and TestOmpDetectedWithPiDir, red at origin/main on that box too (#4033, #3548). gofmt -l clean; go vet ./... (darwin), GOOS=linux and GOOS=windows go vet -tags nogui ./... clean. On macOS (mote-run), the root package and internal/cli ok; internal/gui red in TestSettingsSaveKeepsWhatItDoesNotSend (#4514), TestMainWindowCorners (#3949) and TestColdTurnsTaken (#4510), the first two red at main too. - The full go test ./... -timeout 45m -count=1 ran two rebases earlier, at tree 66a7a4c17 on origin/main3a3bdb9f2, on Linux (wsl-run): 82 packages ok, internal/agent's 4 reds as above, plus TestCopilotFolderFromAnIDEPluginIsNotTheCLI (#4033, red at main). The root package, internal/cli and internal/gui need gtk4 to build, so they ran on macOS as above. An earlier full run at based307aa648was red once in TestUnroutedHeldReportSaved (#4300), which passes 5 of 5 alone at both the head and main. - Each landed patch's diff matches the head ggbdpq posted last on its issue (patch-id). - No GUI file is touched, so no GUI test file was run. - Rebased after these runs onto origin/mainc1457f2ee. Its two commits since3cfba2e37(3f2faef62gui,c1457f2eeagent) touch neither internal/gateway nor internal/redact, nor any file this batch changes, and were not run again here. Broken by hand: gua's ablations, in the thread. The narrowing is broken by hand in that follow-up. Not tried: a real vendor request carrying these values through the gateway; the rules were tested in internal/redact only. Generated-by: GLM-5.3-Flash (ZCode)
183 lines
5.3 KiB
Go
183 lines
5.3 KiB
Go
package redact
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"regexp"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
)
|
|
|
|
// Scrubbing is for what is kept rather than sent — the request archive: a
|
|
// secret goes for good, as [REDACTED:KIND], with no placeholder made and
|
|
// nothing held to put it back. Everything Mask finds as a secret goes,
|
|
// and whatever is in a field or header named for one (Authorization,
|
|
// x-api-key, Cookie, access_token, client_secret…), whatever it looks like.
|
|
|
|
// Scrubbed is what a field or header named for a secret holds once scrubbed.
|
|
const Scrubbed = "[REDACTED]"
|
|
|
|
func scrubbed(kind, _ string) string { return "[REDACTED:" + kind + "]" }
|
|
|
|
// secretName is a field's or header's name that says its value is a secret.
|
|
// A name that ends in pass (a db_pass) is one too (#2946); a name that only
|
|
// ends in key is not — a sort key, a cache key, a public key hold no secret.
|
|
var secretName = regexp.MustCompile(`(?i)` + secretNames.pattern() + `|authori[sz]ation|cookie|^key$|^pass$|_pass$`)
|
|
|
|
// SecretName says a field or header called name holds a secret: a token,
|
|
// key, password or cookie — not a count of tokens (max_tokens).
|
|
func SecretName(name string) bool {
|
|
n := strings.ToLower(name)
|
|
return secretName.MatchString(n) && !strings.HasSuffix(n, "tokens") && !strings.HasSuffix(n, "token_count")
|
|
}
|
|
|
|
// Scrub takes the secrets Mask finds out of s.
|
|
func Scrub(s string) string {
|
|
if len(s) < scrubMemoMin {
|
|
return scrub(s)
|
|
}
|
|
// What Scrub makes of s depends on s and on the values known (known.go)
|
|
// alone: a result is kept under the knownGen it was made at, and none
|
|
// made before a value became known is used again.
|
|
gen := knownGen.Load()
|
|
k := sha256.Sum256([]byte(s))
|
|
if out, ok := scrubMemo.get(k, gen, s); ok {
|
|
return out
|
|
}
|
|
out := scrub(s)
|
|
scrubMemo.put(k, gen, s, out)
|
|
return out
|
|
}
|
|
|
|
func scrub(s string) string {
|
|
if len(s) >= 256 {
|
|
scrubRuns.Add(1)
|
|
}
|
|
out, _ := mask(s, Options{Secrets: true}, scrubbed, false)
|
|
return out
|
|
}
|
|
|
|
// scrubRuns counts the strings of 256 bytes or more the rules have run
|
|
// over (a test reads it).
|
|
var scrubRuns atomic.Uint64
|
|
|
|
// Scrub remembers what it made of a long string. An agent sends its whole
|
|
// history on every turn (Codex: every item of a session, each turn), and
|
|
// what is kept of each request (the request archive, a gateway
|
|
// conversation) is scrubbed whole, so without it every turn ran each rule
|
|
// over the whole history again: a core's worth while Codex ran
|
|
// (yetone/magpie-releases#24). A short string costs less to scrub than to
|
|
// look up. Only a hash of a string is kept, and what it became when that
|
|
// is not the string itself.
|
|
const (
|
|
scrubMemoMin = 256
|
|
scrubMemoEntries = 1 << 15
|
|
scrubMemoBytes = 32 << 20
|
|
)
|
|
|
|
type scrubEntry struct {
|
|
gen uint64
|
|
same bool // s came back as it was
|
|
out string // else what it became
|
|
}
|
|
|
|
// scrubMemo holds two generations of results: when the newer is full, it
|
|
// becomes the older and the older goes, so what is in use stays and at
|
|
// most twice the bounds are held.
|
|
var scrubMemo memo
|
|
|
|
type memo struct {
|
|
sync.Mutex
|
|
cur, old map[[32]byte]scrubEntry
|
|
bytes int
|
|
}
|
|
|
|
func (m *memo) get(k [32]byte, gen uint64, s string) (string, bool) {
|
|
m.Lock()
|
|
defer m.Unlock()
|
|
e, ok := m.cur[k]
|
|
if !ok {
|
|
if e, ok = m.old[k]; ok && e.gen == gen {
|
|
m.add(k, e)
|
|
}
|
|
}
|
|
if !ok || e.gen != gen {
|
|
return "", false
|
|
}
|
|
if e.same {
|
|
return s, true
|
|
}
|
|
return e.out, true
|
|
}
|
|
|
|
func (m *memo) put(k [32]byte, gen uint64, s, out string) {
|
|
e := scrubEntry{gen: gen, same: out == s}
|
|
if !e.same {
|
|
e.out = strings.Clone(out)
|
|
}
|
|
m.Lock()
|
|
defer m.Unlock()
|
|
m.add(k, e)
|
|
}
|
|
|
|
// reset forgets everything (a test starts from nothing).
|
|
func (m *memo) reset() {
|
|
m.Lock()
|
|
defer m.Unlock()
|
|
m.cur, m.old, m.bytes = nil, nil, 0
|
|
}
|
|
|
|
// add is called with m held.
|
|
func (m *memo) add(k [32]byte, e scrubEntry) {
|
|
if m.cur == nil || len(m.cur) >= scrubMemoEntries || m.bytes+len(e.out) > scrubMemoBytes {
|
|
m.old, m.cur, m.bytes = m.cur, make(map[[32]byte]scrubEntry), 0
|
|
}
|
|
m.bytes += len(e.out) - len(m.cur[k].out)
|
|
m.cur[k] = e
|
|
}
|
|
|
|
// ScrubHeader is a header's value with its secrets taken out: all of it
|
|
// for a header named for one or a credential (Bearer, Basic), else what
|
|
// Scrub finds in it.
|
|
func ScrubHeader(name, value string) string {
|
|
v := strings.ToLower(strings.TrimSpace(value))
|
|
if SecretName(name) || strings.HasPrefix(v, "bearer ") || strings.HasPrefix(v, "basic ") {
|
|
return Scrubbed
|
|
}
|
|
return Scrub(value)
|
|
}
|
|
|
|
// ScrubJSON is a body with its secrets taken out: a JSON one string by
|
|
// string, a field named for a secret wholly; a stream's events each as
|
|
// JSON, line by line; anything else as text. A picture's data is left as
|
|
// it is.
|
|
func ScrubJSON(body []byte) []byte {
|
|
if out, ok := scrubJSON(body); ok {
|
|
return out
|
|
}
|
|
lines := strings.SplitAfter(string(body), "\n")
|
|
for i, l := range lines {
|
|
if rest, ok := strings.CutPrefix(l, "data:"); ok {
|
|
trimmed := strings.TrimSpace(rest)
|
|
if out, ok := scrubJSON([]byte(trimmed)); ok && trimmed != "" {
|
|
lines[i] = "data: " + string(out) + l[len(strings.TrimRight(l, "\r\n")):]
|
|
continue
|
|
}
|
|
}
|
|
lines[i] = Scrub(l)
|
|
}
|
|
return []byte(strings.Join(lines, ""))
|
|
}
|
|
|
|
func scrubJSON(b []byte) ([]byte, bool) {
|
|
return walk(b, func(_, key, s string) string {
|
|
switch {
|
|
case strings.HasPrefix(s, "data:"):
|
|
return s
|
|
case key != "" && SecretName(key):
|
|
return Scrubbed
|
|
}
|
|
return Scrub(s)
|
|
})
|
|
}
|