npm · @vitest/mocker
Vitest: Path Traversal / Arbitrary File Read via @vitest/mocker Redirect Mock
@vitest/mocker registers a redirect mock's target path without validating it
against the dev server's file-serving allowlist. An attacker who can reach the
dev server's WebSocket can register a redirect mock pointing outside the project
root; when the mocked module is requested, the plugin's load hook returns
readFile(<attacker path>) as the module source, disclosing local files.
This is exploitable without authentication only through the public
mockerPlugin / standalone interceptorPlugin exports (used by third-party dev
servers), which register the handler on Vite's unauthenticated HMR socket.
Vitest's own browser mode registers mocks over a token-authenticated RPC and
is not remotely reachable by default (see Scope).
packages/mocker/src/node/interceptorPlugin.ts.
The load hook is the file-read sink:
if (mock.type === 'redirect') {
return readFile(mock.redirect, 'utf-8')
}
mock.redirect is derived from client input at registration time with no
boundary check:
if (event.type === 'redirect') {
const redirectUrl = new URL(event.redirect)
event.redirect = join(server.config.root, redirectUrl.pathname)
}
registry.register(event)
There is no server.fs.allow / server.fs.deny check and no assertion that the
resolved path stays within the project root.
mockerPlugin / interceptorPlugin (unauthenticated). In
configureServer, the plugin registers server.ws.on('vitest:interceptor:register', …)
on Vite's HMR WebSocket. That socket performs no token, Origin, or same-origin
check, so any client that can reach it can register a redirect mock. This is
the path the "unauthenticated" impact applies to.registerMock), which sits behind a per-run token (isValidApiRequest, a
random api.token). The interceptor's configureServer socket is not used for
registration here (in v5 it does not run at all, as the plugin is injected per
environment). The same missing boundary check exists on the authenticated RPC
path, but reaching it requires the token, so it is not a remote-unauthenticated
read.new URL(redirect).pathname combined with join(root, pathname) does not
confine reads to the root:
file:, http:) are normalized by WHATWG URL,
so .. segments are collapsed and the result stays under the root. Payloads of
the form file:///../../etc/passwd do not escape... in pathname, so
join(root, "../../…/etc/passwd") resolves outside the root and reads an
arbitrary file.Even without escaping the root, the missing server.fs check allows reading any
in-root file the dev server would otherwise refuse to serve (for example an
in-root .env or source that is denied by server.fs.deny).
localhost by
default and is not reachable from the network unless the developer exposes it
(server.host / 0.0.0.0, a LAN bind, or a proxy).localhost
origins, and server.allowedHosts blocks DNS-rebinding, so a cross-origin page
cannot read the file contents back.Disclosure of local files readable by the dev-server process (source, in-root
.env/secrets, and, via the opaque-scheme payload, files outside the project
root). No integrity or availability impact.
Present since @vitest/mocker was introduced.
@vitest/mocker >= 2.1.0 (shipped in vitest and
@vitest/browser >= 2.1.0), through 4.1.x and the 5.0.0 pre-releases.Validate the resolved redirect target against Vite's file-serving allowlist
(isFileLoadingAllowed) before registering it, at every registration site, and
stop registering the interceptor WebSocket events in Vitest's browser mode
(mocks there flow through the authenticated RPC).
Is your project exposed to this? Stateward checks every dependency on every pull request and flags it only if your code actually reaches it.
Check my repoSources: CISA KEV (public domain), OSV.dev & GitHub Advisory Database (CC-BY-4.0), FIRST EPSS, NVD/CWE (public domain). Served live from the Stateward advisory database.