Files
magpie/internal/redact/dateword.go
T
yetone 5a85a9836c redact: a value at the end of a tool call's arguments keeps its closing quote (xiaojiang091499 on X)
xiaojiang091499 on X (v0.1.1808): "隐私脱敏误把 Codex 工具参数结尾的引号和
花括号一起替换了,破坏了参数格式,导致会话一直报错重连". Still the case on
main before this commit.

Cause, outbound: a tool call's arguments are masked as JSON text
(MaskJSON, escaped). A span is kept out of escapes (#3022) but not inside
its JSON string, so a span that runs past the string's end took the
closing quote with it. The local model does this: Privacy Filter's
tokenizer merges a punctuation run, so in {"cmd":"export
DB_PASSWORD=Hunt3r!"} the real model's SECRET span is `Hunt3r!"`, and a
secret's edges are kept by design (#3591). Same with "the api key is
AbC…!?\"", "ship to 221B Baker Street, London.\"", "pwd=Qq123456@#\"".
A user rule like `tok-\S+` does the same. The vendor got
{"cmd":"export DB_PASSWORD={{SECRET_…}}}: arguments no longer JSON, so
Codex's turn failed and reconnected. The model is on by default since
v0.1.1858, so this hits every user from then.

Fix: inStrings (redact.go) moves each span in escaped text onto the JSON
string it starts in and ends it where that string ends. A span that is a
whole bare value (a number a rule finds, "acct": 12345678901) is kept and
its placeholder goes in quoted (span.bare); any other span outside a
string is dropped. Text that isn't valid JSON is left as before. This is the
path for Responses function_call and Chat tool_calls arguments (JSON
text); Anthropic tool_use input and Gemini functionCall args are
objects, masked string by string, so a span there never leaves its
string.

Sibling, inbound: Writer.event and restoreJSON read an event's "type" as
the walk reached it, so a Responses function_call_arguments.delta whose
"delta" key comes before "type" (as a Go map or any key-sorting proxy
writes it) had its value put back unescaped: a password with a quote in
it ended the arguments' string. typeOf now reads the type first, and the
Writer falls back to the SSE event name. Chat tool_calls and Anthropic
input_json_delta were already right (key "arguments"/"partial_json").

Tests (internal/redact/argstail_test.go):
- TestArgumentsValueAtEndKeepsQuote: the real model's four spans
  (captured from Privacy Filter on mote) and a \S+ rule, in Responses and
  Chat bodies; arguments stay JSON, the value is gone, Restore gives the
  original back.
- TestArgumentsBareValueQuoted: a bare number is masked as a string.
- TestWriterStreamArgumentsValueAtEnd: a value with a quote and a
  backslash, at the end of the arguments, split at every byte after its
  placeholder starts, through Responses deltas (delta before type),
  Chat tool_calls and Anthropic input_json_delta.
- TestRestoreJSONDeltaBeforeType: the whole-body path.

Broken by hand: without inStrings, the first two fail; with the type
read in the walk again in writer.go, the stream test fails at cuts 28-43
({"cmd":"export DB_PASSWORD=Hunt3r!"\x"}); the same in json.go fails
TestRestoreJSONDeltaBeforeType.

Real model: a probe (not committed) ran Privacy Filter on mote over 36
argument texts through MaskJSON as the gateway does; before, the
arguments were broken for each case above; after, all 36 are valid JSON
with the value masked.

Run on mote (mote-run): go test ./... -timeout 45m -count=1 at this
change on 655f8dbd1: every package ok except two reds already filed and
outside redact: internal/gui TestMainWindowCorners/macOS_26_SDK (#3949)
and internal/provider TestClaudeAccountServesModelsListedSinceItsFetch
(#3174). Rebased on 362774210/e5e2aa58e (gateway and routes only), then
internal/redact, internal/pii and internal/masking again at the pushed
head: ok. go vet ./..., GOOS=linux go vet -tags
nogui ./..., GOOS=windows go vet ./..., go build, GOOS=linux go build
-tags nogui, GOOS=windows go build: ok. gofmt -l: nothing.

Not tried: a real Codex session through the gateway with a masked
password; Gemini's functionCall with the real model (its args are an
object, masked string by string, never as JSON text). No docs/subsystems
page describes where spans are placed, so none changed.
2026-10-29 01:17:04 +08:00

184 lines
6.2 KiB
Go

package redact
import (
"regexp"
"strconv"
"strings"
"sync"
"time"
"unicode"
"unicode/utf8"
)
// A masked word that is a date matches that calendar date however it is
// written (JackyCcao on X: with birthdays off, the user's own birthday as a
// masked word, 1990-05-12, let 1990/5/12, 1990年5月12日 and 19900512 go as
// written). The word is a date when it is all of one: year, month and day
// with - / . 、 or a space between (1990-05-12, 1990.5.12); 1990年5月12日 or
// 1990년 5월 12일; day and month before the year where only one reading is
// a date (13/05/1990, 05/13/1990, 05/05/1990, never 05/12/1990); or a month's
// English name (May 12, 1990; 12 May 1990). Eight digits alone are not
// taken for a date word: they are as likely an id.
//
// It then matches the same date, and only it, in each of those forms, with
// or without leading zeros, and as eight digits (19900512); day-first and
// month-first only where the day is over 12 or equals the month, so no
// form can read as another date. A match never has a digit beside it, nor
// a dot and a digit (1990.5.12.1 is a version, 11990-05-12 another number),
// whatever Whole words only says: a date is matched as a whole date.
// dateSep is what stands between a date's numbers.
const dateSep = `(?:[ \t]*[-/.、/-][ \t]*|[ \t])`
var (
dateYMD = regexp.MustCompile(`^(\d{4})` + dateSep + `(\d{1,2})` + dateSep + `(\d{1,2})\.?$`)
dateCJK = regexp.MustCompile(`^(\d{4})[ \t]*[年년][ \t]*(\d{1,2})[ \t]*[月월][ \t]*(\d{1,2})[ \t]*[日号號일]?$`)
dateDMY = regexp.MustCompile(`^(\d{1,2})` + dateSep + `(\d{1,2})` + dateSep + `(\d{4})$`)
dateNameF = regexp.MustCompile(`^([A-Za-z]+)\.?[ \t]+(\d{1,2})(?:st|nd|rd|th)?,?[ \t]+(\d{4})$`)
dateNameB = regexp.MustCompile(`^(\d{1,2})(?:st|nd|rd|th)?(?:[ \t]+|-)([A-Za-z]+)\.?,?(?:[ \t]+|-)(\d{4})$`)
)
var monthNames = []string{"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
// monthOf is the month a name or its first three letters say, 0 for none.
func monthOf(name string) int {
l := strings.ToLower(name)
if l == "sept" {
return 9
}
for i, m := range monthNames {
ml := strings.ToLower(m)
if l == ml || len(l) == 3 && strings.HasPrefix(ml, l) {
return i + 1
}
}
return 0
}
// realDate says y-m-d is a day of the calendar.
func realDate(y, m, d int) bool {
if y < 1000 || y > 2999 || m < 1 || m > 12 || d < 1 {
return false
}
t := time.Date(y, time.Month(m), d, 0, 0, 0, 0, time.UTC)
return t.Day() == d && int(t.Month()) == m
}
// parseDateWord is the date the word w is, where it is one.
func parseDateWord(w string) (y, m, d int, ok bool) {
n := func(s string) int { v, _ := strconv.Atoi(s); return v }
if g := dateYMD.FindStringSubmatch(w); g != nil {
y, m, d = n(g[1]), n(g[2]), n(g[3])
} else if g := dateCJK.FindStringSubmatch(w); g != nil {
y, m, d = n(g[1]), n(g[2]), n(g[3])
} else if g := dateDMY.FindStringSubmatch(w); g != nil {
p, q := n(g[1]), n(g[2])
y = n(g[3])
switch {
case p == q:
m, d = p, q
case p > 12 && q <= 12:
d, m = p, q
case q > 12 && p <= 12:
m, d = p, q
default: // 05/12/1990: May 12th or December 5th
return 0, 0, 0, false
}
} else if g := dateNameF.FindStringSubmatch(w); g != nil {
m, d, y = monthOf(g[1]), n(g[2]), n(g[3])
} else if g := dateNameB.FindStringSubmatch(w); g != nil {
d, m, y = n(g[1]), monthOf(g[2]), n(g[3])
} else {
return 0, 0, 0, false
}
return y, m, d, realDate(y, m, d)
}
// dateForms is a pattern for the date y-m-d in each form a date word
// matches, and the word itself as written.
func dateForms(y, m, d int, w string) string {
num := func(v int) string { // 5 or 05; 12
if v < 10 {
return "0?" + strconv.Itoa(v)
}
return strconv.Itoa(v)
}
Y, M, D := strconv.Itoa(y), num(m), num(d)
full, abbr := monthNames[m-1], monthNames[m-1][:3]
if m == 9 {
abbr = "Sept|Sep"
}
name := `(?i:` + full + `|(?:` + abbr + `)\.?)`
ord := `(?:st|nd|rd|th)?`
forms := []string{
Y + dateSep + M + dateSep + D,
Y + `[ \t]*年[ \t]*` + M + `[ \t]*月[ \t]*` + D + `(?:[ \t]*[日号號])?`,
Y + `[ \t]*년[ \t]*` + M + `[ \t]*월[ \t]*` + D + `(?:[ \t]*일)?`,
Y + strconv.Itoa(100 + m)[1:] + strconv.Itoa(100 + d)[1:],
name + `[ \t]+` + D + ord + `,?[ \t]+` + Y,
D + ord + `(?:[ \t]+|-)` + name + `,?(?:[ \t]+|-)` + Y,
}
// day-first and month-first only where no other date reads the same
if d > 12 || d == m {
forms = append(forms, D+dateSep+M+dateSep+Y)
if d != m {
forms = append(forms, M+dateSep+D+dateSep+Y)
}
}
return `(?:` + strings.Join(forms, "|") + `|` + regexp.QuoteMeta(w) + `)`
}
var dateWords sync.Map // word → *regexp.Regexp, or nil for one that is no date
// dateWord is the pattern for the date the masked word w is, nil where it
// is no date.
func dateWord(w string) *regexp.Regexp {
if re, ok := dateWords.Load(w); ok {
return re.(*regexp.Regexp)
}
var re *regexp.Regexp
if y, m, d, ok := parseDateWord(w); ok {
re = regexp.MustCompile(dateForms(y, m, d, w))
}
dateWords.Store(w, re)
return re
}
// dateWhole says the date at s[a:b] is no part of a longer number: no
// digit beside it, no dot and a digit (a version's 1990.5.12.1, 3.1990.5.12),
// and no letter before a month's name (xMay), save in a script that puts
// no spaces between words (生日是May 12, 1990).
func dateWhole(s string, a, b int) bool {
isDigit := func(c byte) bool { return c >= '0' && c <= '9' }
if a > 0 {
if isDigit(s[a-1]) || s[a-1] == '.' && a > 1 && isDigit(s[a-2]) {
return false
}
if r, _ := utf8.DecodeLastRuneInString(s[:a]); unicode.IsLetter(rune(s[a])) && unicode.IsLetter(r) && !unicode.In(r, unicode.Han, unicode.Hiragana, unicode.Katakana, unicode.Hangul) {
return false
}
}
if b < len(s) {
if isDigit(s[b]) || s[b] == '.' && b+1 < len(s) && isDigit(s[b+1]) {
return false
}
}
return true
}
// dateSpans are where s has the date the masked word w is, nil and false
// where w is no date.
func dateSpans(s, w string) ([]span, bool) {
re := dateWord(w)
if re == nil {
return nil, false
}
var out []span
for _, m := range re.FindAllStringIndex(s, -1) {
if dateWhole(s, m[0], m[1]) {
out = append(out, span{m[0], m[1], "TERM", ByWord, 0, false})
}
}
return out, true
}