日本語

Fixing “Blocked request. This host is not allowed” in the Vite Dev Server

​​Vite​React,Troubleshooting,TypeScript

When you open the Vite dev server by a name other than localhost, the page is replaced by two lines starting with "Blocked request. This host ("〜") is not allowed." Adding that name to server.allowedHosts in vite.config.ts makes the message go away.

This message appears when you open the dev server by a custom domain such as dev.example.com written in your hosts file, or by a single-word name such as frontend (for example, a Docker Compose service name). I reproduced the error in my test environment and included the message exactly as it appeared. I also confirmed that the page is no longer blocked after adding the setting, and that HMR (updating the screen when you save a file) still works.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.5, Vite 8.3.8, React 19.3.1, and TypeScript 7.0.2. I created the project with the react-ts template from npm create vite@latest. The browser was Chromium from Playwright 1.63.0, and I opened dev.example.com with it while pointing that name at 127.0.0.1 (the same setup as the hosts-file entry). For other names, I changed the Host header with curl. I did not test Docker containers, tunnels such as ngrok, or an HTTPS dev server.

The message and why it appears

I started the dev server with npx vite --host 0.0.0.0 --port 5173 --strictPort and opened http://dev.example.com:5173/ in the browser. The page showed only these two lines:

text
Blocked request. This host ("dev.example.com") is not allowed.
To allow this host, add "dev.example.com" to `server.allowedHosts` in vite.config.js.
Browser screen showing only two lines: "Blocked request. This host ("dev.example.com") is not allowed." and a line suggesting adding the host to server.allowedHosts
The screen when opening dev.example.com without the setting

The response status was 403 Forbidden, and the body was plain text. Opening individual files such as /src/main.tsx also returned the same 403. Nothing was printed in the dev server's terminal.

text
HTTP/1.1 403 Forbidden
Vary: Origin
Content-Type: text/plain

Vite checks whether the name in the request's Host header is in its list of allowed names. This protects against DNS rebinding attacks, in which another site could read the dev server's source code. The check was added in Vite 6.0.9 (Vite 6.0.9 changelog and published security advisory GHSA-vg6x-rcgg-rjx6).

The official documentation for server.allowedHosts says that localhost, names ending in .localhost, and all IP addresses are allowed without any configuration. In my test environment, the following Host headers returned 200 and the page loaded:

  • localhost:5173
  • 127.0.0.1:5173
  • 192.0.2.10:5173 (corresponds to opening the dev server by IP address from another device on the same network)
  • [::1]:5173
  • app.localhost:5173

On the other hand, frontend:5173 and example.com:5173 returned 403. This is why the same dev server displays normally when opened by IP address but shows this message when opened by name.

Adding the name to server.allowedHosts

The message ends by mentioning "vite.config.js", but if you created the project from the TypeScript template, write the setting in vite.config.ts.

ts
// vite.config.ts
import react from '@vitejs/plugin-react'
import { defineConfig } from 'vite'

// https://vite.dev/config/
export default defineConfig({
  plugins: [react()],
  server: {
    allowedHosts: ['dev.example.com'],
  },
})

With this configuration, the type check with tsc -b also passed.

I saved the file while the dev server was running. The terminal printed the following two lines, and the dev server restarted automatically.

text
hh:mm:ss [vite] vite.config.ts changed, restarting server...
hh:mm:ss [vite] server restarted.

(I replaced the times with hh:mm:ss.)

When I reopened the same URL, the status was 200 and the app page was displayed.

Page under the heading "Dev server page" showing the host name being opened: dev.example.com
The page opened at the same URL after adding dev.example.com to server.allowedHosts

While the page was open, I changed the heading in src/App.tsx, and the display updated without reloading the page. The HMR WebSocket connected to ws://dev.example.com:5173/. Stopping the dev server and starting it again by hand gave the same result.

Only the names you write are allowed, so www.example.com and example.com still return 403.

Allowing subdomains together

If you put a . at the beginning of a name, that name and all of its subdomains are allowed.

ts
server: {
  allowedHosts: ['.example.com'],
},

In my test environment, example.com, dev.example.com, and a.b.example.com returned 200, while example.org and dev.example.com.evil.test stayed at 403.

The official documentation says that you should add a name to this list only if you control which IP address that name points to. It advises never adding a top-level domain such as .com, because anyone can buy a something.com domain and point it at any IP address they like.

Setting true allows any name

If you set allowedHosts: true, the name check is turned off entirely. In my test environment, dev.example.com, example.org, and frontend all returned 200.

Warning

The official documentation warns that with true, any site can send requests to the dev server through a DNS rebinding attack, which could allow it to read source code and other files. It recommends listing names one at a time.

Adding names without changing the config file

If you do not want to edit vite.config.ts, you can start the server with the environment variable __VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS, set to a comma-separated list of names (this method is described in the same section of the official documentation).

bash
__VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS=dev.example.com,frontend npx vite --host 0.0.0.0 --port 5173 --strictPort

I started the server this way without writing allowedHosts in vite.config.ts. dev.example.com and frontend returned 200, and www.example.com, which was not listed, returned 403.

vite preview uses preview.allowedHosts

vite preview, which lets you check the built output, has the same check. I ran vite preview without allowedHosts and opened it at dev.example.com. This time the message referred to preview.allowedHosts.

text
Blocked request. This host ("dev.example.com") is not allowed.
To allow this host, add "dev.example.com" to `preview.allowedHosts` in vite.config.js.

However, if you do not write preview.allowedHosts, the value of server.allowedHosts is used instead (official documentation for preview.allowedHosts). When I ran vite preview with only server.allowedHosts: ['dev.example.com'] in the config, dev.example.com returned 200.

If you write preview.allowedHosts separately, only that list is used. With preview.allowedHosts: ['preview.example.com'] added, preview.example.com returned 200, and dev.example.com, which was listed only in server.allowedHosts, returned 403. If you use the same names for the dev server and the preview, you can write them only in server.allowedHosts and avoid listing them twice.

​Posted by 86