Skip to content

Commit 378d8c8

Browse files
authored
Merge pull request #15404 from webpack/fix/issue-15401
disallow global renaming in NodeStuffPlugin
2 parents 45e8873 + 82a0f2a commit 378d8c8

4 files changed

Lines changed: 40 additions & 0 deletions

File tree

lib/NodeStuffPlugin.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ class NodeStuffPlugin {
6969
);
7070
}
7171
});
72+
parser.hooks.rename.for("global").tap("NodeStuffPlugin", expr => {
73+
const dep = new ConstDependency(
74+
RuntimeGlobals.global,
75+
expr.range,
76+
[RuntimeGlobals.global]
77+
);
78+
dep.loc = expr.loc;
79+
parser.state.module.addPresentationalDependency(dep);
80+
return false;
81+
});
7282
}
7383

7484
const setModuleConstant = (expressionName, fn, warning) => {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
it("should disallow rename global", () => {
2+
var shouldBeGlobal = global;
3+
// prevent optimizations
4+
var method = shouldBeGlobal[String.fromCharCode(40, 40, 40)];
5+
method && method();
6+
eval("expect(shouldBeGlobal.value1).toBe('value1')");
7+
expect(shouldBeGlobal.test).toBe("test");
8+
expect(global.test).toBe("test");
9+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
moduleScope(scope) {
3+
scope.global = scope.global || {};
4+
scope.global.value1 = "error";
5+
scope.globalThis = scope.globalThis || {};
6+
scope.globalThis.value1 = "value1";
7+
}
8+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const { DefinePlugin } = require("../../../../");
2+
3+
/** @type {import("../../../../").Configuration} */
4+
module.exports = {
5+
node: {
6+
global: true
7+
},
8+
plugins: [
9+
new DefinePlugin({
10+
"global.test": "'test'"
11+
})
12+
]
13+
};

0 commit comments

Comments
 (0)