0

I am developing an offline app and would like to disable networking for security purposes.

Is that possible? If so, how? Thanks for helping out!

sunknudsen
  • 6,356
  • 3
  • 39
  • 76
  • By networking, do you mean if the BrowserWindow tries to navigate to a URL that's not a local file then it should be stopped? – Joshua Oct 17 '21 at 21:21
  • Thanks for helping out @Joshua. I would like to disable all networking features so app cannot connect to the Internet. If a package tries to call home, for example. Btw, I am new to Electron, so perhaps this is done by default. – sunknudsen Oct 18 '21 at 10:35
  • 1
    Duplicate of https://stackoverflow.com/questions/47953464/disable-networking-in-electron – sunknudsen Oct 18 '21 at 21:21

1 Answers1

1

This answer only works on the renderer process, not on the main process

You may want to use network emulation:

const { session } = require('electron');

session.defaultSession.enableNetworkEmulation({
  offline: true,
});

// Or

window.webContents.session.enableNetworkEmulation({
  offline: true,
});

It probably is equivalent to opening the devtools and using network throttling.

On the documentation: https://www.electronjs.org/docs/latest/api/session#sesenablenetworkemulationoptions

Autumn
  • 325
  • 3
  • 13
  • Interesting use of `enableNetworkEmulation`. Thanks for sharing! – sunknudsen Oct 18 '21 at 21:17
  • My gut feeling is that this solution would not prevent rogue npm package from calling home at the Node level. Correct? – sunknudsen Oct 18 '21 at 21:19
  • Yeah it wouldn't prevent the main process from connecting to the internet, I'm not really sure if there's a way to prevent the whole app from conecting to the internet, maybe with App Store or Microsoft Store signing that does not give the internet permission? but I've never used signing so I have no idea of it's capabilities. – Autumn Oct 20 '21 at 02:17