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) <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5.5 committed 2026-10-09 15:44:47 +02:00
1 parent 8932502768
commit a133270725
3 files changed
+57 -1

No files matched your search

+4
View File
@@ -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)
}
+39
View File
@@ -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, `<meta name="chat-owner" content="u-42">`) {
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
+14 -1
View File
@@ -7,6 +7,7 @@
<script defer src="/_static/katex/auto-render.min.js"></script>
<script defer src="/_static/pages.js"></script>
<meta name="csrf" content="{{.CSRF}}">
<meta name="chat-owner" content="{{.User.ID}}">
</head><body><div class="wrap">
<header>
<h1>{{t "chat.title"}}</h1>
@@ -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 {