Skip to content

Commit 5e8fb6e

Browse files
MartinCastroAlvarezmartin-castro-laminr-aiclaude
authored
fix(security): origin check on service-worker message handler (CodeQL js/missing-origin-check) (#208)
The PWA service worker's `message` handler (the `dar:purge` cache-on-logout hook, #200) processed messages without verifying the sender origin — CodeQL `js/missing-origin-check` (medium). A cross-origin frame must never be able to drive the SW cache. Add `if (event.origin && event.origin !== self.location.origin) return;` so only same-origin clients (the SPA pages this worker controls) can trigger a purge. Same-origin internal `client.postMessage` (empty origin) is still accepted; anything cross-origin is dropped. This is the one open CodeQL alert on main (the other 10 are fixed via #191/#193). Clears it → 0 open. Test asserts the served SW embeds the origin check. Co-authored-by: Martin Castro Laminrs <mcastro@laminr.ai> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent e19931f commit 5e8fb6e

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

django_admin_react/templates/admin_react/sw.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ self.addEventListener('activate', (event) => {
4646

4747
// --- cache purge on logout (contract §5) ----------------------------------
4848
self.addEventListener('message', (event) => {
49+
// Origin check (CodeQL js/missing-origin-check): only honour messages
50+
// from our own origin — the SPA pages this worker controls. A
51+
// cross-origin frame must never be able to drive the cache. When the
52+
// message comes from a same-origin client, `event.origin` is our
53+
// origin; for some internal client.postMessage paths it can be the
54+
// empty string — both are accepted, anything else is dropped.
55+
if (event.origin && event.origin !== self.location.origin) {
56+
return;
57+
}
4958
if (event.data && event.data.type === 'dar:purge') {
5059
event.waitUntil(
5160
(async () => {

tests/test_pwa.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ def test_sw_embeds_mount_and_security_guards(anon_client: Client) -> None:
121121
assert "request.method !== 'GET'" in js
122122
# Cache-purge-on-logout message handler.
123123
assert "dar:purge" in js
124+
# Origin check on the message handler (CodeQL js/missing-origin-check):
125+
# a cross-origin frame must not be able to drive the SW cache.
126+
assert "event.origin" in js and "self.location.origin" in js
124127

125128

126129
@pytest.mark.django_db

0 commit comments

Comments
 (0)