Private
Public Access
Purge the PWA page cache on every login/logout/expiry transition (R2-S3, #35)
The service worker cached authenticated pages in one origin-wide cache and only purged it when the logout request itself passed through it — not on session expiry, externally cleared cookies, or an account switch, so a shared device could show the previous session's pages offline. Every HTML response now carries X-Evection-Auth: an opaque HMAC of the user (or "anon"), via a small middleware. The service worker remembers the marker its cached pages belong to and, whenever a navigation brings a different one, deletes the pages cache before storing anything; "anon" responses (the login page) are never cached. So the cache always holds exactly one session's pages, and any transition the worker can observe online clears it. (What no design can detect is a session ending while the device stays offline.) Verified in a real browser: pages cached after login; expiring the session server-side (no logout request) and loading a page left the cache deleted and the marker "anon"; logging in as a second user afterward cached only that user's pages, none of the first user's. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
91a4c360a4
commit
aacb41d1d3
4 files changed
+107
-3
No files matched your search
+29
-3
@@ -42,12 +42,38 @@ async function trimCache(cacheName, maxItems) {
|
||||
await Promise.all(toDelete.map((key) => cache.delete(key)));
|
||||
}
|
||||
|
||||
// Every HTML response carries X-Evection-Auth: an opaque id for who it was
|
||||
// rendered for ("anon" when no one is logged in — see apps.pwa.middleware).
|
||||
// PAGES_CACHE holds one person's pages; the marker it belongs to is kept in
|
||||
// the shell cache (a service worker's own memory doesn't survive being
|
||||
// stopped). Any change — session expired, logged in, switched accounts —
|
||||
// purges the pages before anything new is stored, so one session's
|
||||
// authenticated HTML can never be served to the next, whether or not the
|
||||
// logout request itself passed through this worker.
|
||||
const AUTH_MARKER_KEY = "/__evection-auth-marker__";
|
||||
|
||||
async function syncAuthMarker(response) {
|
||||
const marker = response.headers.get("X-Evection-Auth");
|
||||
if (marker === null) return null; // not an HTML page we can attribute
|
||||
const shell = await caches.open(SHELL_CACHE);
|
||||
const stored = await shell.match(AUTH_MARKER_KEY);
|
||||
const previous = stored ? await stored.text() : null;
|
||||
if (previous !== marker) {
|
||||
await caches.delete(PAGES_CACHE);
|
||||
await shell.put(AUTH_MARKER_KEY, new Response(marker));
|
||||
}
|
||||
return marker;
|
||||
}
|
||||
|
||||
async function networkFirstForNavigation(request) {
|
||||
try {
|
||||
const response = await fetch(request);
|
||||
const cache = await caches.open(PAGES_CACHE);
|
||||
cache.put(request, response.clone());
|
||||
trimCache(PAGES_CACHE, MAX_CACHED_PAGES);
|
||||
const marker = await syncAuthMarker(response);
|
||||
if (marker !== null && marker !== "anon") {
|
||||
const cache = await caches.open(PAGES_CACHE);
|
||||
cache.put(request, response.clone());
|
||||
trimCache(PAGES_CACHE, MAX_CACHED_PAGES);
|
||||
}
|
||||
return response;
|
||||
} catch (err) {
|
||||
const cached = await caches.match(request);
|
||||
|
||||
Reference in new issue
Block a user