From a1332707252fb6c202d252452cbb002506fb7e19 Mon Sep 17 00:00:00 2001 From: Ilker Manap Date: Fri, 9 Oct 2026 15:44:47 +0200 Subject: [PATCH] The chat's kept conversation belongs to the account that kept it It sat in localStorage under one fixed key, so the next person to sign in on the same browser opened the chat on the last one's conversation. It is now kept under the account's id, anyone else's is dropped when the page loads, and signing out sends Clear-Site-Data: "storage" so nothing the pages kept outlives the session. Co-Authored-By: Claude Opus 5.5 (1M context) --- internal/web/account.go | 4 ++++ internal/web/chat_test.go | 39 ++++++++++++++++++++++++++++++++ internal/web/templates/chat.html | 15 +++++++++++- 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/internal/web/account.go b/internal/web/account.go index 5d03db9..e421247 100644 --- a/internal/web/account.go +++ b/internal/web/account.go @@ -327,6 +327,10 @@ func (p *AccountPages) logout(w http.ResponseWriter, r *http.Request) { } } clearSessionCookie(w, r) + // What the pages kept in this browser goes with the session: the chat's + // conversation, htmx's copies of the pages visited. The chat keys its + // own by account as well, for a browser that ignores this header. + w.Header().Set("Clear-Site-Data", `"storage"`) http.Redirect(w, r, "/login", http.StatusSeeOther) } diff --git a/internal/web/chat_test.go b/internal/web/chat_test.go index 611683e..ac1274a 100644 --- a/internal/web/chat_test.go +++ b/internal/web/chat_test.go @@ -1,6 +1,8 @@ package web import ( + "io" + "log/slog" "net/http" "net/http/httptest" "strings" @@ -113,6 +115,43 @@ func TestTheChatPageOffersWhatItWasGiven(t *testing.T) { } } +func TestTheKeptConversationBelongsToWhoeverKeptIt(t *testing.T) { + // One browser, two people: an administrator signs out, a new account signs + // in, and the chat must not open on the administrator's conversation. + h := Chat(store.User{ID: "u-42", Email: "a@example.com"}, []ChatModel{{Name: "m"}}, nil, "", "v1", ChatKnowledge{}) + w := httptest.NewRecorder() + h(w, httptest.NewRequest(http.MethodGet, "http://x/chat", nil)) + page := w.Body.String() + if !strings.Contains(page, ``) { + t.Fatal("the page does not say whose conversation it keeps") + } + if !strings.Contains(page, `const KEY = PREFIX + "." + document.querySelector('meta[name="chat-owner"]').content;`) { + t.Error("the conversation is not kept under the account") + } + // The unowned key from before, and anyone else's, are dropped before the + // kept conversation is read. + drop, read := strings.Index(page, "localStorage.removeItem(k)"), strings.Index(page, "const kept = load_()") + if drop < 0 || read < 0 || drop > read { + t.Error("someone else's conversation is not dropped before this one is put back") + } +} + +func TestSigningOutClearsWhatTheBrowserKept(t *testing.T) { + p := NewAccountPages(fakeAccounts{user: store.User{ID: "u1", Email: "a@example.com"}}, nil, nil, "v0", + slog.New(slog.NewTextHandler(io.Discard, nil))) + r := httptest.NewRequest(http.MethodPost, "http://x/logout", strings.NewReader("csrf=tok")) + r.Header.Set("Content-Type", "application/x-www-form-urlencoded") + r.AddCookie(&http.Cookie{Name: SessionCookie, Value: "good"}) + w := httptest.NewRecorder() + p.logout(w, r) + if w.Code != http.StatusSeeOther { + t.Fatalf("status %d", w.Code) + } + if got := w.Header().Get("Clear-Site-Data"); got != `"storage"` { + t.Errorf("Clear-Site-Data = %q, want \"storage\"", got) + } +} + func TestTheSimulatedModelSaysSo(t *testing.T) { // big-model returns Greek letters at a fixed rate. Offered next to a real // model with nothing to tell them apart, it reads as a broken model rather diff --git a/internal/web/templates/chat.html b/internal/web/templates/chat.html index e56b820..7fa67dd 100644 --- a/internal/web/templates/chat.html +++ b/internal/web/templates/chat.html @@ -7,6 +7,7 @@ +

{{t "chat.title"}}

@@ -131,10 +132,22 @@ // In this browser's storage, and only there: the gateway keeps no // conversation. Every read and write is guarded — a private window, or a // browser that refuses storage, simply starts empty each time. + // + // Kept under the account's id: a browser is often shared, and the next + // person to sign in on it must not be handed the last one's conversation. + // Anyone else's left here is dropped on the way in, as is the one from + // before the key carried an owner. const sys = document.getElementById("system"); const temp = document.getElementById("temperature"); const tempOut = document.getElementById("temp-out"); - const KEY = "llmgw.chat.v1"; + const PREFIX = "llmgw.chat.v1"; + const KEY = PREFIX + "." + document.querySelector('meta[name="chat-owner"]').content; + try { + for (let i = localStorage.length - 1; i >= 0; i--) { + const k = localStorage.key(i); + if (k && (k === PREFIX || k.startsWith(PREFIX + ".")) && k !== KEY) localStorage.removeItem(k); + } + } catch (_) { /* no storage: nothing to drop */ } const load_ = () => { try { return JSON.parse(localStorage.getItem(KEY) || "null"); } catch (_) { return null; } }; const save = () => { try {