Skip to content

Commit 1aa595e

Browse files
committed
src: throw on process.env symbol usage
This commit causes process.env to throw when a symbol is used as either a key or a value. Fixes: #9429 PR-URL: #9446 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Minwoo Jung <jmwsoft@gmail.com>
1 parent b315e24 commit 1aa595e

File tree

3 files changed

+32
-42
lines changed

3 files changed

+32
-42
lines changed

src/node.cc

+6-8
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ using v8::ObjectTemplate;
122122
using v8::Promise;
123123
using v8::PromiseRejectMessage;
124124
using v8::PropertyCallbackInfo;
125-
using v8::PropertyHandlerFlags;
126125
using v8::ScriptOrigin;
127126
using v8::SealHandleScope;
128127
using v8::String;
@@ -2685,7 +2684,7 @@ static void EnvGetter(Local<Name> property,
26852684
return info.GetReturnValue().Set(String::NewFromUtf8(isolate, val));
26862685
}
26872686
#else // _WIN32
2688-
String::Value key(property);
2687+
node::TwoByteValue key(isolate, property);
26892688
WCHAR buffer[32767]; // The maximum size allowed for environment variables.
26902689
DWORD result = GetEnvironmentVariableW(reinterpret_cast<WCHAR*>(*key),
26912690
buffer,
@@ -2711,8 +2710,8 @@ static void EnvSetter(Local<Name> property,
27112710
node::Utf8Value val(info.GetIsolate(), value);
27122711
setenv(*key, *val, 1);
27132712
#else // _WIN32
2714-
String::Value key(property);
2715-
String::Value val(value);
2713+
node::TwoByteValue key(info.GetIsolate(), property);
2714+
node::TwoByteValue val(info.GetIsolate(), value);
27162715
WCHAR* key_ptr = reinterpret_cast<WCHAR*>(*key);
27172716
// Environment variables that start with '=' are read-only.
27182717
if (key_ptr[0] != L'=') {
@@ -2732,7 +2731,7 @@ static void EnvQuery(Local<Name> property,
27322731
if (getenv(*key))
27332732
rc = 0;
27342733
#else // _WIN32
2735-
String::Value key(property);
2734+
node::TwoByteValue key(info.GetIsolate(), property);
27362735
WCHAR* key_ptr = reinterpret_cast<WCHAR*>(*key);
27372736
if (GetEnvironmentVariableW(key_ptr, nullptr, 0) > 0 ||
27382737
GetLastError() == ERROR_SUCCESS) {
@@ -2756,7 +2755,7 @@ static void EnvDeleter(Local<Name> property,
27562755
node::Utf8Value key(info.GetIsolate(), property);
27572756
unsetenv(*key);
27582757
#else
2759-
String::Value key(property);
2758+
node::TwoByteValue key(info.GetIsolate(), property);
27602759
WCHAR* key_ptr = reinterpret_cast<WCHAR*>(*key);
27612760
SetEnvironmentVariableW(key_ptr, nullptr);
27622761
#endif
@@ -3155,8 +3154,7 @@ void SetupProcessObject(Environment* env,
31553154
EnvQuery,
31563155
EnvDeleter,
31573156
EnvEnumerator,
3158-
env->as_external(),
3159-
PropertyHandlerFlags::kOnlyInterceptStrings));
3157+
env->as_external()));
31603158

31613159
Local<Object> process_env =
31623160
process_env_template->NewInstance(env->context()).ToLocalChecked();
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
require('../common');
3+
4+
const assert = require('assert');
5+
const symbol = Symbol('sym');
6+
const errRegExp = /^TypeError: Cannot convert a Symbol value to a string$/;
7+
8+
// Verify that getting via a symbol key throws.
9+
assert.throws(() => {
10+
process.env[symbol];
11+
}, errRegExp);
12+
13+
// Verify that assigning via a symbol key throws.
14+
assert.throws(() => {
15+
process.env[symbol] = 42;
16+
}, errRegExp);
17+
18+
// Verify that assigning a symbol value throws.
19+
assert.throws(() => {
20+
process.env.foo = symbol;
21+
}, errRegExp);
22+
23+
// Verify that using a symbol with the in operator throws.
24+
assert.throws(() => {
25+
symbol in process.env;
26+
}, errRegExp);

test/parallel/test-v8-interceptStrings-not-Symbols.js

-34
This file was deleted.

0 commit comments

Comments
 (0)