You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Discussion] Changing the defaults for nodeIntegration and contextIsolation to improve the default security posture of Electron applications #23506
Change the default of contextIsolation from false to true
Without contextIsolation any code running in a renderer process can quite easily reach into Electron internals or your preload script and perform privileged actions that you don't want arbitrary websites to be doing.
For more information on contextIsolation, how to enable it easily and it's security benefits please see our dedicated Context Isolation Document.
We're making this change to improve the default security of Electron apps so that your app is only insecure if you have deliberately opted in to the insecure behaviour.
Timeline
Deprecate the current default of contextIsolation in Electron 10
Change to the new default (true) in Electron 12
Remove the nodeIntegration flag completely
Historically we have recommended that apps use nodeIntegration: false to prevent renderers from having access to Electron internals or the require function. Over time it has become clear that this flag actually has negligible security impact and can easily be bypassed. This was the original motivation for adding the contextIsolation flag.
We are now confident enough in the contextIsolation feature that we intend to remove the misleading nodeIntegration flag and instead strongly recommend usage of contextIsolation.
Timeline
Deprecate the flag and instruct folks to use contextIsolation: true instead of nodeIntegration: false in Electron 10
Remove the flag and any effect it had in Electron 12
Reacted by Nicolas Boutet, Jeremy Rose, reZach, Milan Burda, Joe, Stephen Wicklund, Alexander Prinzhorn, Alex Yang, vi, Muhammad Ammar and 21 more
Reacted by Jeremy Rose, Joe, Stephen Wicklund, Varun M, Erick Zhao, bl-ue, feiyue, Justin Sun, Rajeh Taher, Shuqiao Zhang and 1 more
This is marked as discussion, so I hope I'm allowed to post, if not, please limit to just collaborators.
I feel this is a good change, but not many are prepared for. There are a lot of packages/templates that make use of remote and do not use IPC to talk between the processes. I've built a template that's supportive of this change, but I feel a lot will be discussing once this change is implemented.
I'm not sure if this has been discussed anywhere (or if I'm not thinking through it correctly), but it does seem like there's a scenario in which:
You're creating a webContents with nodeIntegration: false, contextIsolation: false in an old Electron version.
You upgrade directly from an old version to 12, or you don't read the deprecation warnings in the intermediate versions.
The effect would be that node integration would be silently enabled in your app after the upgrade, which would potentially cause severe security issues. (I suppose that wouldn't matter if nodeIntegration can already be bypassed, but as far as I know it's still reasonably secure as long as you don't have a preload script).
As a solution for this, would it be possible to check if nodeIntegration: false is set on a webContents, and throw an error (and not create the contents) if it is?
That's a reasonable point, I don't think many people would explicitly say contextIsolation: false but we should probably handle that in some way @nornagon
nodeIntegration: false is not very much protection at all. I spent an hour this morning looking and found at least one attack vector that provides access to internal methods that nodeIntegration: false doesn't protect against. Ultimately, there is no reliable way to make having node and web content in the same context safe. If you care at all about security, contextIsolation: true is the way to go (and, ideally, sandbox: true as well).
I can imagine throwing on nodeIntegration: false being a better way of alerting to the developer that they need to change something, though!
Reacted by Lorenzo Stella, Keeley Hammond, Rajeh Taher, Matheus Rotta Alves and Salif Dianda
@MarshallOfSound From some previous discussion with you, you mentioned that contextIsolation should be enabled for all BrowserWindows/BrowserViews regardless of whether it has a preload script or not. You told me that it should be enabled even when loading only local content.
The docs currently make it seems like this is only useful when there's a preload script, or you're loading a URL. Should they be updated to indicate that it should be on everywhere (even though that will be the default behavior anyway)
Does this mean Native modules will not work anymore in electron? I just added contextIsolation: false, to make it work again but got same error. Loading non-context-aware native module in renderer:
Sounds like a great change to me. I'm always for security-by-default and always wondered why nodeIntegration exists to begin with.
Anyway, what about nodeIntegrationInSubFrames? I feel like this would've been a great moment to normalize it as well. There's a lot of discussion about it in #22582. It is ill named and has the side effect of enabling preload in subframes. It looks like this was one of the intentions but the name doesn't reflect that. When nodeIntegration is removed it only makes sense to remove it's sibling nodeIntegrationInSubFrames as well. And instead rename it to preloadInSubFrame (as a boolean) or even framePreload (pointing to a script to be used as preload in frames). And also make sure it doesn't actually enable node integration but only preload.
Same with nodeIntegrationInWorker btw. It'd be weird to keep them around (at least with the same name) when nodeIntegration is removed.
Change the default of contextIsolation from false to true
Without contextIsolation any code running in a renderer process can quite easily reach into Electron internals or your preload script and perform privileged actions that you don't want arbitrary websites to be doing.
For more information on contextIsolation, how to enable it easily and it's security benefits please see our dedicated Context Isolation Document.
We're making this change to improve the default security of Electron apps so that your app is only insecure if you have deliberately opted in to the insecure behaviour.
Timeline
Deprecate the current default of contextIsolation in Electron 10
Change to the new default (true) in Electron 12
Remove the nodeIntegration flag completely
Historically we have recommended that apps use nodeIntegration: false to prevent renderers from having access to Electron internals or the require function. Over time it has become clear that this flag actually has negligible security impact and can easily be bypassed. This was the original motivation for adding the contextIsolation flag.
We are now confident enough in the contextIsolation feature that we intend to remove the misleading nodeIntegration flag and instead strongly recommend usage of contextIsolation.
Timeline
Deprecate the flag and instruct folks to use contextIsolation: true instead of nodeIntegration: false in Electron 10
Remove the flag and any effect it had in Electron 12
As I am providing a few small apps that works with nodeintegration set to true for realm db, react js, babel and webpack,
question is how I can then "set nodeintegration to true" ?
Reacted by Rand Scullard, Remi Guan, Alex Seifert, ZainChen, Alexandre Desroches, shaund, CyrilCommando and Keith Hill
@petef19 I want to be clear here, this is not a discussion. From my side, this is an explanation at best. The work described in this issue was completed over a year ago now and we continue to strive towards a more secure future for Electron. Whether or not you understand those moves or think they're the right moves is unfortunately irrelevant. Passive aggressive insistence on continued explanations, making unsubstantiated claims about what the maintainers do and playing the "what if" game is not a valid way to engage with maintainers.
I will be closing this issue as the work / change was done a long time ago. Questions about Electron apps, security, etc. can be directed to the Community Discord. Bugs / feature requests should be raised on the issue tracker as new issues.
@MarshallOfSound
Thanks, unfortunately you responded w/o answering the question.
is there somebody else from the team that could answer our questions ?
Or could you possibly escalate this to somebody else that would know the answer to our simple question ?
Multiple community members are developing apps of type (2) and (3). We need to know what (possible) consequences are in those scenarios if we deviate from the recommended contextIsolation settings
@petef19 if you have questions, I can point you to resources on contextIsolation and scenarios regarding it. You can post a comment on my repo and we can connect there. We can keep conversation off of this issue as Marshall has said.
I think that the docs on security could use a little bit of updating, or maybe it's my knowledge that needs updating.
@MarshallOfSound thanks so much for all your explanations in this thread; I'd be very grateful if you could correct any misunderstandings I have here or confirm that the docs could use some changes.
It suggests contextIsolation is only needed when loading remote content, but I think it should really be whenever loading remote content or user generated content at least, if not always?
Even though contextIsolation is only recommended for remote content, enabling sandbox is suggested as something that should be done for all apps ("You should at least follow these steps to improve the security of your application"). I would think that if sandbox is being recommended as a minimum for all apps then contextIsolation certainly should be too, no?
"Note that because the environment presented to the preload script is substantially more privileged than that of a sandboxed renderer, it is still possible to leak privileged APIs to untrusted code running in the renderer process unless contextIsolation is enabled."
I assume that should read "unless contextIsolation is also enabled; because as written it seems to suggest that contextIsolation alone would be sufficient to prevent privileged APIs being accessible to untrusted code, even without sandbox
On a related note, if everything Node related should be handled in the main process and blocking the main process causes all renderer windows to freeze, am I wrong to think special considerations might be needed from developers when running node code in the main process to avoid blocking it, and can you make any recommendations for handling this?
@slapbox The "for remote content" bit was removed in this commit actually, but maybe there's some other document that needs to be updated? Or it hasn't been "released" yet?
Hi,
I'm checking the side effects when enabling the contextIsolation on my project, to be closer to Electron security recommendation there are multi-methods that are declared on the renderer side (I'm using loadURL to generate Eelctron desktop app which depends on the website).
So I'm calling these methods from the preload scripts, so all the logic for our app and services on the renderer side (web app), currently we are using these services from the preload script, but when enabling the contextIsolation, I can not use the renderer services (I know I can create a bridge, but it's one-directional, the renderer side can use the APIs via that bridge which declared from preload script, while the preload script can not use any method from renderer).
Anyone has an idea to avoid these issues because there are hundreds of services on our app which are declared on our website side and Irreplaceable for the desktop app side?
while the preload script can not use any method from renderer
It can, in an indirect way - but it's quite tedious - too much so for me to outline here - but basically you need to pass any data you need from the renderer into your preload functions as arguments. If you have hundreds of services to tend to: pain.
Activity
reZach commentedon Jun 4, 2020
This is marked as discussion, so I hope I'm allowed to post, if not, please limit to just collaborators.
I feel this is a good change, but not many are prepared for. There are a lot of packages/templates that make use of remote and do not use IPC to talk between the processes. I've built a template that's supportive of this change, but I feel a lot will be discussing once this change is implemented.
PalmerAL commentedon Jun 24, 2020
I'm not sure if this has been discussed anywhere (or if I'm not thinking through it correctly), but it does seem like there's a scenario in which:
nodeIntegration: false, contextIsolation: falsein an old Electron version.The effect would be that node integration would be silently enabled in your app after the upgrade, which would potentially cause severe security issues. (I suppose that wouldn't matter if
nodeIntegrationcan already be bypassed, but as far as I know it's still reasonably secure as long as you don't have a preload script).As a solution for this, would it be possible to check if
nodeIntegration: falseis set on a webContents, and throw an error (and not create the contents) if it is?MarshallOfSound commentedon Jun 24, 2020
That's a reasonable point, I don't think many people would explicitly say contextIsolation: false but we should probably handle that in some way @nornagon
nornagon commentedon Jun 24, 2020
nodeIntegration: falseis not very much protection at all. I spent an hour this morning looking and found at least one attack vector that provides access to internal methods thatnodeIntegration: falsedoesn't protect against. Ultimately, there is no reliable way to make having node and web content in the same context safe. If you care at all about security,contextIsolation: trueis the way to go (and, ideally,sandbox: trueas well).I can imagine throwing on
nodeIntegration: falsebeing a better way of alerting to the developer that they need to change something, though!pushkin- commentedon Jul 3, 2020
@MarshallOfSound From some previous discussion with you, you mentioned that
contextIsolationshould be enabled for allBrowserWindows/BrowserViewsregardless of whether it has a preload script or not. You told me that it should be enabled even when loading only local content.The docs currently make it seems like this is only useful when there's a preload script, or you're loading a URL. Should they be updated to indicate that it should be on everywhere (even though that will be the default behavior anyway)
ivanjeremic commentedon Jul 7, 2020
Does this mean Native modules will not work anymore in electron? I just added
contextIsolation: false, to make it work again but got same error.Loading non-context-aware native module in renderer:nornagon commentedon Jul 7, 2020
No.
See #18397
ivanjeremic commentedon Jul 7, 2020
This doesn't help.
Prinzhorn commentedon Oct 13, 2020
Sounds like a great change to me. I'm always for security-by-default and always wondered why
nodeIntegrationexists to begin with.Anyway, what about
nodeIntegrationInSubFrames? I feel like this would've been a great moment to normalize it as well. There's a lot of discussion about it in #22582. It is ill named and has the side effect of enabling preload in subframes. It looks like this was one of the intentions but the name doesn't reflect that. WhennodeIntegrationis removed it only makes sense to remove it's siblingnodeIntegrationInSubFramesas well. And instead rename it topreloadInSubFrame(as a boolean) or evenframePreload(pointing to a script to be used as preload in frames). And also make sure it doesn't actually enable node integration but only preload.Same with
nodeIntegrationInWorkerbtw. It'd be weird to keep them around (at least with the same name) whennodeIntegrationis removed.explicitly set contextIsolation flag
Update electron to latest stable version and replace deprecated node …
RAMKUMARDANE commentedon Dec 5, 2020
As I am providing a few small apps that works with nodeintegration set to true for realm db, react js, babel and webpack,
question is how I can then "set nodeintegration to true" ?
适配 electron/electron#23506
Support new contextIsolation default value
52 remaining items
MarshallOfSound commentedon Apr 19, 2022
@petef19 I want to be clear here, this is not a discussion. From my side, this is an explanation at best. The work described in this issue was completed over a year ago now and we continue to strive towards a more secure future for Electron. Whether or not you understand those moves or think they're the right moves is unfortunately irrelevant. Passive aggressive insistence on continued explanations, making unsubstantiated claims about what the maintainers do and playing the "what if" game is not a valid way to engage with maintainers.
I will be closing this issue as the work / change was done a long time ago. Questions about Electron apps, security, etc. can be directed to the Community Discord. Bugs / feature requests should be raised on the issue tracker as new issues.
petef19 commentedon Apr 19, 2022
@MarshallOfSound
Thanks, unfortunately you responded w/o answering the question.
is there somebody else from the team that could answer our questions ?
Or could you possibly escalate this to somebody else that would know the answer to our simple question ?
Multiple community members are developing apps of type (2) and (3). We need to know what (possible) consequences are in those scenarios if we deviate from the recommended
contextIsolationsettingsreZach commentedon Apr 19, 2022
@petef19 if you have questions, I can point you to resources on
contextIsolationand scenarios regarding it. You can post a comment on my repo and we can connect there. We can keep conversation off of this issue as Marshall has said.petef19 commentedon Apr 23, 2022
@reZach
thanks for this, started a dedicated thread here
Changing the defaults for nodeIntegration and contextIsolation
Changing the defaults for nodeIntegration and contextIsolation
Nantris commentedon Jul 21, 2022
I think that the docs on security could use a little bit of updating, or maybe it's my knowledge that needs updating.
@MarshallOfSound thanks so much for all your explanations in this thread; I'd be very grateful if you could correct any misunderstandings I have here or confirm that the docs could use some changes.
https://www.electronjs.org/docs/latest/tutorial/security
It suggests
contextIsolationis only needed when loading remote content, but I think it should really be whenever loading remote content or user generated content at least, if not always?Even though
contextIsolationis only recommended for remote content, enablingsandboxis suggested as something that should be done for all apps ("You should at least follow these steps to improve the security of your application"). I would think that ifsandboxis being recommended as a minimum for all apps thencontextIsolationcertainly should be too, no?From the Sandbox tutorial:
contextIsolationis also enabled; because as written it seems to suggest thatcontextIsolationalone would be sufficient to prevent privileged APIs being accessible to untrusted code, even withoutsandboxOn a related note, if everything Node related should be handled in the
mainprocess and blocking themainprocess causes all renderer windows to freeze, am I wrong to think special considerations might be needed from developers when running node code in themainprocess to avoid blocking it, and can you make any recommendations for handling this?pushkin- commentedon Jul 21, 2022
@slapbox The "for remote content" bit was removed in this commit actually, but maybe there's some other document that needs to be updated? Or it hasn't been "released" yet?
Changing the defaults for nodeIntegration and contextIsolation
oalfroukh commentedon Sep 22, 2022
Hi,
I'm checking the side effects when enabling the
contextIsolationon my project, to be closer to Electron security recommendation there are multi-methods that are declared on the renderer side (I'm using loadURL to generate Eelctron desktop app which depends on the website).So I'm calling these methods from the preload scripts, so all the logic for our app and services on the renderer side (web app), currently we are using these services from the preload script, but when enabling the
contextIsolation, I can not use the renderer services (I know I can create a bridge, but it's one-directional, the renderer side can use the APIs via that bridge which declared from preload script, while the preload script can not use any method from renderer).Anyone has an idea to avoid these issues because there are hundreds of services on our app which are declared on our website side and Irreplaceable for the desktop app side?
Nantris commentedon Sep 22, 2022
It can, in an indirect way - but it's quite tedious - too much so for me to outline here - but basically you need to pass any data you need from the
rendererinto yourpreloadfunctions as arguments. If you have hundreds of services to tend to: pain.Fix notifications broken since Nativefier 43 / Electron 12 defaulting…
Update electron to latest stable version and replace deprecated node …
Update ProcessManagerWindow.js