Skip to content

Commit 3d03899

Browse files
authoredJan 21, 2025··
fix: allow CORS from loopback addresses by default (#19249)
1 parent aeb3ec8 commit 3d03899

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed
 

‎docs/config/server-options.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ export default defineConfig({
161161
## server.cors
162162

163163
- **Type:** `boolean | CorsOptions`
164-
- **Default:** `false`
164+
- **Default:** `{ origin: /^https?:\/\/(?:(?:[^:]+\.)?localhost|127\.0\.0\.1|\[::1\])(?::\d+)?$/ }` (allows localhost, `127.0.0.1` and `::1`)
165165

166166
Configure CORS for the dev server. Pass an [options object](https://github.com/expressjs/cors#configuration-options) to fine tune the behavior or `true` to allow any origin.
167167

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { expect, test } from 'vitest'
2+
import { defaultAllowedOrigins } from '../constants'
3+
4+
test('defaultAllowedOrigins', () => {
5+
const allowed = [
6+
'http://localhost',
7+
'http://foo.localhost',
8+
'http://localhost:3000',
9+
'https://localhost:3000',
10+
'http://127.0.0.1',
11+
'http://[::1]',
12+
'http://[::1]:3000',
13+
]
14+
const denied = [
15+
'file:///foo',
16+
'http://localhost.example.com',
17+
'http://foo.example.com:localhost',
18+
'http://',
19+
'http://192.0.2',
20+
'http://[2001:db8::1]',
21+
'http://vite',
22+
'http://vite:3000',
23+
]
24+
25+
for (const origin of allowed) {
26+
expect(defaultAllowedOrigins.test(origin), origin).toBe(true)
27+
}
28+
29+
for (const origin of denied) {
30+
expect(defaultAllowedOrigins.test(origin), origin).toBe(false)
31+
}
32+
})

‎packages/vite/src/node/constants.ts

+7
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,13 @@ export const DEFAULT_PREVIEW_PORT = 4173
183183

184184
export const DEFAULT_ASSETS_INLINE_LIMIT = 4096
185185

186+
// the regex to allow loopback address origins:
187+
// - localhost domains (which will always resolve to the loopback address by RFC 6761 section 6.3)
188+
// - 127.0.0.1
189+
// - ::1
190+
export const defaultAllowedOrigins =
191+
/^https?:\/\/(?:(?:[^:]+\.)?localhost|127\.0\.0\.1|\[::1\])(?::\d+)?$/
192+
186193
export const METADATA_FILENAME = '_metadata.json'
187194

188195
export const ERR_OPTIMIZE_DEPS_PROCESSING_ERROR =

‎packages/vite/src/node/server/index.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ import { reloadOnTsconfigChange } from '../plugins/esbuild'
4444
import { bindCLIShortcuts } from '../shortcuts'
4545
import type { BindCLIShortcutsOptions } from '../shortcuts'
4646
import { ERR_OUTDATED_OPTIMIZED_DEP } from '../../shared/constants'
47-
import { CLIENT_DIR, DEFAULT_DEV_PORT } from '../constants'
47+
import {
48+
CLIENT_DIR,
49+
DEFAULT_DEV_PORT,
50+
defaultAllowedOrigins,
51+
} from '../constants'
4852
import type { Logger } from '../logger'
4953
import { printServerUrls } from '../logger'
5054
import { warnFutureDeprecation } from '../deprecations'
@@ -1055,7 +1059,7 @@ export const serverConfigDefaults = Object.freeze({
10551059
https: undefined,
10561060
open: false,
10571061
proxy: undefined,
1058-
cors: false,
1062+
cors: { origin: defaultAllowedOrigins },
10591063
headers: {},
10601064
// hmr
10611065
// ws

0 commit comments

Comments
 (0)
Please sign in to comment.