normalize stepped each rune by utf8.RuneLen(r). For a malformed byte
that is RuneLen(RuneError) = 3, while the byte itself is 1 long, so an
Embedding input like "hello \xff" (the reporter's) sliced past the
text's end at tokenizer.go:266. The panic ran on the pii worker
goroutine ((*backend).find.func2), outside the request's recovery, so
the whole magpie process exited. Both loops (the mapper and the
grapheme fallback) now step by utf8.DecodeRuneInString's size.
The patch is ggbdpq's, the same one SunnyRx proposed on the issue.
Tests: TestNormalizeMalformedBytes (with the reporter's "hello \xff"
and an encode bounds check), TestNormalizeMappedMalformedByte,
TestModelFindsAroundMalformedBytes (real Guard model 7216b6a3b897,
spans in bounds, the phone still found), and
TestEmbeddingsWithAStrayByteUnderAstrLinkGuard in the gateway: the
reporter's body {"model":"lib/embed-1","input":["hello \xff","world"]}
returns 200, the next request's phone is masked, the model is Running.
Red on main: TestModelFindsAroundMalformedBytes panics "slice bounds
out of range [:3] with length 1" at tokenizer.go:266; the gateway test
on main's code ends the test binary with the reporter's trace through
(*backend).find.func2 (mote, macOS). On Windows (xiaomi-gpu, go1.26.8)
with main's tokenizer and this stack's recovery, the gateway test is
red with the same tokenizer.go:266 trace, caught as a 503.
Run (mote, sandbox HOME, MAGPIE_GUARD_DIR = a copy of the real model):
gofmt -l clean; go vet ./..., GOOS=linux go vet -tags nogui ./...,
GOOS=windows go vet ./... ok; go test -tags nogui -timeout 45m
-count=1 ./internal/pii/... ./internal/masking/ ./internal/gateway/.
Windows via win-run: ./internal/pii/... ./internal/masking/ ok, and
-run Embeddings ./internal/gateway/ ok.
Not tried: a built binary or a live gateway with a real upstream; the
full ./... suite. The gateway test writes the model into the cache
folder named for guardRev's first 12 characters.
Co-authored-by: ggbdpq <ggbdpq@gmail.com>
Co-authored-by: SunnyRx <7306148+SunnyRx@users.noreply.github.com>
77 lines
2.4 KiB
Go
77 lines
2.4 KiB
Go
package gateway
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/appdir"
|
|
"github.com/yetone/magpie/internal/pii"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// #4553 (SunnyRx, Windows): with AstrLink Guard masking requests, an
|
|
// embeddings input with a stray byte (the reporter's body, a lone 0xff)
|
|
// panicked in the model's tokenizer, on the model's own goroutine, and
|
|
// magpie exited with code 2. The real model reads it now, the vendor gets
|
|
// the request, and the next request is served. It needs the model's
|
|
// files, in MAGPIE_GUARD_DIR, as internal/pii/guard's tests do.
|
|
func TestEmbeddingsWithAStrayByteUnderAstrLinkGuard(t *testing.T) {
|
|
src := os.Getenv("MAGPIE_GUARD_DIR")
|
|
if src == "" {
|
|
t.Skip("MAGPIE_GUARD_DIR is not set")
|
|
}
|
|
s, l := shelved(t)
|
|
// where pii looks for the pinned revision's files
|
|
dst := filepath.Join(appdir.Cache(), "astrlink-guard", "7216b6a3b897")
|
|
if err := os.MkdirAll(dst, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, f := range []string{"model_int8.onnx", "model_int8.onnx_data", "tokenizer.json"} {
|
|
copyFile(t, filepath.Join(src, f), filepath.Join(dst, f))
|
|
}
|
|
g := pii.Of(pii.AstrLinkGuard)
|
|
if !g.Installed() {
|
|
t.Fatalf("the model isn't installed from %s: the pinned revision moved, and this test's folder with it", dst)
|
|
}
|
|
t.Cleanup(g.Stop)
|
|
if err := settings.Save(settings.Settings{Redact: true, RedactPersonal: true, RedactModel: true, RedactModelName: pii.AstrLinkGuard}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
body := `{"model":"lib/embed-1","input":["hello ` + string([]byte{0xff}) + `","world"]}`
|
|
if code, raw := postRetrieval(t, s, "/v1/embeddings", body); code != 200 {
|
|
t.Fatalf("%d %s", code, raw)
|
|
}
|
|
if code, raw := postRetrieval(t, s, "/v1/embeddings", `{"model":"lib/embed-1","input":["call 13800138000","world"]}`); code != 200 {
|
|
t.Fatalf("the next request: %d %s", code, raw)
|
|
}
|
|
sent := l.got("/v1/embeddings")
|
|
if len(sent) != 2 || !strings.Contains(sent[0], "hello") || strings.Contains(sent[1], "13800138000") {
|
|
t.Fatalf("vendor was sent %q", sent)
|
|
}
|
|
if st := g.Now(); !st.Running {
|
|
t.Fatalf("the model didn't read the requests: %+v", st)
|
|
}
|
|
}
|
|
|
|
func copyFile(t *testing.T, from, to string) {
|
|
t.Helper()
|
|
in, err := os.Open(from)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer in.Close()
|
|
out, err := os.Create(to)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := io.Copy(out, in); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := out.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|