Skip to content

Commit

Permalink
feat: add test/.test to server.allowedHosts/`server.cors.origin…
Browse files Browse the repository at this point in the history
…` by default
  • Loading branch information
sapphi-red committed Jan 21, 2025
1 parent 576e87d commit 46fd0d2
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/config/server-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ See [the WSL document](https://learn.microsoft.com/en-us/windows/wsl/networking#
- **Default:** `[]`

The hostnames that Vite is allowed to respond to.
`localhost` and domains under `.localhost` and all IP addresses are allowed by default.
`localhost`/`test` and domains under `.localhost`/`.test` and all IP addresses are allowed by default.
When using HTTPS, this check is skipped.

If a string starts with `.`, it will allow that hostname without the `.` and all subdomains under the hostname. For example, `.example.com` will allow `example.com`, `foo.example.com`, and `foo.bar.example.com`.
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/node/__tests__/constants.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ test('defaultAllowedOrigins', () => {
const allowed = [
'http://localhost',
'http://foo.localhost',
'http://foo.test',
'http://localhost:3000',
'https://localhost:3000',
'http://127.0.0.1',
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/node/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,11 @@ export const DEFAULT_ASSETS_INLINE_LIMIT = 4096

// the regex to allow loopback address origins:
// - localhost domains (which will always resolve to the loopback address by RFC 6761 section 6.3)
// - test domains (which will never be registered by RFC 6761 section 6.2)
// - 127.0.0.1
// - ::1
export const defaultAllowedOrigins =
/^https?:\/\/(?:(?:[^:]+\.)?localhost|127\.0\.0\.1|\[::1\])(?::\d+)?$/
/^https?:\/\/(?:(?:[^:]+\.)?(?:localhost|test)|127\.0\.0\.1|\[::1\])(?::\d+)?$/

export const METADATA_FILENAME = '_metadata.json'

Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface CommonServerOptions {
host?: string | boolean
/**
* The hostnames that Vite is allowed to respond to.
* `localhost` and subdomains under `.localhost` and all IP addresses are allowed by default.
* `localhost`/`test` and domains under `.localhost`/`.test` and all IP addresses are allowed by default.
* When using HTTPS, this check is skipped.
*
* If a string starts with `.`, it will allow that hostname without the `.` and all subdomains under the hostname.
Expand Down
5 changes: 5 additions & 0 deletions packages/vite/src/node/server/middlewares/hostCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ export function isHostAllowedWithoutCache(
if (hostname === 'localhost' || hostname.endsWith('.localhost')) {
return true
}
// allow test and .test by default as they will never be registered
// https://datatracker.ietf.org/doc/html/rfc6761#section-6.2
if (hostname === 'test' || hostname.endsWith('.test')) {
return true
}

for (const additionalAllowedHost of additionalAllowedHosts) {
if (additionalAllowedHost === hostname) {
Expand Down

0 comments on commit 46fd0d2

Please sign in to comment.