package webui import ( "bytes" "context" "errors" "log/slog" "net/http" "net/http/httptest" "strings" "testing" "github.com/samcharles93/archie-core/internal/domain/identity" "github.com/samcharles93/archie-core/internal/domain/messaging" "github.com/samcharles93/archie-core/internal/releaseupdate" ) // internalDetail stands in for the SQL fragments, gRPC status details and host // paths a real contract failure carries; none of it may reach the browser. const internalDetail = `pq: relation "chat_messages" does not exist: SELECT * FROM chat_messages WHERE session_id=$1 (/var/lib/archie/state.db): rpc error: code = Internal desc = session store unavailable` var errInternal = errors.New(internalDetail) // plainChatFailure is the sentence the browser must see instead; spelled out // here so the test also compiles, and fails, against the unsanitised handlers. const plainChatFailure = "something went wrong on Archie's side. Try again; the server log has the cause." // failingChat fails the contract methods its fields name, so one fake covers // every chat endpoint. type failingChat struct { messaging.ChatContract snapshotErr, sessionErr, messagesErr, turnsErr, cancelErr, streamErr error } func (f failingChat) Snapshot(context.Context) (messaging.ChatSnapshot, error) { if f.snapshotErr != nil { return messaging.ChatSnapshot{}, f.snapshotErr } return messaging.ChatSnapshot{CancellationAvailable: true}, nil } func (f failingChat) GetSession(context.Context, string) (messaging.SessionContext, bool, error) { if f.sessionErr != nil { return messaging.SessionContext{}, false, f.sessionErr } return messaging.SessionContext{SessionID: "s1", Source: messaging.SessionSource{Platform: "web", ThreadID: "alice"}}, true, nil } func (f failingChat) RecentMessages(context.Context, string, int) ([]messaging.Message, error) { if f.messagesErr != nil { return nil, f.messagesErr } return []messaging.Message{}, nil } func (f failingChat) RecentTurns(context.Context, string, int) ([]messaging.TurnRecord, error) { if f.turnsErr != nil { return nil, f.turnsErr } return []messaging.TurnRecord{}, nil } func (f failingChat) Stream(context.Context, messaging.Inbound) (<-chan messaging.ChatEvent, error) { return nil, f.streamErr } func (f failingChat) Cancel(context.Context, string) (messaging.ChatCancellation, error) { if f.cancelErr != nil { return messaging.ChatCancellation{}, f.cancelErr } return messaging.ChatCancellation{Cancelled: true}, nil } type failingUpdates struct { ChatUpdateService err error } func (f failingUpdates) Check(context.Context, int64) (releaseupdate.Snapshot, error) { return releaseupdate.Snapshot{}, f.err } func chatErrorRequest(t *testing.T, method, target, body string) *http.Request { t.Helper() ctx := WithActingIdentity(t.Context(), identity.Identity{ID: identity.IdentityID("alice")}) req := httptest.NewRequestWithContext(ctx, method, target, strings.NewReader(body)) req.SetPathValue("id", "s1") return req } func assertSanitised(t *testing.T, body, log string) { t.Helper() if strings.Contains(body, internalDetail) { t.Errorf("response leaks the internal error: %s", body) } for _, fragment := range []string{"pq:", "chat_messages", "/var/lib", "rpc error"} { if strings.Contains(body, fragment) { t.Errorf("response leaks internal fragment %q: %s", fragment, body) } } if !strings.Contains(body, plainChatFailure) { t.Errorf("response is missing the plain message, got: %s", body) } if !strings.Contains(log, "session store unavailable") { t.Errorf("the real error was not logged server-side, log: %s", log) } } func TestChatEndpointErrorsAreSanitised(t *testing.T) { for _, tc := range []struct { name string contract failingChat handler func(*chatAPI, http.ResponseWriter, *http.Request) body string wantStatus int }{ {"sessions snapshot", failingChat{snapshotErr: errInternal}, (*chatAPI).handleChatSessions, "", http.StatusInternalServerError}, {"messages session lookup", failingChat{sessionErr: errInternal}, (*chatAPI).handleChatMessages, "", http.StatusInternalServerError}, {"messages history", failingChat{messagesErr: errInternal}, (*chatAPI).handleChatMessages, "", http.StatusInternalServerError}, {"turns session lookup", failingChat{sessionErr: errInternal}, (*chatAPI).handleChatTurns, "", http.StatusInternalServerError}, {"turns history", failingChat{turnsErr: errInternal}, (*chatAPI).handleChatTurns, "", http.StatusInternalServerError}, {"cancel snapshot", failingChat{snapshotErr: errInternal}, (*chatAPI).handleChatCancel, `{"session_id":"s1"}`, http.StatusInternalServerError}, {"cancel session lookup", failingChat{sessionErr: errInternal}, (*chatAPI).handleChatCancel, `{"session_id":"s1"}`, http.StatusInternalServerError}, {"cancel call", failingChat{cancelErr: errInternal}, (*chatAPI).handleChatCancel, `{"session_id":"s1"}`, http.StatusInternalServerError}, } { t.Run(tc.name, func(t *testing.T) { var logBuf bytes.Buffer srv := &Server{Log: slog.New(slog.NewTextHandler(&logBuf, nil)), Chat: &ChatService{Contract: tc.contract}} rec := httptest.NewRecorder() tc.handler(newChatAPI(srv), rec, chatErrorRequest(t, "POST", "https://archie.example/api/chat/x", tc.body)) if rec.Code != tc.wantStatus { t.Fatalf("status=%d want=%d body=%s", rec.Code, tc.wantStatus, rec.Body.String()) } assertSanitised(t, rec.Body.String(), logBuf.String()) }) } } func TestChatStreamErrorFrameIsSanitised(t *testing.T) { var logBuf bytes.Buffer srv := &Server{Log: slog.New(slog.NewTextHandler(&logBuf, nil)), Chat: &ChatService{Contract: failingChat{streamErr: errInternal}}} rec := httptest.NewRecorder() req := chatErrorRequest(t, "POST", "https://archie.example/api/chat/stream", `{"channel_id":"c","text":"hi"}`) newChatAPI(srv).handleChatStream(rec, req) body := rec.Body.String() if !strings.Contains(body, `"type":"error"`) { t.Fatalf("no error frame in stream: %s", body) } assertSanitised(t, body, logBuf.String()) } func TestChatUpdateCheckErrorIsSanitised(t *testing.T) { for _, tc := range []struct { name string err error wantStatus int wantBody string }{ {"internal cause", errInternal, http.StatusBadGateway, plainChatFailure}, {"not configured", releaseupdate.ErrNotConfigured, http.StatusNotImplemented, "updates are not configured"}, } { t.Run(tc.name, func(t *testing.T) { var logBuf bytes.Buffer srv := &Server{Log: slog.New(slog.NewTextHandler(&logBuf, nil)), Chat: &ChatService{Contract: failingChat{}, Updates: failingUpdates{err: tc.err}}} rec := httptest.NewRecorder() newChatAPI(srv).handleChatUpdate(rec, chatErrorRequest(t, "GET", "https://archie.example/api/chat/update", "")) if rec.Code != tc.wantStatus { t.Fatalf("status=%d want=%d body=%s", rec.Code, tc.wantStatus, rec.Body.String()) } if got := strings.TrimSpace(rec.Body.String()); got != tc.wantBody { t.Fatalf("body=%q want=%q", got, tc.wantBody) } if errors.Is(tc.err, errInternal) { assertSanitised(t, rec.Body.String(), logBuf.String()) } }) } } func TestChatValidationMessagesStayUserFacing(t *testing.T) { for _, tc := range []struct { name, body, want string }{ {"missing fields", `{"channel_id":"","text":""}`, "text and channel_id are required"}, {"undecodable", `{`, "invalid chat message"}, {"empty voice", `{"channel_id":"c","text":"hi","voice":{"data":"","mime_type":"audio/ogg"}}`, "voice clip is empty or too long"}, } { t.Run(tc.name, func(t *testing.T) { rec := httptest.NewRecorder() req := chatErrorRequest(t, "POST", "https://archie.example/api/chat/stream", tc.body) if _, ok := newChatAPI(&Server{}).decodeChatMessage(rec, req); ok { t.Fatal("message accepted") } if rec.Code != http.StatusBadRequest { t.Fatalf("status=%d want=400", rec.Code) } if got := strings.TrimSpace(rec.Body.String()); got != tc.want { t.Fatalf("body=%q want=%q", got, tc.want) } }) } }