Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/config/server-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Setting `server.allowedHosts` to `true` allows any website to send requests to y
:::

::: details Configure via environment variable
You can set the environment variable `__VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS` to add an additional allowed host.
You can set the environment variable `__VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS` to add additional allowed hosts. Use commas to separate multiple hosts (e.g., `host1.example.com,host2.example.com`).
:::

## server.port
Expand Down
68 changes: 68 additions & 0 deletions packages/vite/src/node/__tests__/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { afterEach, describe, expect, test, vi } from 'vitest'
import type { InlineConfig, PluginOption } from '..'
import type { UserConfig, UserConfigExport } from '../config'
import { defineConfig, loadConfigFromFile, resolveConfig } from '../config'
import { resolveServerOptions } from '../server'
import { resolveEnvPrefix } from '../env'
import { mergeConfig } from '../utils'
import { createLogger } from '../logger'
Expand Down Expand Up @@ -1221,3 +1222,70 @@ describe('loadConfigFromFile', () => {
})
})
})

describe('resolveServerOptions', () => {
const logger = createLogger()

afterEach(() => {
delete process.env.__VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS
})

test('adds single host from __VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS', () => {
process.env.__VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS = 'example.com'
const resolved = resolveServerOptions('/root', { allowedHosts: [] }, logger)
expect(resolved.allowedHosts).toEqual(['example.com'])
})

test('adds multiple hosts from __VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS', () => {
process.env.__VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS =
'example.com,test.com,dev.example.org'
const resolved = resolveServerOptions('/root', { allowedHosts: [] }, logger)
expect(resolved.allowedHosts).toEqual([
'example.com',
'test.com',
'dev.example.org',
])
})

test('trims whitespace from hosts in __VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS', () => {
process.env.__VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS =
' example.com , test.com , dev.example.org '
const resolved = resolveServerOptions('/root', { allowedHosts: [] }, logger)
expect(resolved.allowedHosts).toEqual([
'example.com',
'test.com',
'dev.example.org',
])
})

test('filters empty hosts from __VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS', () => {
process.env.__VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS =
'example.com,,test.com,,'
const resolved = resolveServerOptions('/root', { allowedHosts: [] }, logger)
expect(resolved.allowedHosts).toEqual(['example.com', 'test.com'])
})

test('appends to existing allowedHosts', () => {
process.env.__VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS = 'new.com,another.com'
const resolved = resolveServerOptions(
'/root',
{ allowedHosts: ['existing.com'] },
logger,
)
expect(resolved.allowedHosts).toEqual([
'existing.com',
'new.com',
'another.com',
])
})

test('does not modify allowedHosts when set to true', () => {
process.env.__VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS = 'example.com'
const resolved = resolveServerOptions(
'/root',
{ allowedHosts: true },
logger,
)
expect(resolved.allowedHosts).toBe(true)
})
})
7 changes: 5 additions & 2 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1219,8 +1219,11 @@ export function resolveServerOptions(
process.env.__VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS &&
Array.isArray(server.allowedHosts)
) {
const additionalHost = process.env.__VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS
server.allowedHosts = [...server.allowedHosts, additionalHost]
const additionalHosts = process.env.__VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS
.split(',')
.map((host) => host.trim())
.filter(Boolean)
server.allowedHosts = [...server.allowedHosts, ...additionalHosts]
}

return server
Expand Down
Loading