Skip to content

Commit

Permalink
security: fix DOM clobbering in auto public path
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Aug 22, 2024
2 parents 5ac3d7f + 9822387 commit 955e057
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
5 changes: 4 additions & 1 deletion lib/runtime/AutoPublicPathRuntimeModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ class AutoPublicPathRuntimeModule extends RuntimeModule {
`var document = ${RuntimeGlobals.global}.document;`,
"if (!scriptUrl && document) {",
Template.indent([
"if (document.currentScript)",
// Technically we could use `document.currentScript instanceof window.HTMLScriptElement`,
// but an attacker could try to inject `<script>HTMLScriptElement = HTMLImageElement</script>`
// and use `<img name="currentScript" src="https://attacker.controlled.server/"></img>`
"if (document.currentScript && document.currentScript.tagName.toUpperCase() === 'SCRIPT')",
Template.indent("scriptUrl = document.currentScript.src;"),
"if (!scriptUrl) {",
Template.indent([
Expand Down
8 changes: 4 additions & 4 deletions test/Stats.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,10 @@ describe("Stats", () => {
"assets": Array [
Object {
"name": "entryB.js",
"size": 3010,
"size": 3060,
},
],
"assetsSize": 3010,
"assetsSize": 3060,
"auxiliaryAssets": undefined,
"auxiliaryAssetsSize": 0,
"childAssets": undefined,
Expand Down Expand Up @@ -238,10 +238,10 @@ describe("Stats", () => {
"info": Object {
"javascriptModule": false,
"minimized": true,
"size": 3010,
"size": 3060,
},
"name": "entryB.js",
"size": 3010,
"size": 3060,
"type": "asset",
},
Object {
Expand Down
1 change: 1 addition & 0 deletions test/helpers/CurrentScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class CurrentScript {
constructor(path = "", type = "text/javascript") {
this.src = `https://test.cases/path/${path}index.js`;
this.type = type;
this.tagName = "script";
}
}

Expand Down

0 comments on commit 955e057

Please sign in to comment.