okami.mom/web-extract-go

git clone https://git.nkp.pet/okami.mom/web-extract-go

nekomimi.petadmin: show what each request's decisions would cost on Jev8af1784

main
9.9 KiB268 linesraw
1package app
2
3import (
4	"context"
5	"encoding/json"
6	"fmt"
7	"net/http"
8	"net/http/httptest"
9	"os"
10	"os/signal"
11	"strings"
12	"syscall"
13	"testing"
14	"time"
15)
16
17func adminCall(handler http.Handler, method, path, body string, cookie *http.Cookie, headers map[string]string) *httptest.ResponseRecorder {
18	r := httptest.NewRequest(method, path, strings.NewReader(body))
19	r.Header.Set("Content-Type", "application/json")
20	if cookie != nil {
21		r.AddCookie(cookie)
22	}
23	for key, value := range headers {
24		r.Header.Set(key, value)
25	}
26	w := httptest.NewRecorder()
27	handler.ServeHTTP(w, r)
28	return w
29}
30
31func adminLogin(t *testing.T, handler http.Handler, password string) *http.Cookie {
32	t.Helper()
33	w := adminCall(handler, "POST", "/admin/api/login", jsonText(map[string]string{"password": password}), nil, nil)
34	if w.Code != 200 {
35		t.Fatalf("login: %d %s", w.Code, w.Body.String())
36	}
37	cookies := w.Result().Cookies()
38	if len(cookies) != 1 || cookies[0].Name != adminCookie {
39		t.Fatal("missing session cookie")
40	}
41	return cookies[0]
42}
43
44func TestAdminAuthenticationBoundary(t *testing.T) {
45	s, _ := fixtureService(t)
46	for _, path := range []string{"/admin", "/admin/", "/admin/api/overview"} {
47		if w := adminCall(s.handler(), "GET", path, "", nil, nil); w.Code != 404 {
48			t.Fatalf("disabled dashboard: %s %d", path, w.Code)
49		}
50	}
51	s.cfg.AdminPassword, s.cfg.AdminCookieSecure = "test-admin-password", true
52	h := s.handler()
53	for _, path := range []string{"/admin/api/session", "/admin/api/overview", "/admin/api/requests", "/admin/api/requests/secret"} {
54		w := adminCall(h, "GET", path, "", nil, map[string]string{"x-api-key": s.cfg.Token})
55		if w.Code != 401 {
56			t.Fatalf("api key bypass: %s %d", path, w.Code)
57		}
58		if w.Header().Get("Cache-Control") != "no-store" {
59			t.Fatal("sensitive response may be cached")
60		}
61	}
62	if w := adminCall(h, "POST", "/admin/api/login", `{"password":"wrong-password"}`, nil, nil); w.Code != 401 {
63		t.Fatal(w.Code)
64	}
65	cookie := adminLogin(t, h, s.cfg.AdminPassword)
66	if !cookie.HttpOnly || !cookie.Secure || cookie.SameSite != http.SameSiteStrictMode || cookie.Path != "/admin" || cookie.MaxAge != 43200 {
67		t.Fatalf("unsafe cookie: %+v", cookie)
68	}
69	if w := adminCall(h, "GET", "/admin/api/session", "", cookie, nil); w.Code != 200 {
70		t.Fatal(w.Code)
71	}
72	if w := adminCall(s.handler(), "GET", "/admin/api/session", "", cookie, nil); w.Code != 401 {
73		t.Fatal("a new server handler accepted an old process-local session")
74	}
75	if w := adminCall(h, "POST", "/search", `{"query":"limits"}`, cookie, nil); w.Code != 401 {
76		t.Fatal("admin session bypassed public api auth")
77	}
78	if w := adminCall(h, "POST", "/admin/api/mcp", `{}`, cookie, nil); w.Code != 404 {
79		t.Fatal("unexpected mcp bridge")
80	}
81	for _, headers := range []map[string]string{{"Origin": "https://evil.example"}, {"Sec-Fetch-Site": "cross-site"}} {
82		w := adminCall(h, "POST", "/admin/api/logout", `{}`, cookie, headers)
83		if w.Code != 403 {
84			t.Fatalf("csrf accepted: %d", w.Code)
85		}
86		w = adminCall(h, "POST", "/admin/api/login", `{"password":"test-admin-password"}`, nil, headers)
87		if w.Code != 403 {
88			t.Fatal("login csrf accepted")
89		}
90	}
91	w := adminCall(h, "POST", "/admin/api/logout", `{}`, cookie, map[string]string{"Content-Type": "text/plain"})
92	if w.Code != 415 {
93		t.Fatal("simple cross-site content type accepted")
94	}
95	w = adminCall(h, "POST", "/admin/api/logout", `{}`, cookie, nil)
96	if w.Code != 200 || w.Result().Cookies()[0].MaxAge != -1 {
97		t.Fatal("logout failed")
98	}
99	if w := adminCall(h, "GET", "/admin/api/overview", "", cookie, nil); w.Code != 401 {
100		t.Fatal("revoked cookie still authorized")
101	}
102	if w := adminCall(h, "GET", "/admin/api/session", "", &http.Cookie{Name: adminCookie, Value: "forged"}, nil); w.Code != 401 {
103		t.Fatal("forged cookie accepted")
104	}
105	page := adminCall(h, "GET", "/admin/", "", nil, nil)
106	if page.Code != 200 || !strings.Contains(page.Body.String(), "/admin/assets/") {
107		t.Fatal("embedded ui missing")
108	}
109	if !strings.Contains(page.Header().Get("Content-Security-Policy"), "frame-ancestors 'none'") {
110		t.Fatal("missing csp")
111	}
112	if w := adminCall(h, "GET", "/admin/.env", "", nil, nil); w.Code != 404 {
113		t.Fatal("static handler exposed non-assets")
114	}
115}
116
117func TestAdminLoginRateLimit(t *testing.T) {
118	s, _ := fixtureService(t)
119	s.cfg.AdminPassword = "test-admin-password"
120	h := s.handler()
121	for i := range 11 {
122		w := adminCall(h, "POST", "/admin/api/login", `{"password":"incorrect"}`, nil, nil)
123		want := 401
124		if i == 10 {
125			want = 429
126		}
127		if w.Code != want {
128			t.Fatalf("attempt %d: %d", i, w.Code)
129		}
130	}
131	if w := adminCall(h, "POST", "/admin/api/login", `{"password":"test-admin-password"}`, nil, nil); w.Code != 429 {
132		t.Fatal("rate limit bypass")
133	}
134}
135
136func TestAdminPlaygroundLogsAndReadModels(t *testing.T) {
137	s, page := fixtureService(t)
138	s.cfg.AdminPassword = "test-admin-password"
139	s.cfg.JevKey, s.cfg.LLMKey = "secret-jev-key", "secret-llm-key"
140	h := s.handler()
141	cookie := adminLogin(t, h, s.cfg.AdminPassword)
142	body := fmt.Sprintf(`{"urls":[%q,%q],"highlights":{"query":"request size"},"summary":{"query":"request size"}}`, page.URL, page.URL+"/missing")
143	w := adminCall(h, "POST", "/admin/api/contents", body, cookie, nil)
144	if w.Code != 200 {
145		t.Fatalf("contents: %s", w.Body.String())
146	}
147	var response ResultsResponse
148	if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil {
149		t.Fatal(err)
150	}
151	if len(response.Results) != 1 || len(response.Statuses) != 2 || response.Statuses[1].Error == nil {
152		t.Fatal("lost partial failure")
153	}
154	trace, err := s.log.adminTrace(context.Background(), response.RequestID)
155	if err != nil {
156		t.Fatal(err)
157	}
158	if len(trace.Requests) != 2 || len(trace.Completions) != 1 || len(trace.Decisions) == 0 || len(trace.Evidence) != 1 {
159		t.Fatalf("incomplete trace: %+v", trace)
160	}
161	if trace.Requests[0]["caller"] != "admin-dashboard" {
162		t.Fatal("missing dashboard caller")
163	}
164	if trace.Completions[0]["response"] != *response.Results[0].Summary {
165		t.Fatal("training reply changed")
166	}
167	w = adminCall(h, "POST", "/admin/api/search", `{"query":""}`, cookie, nil)
168	if w.Code != 400 {
169		t.Fatal("public request validation not reused")
170	}
171	w = adminCall(h, "GET", "/admin/api/overview", "", cookie, nil)
172	if w.Code != 200 {
173		t.Fatal(w.Body.String())
174	}
175	for _, secret := range []string{s.cfg.AdminPassword, s.cfg.Token, s.cfg.JevKey, s.cfg.LLMKey} {
176		if strings.Contains(w.Body.String(), secret) {
177			t.Fatal("credentials in overview")
178		}
179	}
180	var stats struct {
181		Requests struct{ Total, Errors int }
182		Models   struct{ Calls int }
183		Tokens   struct{ Input, Output int }
184	}
185	if err := json.Unmarshal(w.Body.Bytes(), &stats); err != nil {
186		t.Fatal(err)
187	}
188	if stats.Requests.Total != 2 || stats.Requests.Errors != 2 || stats.Models.Calls != len(trace.Decisions)+1 || stats.Tokens.Input != 250 || stats.Tokens.Output != 20 {
189		t.Fatalf("wrong totals: %+v", stats)
190	}
191	for _, path := range []string{"/admin/api/requests?before=oops", "/admin/api/requests?endpoint=extract"} {
192		if w := adminCall(h, "GET", path, "", cookie, nil); w.Code != 400 {
193			t.Fatal("invalid filter accepted")
194		}
195	}
196	if w := adminCall(h, "GET", "/admin/api/requests/missing", "", cookie, nil); w.Code != 404 {
197		t.Fatal("unknown trace should be missing")
198	}
199}
200
201// Opt-in browser fixture: real HTTP/pipeline/SQLite, local mocked providers, no paid calls.
202func TestAdminBrowserFixture(t *testing.T) {
203	path := os.Getenv("ADMIN_BROWSER_FIXTURE")
204	if path == "" {
205		t.Skip("set ADMIN_BROWSER_FIXTURE to start the browser-test server")
206	}
207	s, page := fixtureService(t)
208	s.cfg.AdminPassword = "browser-fixture-password"
209	s.cfg.JevKey, s.cfg.JevModel, s.cfg.LLMKey, s.cfg.LLMModel = "fixture", "jev-test", "fixture", "configured-model"
210	h := s.handler()
211	cookie := adminLogin(t, h, s.cfg.AdminPassword)
212	for i := range 32 {
213		adminCall(h, "POST", "/admin/api/search", fmt.Sprintf(`{"query":"fixture request limits %d","numResults":1}`, i), cookie, nil)
214	}
215	adminCall(h, "POST", "/admin/api/search", `{"query":""}`, cookie, nil)
216	server := httptest.NewServer(h)
217	defer server.Close()
218	if err := os.WriteFile(path, []byte(jsonText(map[string]string{"url": server.URL, "sourceURL": page.URL, "password": s.cfg.AdminPassword})), 0600); err != nil {
219		t.Fatal(err)
220	}
221	ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
222	defer stop()
223	<-ctx.Done()
224}
225
226func TestAdminPaginationAndHistoricalRows(t *testing.T) {
227	s, _ := fixtureService(t)
228	for i := range 65 {
229		ctx := context.WithValue(context.Background(), contextKey{}, callContext{ID: fmt.Sprint(i), Caller: "test"})
230		s.log.request(ctx, "search", map[string]string{"query": "query"}, "ok")
231	}
232	ctx := context.Background()
233	first, err := s.adminRequests(ctx, 0, "", false)
234	if err != nil || len(first.Rows) != 30 || first.NextBefore != 36 {
235		t.Fatalf("first: %+v %v", first, err)
236	}
237	second, err := s.adminRequests(ctx, first.NextBefore, "search", false)
238	if err != nil || len(second.Rows) != 30 || second.Rows[0]["id"] != int64(35) {
239		t.Fatal("pagination skipped or duplicated rows")
240	}
241	third, err := s.adminRequests(ctx, second.NextBefore, "", false)
242	if err != nil || len(third.Rows) != 5 || third.NextBefore != 0 {
243		t.Fatal("last page invalid")
244	}
245	empty, err := s.adminRequests(ctx, 0, "", true)
246	if err != nil || empty.Rows == nil || len(empty.Rows) != 0 {
247		t.Fatal("empty page should be an array")
248	}
249	s.log.write(`INSERT INTO requests(created_at,request_id,endpoint,detail,outcome) VALUES(?,?,?,?,?)`, time.Now().Unix(), "legacy", "search", "not-json", "ok")
250	trace, err := s.log.adminTrace(ctx, "legacy")
251	if err != nil || trace.Requests[0]["detail"] != "not-json" || trace.Decisions == nil {
252		t.Fatal("legacy rows not preserved")
253	}
254}
255
256func TestAdminConfig(t *testing.T) {
257	t.Setenv("API_AUTH_TOKEN", "local-token")
258	t.Setenv("ADMIN_PASSWORD", "short")
259	if _, err := readConfig(); err == nil {
260		t.Fatal("weak password accepted")
261	}
262	t.Setenv("ADMIN_PASSWORD", "test-admin-password")
263	t.Setenv("ADMIN_COOKIE_SECURE", "true")
264	c, err := readConfig()
265	if err != nil || !c.AdminCookieSecure || c.AdminPassword != "test-admin-password" {
266		t.Fatal("admin config missing")
267	}
268}