Electron.NET icon indicating copy to clipboard operation
Electron.NET copied to clipboard

Update WebPreferences's ContextIsolation default value to true

Open Yuvix25 opened this issue 2 years ago • 7 comments

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.

Yuvix25 avatar Jun 20 '23 10:06 Yuvix25

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

GregorBiswanger avatar Jun 20 '23 10:06 GregorBiswanger

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

Yuvix25 avatar Jun 20 '23 10:06 Yuvix25

@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?

Yuvix25 avatar Jun 20 '23 12:06 Yuvix25

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.

GregorBiswanger avatar Jun 20 '23 12:06 GregorBiswanger

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 avatar Jun 20 '23 12:06 Yuvix25

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 avatar Jun 20 '23 12:06 Yuvix25

@GregorBiswanger any idea?

Yuvix25 avatar Jul 18 '23 13:07 Yuvix25