Skip to content

Commit

Permalink
process: make process.config read only
Browse files Browse the repository at this point in the history
  • Loading branch information
gribnoysup committed Jun 30, 2022
1 parent 49a5e81 commit f52598c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 66 deletions.
70 changes: 4 additions & 66 deletions lib/internal/bootstrap/node.js
Expand Up @@ -46,10 +46,8 @@ const {
JSONParse,
ObjectDefineProperty,
ObjectGetPrototypeOf,
ObjectPreventExtensions,
ObjectSetPrototypeOf,
ReflectGet,
ReflectSet,
ObjectFreeze,
SymbolToStringTag,
globalThis,
} = primordials;
Expand All @@ -72,75 +70,15 @@ process._exiting = false;
// process.config is serialized config.gypi
const nativeModule = internalBinding('native_module');

// TODO(@jasnell): Once this has gone through one full major
// release cycle, remove the Proxy and setter and update the
// getter to either return a read-only object or always return
// a freshly parsed version of nativeModule.config.

const deprecationHandler = {
warned: false,
message: 'Setting process.config is deprecated. ' +
'In the future the property will be read-only.',
code: 'DEP0150',
maybeWarn() {
if (!this.warned) {
process.emitWarning(this.message, {
type: 'DeprecationWarning',
code: this.code
});
this.warned = true;
}
},

defineProperty(target, key, descriptor) {
this.maybeWarn();
return ObjectDefineProperty(target, key, descriptor);
},

deleteProperty(target, key) {
this.maybeWarn();
delete target[key];
},

preventExtensions(target) {
this.maybeWarn();
return ObjectPreventExtensions(target);
},

set(target, key, value) {
this.maybeWarn();
return ReflectSet(target, key, value);
},

get(target, key, receiver) {
const val = ReflectGet(target, key, receiver);
if (val != null && typeof val === 'object') {
// eslint-disable-next-line node-core/prefer-primordials
return new Proxy(val, deprecationHandler);
}
return val;
},

setPrototypeOf(target, proto) {
this.maybeWarn();
return ObjectSetPrototypeOf(target, proto);
}
};

// eslint-disable-next-line node-core/prefer-primordials
let processConfig = new Proxy(
JSONParse(nativeModule.config),
deprecationHandler);
const processConfig = JSONParse(nativeModule.config, (_key, value) => {
return ObjectFreeze(value);
})

ObjectDefineProperty(process, 'config', {
__proto__: null,
enumerable: true,
configurable: true,
get() { return processConfig; },
set(value) {
deprecationHandler.maybeWarn();
processConfig = value;
}
});

require('internal/worker/js_transferable').setup();
Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-process-config.js
Expand Up @@ -36,6 +36,16 @@ assert(Object.hasOwn(process, 'config'));
// Ensure that `process.config` is an Object.
assert.strictEqual(Object(process.config), process.config);

// Ensure that you can't change config values
try {
process.config.variables = 42;
} catch (e) {
assert.strictEqual(
e.message,
"Cannot assign to read only property 'variables' of object '#<Object>'"
);
}

const configPath = path.resolve(__dirname, '..', '..', 'config.gypi');

if (!fs.existsSync(configPath)) {
Expand Down

0 comments on commit f52598c

Please sign in to comment.