Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
lib,src: make process global non-writable
  • Loading branch information
bnoordhuis committed Dec 11, 2016
commit 54aec1efeecefe44f3589de699ff82a43738f989
17 changes: 13 additions & 4 deletions lib/internal/bootstrap_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@

'use strict';

(function(process) {
(function(global, process) {

function startup(global, process) {
// Expose the global object as a property on itself
// (Allows you to set stuff on `global` from anywhere in JavaScript.)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please give an example? I don't follow.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't write this comment, I only moved it around. I can remove it if you want.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, it came from C++ code. I think we can remove this now.

global.global = global;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this also be non-writable?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh not yet. Thanks. I'll read that.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(it should be non-enumerable though)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the record: this is just the global->Set(..., global) from node.cc moved to JS land.


function startup() {
const EventEmitter = NativeModule.require('events');
process._eventsCount = 0;

Expand Down Expand Up @@ -196,7 +200,12 @@
enumerable: false,
configurable: true
});
global.process = process;
Object.defineProperty(global, 'process', {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is needed / warranted. If someone wants to delete global.process it should still work. Natives having proper refs to process is already achieved via the wrapper hange.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was requested by @sam-github in #10135 (comment).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I as well as @ljharb disagree with this change.

Copy link
Copy Markdown
Member

@bmeck bmeck Jan 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

basically, if we set this precedent I would be much more comfortable locking down all things that core could depend on, either by adding them to a closure ala https://github.com/bmeck/node/tree/no-globals or by freezing them on the global. There are many of these globals that break npm modules in the wild if they are overwritten. I do not see process as exceptionally high use vs Error for example. I am against freezing them on the global for polyfill reasons.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there many? https://nodejs.org/api/globals.html documents only a bit more than a dozen "globals", and half of them aren't global, they are module-scoped variables. Perhaps all the "globals" should be module-scoped variables? So that its impossible to mess with them globally?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this does not include prototype hijacking protection, thats raw global refs.

value: process,
writable: false,
enumerable: true,
configurable: true
});
const util = NativeModule.require('util');

// Deprecate GLOBAL and root
Expand Down Expand Up @@ -532,5 +541,5 @@
NativeModule._cache[this.id] = this;
};

startup();
startup(global, process);
});
8 changes: 2 additions & 6 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3441,19 +3441,15 @@ void LoadEnvironment(Environment* env) {

env->SetMethod(env->process_object(), "_rawDebug", RawDebug);

// Expose the global object as a property on itself
// (Allows you to set stuff on `global` from anywhere in JavaScript.)
global->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "global"), global);

// Now we call 'f' with the 'process' variable that we've built up with
// all our bindings. Inside bootstrap_node.js and internal/process we'll
// take care of assigning things to their places.

// We start the process this way in order to be more modular. Developers
// who do not like how bootstrap_node.js sets up the module system but do
// like Node's I/O bindings may want to replace 'f' with their own function.
Local<Value> arg = env->process_object();
f->Call(Null(env->isolate()), 1, &arg);
Local<Value> argv[] = { global, env->process_object() };
f->Call(Null(env->isolate()), arraysize(argv), argv);
}

static void PrintHelp() {
Expand Down
6 changes: 4 additions & 2 deletions test/parallel/test-process-clobber.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-disable no-global-assign */
/* eslint-disable required-modules */
'use strict';

process = null; // Should not bring down program.
require('../common');
const assert = require('assert');

assert.throws(() => process = null, /Cannot assign to read only property/);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should throw. Natives should be robust w/o relying on globals once startup finishes

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The built-in libraries are, this checks that process is not assignable per the second commit.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that is appropriate to lock down.