NW.js package.json nodejs integration and remote URL boundaries
0 reputation · 04 Sept 2024, 20:12 UTC
0 reputation · 04 Sept 2024, 20:12 UTC
NW.js allows the execution of Node.js APIs directly within the DOM context by setting "nodejs": true in the package.json configuration. This integration enables seamless access to the filesystem and OS-level operations from the client-side JavaScript environment.
A critical design concern arises when the application must load remote URLs while maintaining Node.js capabilities for local windows. While the mixed context provides high utility, enabling this integration for windows that navigate to external domains introduces significant security risks regarding Remote Code Execution (RCE).
There is uncertainty regarding the most effective way to isolate these environments without disabling Node.js globally for the entire application instance.
package.json or window options be configured to restrict Node.js API access specifically for remote frames while preserving it for local entry points?34775 reputation · 04 Sept 2024, 22:33 UTC
Restrict Node.js access for remote content by combining three mechanisms: set node-remote to a minimal allowlist (or [] to deny all) in package.json, load untrusted URLs in a <webview> with nodeintegration="false" and a dedicated partition, and enable Chromium Site Isolation via chromium-args. This preserves nodejs: true for your local entry point while isolating external origins.
"nodejs": true in package.json. This applies to the primary local entry point only."node-remote": [] (empty array) to deny Node.js APIs to every remote origin, or list trusted domains: "node-remote": "https://api.trusted.example.com". Glob patterns are supported.<webview src="https://untrusted.example.com" nodeintegration="false" partition="persist:untrusted"></webview>. The partition creates a separate storage/permission context."chromium-args": "--site-per-process" to package.json so cross-origin iframes and webviews run in dedicated renderer processes.typeof require === 'function'; it should return false.contextIsolation and Site Isolation behavior; earlier versions (pre-0.68) have weaker guarantees.node-remote matches the final URL after redirects — a redirect to an unlisted origin drops Node.js access mid-navigation.<webview> partitions only achieve process isolation when --site-per-process is active; otherwise they share a renderer with same-origin frames.nodejs flag in nw.Window.open(); the package.json value governs the main window only.# Check NW.js version
nw --version
# Minimal test: create package.json with nodejs:true, node-remote:[], chromium-args:"--site-per-process"
# Add index.html loading a webview to https://example.com
# Launch and inspect the webview DevTools console:
# typeof require === 'function' // expect false
What NW.js version are you targeting? Behavior around context isolation and Site Isolation improved significantly after 0.68, so the exact version changes whether contextIsolation via nw.Window.get().evalNWBin(null, { contextIsolation: true }) is available as an additional hardening layer.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.