Skip to content

server.allowedHosts ignored by @astrojs/cloudflare preview #16198

@adamchal

Description

@adamchal

Astro Info

Astro                    v6.1.3
Vite                     v7.3.1
Node                     v25.8.2
System                   macOS (x64)
Package Manager          npm
Output                   static
Adapter                  @astrojs/cloudflare (v13.1.7)
Integrations             none

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

server.allowedHosts (and vite.preview.allowedHosts) are ignored when using astro preview with the @astrojs/cloudflare adapter. Requests with non-localhost Host headers are blocked with a 403.

This was fixed for the static preview server in #16104, but the @astrojs/cloudflare adapter has its own preview entrypoint that is not covered by that fix.

Reproduce

npm install
npm run build
npm run preview
# In another terminal:
curl -H "Host: foo.example.com" http://localhost:4010/

Actual

< HTTP/1.1 403 Forbidden

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

What's the expected result?

200 response (.example.com is in server.allowedHosts).


Root cause

Two issues:

  1. astro core does not pass server.allowedHosts to adapter preview entrypoints.
    In packages/astro/src/core/preview/index.ts, the call to previewModule.default() includes host, port, headers, etc. but omits allowedHosts.

  2. @astrojs/cloudflare preview entrypoint hardcodes allowedHosts: [].
    In packages/cloudflare/src/entrypoints/preview.ts, the Vite preview config sets allowedHosts: [], ignoring any user configuration.

Workaround

Patch the adapter's preview entrypoint via a postinstall script in package.json:

"postinstall": "node -e \"let f=require('node:fs'),p='node_modules/@astrojs/cloudflare/dist/entrypoints/preview.js';f.existsSync(p)&&f.writeFileSync(p,f.readFileSync(p,'utf8').replace('allowedHosts: []','allowedHosts: true'))\""

Link to Minimal Reproducible Example

https://github.com/adamchal/astro-cloudflare-preview-allowedhosts

Participation

  • I am willing to submit a pull request for this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    - P3: minor bugAn edge case that only affects very specific usage (priority)pkg: astroRelated to the core `astro` package (scope)pkg: cloudflareRelated to the Cloudflare adapter

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions