Conversation
WE2-968 Signed-off-by: Tanel Metsar <taneltm@users.noreply.github.com>
WE2-968 Signed-off-by: Tanel Metsar <taneltm@users.noreply.github.com>
WE2-1179 Signed-off-by: Sven Mitt <svenzik@users.noreply.github.com>
WE2-1179 Signed-off-by: Sven Mitt <svenzik@users.noreply.github.com>
WE2-1179 Signed-off-by: Sven Mitt <svenzik@users.noreply.github.com>
| event.waitUntil(self.clients.claim()) | ||
| }) | ||
|
|
||
| self.addEventListener('message', async function (event) { |
Check warning
Code scanning / CodeQL
Missing origin verification in `postMessage` handler Medium
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI about 5 hours ago
To fix the issue, the message handler should validate incoming messages before acting on them. In a service worker context, event.origin is not the primary control; instead, you can validate the sender (event.source) and enforce that the data is of the expected form (e.g., a known string command). This avoids responding to malformed or unexpected messages and aligns with the spirit of origin verification: don’t trust arbitrary senders or payloads.
The minimal, behavior‑preserving mitigation here is:
- Ensure
event.sourceis present and looks like aClientobject with anid. If not, ignore the message. - Restrict processing to messages where
event.datais one of the known string commands. We already have aswitch (event.data)over a finite set of string literals; we can precede it with a guard that ensurestypeof event.data === 'string'. Non‑string or unexpected data will be ignored instead of falling into theswitch(which currently has no default but still conceptually “handles” any value).
Concretely, in examples/web-eid-angular-example/public/mockServiceWorker.js, inside the self.addEventListener('message', async function (event) { ... }) block:
- Add a check right after entering the handler to verify
event.sourceexists and has anid, and thattypeof event.data === 'string'. If any of these checks fail,returnearly. - This keeps existing functionality for legitimate clients (they already send those specific strings) but drops messages from unexpected sources or with unexpected payload types.
No new imports or external libraries are required.
| @@ -22,6 +22,14 @@ | ||
| }) | ||
|
|
||
| self.addEventListener('message', async function (event) { | ||
| if (!event || !event.source || typeof event.source.id !== 'string') { | ||
| return | ||
| } | ||
|
|
||
| if (typeof event.data !== 'string') { | ||
| return | ||
| } | ||
|
|
||
| const clientId = event.source.id | ||
|
|
||
| if (!clientId || !self.clients) { |
| event.waitUntil(self.clients.claim()) | ||
| }) | ||
|
|
||
| self.addEventListener('message', async function (event) { |
Check warning
Code scanning / CodeQL
Missing origin verification in `postMessage` handler Medium
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI about 5 hours ago
General fix: For message handlers, validate that the message comes from a trusted sender before acting on event.data. In a service worker context, this usually means validating the event.source (a Client) and/or keeping track of clients that have explicitly opted in, and ignoring messages from unknown clients.
Best fix here, without changing external behavior: maintain a set of known client IDs that are allowed to issue control messages, and only process the switch (event.data) if the message’s clientId belongs to that set or if it is the initial message used to register/activate mocking. Since we already have activeClientIds, we can (1) treat 'MOCK_ACTIVATE' as the opt‑in message that registers a client as trusted, and (2) ignore all other message types from clients that are not yet in activeClientIds. This preserves current semantics for legitimate clients (they send MOCK_ACTIVATE first, as MSW expects) while preventing arbitrary pages from sending deactivation or shutdown messages.
Concretely in examples/web-eid-react-example/public/mockServiceWorker.js:
- After obtaining the
client(and before theswitch), add a guard that allows'MOCK_ACTIVATE'unconditionally, but for all otherevent.datavalues, returns early unlessactiveClientIdsalready containsclientId. - Leave the existing
switchbody unchanged so functionality is preserved. - No new imports are needed; we reuse the existing
activeClientIdsset.
| @@ -38,6 +38,12 @@ | ||
| type: 'window', | ||
| }) | ||
|
|
||
| // Only accept control messages from clients that have explicitly | ||
| // activated mocking. Allow "MOCK_ACTIVATE" itself as the opt-in. | ||
| if (event.data !== 'MOCK_ACTIVATE' && !activeClientIds.has(clientId)) { | ||
| return | ||
| } | ||
|
|
||
| switch (event.data) { | ||
| case 'KEEPALIVE_REQUEST': { | ||
| sendToClient(client, { |
WE2-1179 Signed-off-by: Sven Mitt <svenzik@users.noreply.github.com> Co-authored-by: TanelTM <taneltm@users.noreply.github.com>
Supersedes #58
Closes #58