Skip to content

Commit 73a5663

Browse files
committed
process: make process.config read-only
Refs: nodejs#6115
1 parent e67fee0 commit 73a5663

File tree

3 files changed

+50
-14
lines changed

3 files changed

+50
-14
lines changed

doc/api/process.markdown

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -473,22 +473,24 @@ An example of the possible output looks like:
473473
variables:
474474
{
475475
host_arch: 'x64',
476-
node_install_npm: 'true',
476+
node_install_npm: true,
477477
node_prefix: '',
478-
node_shared_cares: 'false',
479-
node_shared_http_parser: 'false',
480-
node_shared_libuv: 'false',
481-
node_shared_zlib: 'false',
482-
node_use_dtrace: 'false',
483-
node_use_openssl: 'true',
484-
node_shared_openssl: 'false',
485-
strict_aliasing: 'true',
478+
node_shared_cares: false,
479+
node_shared_http_parser: false,
480+
node_shared_libuv: false,
481+
node_shared_zlib: false,
482+
node_use_dtrace: false,
483+
node_use_openssl: true,
484+
node_shared_openssl: false,
485+
strict_aliasing: true,
486486
target_arch: 'x64',
487-
v8_use_snapshot: 'true'
487+
v8_use_snapshot: true
488488
}
489489
}
490490
```
491491

492+
The `process.config` object is read-only and cannot be modified or extended.
493+
492494
## process.connected
493495

494496
* {Boolean} Set to false after `process.disconnect()` is called

lib/internal/process.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,30 @@ function setupConfig(_source) {
6060
.replace(/"/g, '\\"')
6161
.replace(/'/g, '"');
6262

63-
process.config = JSON.parse(config, function(key, value) {
64-
if (value === 'true') return true;
65-
if (value === 'false') return false;
66-
return value;
63+
// Use a lazy getter and freeze the config object on parse.
64+
// This makes it slower but ensures that userland cannot
65+
// overwrite the config.
66+
var _config;
67+
Object.defineProperty(process, 'config', {
68+
configurable: false,
69+
enumerable: true,
70+
get: function() {
71+
if (!_config) {
72+
_config = JSON.parse(config, (key, value) => {
73+
if (value === 'true') return true;
74+
if (value === 'false') return false;
75+
if (typeof value === 'object')
76+
Object.freeze(value);
77+
return value;
78+
});
79+
}
80+
return _config;
81+
},
82+
set: function set(val) {
83+
const err = TypeError('process.config is read-only.');
84+
Error.captureStackTrace(err, set);
85+
throw err;
86+
}
6787
});
6888
}
6989

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
6+
const config = process.config;
7+
8+
assert(config);
9+
assert(config.variables);
10+
11+
// These throw because the objects are frozen.
12+
assert.throws(() => process.config = {}, TypeError);
13+
assert.throws(() => process.config.a = 1, TypeError);
14+
assert.throws(() => process.config.variables.a = 1, TypeError);

0 commit comments

Comments
 (0)