Skip to content

Commit

Permalink
lib: add custom eslint rule against global pollution
Browse files Browse the repository at this point in the history
This adds a custom eslint rule to make sure the globals are not
polluted in any way by adding extra properties to them in any way.
Besides that it also removes access to `global` by marking some
globals as available to eslint.
  • Loading branch information
BridgeAR committed Apr 9, 2019
1 parent 8cddf9b commit 631ed0a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/.eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ rules:
# Custom rules in tools/eslint-rules
node-core/lowercase-name-for-primitive: error
node-core/non-ascii-character: error
node-core/no-global-pollute: error
globals:
Intl: false
# Assertions
Expand Down
1 change: 1 addition & 0 deletions lib/internal/main/eval_string.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const { addBuiltinLibsToObject } = require('internal/modules/cjs/helpers');
const { getOptionValue } = require('internal/options');
const source = getOptionValue('--eval');
prepareMainThreadExecution();
// eslint-disable-next-line node-core/no-global-pollute
addBuiltinLibsToObject(global);
markBootstrapComplete();
if (getOptionValue('--entry-type') === 'module')
Expand Down
9 changes: 4 additions & 5 deletions lib/internal/per_context/setup.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
// This file is compiled as if it's wrapped in a function with arguments
// passed by node::NewContext()
/* global global */

'use strict';

// https://github.com/nodejs/node/issues/14909
if (global.Intl) {
delete global.Intl.v8BreakIterator;
if (typeof Intl !== 'undefined') {
delete Intl.v8BreakIterator;
}

// https://github.com/nodejs/node/issues/21219
if (global.Atomics) {
delete global.Atomics.wake;
if (typeof Atomics !== 'undefined') {
delete Atomics.wake;
}
1 change: 1 addition & 0 deletions lib/internal/process/execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ function evalScript(name, body, breakFirstLine) {
const module = new CJSModule(name);
module.filename = path.join(cwd, name);
module.paths = CJSModule._nodeModulePaths(cwd);
// eslint-disable-next-line node-core/no-global-pollute
global.kVmBreakFirstLineSymbol = kVmBreakFirstLineSymbol;
const script = `
global.__filename = ${JSON.stringify(name)};
Expand Down
33 changes: 33 additions & 0 deletions tools/eslint-rules/no-global-pollute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @fileOverview This rule makes sure that the globals don't get polluted by
* adding new entries to `global`.
* @author Ruben Bridgewater <ruben@bridgewater.de>
*/

'use strict';

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = function(context) {
const filename = context.getFilename();

return {
'Program:exit': function() {
const globalScope = context.getScope();
const variable = globalScope.set.get('global');
if (variable &&
!filename.includes('bootstrap') &&
!filename.includes('repl.js')) {
const msg = 'Please do not pollute the global scope';
variable.references.forEach((reference) => {
context.report({
node: reference.identifier,
message: msg
});
});
}
}
};
};

0 comments on commit 631ed0a

Please sign in to comment.