Filtering of service worker scripts via browser.webRequest.filterResponseData not possible?

Hi all,
I have written a tiny demo extension that uses “browser.webRequest.onBeforeRequest.addListener” and inside the listener performs “browser.webRequest.filterResponseData(details.requestId);”. This works like expected (e.g. like the webextensions-examples/http-response). Now, I noticed that for service workers registered via “navigator.serviceWorker.register” in the filtered web page, the web extension is blind. It sees the url of the service worker but does not receive the necessary events to filter the data. Is this behavior expected (e.g. security considerations) or is it a bug?

Best,
Rüdiger

To filter what data? The data that the service worker retrieves from cache using https://developer.mozilla.org/en-US/docs/Web/API/Cache and answers requests with? Because that’s expected behavior, webRequest only allows you to filter requests that hit the network.

I meant the service worker script itself that is loaded. I see the script that makes the register call but not, e.g., the sw.js.

Hi again,
here is the problem again but with a bit more details. Some excerpt of a minimal background.js. This is registered via “browser.webRequest.onBeforeRequest.addListener” :
function listener(details) {
console.log("–> Req url:"+ details.url + " "+ details.type+ " “+ details.requestId);
let filter = browser.webRequest.filterResponseData(details.requestId);

filter.ondata = event => {
currentData = decoder.decode(event.data, {stream: true});
body += currentData;
filter.write(encoder.encode(currentData));
}

filter.onerror = event => {
console.log(--> Error: ${filter.error} for ${details.requestId});
}

filter.onstop = async _ => {

console.log(”<-- Checksum: " + details.url+ " " + bodyHash);
}
return {};
}

Assume we have a minimal web site:
index.html --> ref to loadsw.js
loadsw.js --> preforms register sw
/sw-test/sw.js --> service worker

Now I get the following trace:
–> Req url:http://127.0.0.1:8080/ main_frame 104 background.js:13:11
<-- Checksum: http://127.0.0.1:8080/ eKq… background.js:37:13
–> Req url:http://127.0.0.1:8080/loadsw.js script 105 background.js:13:11
<-- Checksum: http://127.0.0.1:8080/loadsw.js M6Or… background.js:37:13

Next we register a service worker … loadsw.js:1:9
–> Req url:http://127.0.0.1:8080/sw-test/sw.js script 106 background.js:13:11
–> Error: Invalid request ID for 106 background.js:28:10
^^^^^^^^^^^^^^^

Thus the service worker (sw.js) is noticed as a url that is requested in the web extension but the response to this request cannot be inspected and I get an “Invalid request ID” error.

Any ideas why?

Ciao,
Rüdiger

1 Like

Hello from 2025, I have the same problem. Have you solved it?