-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix!: check host header to prevent DNS rebinding attacks and introduc…
…e `server.allowedHosts`
- Loading branch information
1 parent
029dcd6
commit bd896fb
Showing
10 changed files
with
401 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,20 @@ See [the WSL document](https://learn.microsoft.com/en-us/windows/wsl/networking# | |
|
||
::: | ||
|
||
## server.allowedHosts | ||
|
||
- **Type:** `string[] | true` | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
sapphi-red
Author
Member
|
||
- **Default:** `[]` | ||
|
||
The hostnames that Vite is allowed to respond to. | ||
`localhost` and domains under `.localhost` 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`. | ||
|
||
If set to `true`, the server is allowed to respond to requests for any hosts. | ||
This is not recommended as it will be vulnerable to DNS rebinding attacks. | ||
|
||
## server.port | ||
|
||
- **Type:** `number` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
packages/vite/src/node/server/middlewares/__tests__/hostCheck.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { describe, expect, test } from 'vitest' | ||
import { | ||
getAdditionalAllowedHosts, | ||
isHostAllowedWithoutCache, | ||
} from '../hostCheck' | ||
|
||
test('getAdditionalAllowedHosts', async () => { | ||
const actual = getAdditionalAllowedHosts( | ||
{ | ||
host: 'vite.host.example.com', | ||
hmr: { | ||
host: 'vite.hmr-host.example.com', | ||
}, | ||
origin: 'http://vite.origin.example.com:5173', | ||
}, | ||
{ | ||
host: 'vite.preview-host.example.com', | ||
}, | ||
).sort() | ||
expect(actual).toStrictEqual( | ||
[ | ||
'vite.host.example.com', | ||
'vite.hmr-host.example.com', | ||
'vite.origin.example.com', | ||
'vite.preview-host.example.com', | ||
].sort(), | ||
) | ||
}) | ||
|
||
describe('isHostAllowedWithoutCache', () => { | ||
const allowCases = { | ||
'IP address': [ | ||
'192.168.0.0', | ||
'[::1]', | ||
'127.0.0.1:5173', | ||
'[2001:db8:0:0:1:0:0:1]:5173', | ||
], | ||
localhost: [ | ||
'localhost', | ||
'localhost:5173', | ||
'foo.localhost', | ||
'foo.bar.localhost', | ||
], | ||
specialProtocols: [ | ||
// for electron browser window (https://github.com/webpack/webpack-dev-server/issues/3821) | ||
'file:///path/to/file.html', | ||
// for browser extensions (https://github.com/webpack/webpack-dev-server/issues/3807) | ||
'chrome-extension://foo', | ||
], | ||
} | ||
|
||
const disallowCases = { | ||
'IP address': ['255.255.255.256', '[:', '[::z]'], | ||
localhost: ['localhos', 'localhost.foo'], | ||
specialProtocols: ['mailto:[email protected]'], | ||
others: [''], | ||
} | ||
|
||
for (const [name, inputList] of Object.entries(allowCases)) { | ||
test.each(inputList)(`allows ${name} (%s)`, (input) => { | ||
const actual = isHostAllowedWithoutCache([], [], input) | ||
expect(actual).toBe(true) | ||
}) | ||
} | ||
|
||
for (const [name, inputList] of Object.entries(disallowCases)) { | ||
test.each(inputList)(`disallows ${name} (%s)`, (input) => { | ||
const actual = isHostAllowedWithoutCache([], [], input) | ||
expect(actual).toBe(false) | ||
}) | ||
} | ||
|
||
test('allows additionalAlloweHosts option', () => { | ||
const additionalAllowedHosts = ['vite.example.com'] | ||
const actual = isHostAllowedWithoutCache( | ||
[], | ||
additionalAllowedHosts, | ||
'vite.example.com', | ||
) | ||
expect(actual).toBe(true) | ||
}) | ||
|
||
test('allows single allowedHosts', () => { | ||
const cases = { | ||
allowed: ['example.com'], | ||
disallowed: ['vite.dev'], | ||
} | ||
for (const c of cases.allowed) { | ||
const actual = isHostAllowedWithoutCache(['example.com'], [], c) | ||
expect(actual, c).toBe(true) | ||
} | ||
for (const c of cases.disallowed) { | ||
const actual = isHostAllowedWithoutCache(['example.com'], [], c) | ||
expect(actual, c).toBe(false) | ||
} | ||
}) | ||
|
||
test('allows all subdomain allowedHosts', () => { | ||
const cases = { | ||
allowed: ['example.com', 'foo.example.com', 'foo.bar.example.com'], | ||
disallowed: ['vite.dev'], | ||
} | ||
for (const c of cases.allowed) { | ||
const actual = isHostAllowedWithoutCache(['.example.com'], [], c) | ||
expect(actual, c).toBe(true) | ||
} | ||
for (const c of cases.disallowed) { | ||
const actual = isHostAllowedWithoutCache(['.example.com'], [], c) | ||
expect(actual, c).toBe(false) | ||
} | ||
}) | ||
}) |
Oops, something went wrong.
Incident report: a lot of developers reported there is a problem with adding allowedHosts after updated to recent release, Issue: Blocked request. This host ("xxxx") is not allowed.
To allow this host, add "xxxx" to
server.allowedHosts
in vite.config.js.And editing vite.config.js in whatever ways does not solve the problems