diff options
author | Rens Baardman <git@rensbaardman.nl> | 2019-06-11 14:41:59 +0300 |
---|---|---|
committer | Rens Baardman <git@rensbaardman.nl> | 2019-06-11 16:14:58 +0300 |
commit | 7bec7f88e01bad4829ee2ac3f90fe5d9d2616888 (patch) | |
tree | 3e8500e4f4c4c66d07057b5f84eaaa25747eda22 | |
parent | 5bea3d816d0258e5204f1b49b08b9fb302ac53e1 (diff) |
Fix #167: non-enumerable globals are now also prefixed with `var`fix-167-global-var-leakage
-rw-r--r-- | lib/getImportGlobalsSrc.js | 14 | ||||
-rw-r--r-- | test/getImportGlobalsSrc.test.js | 22 |
2 files changed, 27 insertions, 9 deletions
diff --git a/lib/getImportGlobalsSrc.js b/lib/getImportGlobalsSrc.js index 3c29c56..fcce0d0 100644 --- a/lib/getImportGlobalsSrc.js +++ b/lib/getImportGlobalsSrc.js @@ -9,7 +9,6 @@ */ function getImportGlobalsSrc(ignore) { var key, - value, src = "", globalObj = typeof global === "undefined"? window: global; @@ -20,12 +19,21 @@ function getImportGlobalsSrc(ignore) { // shadow the module-internal variables // @see https://github.com/jhnns/rewire-webpack/pull/6 ignore.push("module", "exports", "require"); + // strict mode doesn't allow to (re)define 'undefined', 'eval' & 'arguments' + ignore.push("undefined", "eval", "arguments"); + // 'GLOBAL' and 'root' are deprecated in Node + // (assigning them causes a DeprecationWarning) + ignore.push("GLOBAL", "root"); + // 'NaN' and 'Infinity' are immutable + // (doesn't throw an error if you set 'var NaN = ...', but doesn't work either) + ignore.push("NaN", "Infinity"); - for (key in globalObj) { /* jshint forin: false */ + const globals = Object.getOwnPropertyNames(globalObj); + + for (key of globals) { if (ignore.indexOf(key) !== -1) { continue; } - value = globalObj[key]; // key may be an invalid variable name (e.g. 'a-b') try { diff --git a/test/getImportGlobalsSrc.test.js b/test/getImportGlobalsSrc.test.js index 0c40e5a..51cecbd 100644 --- a/test/getImportGlobalsSrc.test.js +++ b/test/getImportGlobalsSrc.test.js @@ -3,6 +3,7 @@ var expect = require("expect.js"), getImportGlobalsSrc = require("../lib/getImportGlobalsSrc.js");
describe("getImportGlobalsSrc", function () {
+
it("should declare all globals with a var", function () {
var context = {
global: global
@@ -28,15 +29,20 @@ describe("getImportGlobalsSrc", function () { delete global['__core-js_shared__'];
delete global['a-b'];
- expectedGlobals = Object.keys(global);
+ const ignoredGlobals = ["module", "exports", "require", "undefined", "eval", "arguments", "GLOBAL", "root", "NaN", "Infinity"];
+
+ const globals = Object.getOwnPropertyNames(global);
+ expectedGlobals = globals.filter((el) => !ignoredGlobals.includes(el));
vm.runInNewContext(src, context);
- actualGlobals = Object.keys(context);
+ actualGlobals = Object.getOwnPropertyNames(context);
+
actualGlobals.sort();
expectedGlobals.sort();
expect(actualGlobals).to.eql(expectedGlobals);
expect(actualGlobals.length).to.be.above(1);
});
+
it("should ignore the given variables", function () {
var context = {
global: global
@@ -44,18 +50,22 @@ describe("getImportGlobalsSrc", function () { ignore = ["console", "setTimeout"],
src,
actualGlobals,
- expectedGlobals = Object.keys(global);
+ expectedGlobals = Object.getOwnPropertyNames(global);
+
+ const ignoredGlobals = ["module", "exports", "require", "undefined", "eval", "arguments", "GLOBAL", "root", "NaN", "Infinity"];
+ ignore = ignore.concat(ignoredGlobals);
// getImportGlobalsSrc modifies the ignore array, so let's create a copy
src = getImportGlobalsSrc(ignore.slice(0));
- expectedGlobals = expectedGlobals.filter(function filterIgnoredVars(value) {
- return ignore.indexOf(value) === -1;
- });
+ expectedGlobals = expectedGlobals.filter((el) => !ignore.includes(el));
+
vm.runInNewContext(src, context);
actualGlobals = Object.keys(context);
+
actualGlobals.sort();
expectedGlobals.sort();
expect(actualGlobals).to.eql(expectedGlobals);
expect(actualGlobals.length).to.be.above(1);
});
+
});
|