Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module search security 8830 #176

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ Module._nodeModulePaths = function(from) {
// to be absolute. Doing a fully-edge-case-correct path.split
// that works on both Windows and Posix is non-trivial.
var splitRe = process.platform === 'win32' ? /[\/\\]/ : /\//;
var home_dir = process.env[
(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
var paths = [];
var parts = from.split(splitRe);

Expand All @@ -220,6 +222,8 @@ Module._nodeModulePaths = function(from) {
if (parts[tip] === 'node_modules') continue;
var dir = parts.slice(0, tip + 1).concat('node_modules').join(path.sep);
paths.push(dir);
// If we have reached the user's home directory, stop searching
if (parts.slice(0, tip + 1).join(path.sep) == home_dir) break;
}

return paths;
Expand Down
27 changes: 25 additions & 2 deletions test/simple/test-module-nodemodulepaths.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,40 @@ var module = require('module');

var isWindows = process.platform === 'win32';

var file, delimiter, paths;
var file, delimiter, paths, expected_paths, old_home;

if (isWindows) {
file = 'C:\\Users\\Rocko Artischocko\\node_stuff\\foo';
delimiter = '\\'
expected_paths = [
'C:\\Users\\Rocko Artischocko\\node_stuff\\foo\\node_modules',
'C:\\Users\\Rocko Artischocko\\node_stuff\\node_modules',
'C:\\Users\\Rocko Artischocko\\node_modules'
];
old_home = process.env.USERPROFILE;
process.env.USERPROFILE = 'C:\\Users\\Rocko Artischocko';
} else {
file = '/usr/test/lib/node_modules/npm/foo';
delimiter = '/'
expected_paths = [
'/usr/test/lib/node_modules/npm/foo/node_modules',
'/usr/test/lib/node_modules/npm/node_modules',
'/usr/test/lib/node_modules',
'/usr/test/node_modules'
];
old_home = process.env.HOME;
process.env.HOME = '/usr/test';
}

paths = module._nodeModulePaths(file);

assert.ok(paths.indexOf(file + delimiter + 'node_modules') !== -1);
assert.ok(Array.isArray(paths));
assert.deepEqual(expected_paths, paths);
assert.ok(Array.isArray(paths));

// Restore mocked home directory environment variables for other tests
if (isWindows) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this part necessary? Each test is run in a separate process, so the env gets reset.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may not be. That would simplify the test.

process.env.USERPROFILE = old_home;
} else {
process.env.HOME = old_home;
}