Rex Zhou's #4025 proposed default rules for crypto secrets. This ports them with every shape that can also be ordinary text checked first: - WALLET_KEY: 64 hex digits (0x or bare) only after a key's name on the same line (private key, secret, signer, deployer, wallet, pk, 私钥 …), and not when a hash, sha, digest, sum, pub, addr, tx or an id name sits between: tx hashes, block hashes, storage slots and sha256 sums go as written. noIDs keeps it, since the name already says it is a key. - WIF and BIP32 xprv/tprv: base58check (double sha256) and payload length, so a random base58 run, an xpub or an address is not one. - Solana: a base58 secret or a 64-number keyfile counts only when its second half is the ed25519 public key of its first; a tx signature, same length, goes as written. Each number in a keyfile is <= 255. - Stellar S... seeds: StrKey version byte and CRC16-XModem. - Prefixed kinds: NEAR ed25519:, Sui suiprivkey1, Aptos AIP-80 ed25519-priv-0x, Nostr nsec1, age, Zcash secret-extended-key. - SEED_PHRASE: after seed/mnemonic/phrase/recovery (and zh, ja, ko), the longest run of words from the BIP39 English list, cut to 12/15/18/21/ 24/25 words. The list is embedded, its sha256 checked by a test against the published one. A sentence after "recovery" has words off the list. - Exchange keys: secretNames gains passphrase/pass (OKX, Coinbase, KuCoin) and priv_key; API key and secret fields were already masked by name. What the patch had that does not ship: every 0x+64 hex masked (tx and block hashes), unchecked base58 lengths for WIF and Solana (any long base58 run), and a phrase pattern that took prose after "recovery". The three rules with no marker (WIF, Solana, Stellar) run their pattern only after a byte scan finds a long enough run (rule.may). Without it the prose benchmark went from 2.9ms to 15.8ms per MB; with it 3.0ms, the same as without the rules (profile: sha256 of the memo and memmove). Run (wsl-run magpie-desktop, mote refused SSH all session): - go test -count=1 ./internal/redact: ok - go test -run TestCountTokensRedacted ./internal/gateway: ok (crypto on with Redact, off with RedactPersonal only) - gofmt -l; go vet linux, -tags nogui, GOOS=darwin, GOOS=windows for internal/redact and internal/gateway: clean - node --test site/worker.test.mjs (llms-full, docs): 7 of 7 - false positives: 1000 npm sha512 integrity lines, go.sum h1 lines, git shas, uuids, sha256 sums, std and URL-safe base64 blobs, xpub, addresses, IPFS CIDs, bech32, npub, public keys: none masked. - 1MB inputs of each shape stay linear (RE2, no backtracking). Broken by hand, each red: Solana unchecked (Lookalikes, Lengths), WIF unchecked (Lookalikes), WALLET_KEY without its name check (WalletKeyNeedsItsName, LockfilesAndBlobs), phrase without the wordlist (SeedPhrase), Stellar unchecked and keyfile unchecked (Lookalikes), rules unwired (7 tests). Not tried: real wallets (test vectors are the BIPs', NIP-19's and derived keys); Monero's own word list; other BIP39 languages; a Windows or macOS run of the tests; the full suite (after the tag). Co-authored-by: Rex Zhou <zhouzhuojie@gmail.com>
270 lines
10 KiB
Go
270 lines
10 KiB
Go
package gateway
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/redact"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// Each mode needs a fresh process: the redaction key is cached after its
|
|
// first use, so another test's masking would hide initialization races.
|
|
func TestConcurrentRedactedCountTokens(t *testing.T) {
|
|
const modeEnv = "MAGPIE_TEST_REDACT_CONCURRENCY"
|
|
mode := os.Getenv(modeEnv)
|
|
if mode == "" {
|
|
exe, err := os.Executable()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// a child that hangs prints its stacks before this run's own deadline
|
|
// does, rather than at a fixed 20s a loaded machine can spend on
|
|
// starting it
|
|
timeout := "0"
|
|
if d, ok := t.Deadline(); ok {
|
|
timeout = (time.Until(d) * 9 / 10).Round(time.Second).String()
|
|
}
|
|
for _, mode := range []string{"shared", "new_per_request"} {
|
|
t.Run(mode, func(t *testing.T) {
|
|
t.Parallel() // independent processes, each with a cold key cache
|
|
cmd := exec.Command(exe, "-test.run=^TestConcurrentRedactedCountTokens$", "-test.timeout="+timeout)
|
|
cmd.Env = append(os.Environ(), modeEnv+"="+mode)
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("concurrent first requests: %v\n%s", err, out)
|
|
}
|
|
})
|
|
}
|
|
return
|
|
}
|
|
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
|
const workers = 8
|
|
const key = "sk-proj-abcdEFGH1234ijklMNOP5678qrst"
|
|
const wif = "5HueCGU8rMjxEXxiPuD5BD" + "ku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ"
|
|
const reply = `{"input_tokens":17}`
|
|
bodies := make(chan string, workers)
|
|
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
body, err := io.ReadAll(r.Body)
|
|
if err != nil {
|
|
t.Errorf("read upstream request: %v", err)
|
|
w.WriteHeader(500)
|
|
return
|
|
}
|
|
bodies <- string(body)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
io.WriteString(w, reply)
|
|
}))
|
|
t.Cleanup(up.Close)
|
|
if err := provider.Save(provider.Provider{ID: "fake", Name: "Fake", Key: "k", Models: []string{"m1"}, Anthropic: up.URL}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := settings.Save(settings.Settings{Redact: true}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var shared http.Handler
|
|
if mode == "shared" {
|
|
shared = New().Handler()
|
|
}
|
|
start := make(chan struct{})
|
|
var wg sync.WaitGroup
|
|
for range workers {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
<-start
|
|
h := shared
|
|
if h == nil {
|
|
h = New().Handler()
|
|
}
|
|
rec := httptest.NewRecorder()
|
|
req := httptest.NewRequest("POST", "/v1/messages/count_tokens", strings.NewReader(
|
|
`{"model":"fake/m1","messages":[{"role":"user","content":"`+key+`"}]}`))
|
|
h.ServeHTTP(rec, req)
|
|
if rec.Code != 200 || rec.Body.String() != reply {
|
|
t.Errorf("count reply: %d %s", rec.Code, rec.Body.String())
|
|
}
|
|
}()
|
|
}
|
|
close(start)
|
|
wg.Wait()
|
|
close(bodies)
|
|
first, calls := "", 0
|
|
for body := range bodies {
|
|
calls++
|
|
if !json.Valid([]byte(body)) || strings.Contains(body, key) || !strings.Contains(body, "{{API_KEY_") {
|
|
t.Errorf("upstream received an unmasked or invalid request: %s", body)
|
|
}
|
|
if first == "" {
|
|
first = body
|
|
} else if body != first {
|
|
t.Errorf("same secret produced different placeholders: %s / %s", first, body)
|
|
}
|
|
}
|
|
if calls != workers {
|
|
t.Fatalf("upstream calls: %d, want %d", calls, workers)
|
|
}
|
|
if key, err := os.ReadFile(filepath.Join(settings.Dir(), "redact.key")); err != nil || len(key) != 32 {
|
|
t.Fatalf("persisted redaction key: %d bytes, error %v", len(key), err)
|
|
}
|
|
}
|
|
|
|
// the vendor sees placeholders; the agent gets its key back, even from a
|
|
// stream that splits a placeholder and a protocol it was translated from
|
|
func TestRedactedRequest(t *testing.T) {
|
|
const key = "sk-proj-abcdEFGH1234ijklMNOP5678qrst"
|
|
const wif = "5HueCGU8rMjxEXxiPuD5BD" + "ku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ"
|
|
var got []byte
|
|
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
got, _ = io.ReadAll(r.Body)
|
|
p := regexp.MustCompile(`\{\{API_KEY_[a-z2-7]{8}\}\}`).FindString(string(got))
|
|
if p == "" {
|
|
p = "none"
|
|
}
|
|
chunk := func(s string) string {
|
|
b, _ := json.Marshal(map[string]any{"id": "c1", "model": "m1", "choices": []any{map[string]any{"index": 0, "delta": map[string]any{"content": s}}}})
|
|
return "data: " + string(b)
|
|
}
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
split := min(9, len(p))
|
|
io.WriteString(w, sse(chunk("it is "+p[:split]), chunk(p[split:]), `data: {"id":"c1","model":"m1","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":2}}`, "data: [DONE]"))
|
|
}))
|
|
defer up.Close()
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
|
if err := provider.Save(provider.Provider{ID: "fake", Name: "Fake", Key: "k", Models: []string{"m1"}, Chat: up.URL + "/v1"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
req := `{"model":"fake/m1","max_tokens":10,"stream":true,"messages":[{"role":"user","content":"my key is ` + key + `"}]}`
|
|
|
|
// off: as it was
|
|
if code, body := post(t, "/v1/messages", req); code != 200 || !strings.Contains(string(got), key) || !strings.Contains(body, "it is none") {
|
|
t.Fatalf("off: %d %s, sent: %s", code, body, got)
|
|
}
|
|
|
|
if err := settings.Save(settings.Settings{Redact: true}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
code, body := post(t, "/v1/messages", req)
|
|
if code != 200 || strings.Contains(string(got), key) || !strings.Contains(string(got), "{{API_KEY_") {
|
|
t.Fatalf("%d, sent: %s", code, got)
|
|
}
|
|
var text strings.Builder
|
|
for _, e := range events(body) {
|
|
if d, ok := e["delta"].(map[string]any); ok {
|
|
s, _ := d["text"].(string)
|
|
text.WriteString(s)
|
|
}
|
|
}
|
|
if text.String() != "it is "+key {
|
|
t.Fatalf("text %q in:\n%s", text.String(), body)
|
|
}
|
|
}
|
|
|
|
func TestCountTokensRedacted(t *testing.T) {
|
|
const key = "sk-proj-abcdEFGH1234ijklMNOP5678qrst"
|
|
const wif = "5HueCGU8rMjxEXxiPuD5BD" + "ku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ"
|
|
cases := []struct {
|
|
name string
|
|
settings settings.Settings
|
|
value string
|
|
prefix string
|
|
}{
|
|
{"secrets", settings.Settings{Redact: true}, key, "{{API_KEY_"},
|
|
{"personal", settings.Settings{RedactPersonal: true}, "13812345678", "{{PHONE_"},
|
|
// the Bitcoin wiki's WIF example, a wallet key
|
|
{"crypto", settings.Settings{Redact: true}, wif, "{{WIF_KEY_"},
|
|
// the secrets off, the same key goes as written
|
|
{"crypto_off", settings.Settings{RedactPersonal: true}, wif, ""},
|
|
{"words", settings.Settings{RedactWords: []string{"Nightjar"}}, "Nightjar", "{{TERM_"},
|
|
// a word in other capitals only when the user asked for any case
|
|
{"words_as_written", settings.Settings{RedactWords: []string{"Nightjar"}}, "NIGHTJAR", ""},
|
|
{"words_any_case", settings.Settings{RedactWords: []string{"Nightjar"}, RedactWordsAnyCase: true}, "NIGHTJAR", "{{TERM_"},
|
|
// a word inside a longer one goes as written only with whole words
|
|
// on (acmilan_ on Discord), and a whole one is masked still
|
|
{"words_inside", settings.Settings{RedactWords: []string{"ABC"}}, "XABCD", "{{TERM_"},
|
|
{"words_whole", settings.Settings{RedactWords: []string{"ABC"}, RedactWordsWhole: true}, "XABCD", ""},
|
|
{"words_whole_alone", settings.Settings{RedactWords: []string{"ABC"}, RedactWordsWhole: true}, "name='ABC'", "{{TERM_"},
|
|
{"rules", settings.Settings{Redact: true, RedactRules: []redact.Rule{{Kind: "GW_KEY", Prefix: "acme-"}}}, "acme-Zx9ab12cdEF", "{{GW_KEY_"},
|
|
{"rules_off", settings.Settings{RedactRules: []redact.Rule{{Kind: "GW_KEY", Prefix: "acme-"}}}, "acme-Zx9ab12cdEF", ""},
|
|
{"off", settings.Settings{}, key, ""},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
f := &fake{ctype: "application/json", reply: `{"input_tokens":17}`}
|
|
setup(t, provider.Anthropic, f)
|
|
if err := settings.Save(tc.settings); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
req := `{"model":"fake/m1","system":"` + tc.value + `","messages":[{"role":"user","content":"` + tc.value + `"}],` +
|
|
`"tools":[{"name":"lookup","description":"` + tc.value + `","input_schema":{"type":"object"}}]}`
|
|
code, body := post(t, "/v1/messages/count_tokens", req)
|
|
if code != 200 || body != f.reply {
|
|
t.Fatalf("count reply: %d %q, want %q", code, body, f.reply)
|
|
}
|
|
if f.calls != 1 || f.path != "/v1/messages/count_tokens" || !json.Valid(f.got) || modelOf(f.got) != "m1" {
|
|
t.Fatalf("upstream: %d calls to %s, body %s", f.calls, f.path, f.got)
|
|
}
|
|
if tc.prefix == "" {
|
|
if strings.Count(string(f.got), tc.value) != 3 {
|
|
t.Fatalf("redaction off changed content: %s", f.got)
|
|
}
|
|
} else if strings.Contains(string(f.got), tc.value) || strings.Count(string(f.got), tc.prefix) != 3 {
|
|
t.Fatalf("system, message and tool description must be masked: %s", f.got)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCountTokensEstimateRedacted(t *testing.T) {
|
|
const contents = `{"contents":[{"role":"user","parts":[{"text":"13812345678"}]}]}`
|
|
cases := []struct {
|
|
name, path, body, field string
|
|
masked int
|
|
}{
|
|
{"anthropic_relay", "/v1/messages/count_tokens", `{"model":"fake/m1","messages":[{"role":"user","content":"13812345678"}]}`, "input_tokens", 4},
|
|
{"anthropic_unknown", "/v1/messages/count_tokens", `{"model":"unknown","messages":[{"role":"user","content":"13812345678"}]}`, "input_tokens", 4},
|
|
{"gemini", "/v1beta/models/fake/m1:countTokens", contents, "totalTokens", 2},
|
|
{"gemini_wrapped", "/v1beta/models/fake/m1:countTokens", `{"generateContentRequest":` + contents + `}`, "totalTokens", 2},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
f := &fake{}
|
|
setup(t, provider.Chat, f)
|
|
// Anthropic counts the 18-byte placeholder for this 11-byte
|
|
// phone. Gemini only estimates locally, so keeps the original.
|
|
for _, mode := range []struct {
|
|
name string
|
|
on bool
|
|
want int
|
|
}{{"off", false, 2}, {"on", true, tc.masked}} {
|
|
t.Run(mode.name, func(t *testing.T) {
|
|
if err := settings.Save(settings.Settings{RedactPersonal: mode.on}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
code, body := post(t, tc.path, tc.body)
|
|
var counts map[string]int
|
|
if err := json.Unmarshal([]byte(body), &counts); err != nil || code != 200 || counts[tc.field] != mode.want {
|
|
t.Fatalf("estimate: %d %s, want %s=%d (decode: %v)", code, body, tc.field, mode.want, err)
|
|
}
|
|
if f.calls != 0 {
|
|
t.Fatalf("local estimate made %d upstream calls", f.calls)
|
|
}
|
|
})
|
|
}
|
|
})
|
|
}
|
|
}
|