Skip to content

Commit 3295a7f

Browse files
committed
src: allow getting Symbols on process.env
1aa595e introduced a `throw` for accessing `Symbol` properties of `process.env`. However, this breaks `util.inspect(process)` and things like `Object.prototype.toString.call(process.env)`, so this patch changes the behaviour for the getter to just always return `undefined`. Ref: #9446 Fixes: #9641 PR-URL: #9631 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 6b86ecc commit 3295a7f

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

src/node.cc

+3
Original file line numberDiff line numberDiff line change
@@ -2681,6 +2681,9 @@ static void ProcessTitleSetter(Local<Name> property,
26812681
static void EnvGetter(Local<Name> property,
26822682
const PropertyCallbackInfo<Value>& info) {
26832683
Isolate* isolate = info.GetIsolate();
2684+
if (property->IsSymbol()) {
2685+
return info.GetReturnValue().SetUndefined();
2686+
}
26842687
#ifdef __POSIX__
26852688
node::Utf8Value key(isolate, property);
26862689
const char* val = getenv(*key);

test/parallel/test-process-env-symbols.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ const assert = require('assert');
55
const symbol = Symbol('sym');
66
const errRegExp = /^TypeError: Cannot convert a Symbol value to a string$/;
77

8-
// Verify that getting via a symbol key throws.
9-
assert.throws(() => {
10-
process.env[symbol];
11-
}, errRegExp);
8+
// Verify that getting via a symbol key returns undefined.
9+
assert.strictEqual(process.env[symbol], undefined);
1210

1311
// Verify that assigning via a symbol key throws.
1412
assert.throws(() => {
@@ -24,3 +22,6 @@ assert.throws(() => {
2422
assert.throws(() => {
2523
symbol in process.env;
2624
}, errRegExp);
25+
26+
// Checks that well-known symbols like `Symbol.toStringTag` won’t throw.
27+
assert.doesNotThrow(() => Object.prototype.toString.call(process.env));

test/parallel/test-util-inspect.js

+2
Original file line numberDiff line numberDiff line change
@@ -925,3 +925,5 @@ checkAlignment(new Map(big_array.map(function(y) { return [y, null]; })));
925925
util.inspect.defaultOptions = 'bad';
926926
}, /"options" must be an object/);
927927
}
928+
929+
assert.doesNotThrow(() => util.inspect(process));

0 commit comments

Comments
 (0)