diff options
author | Anna Henningsen <anna@addaleax.net> | 2019-03-13 15:24:27 +0300 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2019-03-31 00:25:33 +0300 |
commit | f5b5fe3937ec353e43d30ae976725e0773e14c6f (patch) | |
tree | 57e532e5d82dbf3c553c83bfaa41bb50c76c4b71 /src/node_credentials.cc | |
parent | e4e2b0ce134ce781847272ae0b4bb889e75f223f (diff) |
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>
Diffstat (limited to 'src/node_credentials.cc')
-rw-r--r-- | src/node_credentials.cc | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/src/node_credentials.cc b/src/node_credentials.cc index 8d38c38a0c5..23b9ad2893d 100644 --- a/src/node_credentials.cc +++ b/src/node_credentials.cc @@ -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); |