Skip to content

Commit

Permalink
lib,src: make constants not inherit from Object
Browse files Browse the repository at this point in the history
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 <jasnell@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Brian White <mscdex@mscdex.net>
  • Loading branch information
thefourtheye authored and jasnell committed Mar 22, 2017
1 parent 221b03a commit caf9ae7
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,7 @@ fs.fchmodSync = function(fd, mode) {
return binding.fchmod(fd, modeNum(mode));
};

if (constants.hasOwnProperty('O_SYMLINK')) {
if (constants.O_SYMLINK !== undefined) {
fs.lchmod = function(path, mode, callback) {
callback = maybeCallback(callback);
fs.open(path, constants.O_WRONLY | constants.O_SYMLINK, function(err, fd) {
Expand Down Expand Up @@ -1116,7 +1116,7 @@ fs.chmodSync = function(path, mode) {
return binding.chmod(pathModule._makeLong(path), modeNum(mode));
};

if (constants.hasOwnProperty('O_SYMLINK')) {
if (constants.O_SYMLINK !== undefined) {
fs.lchown = function(path, uid, gid, callback) {
callback = maybeCallback(callback);
fs.open(path, constants.O_WRONLY | constants.O_SYMLINK, function(err, fd) {
Expand Down
3 changes: 1 addition & 2 deletions lib/internal/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,7 @@ function setupSignalHandlers() {
const signalWraps = {};

function isSignal(event) {
return typeof event === 'string' &&
lazyConstants().hasOwnProperty(event);
return typeof event === 'string' && lazyConstants()[event] !== undefined;
}

// Detect presence of a listener for the special signal types
Expand Down
2 changes: 2 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2673,6 +2673,8 @@ static void Binding(const FunctionCallbackInfo<Value>& args) {
cache->Set(module, exports);
} else if (!strcmp(*module_v, "constants")) {
exports = Object::New(env->isolate());
CHECK(exports->SetPrototype(env->context(),
Null(env->isolate())).FromJust());
DefineConstants(env->isolate(), exports);
cache->Set(module, exports);
} else if (!strcmp(*module_v, "natives")) {
Expand Down
19 changes: 19 additions & 0 deletions src/node_constants.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1245,12 +1245,31 @@ void DefineZlibConstants(Local<Object> target) {
}

void DefineConstants(v8::Isolate* isolate, Local<Object> target) {
Environment* env = Environment::GetCurrent(isolate);

Local<Object> os_constants = Object::New(isolate);
CHECK(os_constants->SetPrototype(env->context(),
Null(env->isolate())).FromJust());

Local<Object> err_constants = Object::New(isolate);
CHECK(err_constants->SetPrototype(env->context(),
Null(env->isolate())).FromJust());

Local<Object> sig_constants = Object::New(isolate);
CHECK(sig_constants->SetPrototype(env->context(),
Null(env->isolate())).FromJust());

Local<Object> fs_constants = Object::New(isolate);
CHECK(fs_constants->SetPrototype(env->context(),
Null(env->isolate())).FromJust());

Local<Object> crypto_constants = Object::New(isolate);
CHECK(crypto_constants->SetPrototype(env->context(),
Null(env->isolate())).FromJust());

Local<Object> zlib_constants = Object::New(isolate);
CHECK(zlib_constants->SetPrototype(env->context(),
Null(env->isolate())).FromJust());

DefineErrnoConstants(err_constants);
DefineWindowsErrorConstants(err_constants);
Expand Down
30 changes: 30 additions & 0 deletions test/parallel/test-binding-constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

require('../common');
const constants = process.binding('constants');
const assert = require('assert');

assert.deepStrictEqual(
Object.keys(constants).sort(), ['crypto', 'fs', 'os', 'zlib']
);

assert.deepStrictEqual(
Object.keys(constants.os).sort(), ['UV_UDP_REUSEADDR', 'errno', 'signals']
);

// Make sure all the constants objects don't inherit from Object.prototype
const inheritedProperties = Object.getOwnPropertyNames(Object.prototype);
function test(obj) {
assert(obj);
assert.strictEqual(Object.prototype.toString.call(obj), '[object Object]');
assert.strictEqual(Object.getPrototypeOf(obj), null);

inheritedProperties.forEach((property) => {
assert.strictEqual(property in obj, false);
});
}

[
constants, constants.crypto, constants.fs, constants.os, constants.zlib,
constants.os.errno, constants.os.signals
].forEach(test);

0 comments on commit caf9ae7

Please sign in to comment.