You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Listen to port and serve API. When set to true, the default port is 51204
1814
1814
1815
+
#### api.allowWrite {#api-allowwrite}
1816
+
1817
+
-**Type:**`boolean`
1818
+
-**Default:**`true` if API is not exposed to the network, `false` otherwise
1819
+
1820
+
Allows API clients to write files, including updating test files from the UI. If `api.host` is set to anything other than `localhost` or `127.0.0.1`, Vitest disables write operations by default.
1821
+
1822
+
#### api.allowExec {#api-allowexec}
1823
+
1824
+
-**Type:**`boolean`
1825
+
-**Default:**`true` if API is not exposed to the network, `false` otherwise
1826
+
1827
+
Allows API clients to run tests. If `api.host` is exposed to the network and write/exec operations are enabled, anyone who can reach the API server can run arbitrary code on your machine.
Copy file name to clipboardExpand all lines: docs/guide/browser/commands.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,6 +17,8 @@ By default, Vitest uses `utf-8` encoding but you can override it with options.
17
17
18
18
::: tip
19
19
This API follows [`server.fs`](https://vitejs.dev/config/server-options.html#server-fs-allow) limitations for security reasons.
20
+
21
+
`writeFile` and `removeFile` also require write access through [`browser.api.allowWrite`](/guide/browser/config#browser-api-allowwrite) and [`api.allowWrite`](/config/#api-allowwrite).
Configure options for Vite server that serves code in the browser. Does not affect [`test.api`](#api) option. By default, Vitest assigns port `63315` to avoid conflicts with the development server, allowing you to run both in parallel.
-**Default:** inherited from [`api.allowWrite`](/config/#api-allowwrite)
157
+
158
+
Allows browser API clients to write files, including snapshots and browser command writes. If `browser.api.host` is set to anything other than `localhost` or `127.0.0.1`, Vitest disables write operations by default unless this option or [`api.allowWrite`](/config/#api-allowwrite) is explicitly enabled.
-**Default:** inherited from [`api.allowExec`](/config/#api-allowexec)
164
+
165
+
Allows browser API clients to run tests from the UI. If `browser.api.host` is exposed to the network and write/exec operations are enabled, anyone who can reach the browser API server can run arbitrary code on your machine.
thrownewError(`Cannot modify file "${path}". File writing is disabled because server is exposed to the internet, see https://vitest.dev/config/browser/api.`)
@@ -191,11 +201,23 @@ export function setupBrowserRpc(globalServer: ParentBrowserProject, defaultMocke
191
201
},
192
202
asyncsaveSnapshotFile(id,content){
193
203
checkFileAccess(id)
204
+
if(!canWrite(project)){
205
+
vitest.logger.error(
206
+
`[vitest] Cannot save snapshot file "${id}". File writing is disabled because server is exposed to the internet, see https://vitest.dev/config/browser/api.`,
207
+
)
208
+
return
209
+
}
194
210
awaitfs.mkdir(dirname(id),{recursive: true})
195
211
returnfs.writeFile(id,content,'utf-8')
196
212
},
197
213
asyncremoveSnapshotFile(id){
198
214
checkFileAccess(id)
215
+
if(!canWrite(project)){
216
+
vitest.logger.error(
217
+
`[vitest] Cannot remove snapshot file "${id}". File writing is disabled because server is exposed to the internet, see https://vitest.dev/config/browser/api.`,
218
+
)
219
+
return
220
+
}
199
221
if(!existsSync(id)){
200
222
thrownewError(`Snapshot file "${id}" does not exist.`)
0 commit comments