From d7b433e5806ee310d59b182301705c6d3d3295a2 Mon Sep 17 00:00:00 2001 From: Keith Smith Date: Sat, 31 Oct 2026 12:03:10 -0600 Subject: [PATCH] Purge the service worker's authenticated-page cache on logout (S3) PAGES_CACHE held every cached navigation response (including authenticated article pages) with no logout-time purge, so a later offline navigation on a shared or account-switched device could still serve another user's cached pages. Logout is a plain POST, so the fetch handler now special-cases that request: it deletes PAGES_CACHE and lets the real logout request through, with no race against the navigation that follows since it's synchronous with the same request. Co-Authored-By: Claude Sonnet 5 --- apps/pwa/tests.py | 5 +++++ templates/pwa/sw.js | 16 +++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/apps/pwa/tests.py b/apps/pwa/tests.py index 0f14d4a..423698a 100644 --- a/apps/pwa/tests.py +++ b/apps/pwa/tests.py @@ -32,6 +32,11 @@ def test_service_worker_is_served_at_root_with_correct_type_and_scope(client): assert 'self.addEventListener("fetch"' in body # Mutations must stay network-only, never intercepted. assert 'request.method !== "GET"' in body + # Logging out must purge PAGES_CACHE so a later offline navigation on + # a shared/account-switched device can't serve another user's + # previously cached authenticated pages. + assert "caches.delete(PAGES_CACHE)" in body + assert reverse("accounts:logout") in body @pytest.mark.django_db diff --git a/templates/pwa/sw.js b/templates/pwa/sw.js index 5fd79da..424d127 100644 --- a/templates/pwa/sw.js +++ b/templates/pwa/sw.js @@ -66,8 +66,23 @@ async function cacheFirstForAsset(request) { return response; } +const LOGOUT_PATH = "{% url 'accounts:logout' %}"; + self.addEventListener("fetch", (event) => { const { request } = event; + const url = new URL(request.url); + + // Logging out ends this device's session, so any authenticated HTML + // sitting in PAGES_CACHE must not outlive it — otherwise a later + // offline navigation (shared device, different account logging in + // next) could still serve a previous user's cached article pages. + // Purging it synchronously with the logout request itself, rather + // than via a postMessage from the page, means there's no race with + // the navigation that follows. + if (request.method === "POST" && url.pathname === LOGOUT_PATH) { + event.respondWith(caches.delete(PAGES_CACHE).then(() => fetch(request))); + return; + } // Mutations (mark-read, star, OPML upload, forms...) are always // network-only — never intercept anything but GET. @@ -78,7 +93,6 @@ self.addEventListener("fetch", (event) => { return; } - const url = new URL(request.url); if (url.pathname.startsWith("/static/")) { event.respondWith(cacheFirstForAsset(request)); }