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

Update WebPreferences's ContextIsolation default value to true #785

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Yuvix25
Copy link
Contributor

@Yuvix25 Yuvix25 commented Jun 20, 2023

I've encountered the following error: "Error: contextBridge API can only be used when contextIsolation is enabled", even though I did enable ContextIsolation.

In #411, the same issue was reported, and the problem was traced down to the fact that the default value of ContextIsolation was set to the wrong value, which made that attribute un-editable.

I then found out that the default value of ContextIsolation was changed from false to true since Electron 12, as noted here, so I fixed it.

@GregorBiswanger
Copy link
Member

Thanks for your PR! Have you checked whether the IPC communication is still working afterwards?

@Yuvix25
Copy link
Contributor Author

Yuvix25 commented Jun 20, 2023

Not yet. Watching the "Contributing Getting Started" video as we speak.
Will update once I do 👍🏻

@Yuvix25
Copy link
Contributor Author

Yuvix25 commented Jun 20, 2023

@GregorBiswanger I compiled it on my side, with the default value now being set to true, but I still get that error for some reason. Any idea what could be the cause?

@GregorBiswanger
Copy link
Member

We changed the default value to true in the past. Electron already has it set to false for security reasons. However, we need it for the internal communication of our .NET API to the native Electron API. So I thought you found a solution with your PR.

Without an error message, I can't help you as to what your problem might be.

@Yuvix25
Copy link
Contributor Author

Yuvix25 commented Jun 20, 2023

These are the WebPrefrences I'm using:

WebPreferences = new WebPreferences()
{
  ContextIsolation = true,
  EnableRemoteModule = true,
  NodeIntegration = true,
  Preload = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "js", "utils.js"),
}

And this is the error I'm receiving:

node:electron/js2c/renderer_init:2 Unable to load preload script: C:\Users\[username]\[path-to-project]\obj\Host\bin\wwwroot\js\utils.js
(anonymous) @ node:electron/js2c/renderer_init:2
node:electron/js2c/renderer_init:2 Error: contextBridge API can only be used when contextIsolation is enabled
    at checkContextIsolationEnabled (node:electron/js2c/renderer_init:2:5653)
    at Object.exposeInMainWorld (node:electron/js2c/renderer_init:2:5764)
    at Object.<anonymous> (C:\Users\[username]\[path-to-project]\obj\Host\bin\wwwroot\js\utils.js:441:15)
    at Object.<anonymous> (C:\Users\[username]\[path-to-project]\obj\Host\bin\wwwroot\js\utils.js:457:3)
    at Module._compile (node:internal/modules/cjs/loader:1174:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1229:10)
    at Module.load (node:internal/modules/cjs/loader:1044:32)
    at Module._load (node:internal/modules/cjs/loader:885:12)
    at f._load (node:electron/js2c/asar_bundle:2:13330)
    at o._load (node:electron/js2c/renderer_init:2:3109)

Also, they did change the default value to true now. See the link I posted in the original comment.

@Yuvix25
Copy link
Contributor Author

Yuvix25 commented Jun 20, 2023

Maybe I need to remove <PackageReference Include="ElectronNET.API" Version="23.6.1" /> from the [project-name].csproj file? Or perhaps replace it with something local?

@Yuvix25
Copy link
Contributor Author

Yuvix25 commented Jul 18, 2023

@GregorBiswanger any idea?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants