Skip to content

Commit caf9ae7

Browse files
thefourtheyejasnell
authored andcommitted
lib,src: make constants not inherit from Object
Make sure `constants` object and all the nested objects don't inherit from `Object.prototype` but from `null`. PR-URL: #10458 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Brian White <[email protected]>
1 parent 221b03a commit caf9ae7

File tree

5 files changed

+54
-4
lines changed

5 files changed

+54
-4
lines changed

lib/fs.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,7 @@ fs.fchmodSync = function(fd, mode) {
10581058
return binding.fchmod(fd, modeNum(mode));
10591059
};
10601060

1061-
if (constants.hasOwnProperty('O_SYMLINK')) {
1061+
if (constants.O_SYMLINK !== undefined) {
10621062
fs.lchmod = function(path, mode, callback) {
10631063
callback = maybeCallback(callback);
10641064
fs.open(path, constants.O_WRONLY | constants.O_SYMLINK, function(err, fd) {
@@ -1116,7 +1116,7 @@ fs.chmodSync = function(path, mode) {
11161116
return binding.chmod(pathModule._makeLong(path), modeNum(mode));
11171117
};
11181118

1119-
if (constants.hasOwnProperty('O_SYMLINK')) {
1119+
if (constants.O_SYMLINK !== undefined) {
11201120
fs.lchown = function(path, uid, gid, callback) {
11211121
callback = maybeCallback(callback);
11221122
fs.open(path, constants.O_WRONLY | constants.O_SYMLINK, function(err, fd) {

lib/internal/process.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,7 @@ function setupSignalHandlers() {
201201
const signalWraps = {};
202202

203203
function isSignal(event) {
204-
return typeof event === 'string' &&
205-
lazyConstants().hasOwnProperty(event);
204+
return typeof event === 'string' && lazyConstants()[event] !== undefined;
206205
}
207206

208207
// Detect presence of a listener for the special signal types

src/node.cc

+2
Original file line numberDiff line numberDiff line change
@@ -2673,6 +2673,8 @@ static void Binding(const FunctionCallbackInfo<Value>& args) {
26732673
cache->Set(module, exports);
26742674
} else if (!strcmp(*module_v, "constants")) {
26752675
exports = Object::New(env->isolate());
2676+
CHECK(exports->SetPrototype(env->context(),
2677+
Null(env->isolate())).FromJust());
26762678
DefineConstants(env->isolate(), exports);
26772679
cache->Set(module, exports);
26782680
} else if (!strcmp(*module_v, "natives")) {

src/node_constants.cc

+19
Original file line numberDiff line numberDiff line change
@@ -1245,12 +1245,31 @@ void DefineZlibConstants(Local<Object> target) {
12451245
}
12461246

12471247
void DefineConstants(v8::Isolate* isolate, Local<Object> target) {
1248+
Environment* env = Environment::GetCurrent(isolate);
1249+
12481250
Local<Object> os_constants = Object::New(isolate);
1251+
CHECK(os_constants->SetPrototype(env->context(),
1252+
Null(env->isolate())).FromJust());
1253+
12491254
Local<Object> err_constants = Object::New(isolate);
1255+
CHECK(err_constants->SetPrototype(env->context(),
1256+
Null(env->isolate())).FromJust());
1257+
12501258
Local<Object> sig_constants = Object::New(isolate);
1259+
CHECK(sig_constants->SetPrototype(env->context(),
1260+
Null(env->isolate())).FromJust());
1261+
12511262
Local<Object> fs_constants = Object::New(isolate);
1263+
CHECK(fs_constants->SetPrototype(env->context(),
1264+
Null(env->isolate())).FromJust());
1265+
12521266
Local<Object> crypto_constants = Object::New(isolate);
1267+
CHECK(crypto_constants->SetPrototype(env->context(),
1268+
Null(env->isolate())).FromJust());
1269+
12531270
Local<Object> zlib_constants = Object::New(isolate);
1271+
CHECK(zlib_constants->SetPrototype(env->context(),
1272+
Null(env->isolate())).FromJust());
12541273

12551274
DefineErrnoConstants(err_constants);
12561275
DefineWindowsErrorConstants(err_constants);
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
require('../common');
4+
const constants = process.binding('constants');
5+
const assert = require('assert');
6+
7+
assert.deepStrictEqual(
8+
Object.keys(constants).sort(), ['crypto', 'fs', 'os', 'zlib']
9+
);
10+
11+
assert.deepStrictEqual(
12+
Object.keys(constants.os).sort(), ['UV_UDP_REUSEADDR', 'errno', 'signals']
13+
);
14+
15+
// Make sure all the constants objects don't inherit from Object.prototype
16+
const inheritedProperties = Object.getOwnPropertyNames(Object.prototype);
17+
function test(obj) {
18+
assert(obj);
19+
assert.strictEqual(Object.prototype.toString.call(obj), '[object Object]');
20+
assert.strictEqual(Object.getPrototypeOf(obj), null);
21+
22+
inheritedProperties.forEach((property) => {
23+
assert.strictEqual(property in obj, false);
24+
});
25+
}
26+
27+
[
28+
constants, constants.crypto, constants.fs, constants.os, constants.zlib,
29+
constants.os.errno, constants.os.signals
30+
].forEach(test);

0 commit comments

Comments
 (0)