vite: [vite 6.0.9] option server.allowedHosts doesn't take into account "true"

Describe the bug

hi, i’ve recently updated vite to 6.0.9, the main change being the allowedHosts server conf field,

it’s said in the documentation that if set to true, it will accept any hosts, but even when setting it to true i get the error:

Blocked request. This host (“hidden”) is not allowed. To allow this host, add “hidden” to server.allowedHosts in vite.config.js.

(maybe i could be dumb but it really doesn’t seem to work)

Reproduction

google.com

Steps to reproduce

No response

System Info

System:
    OS: Linux 6.8 Ubuntu 24.04.1 LTS 24.04.1 LTS (Noble Numbat)
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
    Memory: 4.68 GB / 15.34 GB
    Container: Yes
    Shell: 5.9 - /usr/bin/zsh
  Binaries:
    Node: 18.19.1 - /usr/bin/node
    npm: 9.2.0 - /usr/bin/npm
  Browsers:
    Chrome: 132.0.6834.83
  npmPackages:
    @vitejs/plugin-react: ^4.2.0 => 4.2.1 
    vite: 6.0.9 => 6.0.9

Used Package Manager

npm

Logs

No response

Validations

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 21
  • Comments: 55 (8 by maintainers)

Commits related to this issue

Most upvoted comments

Yes, same issue coming to me as well when I am trying below config in my vite config file (vite.config.ts). Please fix it.

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

export default defineConfig({
	plugins: [react()],
	build: {
		outDir: 'dist',
		target: 'esnext',
	},
	preview: {
		port: 3000,
		strictPort: true,
		host: '0.0.0.0', 
		allowedHosts: true,
	},
	server: {
		allowedHosts: true
	},
});

I resolved this by updating vite.config - what’s odd here is that I had not upgraded to 6+ and the error just presented overnight. Also the type definitions are not happy with this addition, but it works:

export default defineConfig({
	plugins: [sveltekit()],
	server: {
		cors: {
			origin: ['https://mysubdomain.domain.io', 'http://localhost:5173'],
			methods: ['GET', 'POST'],
			allowedHeaders: ['Content-Type']
		},
		allowedHosts: ['mysubdomain.domain.io'] //added this
	}
});

The problem still exists in 6.0.11.

Downgrading to 6.0.8 makes it work.

I had the same issue while developing a Shopify-Remix app and using Ngrok as a tunnel. At first everything was working fine and suddenly it stopped working and vite was blocking all Ngrok requests even though it was added in server.allowedHosts.

After some debugging and trying to run vite separately using npm run vite it gave an error: Port 3000 is in use, trying another one

And indeed there was something already running on port 3000. The issue went away after that.

I suggest running vite on the project to check for any errors

npm run vite

Hope this helps someone ✌

So, we have two choices: an insecure version or a buggy version…

No, those are not the only choices. You have a third choice to create a PR that fixes your problem, and a fourth choice to create a reproduction so that others can investigate the cause.

Also happening for latest 4.x.x version. Downgrading to v4.4.5 fixed it.

[!CAUTION] Please note that downgrading will make you vulnerable to attacks described in GHSA-vg6x-rcgg-rjx6.

i’m not sure i have the time rn to make a replication for something this simple, if someone want to provide one

in the mean time i’ve rollbacked to 6.0.8

it’s really a breaking issue for a lot of people,

changing the default allowedHost from 0.0.0.0 to localhost and having a bug on the same field to set back to 0.0.0.0 will break a lot of vite installations, i’ll stick on 6.0.8 until it’s fixed, thanks for your hard work nonetheless, vite is great

https://vite.dev/config/server-options#server-allowedhosts

Tried to follow the same procedure but not working…

Blocked request. This host (“hostname”) is not allowed. To allow this host, add “hostname” to server.allowedHosts in vite.config.js.

I resolved the issue by NOT using vite preview anymore, but installing http-server and running http-server dist. Worked right out of the box! 🚀

Blocked request. This host (“myhost.com”) is not allowed. To allow this host, add “myhost.com” to preview.allowedHosts in vite.config.js.

  • I added it to vite.config.js and it didn’t work.
  • I downgraded to version 4.5.0 and it remains the same
  • I’m using nginx in my server
  • The application is being run in Docker

Downgrade to 4.4.5.

FIXED (at least for me)

so i was making a git for reproduction, a basic vite + react project, + custom domain in the hosts file but i didn’t manage to reproduce. then i tried everything that was different from my main project, and then when i used a dockerfile to host the reproduction project, i reproduced! so the hunt begin to understand the fuck happened in this dockerfile here is the one i used, if you can see the issue

FROM node:22.13.0-alpine AS base

RUN mkdir -p /home/node/app && chown node:node /home/node/app
WORKDIR /home/node/app
USER node

FROM base AS build
COPY --chown=node:node . .
RUN npm i


RUN npm run build

FROM base AS main
# ENV NODE_ENV=production
COPY --from=build ./home/node/app/package.json ./
COPY --from=build --chown=node:node /home/node/app/dist ./dist
RUN npm i

CMD ["npm", "run", "preview", "--", "--host", "--port", "9001"]

yes, in the final stage i miss the vite.config.ts file, should have the line: COPY --from=build ./home/node/app/vite.config.ts ./ but i didn’t thought it would be needed because the others options where taken into account so from the start of the projet i never added it in the final stage (here is mine for reference)

import { defineConfig } from 'vite'

const VITE_URL_PREFIX= process.env.VITE_URL_PREFIX


// https://vitejs.dev/config/
export default defineConfig({
  build:{
    outDir: `dist${VITE_URL_PREFIX}` // Les fichiers construits iront dans dist/hello
  },
  base:`${VITE_URL_PREFIX? VITE_URL_PREFIX: `/`}`,// Définit le chemin de base pour toutes les ressources
  server: {
	allowedHosts: true
  }
})

because even if all of the other values are used during the build stage, the “server” is used when invoking the vite server, so he don’t have it, right?

so the issue was: IM A DUMBASS THAT FORGOT THE VITE.CONFIG.TS FILE (if you come accross this issue in the future, check again if you have the config file at the moment you invoke vite)

@patak-dev @sapphi-red you can tell me “told you, can’t reproduce” 😂

Yeah, we are using vite: 5.1.3 with React 18 (works for local dev…but when we do deployment on docker image in K8s with nginx proxy we encouter this issue too!!) Ay Karamba! Trying to leave no stone unturned to fix it!

same here, two of my applications are affected…

Thank you, @mdesantis ! Setting the --host parameter did the trick for me.

Same issue when running:

cloudflared tunnel --url http://localhost:8081

on a react app that runs with pnpm run dev (note that accessing localhost works perfectly fine – it’s the cloudflared service that does not).

I’m on a macbook air m1 chip from 2020.

No resolution and blocked if using 6.0.9, 6.1.0 and 6.1.1. Downgraded to 6.0.8 and it is now resolved.

Jezus what a complete mess sorry but how was this release to prod when its just breaking for everyone?

Of course, this could play a part, but my feeling based on this thread was that it affected more than sveltekit users

My project is just React + Vite, a simple SPA. And it is affected.

Thanks @OxCom . Of course, this could play a part, but my feeling based on this thread was that it affected more than sveltekit users

Thanks @sapphi-red , a couple of things stand out -

default for allowedHosts being set to [] but not being overridden by true - so, an issue with where the user config is merged with the default config.

and / or, https://github.com/vitejs/vite/commit/bd896fb5f312fc0ff1730166d1d142fc0d34ba6d#diff-abb3345b6e3b2ec6d297c2ebc54cca85ae4487a31bac3cc9e78457f5114adb26R864 removing allowedHosts !== true here so as not to trigger the middleware when not required?

Will hopefully have more time later this week to look more

Blocked request. This host (“myhost.com”) is not allowed. To allow this host, add “myhost.com” to preview.allowedHosts in vite.config.js.

  • I added it to vite.config.js and it didn’t work.
  • I downgraded to version 4.5.0 and it remains the same
  • I’m using nginx in my server
  • The application is being run in Docker

That is why I hate vite, one day it works and other just dont.

Hm… please take a look into tools that use vite to run. For example I have same issue with vite + storybook, because of storybook. I defined in storybook config the server options:

import {mergeConfig} from 'vite';

// ...

async viteFinal(config) {
        return mergeConfig(config, {
            server: {
                hmr: {
                    path: '/ws'
                }
            }
        });
    },

and the problem is that mergeConfig will replace server options from vite

Also encountering this after pushing an update today, no changes to vite config Running on GCP / Cloud Run