package guard import ( "context" "encoding/json" "net/http" "net/http/httptest" "os" "path/filepath" "strings" "testing" ) // classifier is a stand-in for Lakera or Azure Content Safety: it finds // "evil" unsafe, and refuses a call without its key. func classifier(t *testing.T) (*httptest.Server, string) { key := filepath.Join(t.TempDir(), "key") if err := os.WriteFile(key, []byte("Bearer k1\n"), 0o600); err != nil { t.Fatal(err) } srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Header.Get("Authorization") != "Bearer k1" { http.Error(w, "no", http.StatusUnauthorized) return } var in struct{ Input string } _ = json.NewDecoder(r.Body).Decode(&in) flagged := strings.Contains(in.Input, "evil") _ = json.NewEncoder(w).Encode(map[string]any{"results": []any{map[string]any{"flagged": flagged, "category": "injection"}}}) })) t.Cleanup(srv.Close) return srv, key } func svcPolicy(url, key, on, action, fail string) Config { return lib(map[string]Policy{"svc": {Fail: fail, Services: []Service{{ Name: "cls", URL: url, HeaderFiles: map[string]string{"Authorization": key}, Blocked: "results.0.flagged", Reason: "results.0.category", On: on, Action: action, }}}}, "svc") } func TestAnOutsideServiceBlocksOrFlagsWhatItFindsUnsafe(t *testing.T) { srv, key := classifier(t) ctx := context.Background() g := gate(t, svcPolicy(srv.URL, key, "", "", "")) v, err := g.Input(ctx, Subject{}, chat("user", "something evil")) if err != nil || v.Action != Block || v.Rule != "service:cls:injection" { t.Errorf("unsafe: %+v %v", v, err) } if v, _ := g.Input(ctx, Subject{}, chat("user", "hello")); v.Action != Allow { t.Errorf("safe: %+v", v) } g = gate(t, svcPolicy(srv.URL, key, "input", "flag", "")) if v, _ := g.Input(ctx, Subject{}, chat("user", "evil")); v.Action != Flag { t.Errorf("flag: %+v", v) } } func TestAServiceThatCannotBeAskedIsWhatFailSays(t *testing.T) { srv, _ := classifier(t) ctx := context.Background() // The wrong key: the service answers 401. wrong := filepath.Join(t.TempDir(), "wrong") _ = os.WriteFile(wrong, []byte("Bearer nope"), 0o600) if _, err := gate(t, svcPolicy(srv.URL, wrong, "", "", "closed")).Input(ctx, Subject{}, chat("user", "hi")); err == nil { t.Error("fail: closed served a request no service could check") } if v, err := gate(t, svcPolicy(srv.URL, wrong, "", "", "open")).Input(ctx, Subject{}, chat("user", "hi")); err != nil || v.Action != Allow { t.Errorf("fail: open: %+v %v", v, err) } } func TestAnOutsideServiceReadsTheWholeAnswer(t *testing.T) { srv, key := classifier(t) g := gate(t, svcPolicy(srv.URL, key, "output", "", "")) f := g.Output(context.Background(), Subject{}) if f == nil { t.Fatal("no output filter for an output service") } out := f.Buffered([]byte(`{"choices":[{"index":0,"message":{"role":"assistant","content":"an evil plan"},"finish_reason":"stop"}]}`)) if !strings.Contains(string(out), "content_filter") || strings.Contains(string(out), "evil") || f.Verdict().Action != Block { t.Errorf("%s %+v", out, f.Verdict()) } } func TestAServiceIsCheckedWhenThePolicyIsRead(t *testing.T) { for _, s := range []Service{ {Name: "a", URL: "ftp://x", Blocked: "x"}, {Name: "b", URL: "https://x"}, {Name: "c", URL: "https://x", Blocked: "x", On: "sideways"}, {Name: "d", URL: "https://x", Blocked: "x", Body: "{{"}, } { if err := (Policy{Services: []Service{s}}).validate(); err == nil { t.Errorf("%s passed", s.Name) } } if v, ok := at(map[string]any{"a": []any{map[string]any{"b": 2.0}}}, "a.0.b"); !ok || !truthy(v) { t.Errorf("at: %v %v", v, ok) } }