Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
redact: 试一试 shows the made-up key when "keep key form" is on, short keys and named keys too
yetone359da679db95
diff --git a/internal/gui/masking_test.go b/internal/gui/masking_test.goindex f53fcea4c..c3a2ba09a 100644--- a/internal/gui/masking_test.go+++ b/internal/gui/masking_test.go@@ -106,3 +106,34 @@ func TestMaskingRecordsAPI(t *testing.T) {t.Fatalf("values %d %s %v", rw.Code, rw.Body, rw.Header())}}++// Try it under Settings › Privacy shows what a request would send: with+// Keep a key's form on, a made-up key of the same form, not a+// placeholder.+func TestMaskingPreviewKeepsKeyForm(t *testing.T) {+ const key = "sk_w3axCByTQgK1PxqPhfKTrJOLBqz6sbZv"+ h := t.TempDir()+ t.Setenv("HOME", h)+ t.Setenv("USERPROFILE", h)+ t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))+ t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))+ srv := Handler(webHost{func() {}}, nil)+ preview := func() string {+ rw := httptest.NewRecorder()+ srv.ServeHTTP(rw, httptest.NewRequest("POST", "/api/masking/preview", strings.NewReader(`{"text":"用密钥 `+key+` 部署可以吗,或用密钥 OPENAI_API_KEY=`+key+`"}`)))+ var out struct{ Text string }+ if err := json.Unmarshal(rw.Body.Bytes(), &out); err != nil {+ t.Fatalf("%v %s", err, rw.Body)+ }+ return out.Text+ }+ for _, shape := range []bool{false, true} {+ if err := settings.Save(settings.Settings{Redact: true, RedactShape: shape}); err != nil {+ t.Fatal(err)+ }+ got := preview()+ if strings.Contains(got, key) || strings.Contains(got, "{{") == shape || shape && (!strings.Contains(got, "=sk_") || !strings.Contains(got, "用密钥 sk_")) {+ t.Errorf("keep form %v: %q", shape, got)+ }+ }+}diff --git a/internal/redact/redact.go b/internal/redact/redact.goindex 43b63388f..28a80348f 100644--- a/internal/redact/redact.go+++ b/internal/redact/redact.go@@ -622,10 +622,20 @@ func Mask(s string, o Options) (string, int) {}// Preview is Mask with nothing remembered: each value goes as the-// placeholder it would have, which isn't kept for Restore or as a value-// seen before (known.go).+// placeholder, or with Options.Shape the made-up key, it would have, which+// isn't kept for Restore or as a value seen before (known.go).func Preview(s string, o Options) string {- out, _ := mask(s, withModel(o, []string{s}), tag, false)+ put := tag+ if o.Shape {+ put = func(kind, v string) string {+ name, k := splitName(v)+ if f, ok := shapeOf(kind, k); ok {+ return name + f+ }+ return tag(kind, v)+ }+ }+ out, _ := mask(s, withModel(o, []string{s}), put, false)return out}diff --git a/internal/redact/shape.go b/internal/redact/shape.goindex 5ddc472f9..a8a24eb6d 100644--- a/internal/redact/shape.go+++ b/internal/redact/shape.go@@ -4,6 +4,7 @@ import ("crypto/hmac""crypto/sha256""encoding/binary"+ "regexp""slices""strings")@@ -17,12 +18,17 @@ import (// as the same made-up one every turn and a vendor's cache still matches,// and it comes back as it was wherever the vendor writes it.//-// Only a secret's kind, of minShape characters or more, and only letters,-// digits, '-', '_' and '.': a made-up key that long is in no other text by-// chance, and it needs no escaping in JSON. Anything else goes as a-// placeholder still.+// Only a secret's kind, of minShape characters or more with minRandom of+// them made up (not its prefix or separators), and only letters, digits,+// '-', '_' and '.': a made-up key like that is in no other text by chance,+// and it needs no escaping in JSON. Anything else goes as a placeholder+// still. 16 left a short key such as sk_abcdefg a placeholder, which read+// as the setting not working.-const minShape = 16+const (+ minShape = 8+ minRandom = 6+)var (// shapes are the made-up keys in values, with their kinds; guarded by@@ -59,6 +65,19 @@ func keyHead(v string) string {return ""}+// namedValue is a name before = or : and the value after it.+var namedValue = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_.-]*[ \t]*[=:][ \t]*()`)++// splitName parts a value found with its name, as 密钥+// OPENAI_API_KEY=sk_… is, into the name, which stays as written, and the+// key after it, which alone is made up and put back.+func splitName(v string) (name, key string) {+ if m := namedValue.FindStringSubmatchIndex(v); m != nil {+ return v[:m[2]], v[m[2]:]+ }+ return "", v+}+func shapeByte(c byte) bool {return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '-' || c == '_' || c == '.'}@@ -112,7 +131,7 @@ func shapeOf(kind, v string) (string, bool) {lower = "abcdefghijklmnopqrstuvwxyz"digit = "0123456789")- out := []byte(head)+ out, made := []byte(head), 0for i := 0; i < len(rest); i++ {c := rest[i]set := ""@@ -133,8 +152,12 @@ func shapeOf(kind, v string) (string, bool) {out = append(out, c)continue}+ made++out = append(out, set[next(len(set))])}+ if made < minRandom {+ return "", false+ }f := string(out)return f, f != v}@@ -142,7 +165,8 @@ func shapeOf(kind, v string) (string, bool) {// shaped is put for Options.Shape: v as a made-up key of its form where it// may go as one, else as its placeholder.func shaped(kind, v string) string {- f, ok := shapeOf(kind, v)+ name, k := splitName(v)+ f, ok := shapeOf(kind, k)if !ok {return placeholder(kind, v)}@@ -150,17 +174,17 @@ func shaped(kind, v string) string {have, taken := values[f]if !taken {makeRoom()- values[f] = v+ values[f] = kkinds[kind] = trueshapes[f] = kind- addKnown(f, v)+ addKnown(f, k)}mu.Unlock()- if taken && have != v {+ if taken && have != k {// another value's made-up key, which nothing ever is by chancereturn placeholder(kind, v)}- return f+ return name + f}// restoreShapes puts back each made-up key in s, counting it in putdiff --git a/internal/redact/shape_test.go b/internal/redact/shape_test.goindex 92e24504e..b6fff9b4c 100644--- a/internal/redact/shape_test.go+++ b/internal/redact/shape_test.go@@ -79,6 +79,8 @@ func TestShapeOnlyForKeys(t *testing.T) {{"EMAIL", "[email protected]"},{"PHONE", "13800138000123456"},{"PASSWORD", "short1"},+ {"SECRET", "sk-ab-cd"}, // 8, but 4 made up+ {"SECRET", "ghp_abcde"}, // 9, but 5 made up{"PASSWORD", "has space in it 12345"},{"PASSWORD", `quote"inside1234567`},} {@@ -184,3 +186,48 @@ func TestShapeStream(t *testing.T) {t.Fatalf("events changed:\n%s", rec.Body.String())}}++// A short key keeps its form too, and the settings' Try it shows what a+// request would send: the made-up key, not a placeholder (the owner's+// sk_abcdefg showed {{SECRET_…}} with the setting on).+func TestShapeShortAndPreview(t *testing.T) {+ f, ok := shapeOf("SECRET", "sk_abcdefg")+ if !ok || len(f) != len("sk_abcdefg") || !strings.HasPrefix(f, "sk_") || f == "sk_abcdefg" {+ t.Fatalf("sk_abcdefg as %q %v", f, ok)+ }+ const key = "sk_prevw9ZqX1mT3vB7nK2pLr4"+ o := Options{Secrets: true, Shape: true}+ text := "OPENAI_API_KEY=" + key+ got := Preview(text, o)+ want, _ := shapeOf("SECRET", key)+ mu.RLock()+ _, kept := values[want]+ mu.RUnlock()+ if kept {+ t.Fatal("preview kept the made-up key for Restore")+ }+ if !strings.Contains(got, want) || strings.Contains(got, "{{") || strings.Contains(got, key) {+ t.Fatalf("preview with Shape: %q, want %q in it", got, want)+ }+ if got := Preview(text, Options{Secrets: true}); !strings.Contains(got, "{{") {+ t.Fatalf("preview without Shape: %q", got)+ }+}++// 密钥 OPENAI_API_KEY=sk_… is found with its name: the name stays, the+// key after it is made up, and the key alone is put back, as a reply that+// names only the key has it.+func TestShapeKeepsAName(t *testing.T) {+ const key = "sk_nmdv8QpX2rT6yW4uZ1oKs"+ got := shaped("SECRET", "OPENAI_API_KEY="+key)+ f := strings.TrimPrefix(got, "OPENAI_API_KEY=")+ if f == got || !strings.HasPrefix(f, "sk_") || len(f) != len(key) || f == key {+ t.Fatalf("%q", got)+ }+ if back := Restore("the key is "+f, false); back != "the key is "+key {+ t.Fatalf("%q", back)+ }+ if got := shaped("SECRET", "OPENAI_API_KEY=a b c d e f"); !strings.HasPrefix(got, "{{") {+ t.Fatalf("a value with spaces made up: %q", got)+ }+}