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
@@ -66,3 +66,42 @@ def test_base_template_links_manifest_and_registers_service_worker(client, djang
|
||||
assert 'rel="manifest"' in content
|
||||
assert "register-sw.js" in content
|
||||
assert "apple-touch-icon" in content
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_html_responses_are_stamped_with_who_they_were_rendered_for(client, django_user_model):
|
||||
"""R2-S3: the service worker purges its page cache whenever this marker
|
||||
changes, so it has to be stable per user, different between users,
|
||||
and "anon" when logged out."""
|
||||
django_user_model.objects.create_user("alice", password="pw-alice-123")
|
||||
django_user_model.objects.create_user("bob", password="pw-bob-123")
|
||||
|
||||
anon = client.get(reverse("accounts:login"))["X-Evection-Auth"]
|
||||
assert anon == "anon"
|
||||
|
||||
client.login(username="alice", password="pw-alice-123")
|
||||
alice_1 = client.get(reverse("reader:stream"))["X-Evection-Auth"]
|
||||
alice_2 = client.get(reverse("reader:stream"))["X-Evection-Auth"]
|
||||
client.logout()
|
||||
client.login(username="bob", password="pw-bob-123")
|
||||
bob = client.get(reverse("reader:stream"))["X-Evection-Auth"]
|
||||
|
||||
assert alice_1 == alice_2
|
||||
assert len({anon, alice_1, bob}) == 3
|
||||
assert "alice" not in alice_1 and not alice_1.isdigit()
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_non_html_responses_are_not_stamped(client, django_user_model):
|
||||
django_user_model.objects.create_user("alice", password="pw-alice-123")
|
||||
client.login(username="alice", password="pw-alice-123")
|
||||
|
||||
assert "X-Evection-Auth" not in client.get(reverse("core:health"))
|
||||
|
||||
|
||||
def test_service_worker_purges_its_page_cache_when_the_auth_marker_changes(client):
|
||||
body = client.get(reverse("pwa:service_worker")).content.decode()
|
||||
|
||||
assert "X-Evection-Auth" in body
|
||||
assert "caches.delete(PAGES_CACHE)" in body
|
||||
assert "previous !== marker" in body
|
||||
Reference in new issue
Block a user