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

Calling Sentry.init in Electron will result in an error. #14133

Open
3 tasks done
HaoZhouInRC opened this issue Oct 30, 2024 · 11 comments
Open
3 tasks done

Calling Sentry.init in Electron will result in an error. #14133

HaoZhouInRC opened this issue Oct 30, 2024 · 11 comments
Labels
Package: browser Issues related to the Sentry Browser SDK

Comments

@HaoZhouInRC
Copy link

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/browser

SDK Version

8.27.0

Framework Version

No response

Link to Sentry event

No response

Reproduction Example/SDK Setup

sentry.init({

})

Steps to Reproduce

  1. run in electron
  2. init sentry

Expected Result

Can be initialized normally.

Actual Result

Image

Image

Cause by #12668

Electron chrome runtime https://www.electronjs.org/docs/latest/api/extensions#chromeruntime

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Oct 30, 2024
@github-actions github-actions bot added the Package: browser Issues related to the Sentry Browser SDK label Oct 30, 2024
@Lms24
Copy link
Member

Lms24 commented Oct 30, 2024

Hi, can you confirm that you're using @sentry/electron and you followed our electron docs? https://docs.sentry.io/platforms/javascript/guides/electron/

@HaoZhouInRC
Copy link
Author

HaoZhouInRC commented Oct 30, 2024

Hi, can you confirm that you're using @sentry/electron and you followed our electron docs? https://docs.sentry.io/platforms/javascript/guides/electron/

My page can run in both Electron and Chrome, so I believe I should use @sentry/browser.

And I am collecting issues from web pages, not from Electron.

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Oct 30, 2024
@chargome
Copy link
Member

@HaoZhouInRC can you give us more context on your setup? Does your app run inside of a browser extension? Please also share the config of your init call.

@HaoZhouInRC
Copy link
Author

HaoZhouInRC commented Oct 31, 2024

Sentry.init({
    "environment": "develop",
    "dsn": "xxxxxx",
    "debug": false,
    "release": "24.4.20",
    "ignoreErrors": [],
    "maxBreadcrumbs": 50,
    "integrations": [Breadcrumbs],
    "transportOptions": {},
    "initialScope": {"tags": {"desktopRelease": "24.3.360"}}
})

My application is a React web page that can run in Electron or a browser.

@chargome

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Oct 31, 2024
@HaoZhouInRC
Copy link
Author

Add an additional question: How does Sentry capture web page crashes?

@Lms24
Copy link
Member

Lms24 commented Oct 31, 2024

My application is a React web page that can run in Electron or a browser.

I'm afraid I still don't fully understand why this is a problem in your case and not generally in Electron apps. Do you have a special electron configuration that sets the window.chrome.runtime properties?
I think there must be some additional configuration that causes this situation because our Electron SDK also calls browserInit() when initializing the renderer SDK: https://github.com/getsentry/sentry-electron/blob/f4ea1213fc77dd131519bda51aa1471c77d586d6/src/renderer/sdk.ts#L44

How does Sentry capture web page crashes

Do you mean full crashes where the browser shows that the tab crashed? Short answer: We can't report these unfortunately.

In case you mean, how do we catch errors? By listening to window.onerror in our globalHandlersIntegration.

@Lms24
Copy link
Member

Lms24 commented Oct 31, 2024

That being said, we previously already discussed if we should provide an escape hatch for the browser extension check because it already triggered falsely multiple times. This is probably more bundle-size efficient than adding yet another special case in this check.

@HaoZhouInRC
Copy link
Author

HaoZhouInRC commented Oct 31, 2024

Background

First, let's describe the situation of our application. Our app is a web page developed using React, which can run directly in the browser.

However, to expand the capabilities of the app, we have also developed an Electron client for it. This client directly loads our web page for users to use, and at the same time, we can provide more native system capabilities to the web page.

Electron Supports Some Chrome Extension Capabilities

As described in this document https://www.electronjs.org/docs/latest/api/extensions#chromeruntime, web pages opened in Electron will have corresponding APIs injected into window.chrome.

Reason for the Bug

const windowWithMaybeExtension =
    typeof WINDOW.window !== 'undefined' && (WINDOW as typeof WINDOW & ExtensionProperties);
  if (!windowWithMaybeExtension) {
    // No need to show the error if we're not in a browser window environment (e.g. service workers)
    return false;
  }

  const extensionKey = windowWithMaybeExtension.chrome ? 'chrome' : 'browser';
  const extensionObject = windowWithMaybeExtension[extensionKey];

  const runtimeId = extensionObject && extensionObject.runtime && extensionObject.runtime.id; // Can read runtimeId
  const href = (WINDOW.location && WINDOW.location.href) || ''; // href is a web page starting with https://

  const extensionProtocols = ['chrome-extension:', 'moz-extension:', 'ms-browser-extension:', 'safari-web-extension:'];

  // Running the SDK in a dedicated extension page and calling Sentry.init is fine; no risk of data leakage
  const isDedicatedExtensionPage = // This is also false
    !!runtimeId && WINDOW === WINDOW.top && extensionProtocols.some(protocol => href.startsWith(`${protocol}//`));

  // Running the SDK in NW.js, which appears like a browser extension but isn't, is also fine
  // see: https://github.com/getsentry/sentry-javascript/issues/12668
  const isNWjs = typeof windowWithMaybeExtension.nw !== 'undefined'; // This is false

  return !!runtimeId && !isDedicatedExtensionPage && !isNWjs; // Overall is true

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Oct 31, 2024
@HaoZhouInRC
Copy link
Author

That being said, we previously already discussed if we should provide an escape hatch for the browser extension check because it already triggered falsely multiple times. This is probably more bundle-size efficient than adding yet another special case in this check.

This can be considered a solution, thank you.

@Lms24
Copy link
Member

Lms24 commented Oct 31, 2024

I'm still very surprised that window.chrome.runtime.id this is available by default in electron. This would mean that currently the entire renderer SDK for @sentry/electro users would not initialize. To me the docs you linked convey that these APIs/variables are available for extensions running in an electron app (primarily dev tools), not for the app itself (?).

Gonna tag @timfish - any ideas how this could happen?

In other news, I opened and merged #14147 which will let you bypass the check so that you can call Sentry.init. Unless we detect that our current logic actually fails in electron (which I doubt), I think we can close this issue once the next SDK version is released.

@timfish
Copy link
Collaborator

timfish commented Oct 31, 2024

I haven't seen this before with the Electron SDK and it doesn't affect any of our tests and examples.

@HaoZhouInRC are you using any chrome extensions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Package: browser Issues related to the Sentry Browser SDK
Projects
Status: No status
Development

No branches or pull requests

4 participants