GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_env_var.cc Lines: 192 202 95.0 %
Date: 2022-05-23 04:15:47 Branches: 93 128 72.7 %

Line Branch Exec Source
1
#include "debug_utils-inl.h"
2
#include "env-inl.h"
3
#include "node_errors.h"
4
#include "node_external_reference.h"
5
#include "node_i18n.h"
6
#include "node_process-inl.h"
7
8
#include <time.h>  // tzset(), _tzset()
9
10
namespace node {
11
using v8::Array;
12
using v8::Boolean;
13
using v8::Context;
14
using v8::DontDelete;
15
using v8::DontEnum;
16
using v8::EscapableHandleScope;
17
using v8::HandleScope;
18
using v8::Integer;
19
using v8::Isolate;
20
using v8::Just;
21
using v8::Local;
22
using v8::Maybe;
23
using v8::MaybeLocal;
24
using v8::Name;
25
using v8::NamedPropertyHandlerConfiguration;
26
using v8::NewStringType;
27
using v8::Nothing;
28
using v8::Object;
29
using v8::ObjectTemplate;
30
using v8::PropertyCallbackInfo;
31
using v8::PropertyDescriptor;
32
using v8::PropertyHandlerFlags;
33
using v8::ReadOnly;
34
using v8::String;
35
using v8::Value;
36
37
class RealEnvStore final : public KVStore {
38
 public:
39
  MaybeLocal<String> Get(Isolate* isolate, Local<String> key) const override;
40
  Maybe<std::string> Get(const char* key) const override;
41
  void Set(Isolate* isolate, Local<String> key, Local<String> value) override;
42
  int32_t Query(Isolate* isolate, Local<String> key) const override;
43
  int32_t Query(const char* key) const override;
44
  void Delete(Isolate* isolate, Local<String> key) override;
45
  Local<Array> Enumerate(Isolate* isolate) const override;
46
};
47
48
class MapKVStore final : public KVStore {
49
 public:
50
  MaybeLocal<String> Get(Isolate* isolate, Local<String> key) const override;
51
  Maybe<std::string> Get(const char* key) const override;
52
  void Set(Isolate* isolate, Local<String> key, Local<String> value) override;
53
  int32_t Query(Isolate* isolate, Local<String> key) const override;
54
  int32_t Query(const char* key) const override;
55
  void Delete(Isolate* isolate, Local<String> key) override;
56
  Local<Array> Enumerate(Isolate* isolate) const override;
57
58
  std::shared_ptr<KVStore> Clone(Isolate* isolate) const override;
59
60
1025
  MapKVStore() = default;
61
10
  MapKVStore(const MapKVStore& other) : KVStore(), map_(other.map_) {}
62
63
 private:
64
  mutable Mutex mutex_;
65
  std::unordered_map<std::string, std::string> map_;
66
};
67
68
namespace per_process {
69
Mutex env_var_mutex;
70
std::shared_ptr<KVStore> system_environment = std::make_shared<RealEnvStore>();
71
}  // namespace per_process
72
73
template <typename T>
74
8702
void DateTimeConfigurationChangeNotification(
75
    Isolate* isolate,
76
    const T& key,
77
    const char* val = nullptr) {
78


8702
  if (key.length() == 2 && key[0] == 'T' && key[1] == 'Z') {
79
#ifdef __POSIX__
80
66
    tzset();
81
66
    isolate->DateTimeConfigurationChangeNotification(
82
        Isolate::TimeZoneDetection::kRedetect);
83
#else
84
    _tzset();
85
86
# if defined(NODE_HAVE_I18N_SUPPORT)
87
    isolate->DateTimeConfigurationChangeNotification(
88
        Isolate::TimeZoneDetection::kSkip);
89
90
    // On windows, the TZ environment is not supported out of the box.
91
    // By default, v8 will only be able to detect the system configured
92
    // timezone. This supports using the TZ environment variable to set
93
    // the default timezone instead.
94
    if (val != nullptr) i18n::SetDefaultTimeZone(val);
95
# else
96
    isolate->DateTimeConfigurationChangeNotification(
97
        Isolate::TimeZoneDetection::kRedetect);
98
# endif
99
#endif
100
  }
101
8702
}
102
103
645211
Maybe<std::string> RealEnvStore::Get(const char* key) const {
104
1290422
  Mutex::ScopedLock lock(per_process::env_var_mutex);
105
106
645211
  size_t init_sz = 256;
107
1290422
  MaybeStackBuffer<char, 256> val;
108
645211
  int ret = uv_os_getenv(key, *val, &init_sz);
109
110
645211
  if (ret == UV_ENOBUFS) {
111
    // Buffer is not large enough, reallocate to the updated init_sz
112
    // and fetch env value again.
113
7004
    val.AllocateSufficientStorage(init_sz);
114
7004
    ret = uv_os_getenv(key, *val, &init_sz);
115
  }
116
117
645211
  if (ret >= 0) {  // Env key value fetch success.
118
578438
    return Just(std::string(*val, init_sz));
119
  }
120
121
66773
  return Nothing<std::string>();
122
}
123
124
645211
MaybeLocal<String> RealEnvStore::Get(Isolate* isolate,
125
                                     Local<String> property) const {
126
1290422
  node::Utf8Value key(isolate, property);
127
1290422
  Maybe<std::string> value = Get(*key);
128
129
645211
  if (value.IsJust()) {
130
578438
    std::string val = value.FromJust();
131
    return String::NewFromUtf8(
132
578438
        isolate, val.data(), NewStringType::kNormal, val.size());
133
  }
134
135
66773
  return MaybeLocal<String>();
136
}
137
138
7661
void RealEnvStore::Set(Isolate* isolate,
139
                       Local<String> property,
140
                       Local<String> value) {
141
15322
  Mutex::ScopedLock lock(per_process::env_var_mutex);
142
143
15322
  node::Utf8Value key(isolate, property);
144
15322
  node::Utf8Value val(isolate, value);
145
146
#ifdef _WIN32
147
  if (key.length() > 0 && key[0] == '=') return;
148
#endif
149
7661
  uv_os_setenv(*key, *val);
150
7661
  DateTimeConfigurationChangeNotification(isolate, key, *val);
151
7661
}
152
153
700782
int32_t RealEnvStore::Query(const char* key) const {
154
1401564
  Mutex::ScopedLock lock(per_process::env_var_mutex);
155
156
  char val[2];
157
700782
  size_t init_sz = sizeof(val);
158
700782
  int ret = uv_os_getenv(key, val, &init_sz);
159
160
700782
  if (ret == UV_ENOENT) {
161
6108
    return -1;
162
  }
163
164
#ifdef _WIN32
165
  if (key[0] == '=') {
166
    return static_cast<int32_t>(ReadOnly) |
167
           static_cast<int32_t>(DontDelete) |
168
           static_cast<int32_t>(DontEnum);
169
  }
170
#endif
171
172
694674
  return 0;
173
}
174
175
700782
int32_t RealEnvStore::Query(Isolate* isolate, Local<String> property) const {
176
1401564
  node::Utf8Value key(isolate, property);
177
700782
  return Query(*key);
178
}
179
180
1041
void RealEnvStore::Delete(Isolate* isolate, Local<String> property) {
181
2082
  Mutex::ScopedLock lock(per_process::env_var_mutex);
182
183
2082
  node::Utf8Value key(isolate, property);
184
1041
  uv_os_unsetenv(*key);
185
1041
  DateTimeConfigurationChangeNotification(isolate, key);
186
1041
}
187
188
8602
Local<Array> RealEnvStore::Enumerate(Isolate* isolate) const {
189
17204
  Mutex::ScopedLock lock(per_process::env_var_mutex);
190
  uv_env_item_t* items;
191
  int count;
192
193
25806
  auto cleanup = OnScopeLeave([&]() { uv_os_free_environ(items, count); });
194
8602
  CHECK_EQ(uv_os_environ(&items, &count), 0);
195
196
17204
  MaybeStackBuffer<Local<Value>, 256> env_v(count);
197
8602
  int env_v_index = 0;
198
636910
  for (int i = 0; i < count; i++) {
199
#ifdef _WIN32
200
    // If the key starts with '=' it is a hidden environment variable.
201
    if (items[i].name[0] == '=') continue;
202
#endif
203
628308
    MaybeLocal<String> str = String::NewFromUtf8(isolate, items[i].name);
204
628308
    if (str.IsEmpty()) {
205
      isolate->ThrowException(ERR_STRING_TOO_LONG(isolate));
206
      return Local<Array>();
207
    }
208
628308
    env_v[env_v_index++] = str.ToLocalChecked();
209
  }
210
211
8602
  return Array::New(isolate, env_v.out(), env_v_index);
212
}
213
214
999
std::shared_ptr<KVStore> KVStore::Clone(Isolate* isolate) const {
215
1998
  HandleScope handle_scope(isolate);
216
999
  Local<Context> context = isolate->GetCurrentContext();
217
218
999
  std::shared_ptr<KVStore> copy = KVStore::CreateMapKVStore();
219
999
  Local<Array> keys = Enumerate(isolate);
220
999
  uint32_t keys_length = keys->Length();
221
73184
  for (uint32_t i = 0; i < keys_length; i++) {
222
144370
    Local<Value> key = keys->Get(context, i).ToLocalChecked();
223
144370
    CHECK(key->IsString());
224
144370
    copy->Set(isolate,
225
              key.As<String>(),
226
216555
              Get(isolate, key.As<String>()).ToLocalChecked());
227
  }
228
999
  return copy;
229
}
230
231
38841
Maybe<std::string> MapKVStore::Get(const char* key) const {
232
38841
  Mutex::ScopedLock lock(mutex_);
233
38841
  auto it = map_.find(key);
234
38841
  return it == map_.end() ? Nothing<std::string>() : Just(it->second);
235
}
236
237
37737
MaybeLocal<String> MapKVStore::Get(Isolate* isolate, Local<String> key) const {
238
75474
  Utf8Value str(isolate, key);
239
75474
  Maybe<std::string> value = Get(*str);
240
42777
  if (value.IsNothing()) return Local<String>();
241
32697
  std::string val = value.FromJust();
242
  return String::NewFromUtf8(
243
32697
      isolate, val.data(), NewStringType::kNormal, val.size());
244
}
245
246
73924
void MapKVStore::Set(Isolate* isolate, Local<String> key, Local<String> value) {
247
147848
  Mutex::ScopedLock lock(mutex_);
248
147848
  Utf8Value key_str(isolate, key);
249
147848
  Utf8Value value_str(isolate, value);
250


73924
  if (*key_str != nullptr && key_str.length() > 0 && *value_str != nullptr) {
251
147848
    map_[std::string(*key_str, key_str.length())] =
252
221772
        std::string(*value_str, value_str.length());
253
  }
254
73924
}
255
256
53869
int32_t MapKVStore::Query(const char* key) const {
257
53869
  Mutex::ScopedLock lock(mutex_);
258
53869
  return map_.find(key) == map_.end() ? -1 : 0;
259
}
260
261
53869
int32_t MapKVStore::Query(Isolate* isolate, Local<String> key) const {
262
107738
  Utf8Value str(isolate, key);
263
53869
  return Query(*str);
264
}
265
266
void MapKVStore::Delete(Isolate* isolate, Local<String> key) {
267
  Mutex::ScopedLock lock(mutex_);
268
  Utf8Value str(isolate, key);
269
  map_.erase(std::string(*str, str.length()));
270
}
271
272
722
Local<Array> MapKVStore::Enumerate(Isolate* isolate) const {
273
1444
  Mutex::ScopedLock lock(mutex_);
274
1444
  std::vector<Local<Value>> values;
275
722
  values.reserve(map_.size());
276
53867
  for (const auto& pair : map_) {
277
    values.emplace_back(
278
53145
        String::NewFromUtf8(isolate, pair.first.data(),
279
53145
                            NewStringType::kNormal, pair.first.size())
280
53145
            .ToLocalChecked());
281
  }
282
722
  return Array::New(isolate, values.data(), values.size());
283
}
284
285
10
std::shared_ptr<KVStore> MapKVStore::Clone(Isolate* isolate) const {
286
10
  return std::make_shared<MapKVStore>(*this);
287
}
288
289
1025
std::shared_ptr<KVStore> KVStore::CreateMapKVStore() {
290
1025
  return std::make_shared<MapKVStore>();
291
}
292
293
26
Maybe<bool> KVStore::AssignFromObject(Local<Context> context,
294
                                      Local<Object> entries) {
295
26
  Isolate* isolate = context->GetIsolate();
296
52
  HandleScope handle_scope(isolate);
297
  Local<Array> keys;
298
52
  if (!entries->GetOwnPropertyNames(context).ToLocal(&keys))
299
    return Nothing<bool>();
300
26
  uint32_t keys_length = keys->Length();
301
1758
  for (uint32_t i = 0; i < keys_length; i++) {
302
    Local<Value> key;
303
3464
    if (!keys->Get(context, i).ToLocal(&key))
304
      return Nothing<bool>();
305
3464
    if (!key->IsString()) continue;
306
307
    Local<Value> value;
308
    Local<String> value_string;
309
5196
    if (!entries->Get(context, key).ToLocal(&value) ||
310

5196
        !value->ToString(context).ToLocal(&value_string)) {
311
      return Nothing<bool>();
312
    }
313
314
3464
    Set(isolate, key.As<String>(), value_string);
315
  }
316
26
  return Just(true);
317
}
318
319
594097
static void EnvGetter(Local<Name> property,
320
                      const PropertyCallbackInfo<Value>& info) {
321
594097
  Environment* env = Environment::GetCurrent(info);
322
594097
  CHECK(env->has_run_bootstrapping_code());
323
594097
  if (property->IsSymbol()) {
324
16044
    return info.GetReturnValue().SetUndefined();
325
  }
326
1172150
  CHECK(property->IsString());
327
  MaybeLocal<String> value_string =
328
1172150
      env->env_vars()->Get(env->isolate(), property.As<String>());
329
586075
  if (!value_string.IsEmpty()) {
330
1053040
    info.GetReturnValue().Set(value_string.ToLocalChecked());
331
  }
332
}
333
334
7670
static void EnvSetter(Local<Name> property,
335
                      Local<Value> value,
336
                      const PropertyCallbackInfo<Value>& info) {
337
7670
  Environment* env = Environment::GetCurrent(info);
338
7670
  CHECK(env->has_run_bootstrapping_code());
339
  // calling env->EmitProcessEnvWarning() sets a variable indicating that
340
  // warnings have been emitted. It should be called last after other
341
  // conditions leading to a warning have been met.
342

7710
  if (env->options()->pending_deprecation && !value->IsString() &&
343


7692
      !value->IsNumber() && !value->IsBoolean() &&
344
1
      env->EmitProcessEnvWarning()) {
345
1
    if (ProcessEmitDeprecationWarning(
346
            env,
347
            "Assigning any value other than a string, number, or boolean to a "
348
            "process.env property is deprecated. Please make sure to convert "
349
            "the "
350
            "value to a string before setting process.env with it.",
351
1
            "DEP0104")
352
1
            .IsNothing())
353
2
      return;
354
  }
355
356
  Local<String> key;
357
  Local<String> value_string;
358
23009
  if (!property->ToString(env->context()).ToLocal(&key) ||
359

23008
      !value->ToString(env->context()).ToLocal(&value_string)) {
360
2
    return;
361
  }
362
363
7668
  env->env_vars()->Set(env->isolate(), key, value_string);
364
365
  // Whether it worked or not, always return value.
366
15336
  info.GetReturnValue().Set(value);
367
}
368
369
754652
static void EnvQuery(Local<Name> property,
370
                     const PropertyCallbackInfo<Integer>& info) {
371
754652
  Environment* env = Environment::GetCurrent(info);
372
754652
  CHECK(env->has_run_bootstrapping_code());
373
1509304
  if (property->IsString()) {
374
1509302
    int32_t rc = env->env_vars()->Query(env->isolate(), property.As<String>());
375
2251013
    if (rc != -1) info.GetReturnValue().Set(rc);
376
  }
377
754652
}
378
379
1042
static void EnvDeleter(Local<Name> property,
380
                       const PropertyCallbackInfo<Boolean>& info) {
381
1042
  Environment* env = Environment::GetCurrent(info);
382
1042
  CHECK(env->has_run_bootstrapping_code());
383
2084
  if (property->IsString()) {
384
2082
    env->env_vars()->Delete(env->isolate(), property.As<String>());
385
  }
386
387
  // process.env never has non-configurable properties, so always
388
  // return true like the tc39 delete operator.
389
1042
  info.GetReturnValue().Set(true);
390
1042
}
391
392
8325
static void EnvEnumerator(const PropertyCallbackInfo<Array>& info) {
393
8325
  Environment* env = Environment::GetCurrent(info);
394
8325
  CHECK(env->has_run_bootstrapping_code());
395
396
8325
  info.GetReturnValue().Set(
397
16650
      env->env_vars()->Enumerate(env->isolate()));
398
8325
}
399
400
7
static void EnvDefiner(Local<Name> property,
401
                       const PropertyDescriptor& desc,
402
                       const PropertyCallbackInfo<Value>& info) {
403
7
  Environment* env = Environment::GetCurrent(info);
404
7
  if (desc.has_value()) {
405
3
    if (!desc.has_writable() ||
406

4
        !desc.has_enumerable() ||
407
1
        !desc.has_configurable()) {
408
2
      THROW_ERR_INVALID_OBJECT_DEFINE_PROPERTY(env,
409
                                               "'process.env' only accepts a "
410
                                               "configurable, writable,"
411
                                               " and enumerable "
412
                                               "data descriptor");
413
1
    } else if (!desc.configurable() ||
414

2
               !desc.enumerable() ||
415
1
               !desc.writable()) {
416
      THROW_ERR_INVALID_OBJECT_DEFINE_PROPERTY(env,
417
                                               "'process.env' only accepts a "
418
                                               "configurable, writable,"
419
                                               " and enumerable "
420
                                               "data descriptor");
421
    } else {
422
1
      return EnvSetter(property, desc.value(), info);
423
    }
424

4
  } else if (desc.has_get() || desc.has_set()) {
425
    // we don't accept a getter/setter in 'process.env'
426
1
    THROW_ERR_INVALID_OBJECT_DEFINE_PROPERTY(env,
427
                             "'process.env' does not accept an"
428
                                             "accessor(getter/setter)"
429
                                             " descriptor");
430
  } else {
431
3
    THROW_ERR_INVALID_OBJECT_DEFINE_PROPERTY(env,
432
                                             "'process.env' only accepts a "
433
                                             "configurable, writable,"
434
                                             " and enumerable "
435
                                             "data descriptor");
436
  }
437
}
438
439
847
MaybeLocal<Object> CreateEnvVarProxy(Local<Context> context, Isolate* isolate) {
440
847
  EscapableHandleScope scope(isolate);
441
847
  Local<ObjectTemplate> env_proxy_template = ObjectTemplate::New(isolate);
442
1694
  env_proxy_template->SetHandler(NamedPropertyHandlerConfiguration(
443
      EnvGetter,
444
      EnvSetter,
445
      EnvQuery,
446
      EnvDeleter,
447
      EnvEnumerator,
448
      EnvDefiner,
449
      nullptr,
450
      Local<Value>(),
451
      PropertyHandlerFlags::kHasNoSideEffect));
452
1694
  return scope.EscapeMaybe(env_proxy_template->NewInstance(context));
453
}
454
455
5187
void RegisterEnvVarExternalReferences(ExternalReferenceRegistry* registry) {
456
5187
  registry->Register(EnvGetter);
457
5187
  registry->Register(EnvSetter);
458
5187
  registry->Register(EnvQuery);
459
5187
  registry->Register(EnvDeleter);
460
5187
  registry->Register(EnvEnumerator);
461
5187
  registry->Register(EnvDefiner);
462
5187
}
463
}  // namespace node
464
465
5187
NODE_MODULE_EXTERNAL_REFERENCE(env_var, node::RegisterEnvVarExternalReferences)