Vite Dev Server's CVSS 9.1 Critical Flaw Lets Attackers Inject Code via `--host`
Last Tuesday (November 10th) Vite officially released a security advisory GHSA-p4f5-76q4-ffq7. The title is long, but boils down to one sentence: for projects that use --host or server.host to expose the dev server to the network, attackers can craft URLs that cause the dev server to generate modules containing malicious code. CVSS 4.0 score 9.1, Critical.
What's troublesome is that this advisory still has no CVE number and was published directly as a GitHub Advisory. The day after disclosure, npm audit and Dependabot had not yet alerted; the scanner ecosystem lagged behind, so you could only rely on manual oversight. This morning I set up an environment to reproduce it. After half a day of fiddling around, the process was more winding than expected. The vulnerability itself is not complex, but a few pitfalls along the way are quite typical.
What is the vulnerability mechanism
Let's first give the conclusion. Vite's dev server has several virtual modules; the most typical is __vite-browser-external. When browser code tries to import a Node built-in module (like fs), the dev server doesn't actually resolve it, but returns a "placeholder code" that throws an explanatory error if you access any of its properties.
The problem lies in how this placeholder code is generated. Searching for __vite-browser-external in the dist of the affected version reveals this load logic (in 7.3.7 it's in chunks/config.js; different minor versions have different bundle structures, but this symbol is consistent):
if (id.startsWith(browserExternalId))
if (isProduction) return `export default {}`;
else {
id = id.slice(24);
return `\
export default new Proxy({}, {
get(_, key) {
throw new Error(`Module "${id}" has been externalized for browser compatibility. Cannot access "${id}.${key}" in client code. See https://vite.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details.`)
}
})`;
}
Do you see it? The value id after slice(24) is directly concatenated into the text area of the template string. And id comes from the URL: the part after the colon in the request path received by the dev server is this id.
What does concatenating into a template string's text area mean? It means if id contains ` (backtick) or ${, it's no longer plain text. ${} is an interpolation expression in template strings. When the browser parses the generated code, the expression inside the interpolation will actually be executed.
Another virtual module in the same file, __vite-optional-peer-dep (which generates placeholder code when an optional peer dependency is not installed), also uses the same concatenation method. Both modules follow the same pattern.
There's also a precondition that needs to be stated clearly: this entire issue exists only on the dev server. When isProduction is true, it returns export default {} directly, no concatenation. Also, the default dev server listening on localhost is not affected; you must actively use --host or configure server.host to expose it.
Setting up the environment to reproduce
I chose 7.3.7 to reproduce. In the 7.x series, the vulnerable code is in Vite's own JavaScript, making white-box analysis easier. The similar logic in the 8.x series hides in Rolldown's binary (which is why the 8.x fix requires a Rolldown dependency upgrade). The fixed version for 7.x is 7.3.8, so 7.3.7 is the last affected version, making the comparison clean.
The environment was ready quickly:
$ npm init -y && npm install [email protected]
$ npx vite --host 0.0.0.0 --port 5173
VITE v7.3.7 ready in 231 ms
➜ Local: http://localhost:5173/
➜ Network: http://9.97.125.190:5173/
Notice the Network line; that's the dangerous state: the dev server is bound to 0.0.0.0, so any device on the same Wi-Fi can access it. Many people do this for LAN debugging or real-device previews.
First attempt: direct injection, failed
I tried the most intuitive path first: directly requesting a virtual module URL with the /@id/ prefix:
GET /@id/__vite-browser-external:hello
The response was the expected placeholder code, with hello appearing verbatim in the template. Then I tried sending a payload with %22 (URL-encoded double quote) to break out of the string. But the page returned index.html directly, which is the SPA fallback.
After digging through the middleware code in dist, I realized Vite decides whether a request should go through JS transformation using isJSRequest, which checks: "extension is empty and not ending with /, or matches known JS extensions". My payload had a console.error; after decoding, the URL ended with .error('...')//. path.extname treated .error(...) as a "file extension", which isn't in the known JS extension list, so the request was treated as a static resource and fell through to the SPA fallback.
I changed the dot in the payload and tried again. The request entered the transformation, but the injection still didn't execute because the generated code had syntax errors. Here I discovered a more fundamental issue: I thought id was concatenated into a double-quoted string, but it's actually concatenated into the text area of a backtick template string. The "..." in Module "${id}" is just normal characters inside the template. To break out, you must first terminate the outer template, and the structure of throw new Error(...) means the injected code must close the parentheses itself. A slight syntax error causes the entire module to fail parsing.
Another approach: no need to escape, just use interpolation
After being stuck for a while, I changed my thinking: since id is concatenated into a template string, I don't need to escape. I can just include ${} in the payload, and after concatenation it naturally becomes an interpolation expression, no syntax changes required.
I tested with the __vite-optional-peer-dep virtual module (its throw is at the top level, easier to observe). The payload was x${(globalThis.INJ=1)}y and the URL was:
/@id/__vite-optional-peer-dep:x${(globalThis.INJ=1)}y%2Fx.js
(The trailing %2Fx.js is there to pass the isJSRequest extension check, making the URL end in .js.)
The returned module source:
export default {};throw new Error(`Could not resolve "x${(globalThis.INJ=1)}y%2Fx.js" imported by "undefined". Is it installed?`)
The value from the URL became source-level interpolation verbatim. I saved this code as a .mjs and executed it with Node to verify:
import('./mod.mjs')
.catch((e) => console.log("[catch] module threw:", e.message))
.finally(() => console.log(globalThis.INJ === 1 ? "[result] INJECTED CODE EXECUTED_OK" : "[result] NOT EXECUTED"));
Output:
[catch] module threw: Could not resolve "x1y%2Fx.js" imported by "undefined". Is it installed?
[result] INJECTED CODE EXECUTED_OK (globalThis.INJ=1)
Both pieces of evidence confirmed it. The error message shows "x1y"; the middle 1 is the evaluated result of the interpolation expression (globalThis.INJ=1), proving the expression executed when the Error was constructed. And globalThis.INJ was indeed set to 1. Note that this module's execution entry point is just a normal URL. The dev server returns it as a normal "unresolved dependency placeholder module" to the browser. As long as any page in the browser loads that URL (for example, via a tricked link or an injected script tag), the injected code runs in the visitor's browser context.
I also hit a notable pitfall: with the same payload, {} mysteriously disappeared the first time I tested. x${(globalThis.INJ=1)}y became x$(globalThis.INJ=1)y. After searching Vite's source with no answer, I finally realized it was curl's fault: curl enables URL globbing by default, and {...} is treated as expansion syntax. Adding -g (or --globoff) fixes it. Reproducing a vulnerability only to find it's a client-tool issue halfway through, that's something only someone who does it by hand can appreciate.
Comparing with the fixed version
The official fixed version is 7.3.8. After installing it and starting a dev server on port 5174, I sent the same payload and got this code:
export default {};const peerDep = "x${(globalThis.INJ=1)}y%2Fx.js";const parentDep = undefined;throw new Error(`Could not resolve "${peerDep}" imported by "${parentDep}". Is it installed?`)
The fix is obvious at a glance: the URL-provided value is no longer directly concatenated into the template's text area. Instead, it's first placed into a double-quoted string literal, then referenced via ${peerDep} interpolation. Inside a double-quoted string, ${} is just plain text, and the interpolation capability is completely neutralized. Running the same verification script:
[catch] threw: Could not resolve "x${(globalThis.INJ=1)}y%2Fx.js" imported by "undefined". Is it installed?
[result] NOT EXECUTED (FIX_OK)
The error message also shows the difference: the vulnerable version threw "x1y" (interpolation executed, value baked into the message), while the fixed version threw the full original text "x${(globalThis.INJ=1)}y%2Fx.js"; the code structure remains unchanged, and ${} is preserved as literal text.
__vite-browser-external is fixed the same way in 7.3.8, changing from direct concatenation to a const path = "..." reference. Both virtual modules are patched.
Impact and self-check
The advisory says affected versions are 4.0 to 8.3.6, meaning all three currently maintained major versions are affected. The fixed versions listed are 8.3.7 (with 8.1.7 and 8.2.5 for the 8.1/8.2 branches), 7.3.8, 6.4.6, and vite-plus 1.5.1. I verified these version numbers on the npm registry: today the latest tag is already 8.3.8, and the previous tag points to 6.4.6, both directly installable. One thing to note: the 8.x fix doesn't change Vite's own JS; it updates the Rolldown dependency (8.3.7 ships with rolldown ~1.2.18, 8.1.7 with ~1.1.7, as confirmed via npm view). When upgrading, your lock file will include Rolldown changes. Don't mistake that for an anomaly and roll it back. Older 4.x/5.x have no corresponding fixed version; you must jump major versions or apply the mitigations below.
Self-check is really about two things: whether the vite version in package.json falls in the affected range, and whether the dev server is actually exposed externally. The latter is determined by whether the startup command includes --host or whether vite.config has server.host. Both conditions must be true to have an exposure surface. Temporary mitigation is also simple: don't expose the dev server during local development. If you truly need LAN preview, upgrade first.
There's one part the advisory mentioned but I didn't reproduce. The advisory classifies this as RCE. Besides browser-context injection, it also mentions that attackers can use Vite's server-side evaluation of worker and glob code to achieve server-side execution, and bypass the server.fs file whitelist to read files. The advisory doesn't disclose exploitation details for these parts. I locally tried several methods to read files outside server.fs.deny (direct /@fs/, ?raw, /@id/ with absolute paths), and all were blocked by 403 or the fallback. So those chains likely require more convoluted construction. I won't speculate here; I defer to the official advisory.
As for why this hasn't gotten much attention yet, I have two impressions. First, there's no CVE number. Dependency scanners like Snyk and Trivy rely on CVE/OSV data, and Vite can evade package-lock validation when it's not in the dependency tree, so they're essentially blind to it. Second, the precondition is that the developer actively uses --host, which sounds like a low-probability action. But when I looked through my recent projects, I found all of them had --host enabled during LAN debugging. A dev machine can run for a whole day, so this exposure surface is longer than many people think.
That's all. The biggest takeaway from this reproduction is that the dev server's security boundary is easy to overlook because "it only runs on localhost" is a default assumption we take for granted. And --host is so common; I rely on it for real-device preview on my phone, and sharing a LAN address with colleagues is just one flag. If your team has a shared CI or dev machine running Vite dev server, upgrade this week. Don't wait for the next advisory.
Top 1 from juejin.cn, machine-translated. The original thread is authoritative.
This vulnerability is hidden deep enough. URL parameters can be spliced into template strings, and once --host is enabled, it's like leaving the door wide open. The reproduction and self-check conditions are all provided. After reading it, the only feeling is: upgrade the version immediately.