From eef99e4bbebaf363840847dce4a33ee72eb37945 Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 8 Nov 2026 11:17:25 +0800 Subject: [PATCH] redact: a value on both Masked words and Never mask is masked; Never mask still beats the user's rules MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TakaNaShi_ShiKi on X asked for a way to keep a value from being masked. That exists already: Settings › Privacy › When a value is found › Never mask (RedactAllow, Options.Allow, since b00c82acf), and a click on a masked value in a request's masking record adds it there (/api/masking/allow). It is applied in apply() to every span, whichever finder found it: magpie's rules, the user's rules, the local model and known values. Nothing new is wired for it here. What was undecided was the order between it and the user's own lists. apply() let a Never mask value go even when the same value was one of the user's Masked words, and an allowed value hid a masked word inside it (Masked word "kelvarn", Never mask "team.inbox@kelvarn.io": the word went out). The value menu already refuses Never mask on a Masked word ("One of your Masked words"), but the settings list doesn't. Decided: of two things the user asked for, the one that keeps a value from the vendor wins. A Masked word is masked even when Never mask lists it, and a Masked word inside a value Never mask lets go is masked. Never mask still wins over every finder, the user's own rules included: a value named there is the exception to the user's pattern (rule PROJ-\d{4}, Never mask PROJ-0001: PROJ-0001 goes as written, PROJ-0002 is masked). Matching is unchanged: a whole value, any case. A user with a value on both lists before this sees it masked from the next request on. Test: TestMaskedWordsWinOverAllow (new). Broken by hand (apply() restored): FAIL "a value on both lists went as written: \"deploy on build-01, mail team.inbox@kelvarn.io, …\"" and "a masked word inside an allowed value". Restored: ok. TestAllowLetsAValueGo still passes. Run (mote-run, at eb7ce732a): gofmt -l internal/redact (empty), go vet ./internal/redact/, go test -count=1 ./internal/redact/... ok. Not tried: a running magpie; no GUI change, so no GUI run in this commit. docs/subsystems/gateway-routing.md says the order beside Never mask. --- docs/subsystems/gateway-routing.md | 2 +- internal/redact/actions_test.go | 21 +++++++++++++++++++++ internal/redact/redact.go | 18 +++++++++++++----- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.md index 54f7c4785..381e1f1e7 100644 --- a/docs/subsystems/gateway-routing.md +++ b/docs/subsystems/gateway-routing.md @@ -166,7 +166,7 @@ recorded as a prediction failure (`noPrediction`). - With *Mask secrets* on, the user's own masking rules (`redactRules`, `redact.Rule`: a placeholder name and a prefix or an RE2 pattern, [`internal/redact/rules.go`](../../internal/redact/rules.go)) run before magpie's. Settings → Privacy adds them, removes them, and edits one in place (`POST /api/settings/redact-rule` with the rule's index and its current value, `editRedactRule` in [`internal/gui/api.go`](../../internal/gui/api.go)). Every rule is checked by `redact.CheckRules` when settings are saved; an edit that doesn't compile, matches the empty string, repeats another rule or names a rule no longer at that index is refused with the rules left as they were. The masker reads the rules from settings on each call (`options` in [`internal/masking`](../../internal/masking/masking.go)), so a change applies to the next request. - The user's own masked words (`redactWords`, `Options.Words`; Settings → Privacy's *Masked words*) go as `{{TERM_…}}` whatever else is on, and come back as written. They match as written, inside longer words too, unless the user asks: *Masked words in any case* (`redactWordsAnyCase`, `WordsAnyCase`) matches in any capitals by Unicode simple folding, and *Whole words only* (`redactWordsWhole`, `WordsWhole`; acmilan_ on Discord) skips a match with a letter, digit or `_` beside it, and for a number a `.` and digit beside it, so `ABC` masks `name='ABC'` but not `XABCD`, and `123456` not `91234567`, `0.123456` or `123456.78` (`wholeWord` in [`redact.go`](../../internal/redact/redact.go)). Chinese and Japanese letters count as no letter there (`wordRune`): a word of theirs is found inside their text, and `ABC` in 用ABC做 is whole. Both are off by default; a page from before them that saves without the field keeps the user's choice (`api.go`). `TestWordsWhole`, `TestWordsWholeJSON`, `TestCountTokensRedacted`. A word that is a date (`1990-05-12`, `1990/5/12`, `1990年5月12日`, `May 12, 1990`; eight digits alone are not one) matches that calendar date in every common form instead (`dateSpans` in [`dateword.go`](../../internal/redact/dateword.go); JackyCcao on X, who masks their own birthday with birthdays off): `-` `/` `.` `、` or a space between, 年月日 or 년월일, with or without leading zeros, `19900512`, the month's English name, and day-first or month-first only where the day is over 12 or equals the month (`13/05/1990`, never `05/12/1990`, which is December 5th too; such a word matches only as written). A match never has a digit, or a dot and a digit, beside it, whatever *Whole words only* says, so `1990.5.12.1` and `11990-05-12` are not that date. `TestDateWordEveryForm`, `TestDateWordNotAnotherDate`, `TestDateWordDayMonthOrder`. - With *Keep a key's form* on (`settings.RedactShape`, `Options.Shape`, under *Mask secrets*; Charles Cao on X), a secret of 16 characters or more, all letters, digits, `-`, `_` or `.`, goes as a made-up key of its form instead of a placeholder: its prefix kept (`sk-proj-`, `ghp_`, `AKIA`, up to four letters and their separator; `keyHead`), the same length and separators, and each other character a random one of its kind (a hex key's any hex digit of its case), drawn from an HMAC of the value under the placeholder key, so one key goes as the same made-up one every turn and a vendor's cache still matches (`shapeOf`, `shaped` in [`internal/redact/shape.go`](../../internal/redact/shape.go)). Personal data, the user's words and anything shorter or with other characters go as placeholders still. A made-up key is kept in the masker's memory with the placeholders, so it is recorded, re-masked in what a vendor sealed, and put back wherever the reply has it, before the placeholders (`restoreShapes`); a stream holds the end of an event that may be the start of one (`shapeTail`, after the last whole one), and a made-up key the vendor sends back unrestored isn't masked again (`outsideShapes`). `TestShapeKeepsForm`, `TestShapeJSON`, `TestShapeStream`. Insights › Masking and Settings' *Try it* mark a made-up key where they mark a placeholder, matching the text against the request's own values only, so a key of the same form the user wrote isn't marked (`maskMatches` in `plugins/redact/page.js`; #3599, masking-records and mask-actions GUI tests). - - What is done with a value found is the user's per kind (#1977, `redactActions` in settings, `Options.Actions`, set by `POST /api/settings/redact-actions`; a kind left out is masked). `redact.Warn` sends it as written: the masker tells it, with a placeholder no one remembers, and it isn't counted as masked. `redact.Block` masks it as any other, and the gateway holds the request back with a 400 that names the kind and the value's hint, never the value (`blocked`, `maskBlocked` in [`redact.go`](../../internal/gateway/redact.go)); nothing is sent and the record is kept as `Blocked` (`noteBlocked`). A value on the *Never mask* list (`redactAllow`, `Options.Allow`, `POST /api/settings/redact-allow`) goes as written when it is the whole value found, in any case, and isn't told; a longer value it is part of is masked. Settings → Privacy's *Try it* posts a text to `POST /api/masking/preview` ([`masking.go`](../../internal/gui/masking.go)), which masks it as a request would be (`redact.Preview`) and remembers nothing: its placeholders stand for nothing afterwards, its values aren't masked later as seen before, and it returns only the masked text and each hit's kind, finder, action, placeholder and hint. The page keeps the text in no storage. + - What is done with a value found is the user's per kind (#1977, `redactActions` in settings, `Options.Actions`, set by `POST /api/settings/redact-actions`; a kind left out is masked). `redact.Warn` sends it as written: the masker tells it, with a placeholder no one remembers, and it isn't counted as masked. `redact.Block` masks it as any other, and the gateway holds the request back with a 400 that names the kind and the value's hint, never the value (`blocked`, `maskBlocked` in [`redact.go`](../../internal/gateway/redact.go)); nothing is sent and the record is kept as `Blocked` (`noteBlocked`). A value on the *Never mask* list (`redactAllow`, `Options.Allow`, `POST /api/settings/redact-allow`) goes as written when it is the whole value found, in any case, and isn't told; a longer value it is part of is masked. It wins over every finder, the user's own rules included (a rule's pattern with one value let go), but not over *Masked words*: a value on both lists is masked, and so is a masked word inside a value the list lets go (`apply` in [`internal/redact/redact.go`](../../internal/redact/redact.go); `TestMaskedWordsWinOverAllow`). Settings → Privacy's *Try it* posts a text to `POST /api/masking/preview` ([`masking.go`](../../internal/gui/masking.go)), which masks it as a request would be (`redact.Preview`) and remembers nothing: its placeholders stand for nothing afterwards, its values aren't masked later as seen before, and it returns only the masked text and each hit's kind, finder, action, placeholder and hint. The page keeps the text in no storage. - Each request that had anything masked is recorded as it goes to the vendor (`noteMasked` in [`masked.go`](../../internal/gateway/masked.go), from chat, the Codex backend, search, images, video, System One, embeddings and audio; count_tokens is masked and not recorded). The masker says what it swapped through `gwmw.Teller` (`JSONHits`, `TextHits`: each placeholder's kind, its finder, `redact.ByRule`, `ByYours`, `ByWord`, `ByModel` or `ByKnown`, and a hint of its first characters and its length), and [`internal/maskrec`](../../internal/maskrec/maskrec.go) keeps one line per request in a file per day under `~/.config/magpie/masking`, 30 days and 100 MB in all: the agent (the one usage records, `callerOf`, so a request another magpie passed on is named by the agent it came from, not as `magpie`; linkaww on Discord), session, provider, model, and each placeholder with its kind, finder, action and hint, never the value. A chat, an embedding or a video whose group can pass it to another member is held in memory until the request is over (`noteMaskedUntil`, `maskrec.Hold`), and its line names the provider and model the last try went to, not the first planned, which a failover may never have reached; the list sorts each day's lines by key, since a long request's line is written after later ones. The text around each placeholder as the vendor saw it is kept in memory only, for the last 400 records, and what a placeholder stood for only in the masker's own memory (`Teller.Value`); after a restart neither is there. A recent call (`Call.Masked`) and the route trace (`Route.Masked`) carry the counts and the record's key, which the Gateway and Routing pages' badge opens in Settings → Privacy (`/api/masking/*`, [`internal/gui/masking.go`](../../internal/gui/masking.go)). A masker without `Teller`, a plugin's, masks as before and records nothing. Under a test binary testenv isolated nothing is written (`maskrec.inTest`). The page's Clear records… (after a confirm) posts `/api/masking/clear`, and `maskrec.Clear` removes every day's file whole under the store's lock, with the in-memory text around placeholders; a day it can't remove stays listed and is named in the error. What the masker holds for each placeholder stays, so a placeholder still in an agent's context is put back in its answers. - `DecidesModel` gives a 400 (`decisionModelRefused`; a decision group, `decisionGroupRefused`), and count_tokens gives the same 400 before the vendor is asked. For OpenRouter, a variant of a model its decisions list names (`inception/mercury-decide:free`, picked by hand) is a decision model too (`openRouterVariant`): OpenRouter lists the model once, and answered such a variant 400 "is a decisions model" on chat/completions (coeo91 on Discord). - A gateway key held to some models (`keyHolds`, #882) gets a 403 for any other model. A routing group passes only through `groupAllowed`, which every route asks (the model lists' `entryAllowed`, `serve`, `countHeld`, embeddings, images, videos, decision groups, a subagent's and masking's route): a group of the user's own only when the key names it, whatever its members (kautism on Discord), a found group (`foundGroup`), or any group for a key that names accounts and no model (`Identity.AccountModels`), when the key may use every member; a bare name that is both a group of the user's the key may not use and a model it may use is that model for the key (`keyGroupFor`; `TestGatewayKeyGivenAModelIsNotGivenItsGroup`). A decision model is one: `serveSystemOne` and the decision list another magpie asks for (`DecidersHeader`) hold the key to it, while a group the key names asks its own decider in `askJev` with no key check (`TestGatewayKeyHeldToAGroupKeepsItsDecider`); the key editor offers the decision models (`GET /api/caller-keys/models`, `decides`, `TestCallerKeyModelsOfferDeciders`, `gateway-key-deciders.test.cjs`). diff --git a/internal/redact/actions_test.go b/internal/redact/actions_test.go index 5a89a10c2..b8341368b 100644 --- a/internal/redact/actions_test.go +++ b/internal/redact/actions_test.go @@ -71,6 +71,27 @@ func TestAllowLetsAValueGo(t *testing.T) { } } +// The user's Masked words win over Never mask, the safe side of two +// things the user asked for: a value on both lists is masked, and so is a +// word inside a value Never mask lets go. Never mask wins over the +// user's own rules: the value it names is the exception to the pattern. +func TestMaskedWordsWinOverAllow(t *testing.T) { + const mail = "team.inbox@kelvarn.io" + o := Options{Secrets: true, Personal: true, Words: []string{"build-01", "kelvarn"}, + Allow: []string{"build-01", mail, "PROJ-0001"}, + Rules: []Rule{{Kind: "TICKET", Regex: `PROJ-\d{4}`}}} + out, _ := Mask("deploy on build-01, mail "+mail+", tickets PROJ-0001 and PROJ-0002", o) + if strings.Contains(out, "build-01") { + t.Errorf("a value on both lists went as written: %q", out) + } + if strings.Contains(out, "kelvarn") || !strings.Contains(out, "team.inbox@{{TERM_") { + t.Errorf("a masked word inside an allowed value: %q", out) + } + if !strings.Contains(out, "PROJ-0001") || strings.Contains(out, "PROJ-0002") { + t.Errorf("Never mask against the user's rule: %q", out) + } +} + // The preview masks as Mask does and remembers nothing: its placeholders // stand for nothing afterwards, and its values aren't masked later as // values seen before. diff --git a/internal/redact/redact.go b/internal/redact/redact.go index 85c2e8025..ad621a81f 100644 --- a/internal/redact/redact.go +++ b/internal/redact/redact.go @@ -1579,6 +1579,12 @@ func (r rule) takes(s string, a, b int, ok func(string) bool) bool { // first and longest where they overlap, nothing inside a placeholder. A // value o allows is left as it is, and one of a kind o warns of is left // and told. o.Seen, where set, is told of each. +// +// The user's Masked words win over Never mask: a value on both lists is +// masked, and so is a word inside a value Never mask lets go, since of +// two things the user asked for the one that keeps the value from the +// vendor is the safe one. Never mask wins over everything else, the +// user's own rules too: a value it names is the exception to them. func apply(s string, found []span, put func(kind, v string) string, o Options) (string, int) { if len(found) == 0 { return s, 0 @@ -1604,15 +1610,17 @@ func apply(s string, found []span, put func(kind, v string) string, o Options) ( }) var b strings.Builder last, next, n := 0, 0, 0 // next: where the next value may start + kept := 0 // the end of a value let go; only a word is masked before it for _, f := range found { - if f.start < next { + if f.start < next || f.start < kept && f.by != ByWord { + continue + } + v := s[f.start:f.end] + if f.by != ByWord && o.allows(v) { + kept = max(kept, f.end) continue } next = f.end - v := s[f.start:f.end] - if o.allows(v) { - continue - } act := o.Actions[f.kind] if act != Warn && act != Block { act = ""