From 14b67133abf6cd3b662798df3b4ea5c995a4e513 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Fri, 13 Aug 2021 11:03:25 +0900 Subject: [PATCH] src: add option to disable global search paths --- lib/internal/bootstrap/pre_execution.js | 7 +++++-- lib/internal/options.js | 9 +++++++-- src/env-inl.h | 4 ++++ src/env.h | 1 + src/node.h | 7 ++++++- src/node_options.cc | 6 ++++++ src/node_worker.cc | 2 ++ 7 files changed, 31 insertions(+), 5 deletions(-) diff --git a/lib/internal/bootstrap/pre_execution.js b/lib/internal/bootstrap/pre_execution.js index 0906b35edc5ff3..abb010cab006c6 100644 --- a/lib/internal/bootstrap/pre_execution.js +++ b/lib/internal/bootstrap/pre_execution.js @@ -11,7 +11,8 @@ const { const { getOptionValue, - shouldNotRegisterESMLoader + shouldNotRegisterESMLoader, + noGlobalSearchPaths } = require('internal/options'); const { reconnectZeroFillToggle } = require('internal/buffer'); @@ -420,7 +421,9 @@ function initializeWASI() { function initializeCJSLoader() { const CJSLoader = require('internal/modules/cjs/loader'); - CJSLoader.Module._initPaths(); + if (!noGlobalSearchPaths) { + CJSLoader.Module._initPaths(); + } // TODO(joyeecheung): deprecate this in favor of a proper hook? CJSLoader.Module.runMain = require('internal/modules/run_main').executeUserEntryPoint; diff --git a/lib/internal/options.js b/lib/internal/options.js index aa9c52e6988d65..f0f7a19280d220 100644 --- a/lib/internal/options.js +++ b/lib/internal/options.js @@ -1,6 +1,10 @@ 'use strict'; -const { getOptions, shouldNotRegisterESMLoader } = internalBinding('options'); +const { + getOptions, + shouldNotRegisterESMLoader, + noGlobalSearchPaths +} = internalBinding('options'); let warnOnAllowUnauthorized = true; @@ -57,5 +61,6 @@ module.exports = { }, getOptionValue, getAllowUnauthorized, - shouldNotRegisterESMLoader + shouldNotRegisterESMLoader, + noGlobalSearchPaths }; diff --git a/src/env-inl.h b/src/env-inl.h index b3b1ea908253b9..b3b832b47b1ac7 100644 --- a/src/env-inl.h +++ b/src/env-inl.h @@ -877,6 +877,10 @@ inline bool Environment::tracks_unmanaged_fds() const { return flags_ & EnvironmentFlags::kTrackUnmanagedFds; } +inline bool Environment::no_global_search_paths() const { + return flags_ & EnvironmentFlags::kNoGlobalSearchPaths; +} + bool Environment::filehandle_close_warning() const { return emit_filehandle_warning_; } diff --git a/src/env.h b/src/env.h index 24b4a48b5f9657..5e5c6a52fc0a82 100644 --- a/src/env.h +++ b/src/env.h @@ -1198,6 +1198,7 @@ class Environment : public MemoryRetainer { inline bool owns_process_state() const; inline bool owns_inspector() const; inline bool tracks_unmanaged_fds() const; + inline bool no_global_search_paths() const; inline uint64_t thread_id() const; inline worker::Worker* worker_context() const; Environment* worker_parent_env() const; diff --git a/src/node.h b/src/node.h index 1628a2147ea203..16fe8c4a64b6a4 100644 --- a/src/node.h +++ b/src/node.h @@ -403,7 +403,12 @@ enum Flags : uint64_t { kNoRegisterESMLoader = 1 << 3, // Set this flag to make Node.js track "raw" file descriptors, i.e. managed // by fs.open() and fs.close(), and close them during FreeEnvironment(). - kTrackUnmanagedFds = 1 << 4 + kTrackUnmanagedFds = 1 << 4, + // Set this flag to disable searching modules from global paths like + // $HOME/.node_modules and $NODE_PATH. This is used by standalone apps that + // do not expect to have their behaviors changed because of globally + // installed modules. + kNoGlobalSearchPaths = 1 << 5 }; } // namespace EnvironmentFlags diff --git a/src/node_options.cc b/src/node_options.cc index 40f8cf8690294e..30373fe033595e 100644 --- a/src/node_options.cc +++ b/src/node_options.cc @@ -1068,6 +1068,12 @@ void Initialize(Local target, Boolean::New(isolate, env->should_not_register_esm_loader())) .Check(); + target + ->Set(context, + FIXED_ONE_BYTE_STRING(env->isolate(), "noGlobalSearchPaths"), + Boolean::New(isolate, env->no_global_search_paths())) + .Check(); + Local types = Object::New(isolate); NODE_DEFINE_CONSTANT(types, kNoOp); NODE_DEFINE_CONSTANT(types, kV8Option); diff --git a/src/node_worker.cc b/src/node_worker.cc index 43a3862cc69dc3..340fac37516ebc 100644 --- a/src/node_worker.cc +++ b/src/node_worker.cc @@ -558,6 +558,8 @@ void Worker::New(const FunctionCallbackInfo& args) { CHECK(args[4]->IsBoolean()); if (args[4]->IsTrue() || env->tracks_unmanaged_fds()) worker->environment_flags_ |= EnvironmentFlags::kTrackUnmanagedFds; + if (env->no_global_search_paths()) + worker->environment_flags_ |= EnvironmentFlags::kNoGlobalSearchPaths; } void Worker::StartThread(const FunctionCallbackInfo& args) {