Skip to content

src: improve error handling in process.env handling #57707

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

Closed
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
72 changes: 47 additions & 25 deletions src/node_env_var.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ using v8::Maybe;
using v8::MaybeLocal;
using v8::Name;
using v8::NamedPropertyHandlerConfiguration;
using v8::NewStringType;
using v8::Nothing;
using v8::Object;
using v8::ObjectTemplate;
Expand All @@ -45,7 +44,7 @@ class RealEnvStore final : public KVStore {
int32_t Query(Isolate* isolate, Local<String> key) const override;
int32_t Query(const char* key) const override;
void Delete(Isolate* isolate, Local<String> key) override;
Local<Array> Enumerate(Isolate* isolate) const override;
MaybeLocal<Array> Enumerate(Isolate* isolate) const override;
};

class MapKVStore final : public KVStore {
Expand All @@ -56,7 +55,7 @@ class MapKVStore final : public KVStore {
int32_t Query(Isolate* isolate, Local<String> key) const override;
int32_t Query(const char* key) const override;
void Delete(Isolate* isolate, Local<String> key) override;
Local<Array> Enumerate(Isolate* isolate) const override;
MaybeLocal<Array> Enumerate(Isolate* isolate) const override;

std::shared_ptr<KVStore> Clone(Isolate* isolate) const override;

Expand Down Expand Up @@ -131,8 +130,12 @@ MaybeLocal<String> RealEnvStore::Get(Isolate* isolate,

if (value.has_value()) {
std::string val = value.value();
return String::NewFromUtf8(
isolate, val.data(), NewStringType::kNormal, val.size());
Local<Value> ret;
if (!ToV8Value(isolate->GetCurrentContext(), val).ToLocal(&ret)) {
return {};
}
DCHECK(ret->IsString());
return ret.As<String>();
}

return MaybeLocal<String>();
Expand Down Expand Up @@ -188,7 +191,7 @@ void RealEnvStore::Delete(Isolate* isolate, Local<String> property) {
DateTimeConfigurationChangeNotification(isolate, key);
}

Local<Array> RealEnvStore::Enumerate(Isolate* isolate) const {
MaybeLocal<Array> RealEnvStore::Enumerate(Isolate* isolate) const {
Mutex::ScopedLock lock(per_process::env_var_mutex);
uv_env_item_t* items;
int count;
Expand All @@ -203,12 +206,12 @@ Local<Array> RealEnvStore::Enumerate(Isolate* isolate) const {
// If the key starts with '=' it is a hidden environment variable.
if (items[i].name[0] == '=') continue;
#endif
MaybeLocal<String> str = String::NewFromUtf8(isolate, items[i].name);
if (str.IsEmpty()) {
Local<Value> str;
if (!String::NewFromUtf8(isolate, items[i].name).ToLocal(&str)) {
isolate->ThrowException(ERR_STRING_TOO_LONG(isolate));
return Local<Array>();
return {};
}
env_v[env_v_index++] = str.ToLocalChecked();
env_v[env_v_index++] = str;
}

return Array::New(isolate, env_v.out(), env_v_index);
Expand All @@ -219,14 +222,22 @@ std::shared_ptr<KVStore> KVStore::Clone(Isolate* isolate) const {
Local<Context> context = isolate->GetCurrentContext();

std::shared_ptr<KVStore> copy = KVStore::CreateMapKVStore();
Local<Array> keys = Enumerate(isolate);
Local<Array> keys;
if (!Enumerate(isolate).ToLocal(&keys)) {
return nullptr;
}
uint32_t keys_length = keys->Length();
for (uint32_t i = 0; i < keys_length; i++) {
Local<Value> key = keys->Get(context, i).ToLocalChecked();
Local<Value> key;
Local<Value> value;
if (!keys->Get(context, i).ToLocal(&key)) {
return nullptr;
}
CHECK(key->IsString());
copy->Set(isolate,
key.As<String>(),
Get(isolate, key.As<String>()).ToLocalChecked());
if (!Get(isolate, key.As<String>()).ToLocal(&value)) {
return nullptr;
}
copy->Set(isolate, key.As<String>(), value.As<String>());
}
return copy;
}
Expand All @@ -242,8 +253,12 @@ MaybeLocal<String> MapKVStore::Get(Isolate* isolate, Local<String> key) const {
std::optional<std::string> value = Get(*str);
if (!value.has_value()) return MaybeLocal<String>();
std::string val = value.value();
return String::NewFromUtf8(
isolate, val.data(), NewStringType::kNormal, val.size());
Local<Value> ret;
if (!ToV8Value(isolate->GetCurrentContext(), val).ToLocal(&ret)) {
return {};
}
DCHECK(ret->IsString());
return ret.As<String>();
}

void MapKVStore::Set(Isolate* isolate, Local<String> key, Local<String> value) {
Expand Down Expand Up @@ -272,15 +287,16 @@ void MapKVStore::Delete(Isolate* isolate, Local<String> key) {
map_.erase(std::string(*str, str.length()));
}

Local<Array> MapKVStore::Enumerate(Isolate* isolate) const {
MaybeLocal<Array> MapKVStore::Enumerate(Isolate* isolate) const {
Mutex::ScopedLock lock(mutex_);
LocalVector<Value> values(isolate);
values.reserve(map_.size());
for (const auto& pair : map_) {
values.emplace_back(
String::NewFromUtf8(isolate, pair.first.data(),
NewStringType::kNormal, pair.first.size())
.ToLocalChecked());
Local<Value> val;
if (!ToV8Value(isolate->GetCurrentContext(), pair.first).ToLocal(&val)) {
return {};
}
values.emplace_back(val);
}
return Array::New(isolate, values.data(), values.size());
}
Expand Down Expand Up @@ -324,7 +340,10 @@ Maybe<void> KVStore::AssignToObject(v8::Isolate* isolate,
v8::Local<v8::Context> context,
v8::Local<v8::Object> object) {
HandleScope scope(isolate);
Local<Array> keys = Enumerate(isolate);
Local<Array> keys;
if (!Enumerate(isolate).ToLocal(&keys)) {
return Nothing<void>();
}
uint32_t keys_length = keys->Length();
for (uint32_t i = 0; i < keys_length; i++) {
Local<Value> key;
Expand Down Expand Up @@ -421,6 +440,7 @@ static Intercepted EnvGetter(Local<Name> property,
TraceEnvVar(env, "get", property.As<String>());

if (has_env) {
// ToLocalChecked here is ok since we check IsEmpty above.
Copy link
Contributor

@aduh95 aduh95 Apr 5, 2025

Choose a reason for hiding this comment

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

Wouldn't it be worth refactoring to use ToLocal instead? (to clarify, I'm not asking you to do it, I'm curious is all, and I get that the current code would not crash as is, but might still be a good idea to refactor this for consistency sake)

Copy link
Member Author

Choose a reason for hiding this comment

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

Possibly? Not that critical tho.

info.GetReturnValue().Set(value_string.ToLocalChecked());
return Intercepted::kYes;
}
Expand Down Expand Up @@ -502,8 +522,10 @@ static void EnvEnumerator(const PropertyCallbackInfo<Array>& info) {

TraceEnvVar(env, "enumerate environment variables");

info.GetReturnValue().Set(
env->env_vars()->Enumerate(env->isolate()));
Local<Array> ret;
if (env->env_vars()->Enumerate(env->isolate()).ToLocal(&ret)) {
info.GetReturnValue().Set(ret);
}
}

static Intercepted EnvDefiner(Local<Name> property,
Expand Down
4 changes: 4 additions & 0 deletions src/node_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,10 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) {
env_vars = env->env_vars();
}

if (!env_vars) {
THROW_ERR_OPERATION_FAILED(env, "Failed to copy environment variables");
}

if (args[1]->IsObject() || args[2]->IsArray()) {
per_isolate_opts.reset(new PerIsolateOptions());

Expand Down
2 changes: 1 addition & 1 deletion src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ class KVStore {
v8::Local<v8::String> key) const = 0;
virtual int32_t Query(const char* 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;
virtual v8::MaybeLocal<v8::Array> Enumerate(v8::Isolate* isolate) const = 0;

virtual std::shared_ptr<KVStore> Clone(v8::Isolate* isolate) const;
virtual v8::Maybe<void> AssignFromObject(v8::Local<v8::Context> context,
Expand Down
Loading