Browse Source

src: allow getting Symbols on process.env

1aa595e5bd 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: https://github.com/nodejs/node/pull/9446
Fixes: https://github.com/nodejs/node/issues/9641
PR-URL: https://github.com/nodejs/node/pull/9631
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
v6
Anna Henningsen 8 years ago
parent
commit
3295a7feba
No known key found for this signature in database GPG Key ID: D8B9F5AEAE84E4CF
  1. 3
      src/node.cc
  2. 9
      test/parallel/test-process-env-symbols.js
  3. 2
      test/parallel/test-util-inspect.js

3
src/node.cc

@ -2681,6 +2681,9 @@ static void ProcessTitleSetter(Local<Name> property,
static void EnvGetter(Local<Name> property,
const PropertyCallbackInfo<Value>& info) {
Isolate* isolate = info.GetIsolate();
if (property->IsSymbol()) {
return info.GetReturnValue().SetUndefined();
}
#ifdef __POSIX__
node::Utf8Value key(isolate, property);
const char* val = getenv(*key);

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

@ -5,10 +5,8 @@ const assert = require('assert');
const symbol = Symbol('sym');
const errRegExp = /^TypeError: Cannot convert a Symbol value to a string$/;
// Verify that getting via a symbol key throws.
assert.throws(() => {
process.env[symbol];
}, errRegExp);
// Verify that getting via a symbol key returns undefined.
assert.strictEqual(process.env[symbol], undefined);
// Verify that assigning via a symbol key throws.
assert.throws(() => {
@ -24,3 +22,6 @@ assert.throws(() => {
assert.throws(() => {
symbol in process.env;
}, errRegExp);
// Checks that well-known symbols like `Symbol.toStringTag` won’t throw.
assert.doesNotThrow(() => Object.prototype.toString.call(process.env));

2
test/parallel/test-util-inspect.js

@ -925,3 +925,5 @@ checkAlignment(new Map(big_array.map(function(y) { return [y, null]; })));
util.inspect.defaultOptions = 'bad';
}, /"options" must be an object/);
}
assert.doesNotThrow(() => util.inspect(process));

Loading…
Cancel
Save