mirror of
https://github.com/nodejs/node.git
synced 2026-11-21 12:59:16 -05:00
process: disallow some uses of Object.defineProperty() on process.env
Disallow the use of Object.defineProperty() to hide entries in process.env or make them immutable. PR-URL: https://github.com/nodejs/node/pull/28006 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
1 parent
df20947e69
commit
e6a7300a10
6 files changed
+151
-3
No files matched your search
@@ -0,0 +1,13 @@
|
||||
'use strict';
|
||||
require('../common');
|
||||
const assert = require('assert');
|
||||
|
||||
process.env.foo = 'foo';
|
||||
assert.strictEqual(process.env.foo, 'foo');
|
||||
process.env.foo = undefined;
|
||||
assert.strictEqual(process.env.foo, 'undefined');
|
||||
|
||||
process.env.foo = 'foo';
|
||||
assert.strictEqual(process.env.foo, 'foo');
|
||||
delete process.env.foo;
|
||||
assert.strictEqual(process.env.foo, undefined);
|
||||
@@ -0,0 +1,67 @@
|
||||
'use strict';
|
||||
require('../common');
|
||||
const assert = require('assert');
|
||||
|
||||
assert.throws(
|
||||
() => {
|
||||
Object.defineProperty(process.env, 'foo', {
|
||||
value: 'foo1'
|
||||
});
|
||||
},
|
||||
{
|
||||
code: 'ERR_INVALID_OBJECT_DEFINE_PROPERTY',
|
||||
name: 'TypeError',
|
||||
message: '\'process.env\' only accepts a ' +
|
||||
'configurable, writable,' +
|
||||
' and enumerable data descriptor'
|
||||
}
|
||||
);
|
||||
|
||||
assert.strictEqual(process.env.foo, undefined);
|
||||
process.env.foo = 'foo2';
|
||||
assert.strictEqual(process.env.foo, 'foo2');
|
||||
|
||||
assert.throws(
|
||||
() => {
|
||||
Object.defineProperty(process.env, 'goo', {
|
||||
get() {
|
||||
return 'goo';
|
||||
},
|
||||
set() {}
|
||||
});
|
||||
},
|
||||
{
|
||||
code: 'ERR_INVALID_OBJECT_DEFINE_PROPERTY',
|
||||
name: 'TypeError',
|
||||
message: '\'process.env\' does not accept an' +
|
||||
'accessor(getter/setter) descriptor'
|
||||
}
|
||||
);
|
||||
|
||||
const attributes = ['configurable', 'writable', 'enumerable'];
|
||||
|
||||
attributes.forEach((attribute) => {
|
||||
assert.throws(
|
||||
() => {
|
||||
Object.defineProperty(process.env, 'goo', {
|
||||
[attribute]: false
|
||||
});
|
||||
},
|
||||
{
|
||||
code: 'ERR_INVALID_OBJECT_DEFINE_PROPERTY',
|
||||
name: 'TypeError',
|
||||
message: '\'process.env\' only accepts a ' +
|
||||
'configurable, writable,' +
|
||||
' and enumerable data descriptor'
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
assert.strictEqual(process.env.goo, undefined);
|
||||
Object.defineProperty(process.env, 'goo', {
|
||||
value: 'goo',
|
||||
configurable: true,
|
||||
writable: true,
|
||||
enumerable: true
|
||||
});
|
||||
assert.strictEqual(process.env.goo, 'goo');
|
||||
@@ -39,8 +39,20 @@ if (!workerData && process.argv[2] !== 'child') {
|
||||
process.env.SET_IN_WORKER = 'set';
|
||||
assert.strictEqual(process.env.SET_IN_WORKER, 'set');
|
||||
|
||||
Object.defineProperty(process.env, 'DEFINED_IN_WORKER', { value: 42 });
|
||||
assert.strictEqual(process.env.DEFINED_IN_WORKER, '42');
|
||||
assert.throws(
|
||||
() => {
|
||||
Object.defineProperty(process.env, 'DEFINED_IN_WORKER', {
|
||||
value: 42
|
||||
});
|
||||
},
|
||||
{
|
||||
code: 'ERR_INVALID_OBJECT_DEFINE_PROPERTY',
|
||||
name: 'TypeError',
|
||||
message: '\'process.env\' only accepts a configurable, ' +
|
||||
'writable, and enumerable data descriptor'
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
const { stderr } =
|
||||
child_process.spawnSync(process.execPath, [__filename, 'child']);
|
||||
|
||||
Reference in new issue
Block a user