The root cause of the vulnerability is a path traversal flaw in how file paths from user input were validated. Specifically, the isFileServingAllowed function was used incorrectly, allowing it to be bypassed on Windows systems using special path sequences like \\?\\..\\. This flaw was present in multiple locations across the Vitest codebase, affecting the UI, browser testing mode, and the developer API server.
This vulnerability manifested in several ways:
- Arbitrary File Read: The most direct exploitation was through the
/__vitest_attachment__ endpoint, where an attacker could read any file on the filesystem that the Vitest process had access to.
- Arbitrary File Write/Delete: Functions like
writeFile, removeFile, and saveSnapshotFile were also vulnerable, allowing an attacker to create, modify, or delete files in arbitrary locations.
- Arbitrary Code Execution: The most critical impact was the ability to achieve remote code execution. An attacker could use a file-write primitive like
saveTestFile to create a malicious test file and then use an execution primitive like rerun to have Vitest execute it.
The patch addresses this vulnerability with a two-pronged approach. First, it replaces the incorrect usage of isFileServingAllowed with a more secure implementation that properly cleans and validates file paths, fixing the underlying path traversal bug. Second, it introduces new security controls, api.allowWrite and api.allowExec, which are disabled by default when the Vitest server is exposed to the network. This acts as a defense-in-depth measure, preventing file write and execution operations even if a similar vulnerability were to be discovered in the future, effectively putting the UI in a read-only mode in such configurations.