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

JS Tracking code may generate "The operation is insecure" warnings with specific Firefox settings #17454

Closed
brainfoolong opened this issue Apr 13, 2021 · 24 comments · Fixed by #17735
Assignees
Labels
Bug For errors / faults / flaws / inconsistencies etc. c: Tracking For issues related to getting tracking data into Matomo.
Milestone

Comments

@brainfoolong
Copy link
Contributor

brainfoolong commented Apr 13, 2021

This bug is just a follow up to a already known bug, but i decided to start a new issue here as the others are marked as resolved or have been merged.

References:

Ok, i can now reproduce this bug with 4.2.1 in Firefox 87.0 on windows. It is still present and still causes errors in our JS error logs. It depends on some specific firefox strict security settings.

Following example:

  • Open any site you want with Firefox
  • Open F12 console
  • Enter and execute this: console.log(navigator.serviceWorker && navigator.serviceWorker.ready)
  • Error occurs

The console command is basically the same as used in matomo here
https://github.com/matomo-org/matomo/blob/4.x-dev/js/piwik.js#L6989

Demo:
firefox-bug

Settings:
image

@brainfoolong brainfoolong added the Potential Bug Something that might be a bug, but needs validation and confirmation it can be reproduced. label Apr 13, 2021
@brainfoolong brainfoolong changed the title Firefox Exception "This operation is insecure" Firefox Exception "The operation is insecure" Apr 13, 2021
@diosmosis
Copy link
Member

Hi @brainfoolong, I have a couple questions:

  • Does just console.log(navigator.serviceWorker) emit the error?
  • Does navigator.serviceWorker.ready.catch(function () {}) also cause the error?

Also, I'm not completely up to speed on the discussion around this issue, so forgive me if this is obvious, but what's the impact of this bug? Does nothing track because of it or is it just an error in the console?

@brainfoolong
Copy link
Contributor Author

brainfoolong commented Apr 14, 2021

Hi, just the try to use navigator.serviceWorker.ready will cause the exception. It causes error in the javascript console. And also causes errors to be logged when you have a system that log javascript exceptions. So does in our case. It clutters our logs.

This code from matomo does not run on firefox in this case because of this exception, but i don't know what it exactly does.
https://github.com/matomo-org/matomo/blob/4.x-dev/js/piwik.js#L6988-L6996

IMHO, i don't see why matomo try to hook into existing service workers. I read it is for offline tracking but cannot find any relevant code for this, as "matomoSync" does only exist once in the matomo code.

@diosmosis
Copy link
Member

Hi @brainfoolong, can you check if 'ready' in navigator.serviceWorker has the same problem?

@brainfoolong
Copy link
Contributor Author

@diosmosis No it does not, but it does not help in this case, at it returns true anyway.
image

@diosmosis
Copy link
Member

That's unfortunate. Thanks for checking.

@diosmosis diosmosis added Bug For errors / faults / flaws / inconsistencies etc. c: Tracking For issues related to getting tracking data into Matomo. and removed Potential Bug Something that might be a bug, but needs validation and confirmation it can be reproduced. labels Apr 14, 2021
@brainfoolong
Copy link
Contributor Author

And another note: You can catch this error, but cannot make the error go away. I've tried 3 different methods:

window.addEventListener('unhandledrejection', function (e) {
  console.log(e)
}, false)

window.addEventListener('error', function (e) {
  console.log(e)
}, false)

try{
	navigator.serviceWorker.ready
}catch(e){
}

image

@diosmosis
Copy link
Member

diosmosis commented Apr 14, 2021

Looks like there's a bug in the firefox tracker for this:

https://bugzilla.mozilla.org/show_bug.cgi?id=1413615
https://stackoverflow.com/questions/49539306/firefox-service-worker-securityerror-domexception-the-operation-is-insecure/

If it's possible to detect when that privacy feature is enabled we could avoid the error. Failing that it could be conditionally enabled/disabled in the tracker.

@brainfoolong
Copy link
Contributor Author

Yes, maybe. But, this issues are years old and nobody cares about... Maybe it could be an option in matomo, to prevent offline tracking and so to prevent the use of service worker hooks?

But my question really is, what does this part of the code? I cant see any use of this, as the registered "matomoSync" does no seem to be used anywhere else.

Also, the use for sync is experimental and not ready for production - https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/sync - This page also list firefox as unsupported. Maybe this code is a bit too experimental for matomo?

I know, it is not really a matomo error, but matomo does generate those errors and i don't see that firefox will fix this anywhere soon.

@diosmosis
Copy link
Member

That comment was just to provide information from my research.

Unfortunately, I don't know what this code is for as I wasn't involved in writing or reviewing it, but @tsteur might be able to answer why it's there.

