Node Integration versus Remote Module/ContextBridge for Secure NW.js Production Builds
0 reputation · 08 Apr 2022, 06:25 UTC
0 reputation · 08 Apr 2022, 06:25 UTC
Goal: Select a method for exposing necessary Node.js functionality to the renderer process in an NW.js application that remains secure when the app loads remote content in production.
Constraints: The chosen approach must prevent code‑injection attacks, require an explicit whitelist of modules, and ensure that any preload script used to bridge Node APIs resolves correctly after the app is packaged into an ASAR archive. Current documentation does not clearly state whether the legacy remote module (still present in NW.js v0.50+) should be retained or if the newer contextBridge‑style API introduced in v0.55 is the preferred path for existing node‑webkit projects.
Should the deprecated remote module continue to be used for existing projects, or is the contextBridge approach recommended for new builds? How does the choice affect module whitelisting and preload script reliability in packaged ASAR archives? What documentation gaps need clarification to guide a safe migration?
For an NW.js app that loads remote content in production, disable nodeIntegration and do not rely on the deprecated remote module. Instead, use a preload script that runs in a Node context and explicitly whitelist the Node APIs you need to expose to the renderer.
remote module, but it is marked deprecated and provides no explicit whitelist; enabling it leaks the full Node environment and defeats context isolation.nodeIntegration: false in the BrowserWindow (or nw.Window) configuration prevents the renderer from accessing require, process, or other Node globals unless they are deliberately exposed via a preload script../preload.js) or via process.resourcesPath resolves correctly after the app is packaged into an ASAR archive because the script is bundled as a plain JavaScript file inside the archive.window object (or to a custom namespace) – this acts as an explicit whitelist. Anything not attached remains inaccessible to the renderer.nodeIntegration: false when creating the window (in package.json's window field or via the nw.Window API).preload.js) that runs before the renderer loads. In this script:require to load the Node modules you need (e.g., const fs = require('fs');).window object under a controlled name, e.g., window.myApi = { readFile: fs.promises.readFile };.require or process objects."preload": "./preload.js" (or compute the path with process.resourcesPath if needed).require('fs') directly (it should throw) but can use the exposed API, e.g., window.myApi.readFile(...).If your renderer absolutely needs to call require for a native Node addon that cannot be forwarded through a whitelisted function, then the remote module (or keeping nodeIntegration:true) may be unavoidable. In that case, please confirm whether any such native addons are required in the renderer.
Current NW.js docs do not clearly state that the remote module is deprecated for security‑critical builds, nor do they provide an explicit example of a preload‑based whitelist that survives ASAR packaging. Clarifying these points would help developers migrate safely.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.