node-argon2 icon indicating copy to clipboard operation
node-argon2 copied to clipboard

Can't be used in Electron renderer process - unsure why

Open Nantris opened this issue 4 years ago • 10 comments

Welcome to the issues section if it's your first time!

Before creating an issue, please be sure to:

  • [x] Checkout to the latest version, including submodules
  • [x] Try to find an isolated way to reproduce the behavior
  • [x] Fill in all the blanks in the most specific way you can

Steps to reproduce

  1. const argon2 = require('argon2') in an Electron renderer process. Even with nodeIntegration: true, argon2 is undefined.

Expected behaviour

Should be able to import into Electron renderer process as you previously could. I'm not sure if some change in Electron maybe broke this? I guess so because the version we had been using 0.26.2, as well as the newest version 0.27.1 are affected.

Actual behaviour

argon2 === undefined

I've tried to discover a reason for this but I can't uncover any clues. The only significant change I'm aware of is the addition of the app.allowRendererProcessReuse option - but it doesn't seem relevant and changing the value for that flag makes no difference.

Environment

Operating system: Windows 7

Node version: 12.18.x

Compiler version: Unsure?

Nantris avatar Mar 06 '21 19:03 Nantris

Seems maybe only affected when using Webpack (version 5) but that might not be true. This is proving harder to create a repro for than expected due to unrelated errors in every repro I've tried to create.

Nantris avatar Mar 06 '21 20:03 Nantris

I got it to work in an attempted repro created with Electron Forge. This has led me to think that perhaps this issue is only occurring when using Webpack to bundle our preload script?

Nantris avatar Mar 08 '21 21:03 Nantris

There have been a couple of issues with Electron around here, most of them are environment issues and not specific to argon2. I can't provide much help as I personally don't use it with Electron though :(

ranisalt avatar Mar 08 '21 22:03 ranisalt

I appreciate your work on this package and willingness to help people who use it! There's only so much that can be asked of package developers of course.

For now I'm running argon2 in the main thread as a workaround - but blocking the main thread makes the whole app lag so it's not ideal. It works though!

Since I have a workaround I probably won't be updating this too often with new findings, but if I revisit this I'll continue to share what I uncover.

Nantris avatar Mar 08 '21 22:03 Nantris

Is it possible for you to require argon2 in the main thread, and pass it around to other threads? The whole intent of Argon2 is to be slow :stuck_out_tongue:

ranisalt avatar Mar 08 '21 23:03 ranisalt

Ah unfortunately you cannot pass argon2 because any data transferred between processes needs to be serialized in line with the Structured Clone Algorithm.

The trouble with blocking the main thread in Electron is that that thread manages even stuff like the window's position on the screen and whether the window is opened or closed. The renderer threads are more like normal browser processes.

I doubt this is something you're looking for further reading on, since you don't use Electron, but in case you're curious - [The Horror of Blocking Electron’s Main Process].(https://medium.com/actualbudget/the-horror-of-blocking-electrons-main-process-351bf11a763c)

Fortunately there's not that much horror when a user knows to expect to wait a moment while their passphrase is verified. Far from the end of the world!

Nantris avatar Mar 08 '21 23:03 Nantris

Hey! Sorry for the long wait. Can you check if the latest commit on master (0d9f0f8) fixes this issue? It fixes related issues with other packagers

ranisalt avatar Mar 01 '22 18:03 ranisalt

I'll definitely give it a try when I can! Thanks so much for your continued great work on this project!

Nantris avatar Mar 01 '22 23:03 Nantris

Unfortunately it doesn't seem to resolve the issue - but fortunately it's easy enough to work around by running it in the main process on Electron.

The error we see is:

19:10:33.632 › ReferenceError: argon2 is not defined at Object.argon2 (C:\projects\MyApp\app\dist\preload.build.js:73741:18) at webpack_require (C:\projects\MyApp\app\dist\preload.build.js:73807:42) at C:\projects\MyApp\app\dist\preload.build.js:14903:16

Just including: const argon2 = require('argon2'); prevents execution of code later in the file, which seems strange.

Nantris avatar Mar 02 '22 00:03 Nantris

In this case, you seem to be hitting the same issue of #261 which was resolved with this: https://github.com/ranisalt/node-argon2/issues/261#issuecomment-639392195

ranisalt avatar Mar 04 '22 19:03 ranisalt

I believe I tried this a while ago and it didn't work, but I'm not sure.

In any event, Electron is moving towards universal sandboxing, so to use it in the renderer process is now considered sub-optimal anyway - Electron developers should install it in the main process and use ipc to communicate with it.

Nantris avatar Sep 07 '22 23:09 Nantris