src: allow per-Environment set of env vars

Abstract the `process.env` backing mechanism in C++ to allow
different kinds of backing stores for `process.env` for different
Environments.

PR-URL: https://github.com/nodejs/node/pull/26544
Fixes: https://github.com/nodejs/node/issues/24947
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Signed-off-by: Beth Griggs <Bethany.Griggs@uk.ibm.com>
This commit is contained in:
Anna Henningsen authored and Beth Griggs committed 2019-04-09 16:36:32 +01:00
1 parent 78fad3210c
commit d3840bcf0d
6 files changed
+202 -110

No files matched your search

+8
View File
@@ -447,6 +447,14 @@ inline uint64_t Environment::timer_base() const {
return timer_base_;
}
inline std::shared_ptr<KVStore> Environment::envvars() {
return envvars_;
}
inline void Environment::set_envvars(std::shared_ptr<KVStore> envvars) {
envvars_ = envvars;
}
inline bool Environment::printed_error() const {
return printed_error_;
}
+3 -1
View File
@@ -207,6 +207,8 @@ Environment::Environment(IsolateData* isolate_data,
set_as_callback_data_template(templ);
}
set_envvars(per_process::real_environment);
// We create new copies of the per-Environment option sets, so that it is
// easier to modify them after Environment creation. The defaults are
// part of the per-Isolate option set, for which in turn the defaults are
@@ -250,7 +252,7 @@ Environment::Environment(IsolateData* isolate_data,
should_abort_on_uncaught_toggle_[0] = 1;
std::string debug_cats;
credentials::SafeGetenv("NODE_DEBUG_NATIVE", &debug_cats);
credentials::SafeGetenv("NODE_DEBUG_NATIVE", &debug_cats, this);
set_debug_categories(debug_cats, true);
isolate()->GetHeapProfiler()->AddBuildEmbedderGraphCallback(
+20
View File
@@ -532,6 +532,23 @@ class AsyncRequest : public MemoryRetainer {
std::atomic_bool stopped_ {true};
};
class KVStore {
public:
virtual v8::Local<v8::String> Get(v8::Isolate* isolate,
v8::Local<v8::String> key) const = 0;
virtual void Set(v8::Isolate* isolate,
v8::Local<v8::String> key,
v8::Local<v8::String> value) = 0;
virtual int32_t Query(v8::Isolate* isolate,
v8::Local<v8::String> key) const = 0;
virtual void Delete(v8::Isolate* isolate, v8::Local<v8::String> key) = 0;
virtual v8::Local<v8::Array> Enumerate(v8::Isolate* isolate) const = 0;
};
namespace per_process {
extern std::shared_ptr<KVStore> real_environment;
}
class AsyncHooks {
public:
// Reason for both UidFields and Fields are that one is stored as a double*
@@ -781,6 +798,8 @@ class Environment {
inline ImmediateInfo* immediate_info();
inline TickInfo* tick_info();
inline uint64_t timer_base() const;
inline std::shared_ptr<KVStore> envvars();
inline void set_envvars(std::shared_ptr<KVStore> envvars);
inline IsolateData* isolate_data() const;
@@ -1075,6 +1094,7 @@ class Environment {
ImmediateInfo immediate_info_;
TickInfo tick_info_;
const uint64_t timer_base_;
std::shared_ptr<KVStore> envvars_;
bool printed_error_ = false;
bool emit_env_nonstring_warning_ = true;
bool emit_err_name_warning_ = true;
+21 -3
View File
@@ -15,11 +15,14 @@ using v8::Array;
using v8::Context;
using v8::Function;
using v8::FunctionCallbackInfo;
using v8::HandleScope;
using v8::Isolate;
using v8::Local;
using v8::MaybeLocal;
using v8::NewStringType;
using v8::Object;
using v8::String;
using v8::TryCatch;
using v8::Uint32;
using v8::Value;
@@ -30,13 +33,27 @@ bool linux_at_secure = false;
namespace credentials {
// Look up environment variable unless running as setuid root.
bool SafeGetenv(const char* key, std::string* text) {
bool SafeGetenv(const char* key, std::string* text, Environment* env) {
#if !defined(__CloudABI__) && !defined(_WIN32)
if (per_process::linux_at_secure || getuid() != geteuid() ||
getgid() != getegid())
goto fail;
#endif
if (env != nullptr) {
HandleScope handle_scope(env->isolate());
TryCatch ignore_errors(env->isolate());
MaybeLocal<String> value = env->envvars()->Get(
env->isolate(),
String::NewFromUtf8(env->isolate(), key, NewStringType::kNormal)
.ToLocalChecked());
if (value.IsEmpty()) goto fail;
String::Utf8Value utf8_value(env->isolate(), value.ToLocalChecked());
if (*utf8_value == nullptr) goto fail;
*text = std::string(*utf8_value, utf8_value.length());
return true;
}
{
Mutex::ScopedLock lock(per_process::env_var_mutex);
if (const char* value = getenv(key)) {
@@ -52,10 +69,11 @@ fail:
static void SafeGetenv(const FunctionCallbackInfo<Value>& args) {
CHECK(args[0]->IsString());
Isolate* isolate = args.GetIsolate();
Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();
Utf8Value strenvtag(isolate, args[0]);
std::string text;
if (!SafeGetenv(*strenvtag, &text)) return;
if (!SafeGetenv(*strenvtag, &text, env)) return;
Local<Value> result =
ToV8Value(isolate->GetCurrentContext(), text).ToLocalChecked();
args.GetReturnValue().Set(result);
+149 -105
View File
@@ -27,24 +27,29 @@ using v8::PropertyCallbackInfo;
using v8::String;
using v8::Value;
class RealEnvStore final : public KVStore {
public:
Local<String> Get(Isolate* isolate, Local<String> key) const override;
void Set(Isolate* isolate, Local<String> key, Local<String> value) override;
int32_t Query(Isolate* isolate, Local<String> key) const override;
void Delete(Isolate* isolate, Local<String> key) override;
Local<Array> Enumerate(Isolate* isolate) const override;
};
namespace per_process {
Mutex env_var_mutex;
std::shared_ptr<KVStore> real_environment = std::make_shared<RealEnvStore>();
} // namespace per_process
static void EnvGetter(Local<Name> property,
const PropertyCallbackInfo<Value>& info) {
Isolate* isolate = info.GetIsolate();
if (property->IsSymbol()) {
return info.GetReturnValue().SetUndefined();
}
Local<String> RealEnvStore::Get(Isolate* isolate,
Local<String> property) const {
Mutex::ScopedLock lock(per_process::env_var_mutex);
#ifdef __POSIX__
node::Utf8Value key(isolate, property);
const char* val = getenv(*key);
if (val) {
return info.GetReturnValue().Set(
String::NewFromUtf8(isolate, val, NewStringType::kNormal)
.ToLocalChecked());
return String::NewFromUtf8(isolate, val, NewStringType::kNormal)
.ToLocalChecked();
}
#else // _WIN32
node::TwoByteValue key(isolate, property);
@@ -62,11 +67,129 @@ static void EnvGetter(Local<Name> property,
isolate, two_byte_buffer, NewStringType::kNormal);
if (rc.IsEmpty()) {
isolate->ThrowException(ERR_STRING_TOO_LONG(isolate));
return;
return Local<String>();
}
return info.GetReturnValue().Set(rc.ToLocalChecked());
return rc.ToLocalChecked();
}
#endif
return Local<String>();
}
void RealEnvStore::Set(Isolate* isolate,
Local<String> property,
Local<String> value) {
Mutex::ScopedLock lock(per_process::env_var_mutex);
#ifdef __POSIX__
node::Utf8Value key(isolate, property);
node::Utf8Value val(isolate, value);
setenv(*key, *val, 1);
#else // _WIN32
node::TwoByteValue key(isolate, property);
node::TwoByteValue val(isolate, value);
WCHAR* key_ptr = reinterpret_cast<WCHAR*>(*key);
// Environment variables that start with '=' are read-only.
if (key_ptr[0] != L'=') {
SetEnvironmentVariableW(key_ptr, reinterpret_cast<WCHAR*>(*val));
}
#endif
}
int32_t RealEnvStore::Query(Isolate* isolate, Local<String> property) const {
Mutex::ScopedLock lock(per_process::env_var_mutex);
#ifdef __POSIX__
node::Utf8Value key(isolate, property);
if (getenv(*key)) return 0;
#else // _WIN32
node::TwoByteValue key(isolate, property);
WCHAR* key_ptr = reinterpret_cast<WCHAR*>(*key);
SetLastError(ERROR_SUCCESS);
if (GetEnvironmentVariableW(key_ptr, nullptr, 0) > 0 ||
GetLastError() == ERROR_SUCCESS) {
if (key_ptr[0] == L'=') {
// Environment variables that start with '=' are hidden and read-only.
return static_cast<int32_t>(v8::ReadOnly) |
static_cast<int32_t>(v8::DontDelete) |
static_cast<int32_t>(v8::DontEnum);
}
return 0;
}
#endif
return -1;
}
void RealEnvStore::Delete(Isolate* isolate, Local<String> property) {
Mutex::ScopedLock lock(per_process::env_var_mutex);
#ifdef __POSIX__
node::Utf8Value key(isolate, property);
unsetenv(*key);
#else
node::TwoByteValue key(isolate, property);
WCHAR* key_ptr = reinterpret_cast<WCHAR*>(*key);
SetEnvironmentVariableW(key_ptr, nullptr);
#endif
}
Local<Array> RealEnvStore::Enumerate(Isolate* isolate) const {
Mutex::ScopedLock lock(per_process::env_var_mutex);
#ifdef __POSIX__
int env_size = 0;
while (environ[env_size]) {
env_size++;
}
std::vector<Local<Value>> env_v(env_size);
for (int i = 0; i < env_size; ++i) {
const char* var = environ[i];
const char* s = strchr(var, '=');
const int length = s ? s - var : strlen(var);
env_v[i] = String::NewFromUtf8(isolate, var, NewStringType::kNormal, length)
.ToLocalChecked();
}
#else // _WIN32
std::vector<Local<Value>> env_v;
WCHAR* environment = GetEnvironmentStringsW();
if (environment == nullptr)
return Array::New(isolate); // This should not happen.
WCHAR* p = environment;
while (*p) {
WCHAR* s;
if (*p == L'=') {
// If the key starts with '=' it is a hidden environment variable.
p += wcslen(p) + 1;
continue;
} else {
s = wcschr(p, L'=');
}
if (!s) {
s = p + wcslen(p);
}
const uint16_t* two_byte_buffer = reinterpret_cast<const uint16_t*>(p);
const size_t two_byte_buffer_len = s - p;
v8::MaybeLocal<String> rc = String::NewFromTwoByte(
isolate, two_byte_buffer, NewStringType::kNormal, two_byte_buffer_len);
if (rc.IsEmpty()) {
isolate->ThrowException(ERR_STRING_TOO_LONG(isolate));
FreeEnvironmentStringsW(environment);
return Local<Array>();
}
env_v.push_back(rc.ToLocalChecked());
p = s + wcslen(s) + 1;
}
FreeEnvironmentStringsW(environment);
#endif
return Array::New(isolate, env_v.data(), env_v.size());
}
static void EnvGetter(Local<Name> property,
const PropertyCallbackInfo<Value>& info) {
Environment* env = Environment::GetCurrent(info);
if (property->IsSymbol()) {
return info.GetReturnValue().SetUndefined();
}
CHECK(property->IsString());
info.GetReturnValue().Set(
env->envvars()->Get(env->isolate(), property.As<String>()));
}
static void EnvSetter(Local<Name> property,
@@ -90,63 +213,33 @@ static void EnvSetter(Local<Name> property,
return;
}
Mutex::ScopedLock lock(per_process::env_var_mutex);
#ifdef __POSIX__
node::Utf8Value key(info.GetIsolate(), property);
node::Utf8Value val(info.GetIsolate(), value);
setenv(*key, *val, 1);
#else // _WIN32
node::TwoByteValue key(info.GetIsolate(), property);
node::TwoByteValue val(info.GetIsolate(), value);
WCHAR* key_ptr = reinterpret_cast<WCHAR*>(*key);
// Environment variables that start with '=' are read-only.
if (key_ptr[0] != L'=') {
SetEnvironmentVariableW(key_ptr, reinterpret_cast<WCHAR*>(*val));
Local<String> key;
Local<String> value_string;
if (!property->ToString(env->context()).ToLocal(&key) ||
!value->ToString(env->context()).ToLocal(&value_string)) {
return;
}
#endif
env->envvars()->Set(env->isolate(), key, value_string);
// Whether it worked or not, always return value.
info.GetReturnValue().Set(value);
}
static void EnvQuery(Local<Name> property,
const PropertyCallbackInfo<Integer>& info) {
Mutex::ScopedLock lock(per_process::env_var_mutex);
int32_t rc = -1; // Not found unless proven otherwise.
Environment* env = Environment::GetCurrent(info);
if (property->IsString()) {
#ifdef __POSIX__
node::Utf8Value key(info.GetIsolate(), property);
if (getenv(*key)) rc = 0;
#else // _WIN32
node::TwoByteValue key(info.GetIsolate(), property);
WCHAR* key_ptr = reinterpret_cast<WCHAR*>(*key);
SetLastError(ERROR_SUCCESS);
if (GetEnvironmentVariableW(key_ptr, nullptr, 0) > 0 ||
GetLastError() == ERROR_SUCCESS) {
rc = 0;
if (key_ptr[0] == L'=') {
// Environment variables that start with '=' are hidden and read-only.
rc = static_cast<int32_t>(v8::ReadOnly) |
static_cast<int32_t>(v8::DontDelete) |
static_cast<int32_t>(v8::DontEnum);
}
}
#endif
int32_t rc = env->envvars()->Query(env->isolate(), property.As<String>());
if (rc != -1) info.GetReturnValue().Set(rc);
}
if (rc != -1) info.GetReturnValue().Set(rc);
}
static void EnvDeleter(Local<Name> property,
const PropertyCallbackInfo<Boolean>& info) {
Mutex::ScopedLock lock(per_process::env_var_mutex);
Environment* env = Environment::GetCurrent(info);
if (property->IsString()) {
#ifdef __POSIX__
node::Utf8Value key(info.GetIsolate(), property);
unsetenv(*key);
#else
node::TwoByteValue key(info.GetIsolate(), property);
WCHAR* key_ptr = reinterpret_cast<WCHAR*>(*key);
SetEnvironmentVariableW(key_ptr, nullptr);
#endif
env->envvars()->Delete(env->isolate(), property.As<String>());
}
// process.env never has non-configurable properties, so always
@@ -156,58 +249,9 @@ static void EnvDeleter(Local<Name> property,
static void EnvEnumerator(const PropertyCallbackInfo<Array>& info) {
Environment* env = Environment::GetCurrent(info);
Isolate* isolate = env->isolate();
Mutex::ScopedLock lock(per_process::env_var_mutex);
Local<Array> envarr;
int env_size = 0;
#ifdef __POSIX__
while (environ[env_size]) {
env_size++;
}
std::vector<Local<Value>> env_v(env_size);
for (int i = 0; i < env_size; ++i) {
const char* var = environ[i];
const char* s = strchr(var, '=');
const int length = s ? s - var : strlen(var);
env_v[i] = String::NewFromUtf8(isolate, var, NewStringType::kNormal, length)
.ToLocalChecked();
}
#else // _WIN32
std::vector<Local<Value>> env_v;
WCHAR* environment = GetEnvironmentStringsW();
if (environment == nullptr) return; // This should not happen.
WCHAR* p = environment;
while (*p) {
WCHAR* s;
if (*p == L'=') {
// If the key starts with '=' it is a hidden environment variable.
p += wcslen(p) + 1;
continue;
} else {
s = wcschr(p, L'=');
}
if (!s) {
s = p + wcslen(p);
}
const uint16_t* two_byte_buffer = reinterpret_cast<const uint16_t*>(p);
const size_t two_byte_buffer_len = s - p;
v8::MaybeLocal<String> rc = String::NewFromTwoByte(
isolate, two_byte_buffer, NewStringType::kNormal, two_byte_buffer_len);
if (rc.IsEmpty()) {
isolate->ThrowException(ERR_STRING_TOO_LONG(isolate));
FreeEnvironmentStringsW(environment);
return;
}
env_v.push_back(rc.ToLocalChecked());
p = s + wcslen(s) + 1;
}
FreeEnvironmentStringsW(environment);
#endif
envarr = Array::New(isolate, env_v.data(), env_v.size());
info.GetReturnValue().Set(envarr);
info.GetReturnValue().Set(
env->envvars()->Enumerate(env->isolate()));
}
MaybeLocal<Object> CreateEnvVarProxy(Local<Context> context,
+1 -1
View File
@@ -293,7 +293,7 @@ int ThreadPoolWork::CancelWork() {
#endif // __POSIX__ && !defined(__ANDROID__) && !defined(__CloudABI__)
namespace credentials {
bool SafeGetenv(const char* key, std::string* text);
bool SafeGetenv(const char* key, std::string* text, Environment* env = nullptr);
} // namespace credentials
void DefineZlibConstants(v8::Local<v8::Object> target);