@Findus23
Copy link
Member

But my question really is, what does this part of the code?

The part it interacts with is the service worker in https://github.com/matomo-org/matomo/blob/4.x-dev/offline-service-worker.js

self.addEventListener('sync', function(event) {
if (event.tag === 'matomoSync') {
syncQueue();
}
});

The documentation is just the description of #15970 and https://matomo.org/faq/how-to/how-do-i-set-up-matomo-offline-tracking/ for now.

@brainfoolong
Copy link
Contributor Author

brainfoolong commented Apr 14, 2021

@Findus23 Thx for clearing this up. This is indeed good to know, as we firewalled our matomo instance and only pass matomo.js and matomo.php through, so this wouldn't even work for us.

Beside that, i think using a highly experimental feature, the sync in service worker, is not that good in a widely used production product, isn't it?

@felix-berlin
Copy link

We get similar errors. However, not only in Firefox but also in Safari (iOS and MacOS).

@tsteur tsteur added this to the 4.3.0 milestone Apr 27, 2021
@tsteur
Copy link
Member

tsteur commented Apr 27, 2021

I'll move this into the current 4.3 milestone so we can investigate further.

@mattab mattab modified the milestones: 4.3.0, 4.4.0 May 26, 2021
@justinvelluppillai justinvelluppillai self-assigned this Jun 30, 2021
@justinvelluppillai
Copy link
Contributor

Hi @brainfoolong and @felix-berlin, I have just started to look into resolving this issue and can't reproduce it in Firefox 89. Can you please let me know if you are still seeing the issue, and if so perhaps give me steps and environment to reproduce the issue?

Thanks - maybe some browser updates have fixed this.

@brainfoolong
Copy link
Contributor Author

Hi @justinvelluppillai . The error still exist. Just run this on any website in F12 -> Console. That's basically the core problem of matomo and the most basic example to reproduce.
image

You need to enable the settings as stated in post #17454 (comment)

@justinvelluppillai
Copy link
Contributor

This information has all been mentioned above, but just to clarify this issue:

The relevant setting is

Screen Shot 2021-07-05 at 3 03 48 PM

When it is set, navigator.serviceWorker.ready returns a rejected promise, which we currently don't handle, so firefox shows "The operation is insecure" error.

I have submitted a PR to fix this by handling the rejected promise and silently ignoring it.

@brainfoolong
Copy link
Contributor Author

@justinvelluppillai Thanks, i can confirm that this fix does work.

@justinvelluppillai
Copy link
Contributor

Thanks @brainfoolong appreciate you drawing our attention to this and your patience in providing details to get it fixed.

@felix-berlin
Copy link

In my case navigator.serviceWorker.ready is undefined.

image

@felix-berlin
Copy link

Is there a other way to force the error?

@Findus23
Copy link
Member

Findus23 commented Jul 5, 2021

@felix-berlin Do you by chance use any browser extension that modifies theses browser settings?

For me, if I set dom.serviceWorkers.enabled in about:config to false, navigator.serviceWorker becomes undefined.

@felix-berlin
Copy link

@Findus23 Yes but only two :)

image

@mattab mattab changed the title Firefox Exception "The operation is insecure" JS Tracking code may generate "The operation is insecure" warnings with specific Firefox settings Jul 26, 2021
@RacketyWater7
Copy link

Hi guys!
Any update on resolving this issue?
I'm getting this same error when directing the user from a normal webpage into my extension's options page.
The process is that, when the user is directed to the options page, there its sent with an image src as in the url of options page, the image src is then extracted from url and sent to an API after some processing; this processing includes drawing it on a canvas and this is where this error occurs.
Let me show the relevant code:

// previous code
if (selectImage) {
      setSending(true);
      // Convert the selected image to ImageData
      const imageBitmap = await createImageBitmap(selectImage);
      const canvas = document.createElement('canvas');
      canvas.width = imageBitmap.width;
      canvas.height = imageBitmap.height;
      const context = canvas.getContext('2d');
      context.drawImage(imageBitmap, 0, 0);
      //  >>>>>>>>>>>>>> the execution stops here  <<<<<<<<<<<<
      const imageData = context.getImageData(0, 0, canvas.width, canvas.height);
      // Decode the QR code
      const qrCode = jsQR(imageData.data, imageData.width, imageData.height);

      if (qrCode) {
      // code continued to send the image to server

@sgiehl
Copy link
Member

sgiehl commented Aug 22, 2023

This issue has been resolved in Matomo years ago. Your code also doesn't look related to Matomo at all, so maybe this isn't the correct place for reporting it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug For errors / faults / flaws / inconsistencies etc. c: Tracking For issues related to getting tracking data into Matomo.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants