Skip to content

Commit 08d966c

Browse files
authored
src: handle indexed properties in process.env
Closes: #60795 PR-URL: #60826 Fixes: #60795 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Stefan Stojanovic <stefan.stojanovic@janeasystems.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent bff6ea4 commit 08d966c

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

src/node_env_var.cc

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "node_external_reference.h"
55
#include "node_i18n.h"
66
#include "node_process-inl.h"
7+
#include "util.h"
78

89
#include <time.h> // tzset(), _tzset()
910
#include <optional>
@@ -16,6 +17,7 @@ using v8::DontDelete;
1617
using v8::DontEnum;
1718
using v8::FunctionTemplate;
1819
using v8::HandleScope;
20+
using v8::IndexedPropertyHandlerConfiguration;
1921
using v8::Integer;
2022
using v8::Intercepted;
2123
using v8::Isolate;
@@ -570,6 +572,43 @@ static Intercepted EnvDefiner(Local<Name> property,
570572
}
571573
}
572574

575+
static Intercepted EnvGetterIndexed(uint32_t index,
576+
const PropertyCallbackInfo<Value>& info) {
577+
Environment* env = Environment::GetCurrent(info);
578+
Local<Name> name = Uint32ToString(env->context(), index);
579+
return EnvGetter(name, info);
580+
}
581+
582+
static Intercepted EnvSetterIndexed(uint32_t index,
583+
Local<Value> value,
584+
const PropertyCallbackInfo<void>& info) {
585+
Environment* env = Environment::GetCurrent(info);
586+
Local<Name> name = Uint32ToString(env->context(), index);
587+
return EnvSetter(name, value, info);
588+
}
589+
590+
static Intercepted EnvQueryIndexed(uint32_t index,
591+
const PropertyCallbackInfo<Integer>& info) {
592+
Environment* env = Environment::GetCurrent(info);
593+
Local<Name> name = Uint32ToString(env->context(), index);
594+
return EnvQuery(name, info);
595+
}
596+
597+
static Intercepted EnvDeleterIndexed(
598+
uint32_t index, const PropertyCallbackInfo<Boolean>& info) {
599+
Environment* env = Environment::GetCurrent(info);
600+
Local<Name> name = Uint32ToString(env->context(), index);
601+
return EnvDeleter(name, info);
602+
}
603+
604+
static Intercepted EnvDefinerIndexed(uint32_t index,
605+
const PropertyDescriptor& desc,
606+
const PropertyCallbackInfo<void>& info) {
607+
Environment* env = Environment::GetCurrent(info);
608+
Local<Name> name = Uint32ToString(env->context(), index);
609+
return EnvDefiner(name, desc, info);
610+
}
611+
573612
void CreateEnvProxyTemplate(IsolateData* isolate_data) {
574613
Isolate* isolate = isolate_data->isolate();
575614
HandleScope scope(isolate);
@@ -588,6 +627,16 @@ void CreateEnvProxyTemplate(IsolateData* isolate_data) {
588627
nullptr,
589628
Local<Value>(),
590629
PropertyHandlerFlags::kHasNoSideEffect));
630+
env_proxy_template->SetHandler(IndexedPropertyHandlerConfiguration(
631+
EnvGetterIndexed,
632+
EnvSetterIndexed,
633+
EnvQueryIndexed,
634+
EnvDeleterIndexed,
635+
nullptr,
636+
EnvDefinerIndexed,
637+
nullptr,
638+
Local<Value>(),
639+
PropertyHandlerFlags::kHasNoSideEffect));
591640
isolate_data->set_env_proxy_template(env_proxy_template);
592641
isolate_data->set_env_proxy_ctor_template(env_proxy_ctor_template);
593642
}
@@ -599,6 +648,11 @@ void RegisterEnvVarExternalReferences(ExternalReferenceRegistry* registry) {
599648
registry->Register(EnvDeleter);
600649
registry->Register(EnvEnumerator);
601650
registry->Register(EnvDefiner);
651+
registry->Register(EnvGetterIndexed);
652+
registry->Register(EnvSetterIndexed);
653+
registry->Register(EnvQueryIndexed);
654+
registry->Register(EnvDeleterIndexed);
655+
registry->Register(EnvDefinerIndexed);
602656
}
603657
} // namespace node
604658

test/parallel/test-process-env.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ if (process.argv[2] === 'you-are-the-child') {
2929
assert.strictEqual('NODE_PROCESS_ENV_DELETED' in process.env, false);
3030
assert.strictEqual(process.env.NODE_PROCESS_ENV, '42');
3131
assert.strictEqual(process.env.hasOwnProperty, 'asdf');
32+
assert.strictEqual(process.env[42], 'forty-two');
3233
const has = Object.hasOwn(process.env, 'hasOwnProperty');
3334
assert.strictEqual(has, true);
3435
process.exit(0);
@@ -47,6 +48,9 @@ if (process.argv[2] === 'you-are-the-child') {
4748
process.env.NODE_PROCESS_ENV = 42;
4849
assert.strictEqual(process.env.NODE_PROCESS_ENV, '42');
4950

51+
process.env[42] = 'forty-two';
52+
assert.strictEqual(process.env[42], 'forty-two');
53+
5054
process.env.NODE_PROCESS_ENV_DELETED = 42;
5155
assert.strictEqual('NODE_PROCESS_ENV_DELETED' in process.env, true);
5256

0 commit comments

Comments
 (0)