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: check env NODE_PRELOAD for preload modules #11888

Closed
wants to merge 1 commit into from
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
34 changes: 32 additions & 2 deletions lib/internal/bootstrap_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,38 @@

// Load preload modules
function preloadModules() {
if (process._preload_modules) {
NativeModule.require('module')._preloadModules(process._preload_modules);
var allPreloads = process._preload_modules;
var isSetUidRoot = false;
Copy link
Member

Choose a reason for hiding this comment

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

If this is going to happen, this var should be called isSetUid


if (process.platform !== 'win32') {
isSetUidRoot = process.getuid() !== process.geteuid() ||
Copy link
Contributor

Choose a reason for hiding this comment

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

Why aren't you using SafeGetenv()?

Copy link
Author

Choose a reason for hiding this comment

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

I am not sure how. Can you tell me please?

Copy link
Contributor

Choose a reason for hiding this comment

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

Look for how OPENSSL_CONF is loaded from env, in such a way that its ignored if the IDs aren't safe, much as you are manually trying to check here.

process.getgid() !== process.getegid();
}

if (!isSetUidRoot && process.env.NODE_PRELOAD) {
const path = NativeModule.require('path');

const addPreload = (nmDir, preloads) => {
const addNmDir = (m) => {
return path.isAbsolute(m) ? m : path.join(nmDir, m);
};

if (preloads && preloads.length > 0) {
preloads = preloads.map((m) => addNmDir(m.trim()))
.filter((m) => (m.length > nmDir.length && m.startsWith(nmDir)));

if (preloads.length > 0) {
allPreloads = (allPreloads || []).concat(preloads);
}
}
};

const globalNm = path.join(process.argv[0], '../../lib/node_modules/');
Copy link
Contributor

Choose a reason for hiding this comment

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

It doesn't make any sense to disallow requiring a packages own dependencies, please remove this. Resolution should be identical to -r, obscure special cases decrease security, not increase (and control over environment and fs as this check assumes is complete control of an executable already via LD_PRELOAD).

Copy link
Author

Choose a reason for hiding this comment

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

At the moment I am waiting for an approach that everyone can at least not put a -1 on first. If @nodejs/ctc have an approach that's acceptable, I'd implement that.

addPreload(globalNm, process.env.NODE_PRELOAD.split(path.delimiter));
}

if (allPreloads) {
NativeModule.require('module')._preloadModules(allPreloads);
}
}

Expand Down
149 changes: 149 additions & 0 deletions test/parallel/test-preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const common = require('../common');
const assert = require('assert');
const path = require('path');
const childProcess = require('child_process');
const fs = require('fs');

// Refs: https://github.com/nodejs/node/pull/2253
if (common.isSunOS) {
Expand All @@ -12,6 +13,8 @@ if (common.isSunOS) {
}

const nodeBinary = process.argv[0];
const globalLibDir = path.join(nodeBinary, '../../lib');
const globalNmDir = path.join(globalLibDir, 'node_modules');

const preloadOption = function(preloads) {
let option = '';
Expand All @@ -25,12 +28,21 @@ const fixture = function(name) {
return path.join(common.fixturesDir, name);
};

const globalNmFixture = function(name) {
return path.join(globalNmDir, name);
};

const fixtureA = fixture('printA.js');
const fixtureB = fixture('printB.js');
const fixtureC = fixture('printC.js');
const fixtureD = fixture('define-global.js');
const fixtureThrows = fixture('throws_error4.js');

const globalNmFixtureA = globalNmFixture('printA.js');
const globalNmFixtureB = globalNmFixture('printB.js');
const globalNmFixtureC = globalNmFixture('printC.js');
const globalNmFixtureThrows = globalNmFixture('throws_error4.js');

// test preloading a single module works
childProcess.exec(nodeBinary + ' ' + preloadOption([fixtureA]) + ' ' + fixtureB,
function(err, stdout, stderr) {
Expand Down Expand Up @@ -146,3 +158,140 @@ childProcess.exec(
assert.ok(/worker terminated with code 43/.test(stdout));
}
);

//
// Test NODE_PRELOAD
//
let preloadTestsCount = 0;

const copyFixturesToGlobalNm = function() {
try {
fs.mkdirSync(globalLibDir);
fs.mkdirSync(globalNmDir);
} catch (e) {
}
fs.writeFileSync(globalNmFixtureA, fs.readFileSync(fixtureA));
fs.writeFileSync(globalNmFixtureB, fs.readFileSync(fixtureB));
fs.writeFileSync(globalNmFixtureC, fs.readFileSync(fixtureC));
fs.writeFileSync(globalNmFixtureThrows, fs.readFileSync(fixtureThrows));
};

const cleanGlobalNmFixtures = function() {
preloadTestsCount--;
if (preloadTestsCount > 0) {
return;
}

fs.unlinkSync(globalNmFixtureA);
fs.unlinkSync(globalNmFixtureB);
fs.unlinkSync(globalNmFixtureC);
fs.unlinkSync(globalNmFixtureThrows);

try {
fs.rmdirSync(globalNmDir);
fs.rmdirSync(globalLibDir);
} catch (e) {
}
};

copyFixturesToGlobalNm();

const testNodePreload = function(cb) {
preloadTestsCount++;
cb();
delete process.env.NODE_PRELOAD;
};

// test NODE_PRELOAD with a single module works
testNodePreload(function() {
process.env.NODE_PRELOAD = globalNmFixtureA;
childProcess.exec(nodeBinary + ' ' + fixtureB,
function(err, stdout, stderr) {
cleanGlobalNmFixtures();
assert.ifError(err);
assert.strictEqual(stdout, 'A\nB\n');
});
});

// test NODE_PRELOAD with a relative module works
testNodePreload(function() {
process.env.NODE_PRELOAD = 'printA';
childProcess.exec(nodeBinary + ' ' + fixtureB,
function(err, stdout, stderr) {
cleanGlobalNmFixtures();
assert.ifError(err);
assert.strictEqual(stdout, 'A\nB\n');
});
});

// test NODE_PRELOAD with multiple modules works
testNodePreload(function() {
process.env.NODE_PRELOAD =
globalNmFixtureA + path.delimiter + globalNmFixtureB;
childProcess.exec(
nodeBinary + ' ' + fixtureC,
function(err, stdout, stderr) {
cleanGlobalNmFixtures();
assert.ifError(err);
assert.strictEqual(stdout, 'A\nB\nC\n');
}
);
});

// test NODE_PRELOAD with multiple modules and space between delimiter works
testNodePreload(function() {
process.env.NODE_PRELOAD =
globalNmFixtureA + ' ' + path.delimiter + ' ' + globalNmFixtureB;
childProcess.exec(
nodeBinary + ' ' + fixtureC,
function(err, stdout, stderr) {
cleanGlobalNmFixtures();
assert.ifError(err);
assert.strictEqual(stdout, 'A\nB\nC\n');
}
);
});

// test NODE_PRELOAD with a throwing module aborts
testNodePreload(function() {
process.env.NODE_PRELOAD =
globalNmFixtureA + path.delimiter + globalNmFixtureThrows;
childProcess.exec(
nodeBinary + ' ' + fixtureB,
function(err, stdout, stderr) {
cleanGlobalNmFixtures();
if (err) {
assert.strictEqual(stdout, 'A\n');
} else {
throw new Error('NODE_PRELOAD Preload should have failed');
}
}
);
});

// test mixing NODE_PRELOAD with -r works
testNodePreload(function() {
process.env.NODE_PRELOAD = globalNmFixtureB;
childProcess.exec(
nodeBinary + ' ' + preloadOption([fixtureA]) + ' ' + fixtureC,
function(err, stdout, stderr) {
cleanGlobalNmFixtures();
assert.ifError(err);
assert.strictEqual(stdout, 'A\nB\nC\n');
}
);
});

// test ignoring NODE_PRELOAD modules not under global node_modules works
testNodePreload(function() {
process.env.NODE_PRELOAD =
globalNmFixtureA + ' ' + path.delimiter + ' ' + fixtureB;
childProcess.exec(
nodeBinary + ' ' + fixtureC,
function(err, stdout, stderr) {
cleanGlobalNmFixtures();
assert.ifError(err);
assert.strictEqual(stdout, 'A\nC\n');
}
);
});