Skip to content

Commit deb239d

Browse files
committed
feat: show a warning if host is exposed
1 parent 3efd80c commit deb239d

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

packages/vitest/src/node/config/resolveConfig.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { ResolvedConfig as ResolvedViteConfig } from 'vite'
22
import type { Vitest } from '../core'
3+
import type { Logger } from '../logger'
34
import type { BenchmarkBuiltinReporters } from '../reporters'
45
import type { ResolvedBrowserOptions } from '../types/browser'
56
import type {
@@ -55,10 +56,14 @@ function parseInspector(inspect: string | undefined | boolean | number) {
5556
return { host, port: Number(port) || defaultInspectPort }
5657
}
5758

59+
/**
60+
* @deprecated Internal function
61+
*/
5862
export function resolveApiServerConfig<Options extends ApiConfig & Omit<UserConfig, 'expect'>>(
5963
options: Options,
6064
defaultPort: number,
6165
parentApi?: ApiConfig,
66+
logger?: Logger,
6267
): ApiConfig | undefined {
6368
let api: ApiConfig | undefined
6469

@@ -100,6 +105,16 @@ export function resolveApiServerConfig<Options extends ApiConfig & Omit<UserConf
100105

101106
// if the API server is exposed to network, disable write operations by default
102107
if (!api.middlewareMode && api.host && api.host !== 'localhost' && api.host !== '127.0.0.1') {
108+
// assigned to browser
109+
if (parentApi) {
110+
if (api.allowWrite == null && api.allowExec == null) {
111+
logger?.error(
112+
c.yellow(
113+
`${c.yellowBright(' WARNING ')} API server is exposed to network, disabling write and exec operations by default for security reasons. This can case some APIs to not work as expected. Set \`browser.api.allowExec\` manually to hide this warning. See https://vitest.dev/config/browser/api for more details.`,
114+
),
115+
)
116+
}
117+
}
103118
api.allowWrite ??= parentApi?.allowWrite ?? false
104119
api.allowExec ??= parentApi?.allowExec ?? false
105120
}
@@ -805,6 +820,7 @@ export function resolveConfig(
805820
resolved.browser,
806821
defaultBrowserPort,
807822
resolved.api,
823+
logger,
808824
) || {
809825
port: defaultBrowserPort,
810826
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { expect, test } from 'vitest';
2+
3+
test('basic test', () => {
4+
expect(1 + 1).toBe(2)
5+
})

test/cli/test/config/browser-configs.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,3 +1016,25 @@ test('allows custom transformIndexHtml without custom html file', async () => {
10161016
expect(stdout).toContain('✓ |chromium| browser-custom.test.ts')
10171017
expect(exitCode).toBe(0)
10181018
})
1019+
1020+
test('show a warning if host is exposed', async () => {
1021+
const { stderr } = await runVitest({
1022+
config: false,
1023+
root: './fixtures/basic',
1024+
reporters: [
1025+
{
1026+
onInit() {
1027+
throw new Error('stop')
1028+
},
1029+
},
1030+
],
1031+
browser: {
1032+
api: {
1033+
host: 'custom-host',
1034+
},
1035+
},
1036+
})
1037+
expect(stderr).toContain(
1038+
'API server is exposed to network, disabling write and exec operations by default for security reasons. This can case some APIs to not work as expected. Set `browser.api.allowExec` manually to hide this warning. See https://vitest.dev/config/browser/api for more details.',
1039+
)
1040+
})

0 commit comments

Comments
 (0)