Skip to content

Commit 3a75d7c

Browse files
committed
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`.
1 parent 3196fae commit 3a75d7c

File tree

5 files changed

+54
-4
lines changed

5 files changed

+54
-4
lines changed

lib/fs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,7 +1004,7 @@ fs.fchmodSync = function(fd, mode) {
10041004
return binding.fchmod(fd, modeNum(mode));
10051005
};
10061006

1007-
if (constants.hasOwnProperty('O_SYMLINK')) {
1007+
if (constants.O_SYMLINK !== undefined) {
10081008
fs.lchmod = function(path, mode, callback) {
10091009
callback = maybeCallback(callback);
10101010
fs.open(path, constants.O_WRONLY | constants.O_SYMLINK, function(err, fd) {
@@ -1059,7 +1059,7 @@ fs.chmodSync = function(path, mode) {
10591059
return binding.chmod(pathModule._makeLong(path), modeNum(mode));
10601060
};
10611061

1062-
if (constants.hasOwnProperty('O_SYMLINK')) {
1062+
if (constants.O_SYMLINK !== undefined) {
10631063
fs.lchown = function(path, uid, gid, callback) {
10641064
callback = maybeCallback(callback);
10651065
fs.open(path, constants.O_WRONLY | constants.O_SYMLINK, function(err, fd) {

lib/internal/process.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,7 @@ function setupSignalHandlers() {
184184
const signalWraps = {};
185185

186186
function isSignal(event) {
187-
return typeof event === 'string' &&
188-
lazyConstants().hasOwnProperty(event);
187+
return typeof event === 'string' && lazyConstants()[event] !== undefined;
189188
}
190189

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

src/node.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2621,6 +2621,8 @@ static void Binding(const FunctionCallbackInfo<Value>& args) {
26212621
cache->Set(module, exports);
26222622
} else if (!strcmp(*module_v, "constants")) {
26232623
exports = Object::New(env->isolate());
2624+
CHECK(exports->SetPrototype(env->context(),
2625+
Null(env->isolate())).FromJust());
26242626
DefineConstants(env->isolate(), exports);
26252627
cache->Set(module, exports);
26262628
} else if (!strcmp(*module_v, "natives")) {

src/node_constants.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,12 +1224,31 @@ void DefineZlibConstants(Local<Object> target) {
12241224
}
12251225

12261226
void DefineConstants(v8::Isolate* isolate, Local<Object> target) {
1227+
Environment* env = Environment::GetCurrent(isolate);
1228+
12271229
Local<Object> os_constants = Object::New(isolate);
1230+
CHECK(os_constants->SetPrototype(env->context(),
1231+
Null(env->isolate())).FromJust());
1232+
12281233
Local<Object> err_constants = Object::New(isolate);
1234+
CHECK(err_constants->SetPrototype(env->context(),
1235+
Null(env->isolate())).FromJust());
1236+
12291237
Local<Object> sig_constants = Object::New(isolate);
1238+
CHECK(sig_constants->SetPrototype(env->context(),
1239+
Null(env->isolate())).FromJust());
1240+
12301241
Local<Object> fs_constants = Object::New(isolate);
1242+
CHECK(fs_constants->SetPrototype(env->context(),
1243+
Null(env->isolate())).FromJust());
1244+
12311245
Local<Object> crypto_constants = Object::New(isolate);
1246+
CHECK(crypto_constants->SetPrototype(env->context(),
1247+
Null(env->isolate())).FromJust());
1248+
12321249
Local<Object> zlib_constants = Object::New(isolate);
1250+
CHECK(zlib_constants->SetPrototype(env->context(),
1251+
Null(env->isolate())).FromJust());
12331252

12341253
DefineErrnoConstants(err_constants);
12351254
DefineWindowsErrorConstants(err_constants);
Lines changed: 30 additions & 0 deletions
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)