]> khamidou — GitHub mirrors - electron.git/commitdiff
Prevent Node from adding paths outside this app to search paths
authorCheng Zhao <zcbenz@gmail.com>
Sat, 3 Oct 2015 12:38:39 +0000 (20:38 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Sat, 3 Oct 2015 12:38:39 +0000 (20:38 +0800)
atom/browser/lib/init.coffee
atom/common/lib/init.coffee
atom/common/lib/reset-search-paths.coffee [new file with mode: 0644]
atom/renderer/lib/init.coffee
filenames.gypi

index 44ae29522ebfb207ae198a98a64aaa5f5363a594..b394c0fecc28e66352a5ef07c8bb98d669aab3b3 100644 (file)
@@ -7,6 +7,9 @@ Module = require 'module'
 # we need to restore it here.
 process.argv.splice 1, 1
 
+# Clear search paths.
+require path.resolve(__dirname, '..', '..', 'common', 'lib', 'reset-search-paths')
+
 # Import common settings.
 require path.resolve(__dirname, '..', '..', 'common', 'lib', 'init')
 
index 2bfea3d72afac7524bf26a5ca109e6399bcf0879..1c80e6171a4d4c6af8a11f8aa7283e19ec68a0db 100644 (file)
@@ -9,12 +9,8 @@ process.atomBinding = (name) ->
   catch e
     process.binding "atom_common_#{name}" if /No such module/.test e.message
 
-# Clear node's global search paths.
-globalPaths = Module.globalPaths
-globalPaths.length = 0
-
 # Add common/api/lib to module search paths.
-globalPaths.push path.resolve(__dirname, '..', 'api', 'lib')
+Module.globalPaths.push path.resolve(__dirname, '..', 'api', 'lib')
 
 # setImmediate and process.nextTick makes use of uv_check and uv_prepare to
 # run the callbacks, however since we only run uv loop on requests, the
diff --git a/atom/common/lib/reset-search-paths.coffee b/atom/common/lib/reset-search-paths.coffee
new file mode 100644 (file)
index 0000000..7061103
--- /dev/null
@@ -0,0 +1,29 @@
+path   = require 'path'
+Module = require 'module'
+
+# Clear Node's global search paths.
+Module.globalPaths.length = 0
+
+# Clear current and parent(init.coffee)'s search paths.
+module.paths = []
+module.parent.paths = []
+
+# Prevent Node from adding paths outside this app to search paths.
+Module._nodeModulePaths = (from) ->
+  from = path.resolve from
+
+  # If "from" is outside the app then we do nothing.
+  skipOutsidePaths = from.startsWith process.resourcesPath
+
+  # Following logoic is copied from module.js.
+  splitRe = if process.platform is 'win32' then /[\/\\]/ else /\//
+  paths = []
+
+  parts = from.split splitRe
+  for part, tip in parts by -1
+    continue if part is 'node_modules'
+    dir = parts.slice(0, tip + 1).join path.sep
+    break if skipOutsidePaths and not dir.startsWith process.resourcesPath
+    paths.push path.join(dir, 'node_modules')
+
+  paths
index e5173fcd1ec8ea4b94ce2b9b0c191598317ea22a..b7224b39aeed09305200245cdf9cb025035c3c60 100644 (file)
@@ -7,6 +7,9 @@ Module = require 'module'
 # atom-renderer.js, we need to restore it here.
 process.argv.splice 1, 1
 
+# Clear search paths.
+require path.resolve(__dirname, '..', '..', 'common', 'lib', 'reset-search-paths')
+
 # Import common settings.
 require path.resolve(__dirname, '..', '..', 'common', 'lib', 'init')
 
index 0e7010d7a4cf75df780a14276434403384855183..000633fb96b13c0fdc2c10a760646526673affeb 100644 (file)
@@ -37,6 +37,7 @@
       'atom/common/api/lib/native-image.coffee',
       'atom/common/api/lib/shell.coffee',
       'atom/common/lib/init.coffee',
+      'atom/common/lib/reset-search-paths.coffee',
       'atom/renderer/lib/chrome-api.coffee',
       'atom/renderer/lib/init.coffee',
       'atom/renderer/lib/inspector.coffee',