Electronのnodeモジュール読み込みの問題が修正された

昨年10月に報告した、Electron製アプリケーションを起動した際にアプリケーション外のnodeモジュールを読み込んで実行されてしまうという脆弱性が修正された。

正確には、修正は報告とほぼ同じタイミングにリリースされた v0.33.5 で修正されていたが、4月まで公表がずれ込んだようである。


 Changelog: Don't add paths outside the app to Node module search paths in packaged app.
Release electron v0.33.5 · electron/electron (https://github.com/electron/electron/releases/tag/v0.33.5)

つまり、v0.33.5以前のelectronを使って作成されたアプリケーションでは、アプリケーション外のパスもnodeモジュールの検索パスになるため、ルートフォルダ方向にさかのぼってapp.jsなどが存在すればそれが読み込まれて実行されてしまう。


If the module identifier passed to require() is not a native module, and does not begin with '/', '../', or './', then Node.js starts at the parent directory of the current module, and adds /node_modules, and attempts to load the module from that location. Node will not append node_modules to a path already ending in node_modules.
If it is not found there, then it moves to the parent directory, and so on, until the root of the file system is reached.
そのため、例えば同じPCを利用する他のユーザーが以下のようなファイルを C:\node_modules\app.js という名前で用意していたとすると

// C:\node_modules\app.js
require('child_process').exec('calc', ()=>{});

他のユーザーがそのPC上でElectron製のアプリケーションを起動した瞬間に電卓が起動することになる。
Electron v0.33.5 以前のバージョンでビルドされたアプリケーションは新しいバージョンのElectronでビルドしなおして再配布する必要がある。
なお、nodeモジュールとして読み込まれるファイル名は、app.jsのほかにもapp.nodeやindex.js、index.node等がある。