Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: validate prefetch-sw messages #6942

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lucky-snails-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@builder.io/qwik': minor
DustinJSilk marked this conversation as resolved.
Show resolved Hide resolved
---

Validate prefetch service worker messages and ignore unknown messages
DustinJSilk marked this conversation as resolved.
Show resolved Hide resolved
10 changes: 0 additions & 10 deletions packages/qwik/src/optimizer/src/qwik-binding-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,5 @@ export const QWIK_BINDING_MAP = {
"platformArchABI": "qwik.win32-x64-msvc.node"
}
]
},
"linux": {
"x64": [
{
"platform": "linux",
"arch": "x64",
"abi": "gnu",
"platformArchABI": "qwik.linux-x64-gnu.node"
}
]
DustinJSilk marked this conversation as resolved.
Show resolved Hide resolved
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ export const processMessage = async (state: SWState, msg: SWMessages) => {
} else if (type === 'verbose') {
// eslint-disable-next-line no-console
(state.$log$ = log)('mode: verbose');
} else {
console.error('UNKNOWN MESSAGE:', msg);
}
};

Expand Down
13 changes: 10 additions & 3 deletions packages/qwik/src/prefetch-service-worker/setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { directFetch } from './direct-fetch';
import { drainMsgQueue } from './process-message';
import { drainMsgQueue, type SWMessages } from './process-message';
import { createState, type SWState } from './state';

export const setupServiceWorker = (swScope: ServiceWorkerGlobalScope) => {
Expand All @@ -25,8 +25,10 @@ export const setupServiceWorker = (swScope: ServiceWorkerGlobalScope) => {
}
});
swScope.addEventListener('message', (ev) => {
swState.$msgQueue$.push(ev.data);
drainMsgQueue(swState);
if (isQwikMessages(ev.data)) {
swState.$msgQueue$.push(ev.data);
drainMsgQueue(swState);
}
});
swScope.addEventListener('install', () => {
swScope.skipWaiting();
Expand All @@ -46,3 +48,8 @@ export const setupServiceWorker = (swScope: ServiceWorkerGlobalScope) => {
event.waitUntil(swScope.clients.claim());
});
};

const messageTypes = ['graph', 'graph-url', 'prefetch', 'prefetch-all', 'ping', 'verbose'];
function isQwikMessages(msg: any): msg is SWMessages {
return msg[0] && messageTypes.includes(msg[0][0]);
}