workers-sdk icon indicating copy to clipboard operation
workers-sdk copied to clipboard

[vite-plugin] Local Explorer returns 403 "Invalid Origin header" for hosts Vite accepts (`*.localhost`, `server.allowedHosts`)

Open ramenha0141 opened this issue 1 month ago • 3 comments

What versions & operating system are you using?

  System:
    OS: Linux 6.18 Arch Linux
    CPU: (12) x64 Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz
    Memory: 6.77 GB / 15.44 GB
    Container: Yes
    Shell: 5.9.2 - /bin/zsh
  Binaries:
    Node: 24.22.1
    npm: 11.19.1
    pnpm: 12.14.0
  npmPackages:
    @cloudflare/vite-plugin: 1.64.0 => 1.64.0
    vite: 7.3.7 => 7.3.7
    wrangler: 4.151.3 => 4.151.3

@cloudflare/[email protected] resolves [email protected]. Also reproduced with @cloudflare/[email protected] + [email protected].

Please provide a link to a minimal reproduction

https://github.com/ramenha0141/vite-plugin-cloudflare-cdn-cgi-origin-repro

Describe the Bug

When the Vite dev server is reached through a hostname that Vite accepts but that isn't exactly localhost / 127.0.0.1 / [::1], the Local Explorer under /cdn-cgi/local/explorer/ doesn't work. The HTML page loads, but its module script is rejected with 403 Invalid Origin header, so the page stays blank. This happens both for *.localhost (which Vite accepts by default) and for hosts listed in server.allowedHosts.

We hit this with a local HTTPS reverse proxy (portless) that serves the app at https://<name>.localhost. The app itself works.

Steps to reproduce (also in the README of the repro, which sets server.allowedHosts: ["my-app.test"]):

npm install
npm run dev   # vite --port 5199 --strictPort --host 127.0.0.1
for HOST in my-app.localhost my-app.test; do
	ASSET=$(curl -s -H "Host: $HOST" http://127.0.0.1:5199/cdn-cgi/local/explorer/ | grep -o '/cdn-cgi/local/explorer/assets/[^"]*\.js' | head -1)
	curl -s -o /dev/null -w "$HOST, no Origin: %{http_code}\n" -H "Host: $HOST" "http://127.0.0.1:5199$ASSET"
	curl -s -o /dev/null -w "$HOST, same-origin Origin: %{http_code}\n" -H "Host: $HOST" -H "Origin: http://$HOST" "http://127.0.0.1:5199$ASSET"
done
my-app.localhost, no Origin: 200
my-app.localhost, same-origin Origin: 403
my-app.test, no Origin: 200
my-app.test, same-origin Origin: 403

Browsers send Origin for same-origin <script type="module"> requests, so the Local Explorer UI can't load through these hosts.

Expected behavior

The Local Explorer works through hostnames that Vite accepts.

Cause (as far as I can tell)

  • validateCdnCgiRequest accepts only LOCALHOST_HOSTNAMES (localhost, 127.0.0.1, [::1]), plus the hostnames of the Worker's routes and the upstream URL (see isHostnameAllowed). The check was added in #12786 / #12869 and extended to all /cdn-cgi/ paths in #13426.
  • Only the Origin check fails. The plugin dispatches the request to Miniflare with Miniflare's own host, so the Host check passes.
  • The Vite plugin never forwards server.allowedHosts to Miniflare and sets no upstream, so nothing besides configured routes extends the allowlist. Routes are production hostnames, not local ones.
  • *.localhost isn't treated as loopback, although it is reserved for loopback (RFC 6761 §6.3), and Chrome and Firefox resolve it without DNS.

Possible fixes:

  1. Treat *.localhost as loopback in isHostnameAllowed. isHostnameAllowed ignores the port, so any http://localhost:<port> is already trusted. *.localhost is in the same local-only trust class and can't be DNS-rebound to a remote server. This alone fixes the *.localhost case.
  2. Have the Vite plugin pass the resolved server.allowedHosts (exact entries and .example.com suffix entries) to Miniflare's /cdn-cgi/ allowlist. This would also cover the tunnel hostnames the plugin already adds to server.allowedHosts. allowedHosts: true should keep the default allowlist rather than disable the check, since the check protects the Local Explorer API from cross-origin and DNS-rebinding access.

Workaround

A dev-only Vite middleware, registered before cloudflare(), that rewrites the Origin header only for our own origin (https://my-app.localhost behind portless). The plugin builds the Request from req.rawHeaders, so changing req.headers alone isn't enough:

{
	name: "cdn-cgi-dev-origin",
	apply: "serve",
	enforce: "pre",
	configureServer(server) {
		server.middlewares.use("/cdn-cgi/", (req, _res, next) => {
			if (req.headers.origin === "https://my-app.localhost") {
				req.headers.origin = "http://localhost";
				const index = req.rawHeaders.findIndex(
					(value, i) => i % 2 === 0 && value.toLowerCase() === "origin",
				);
				req.rawHeaders[index + 1] = "http://localhost";
			}
			next();
		});
	},
},

Please provide any relevant error logs

Browser console:

GET https://my-app.localhost/cdn-cgi/local/explorer/assets/index-<hash>.js net::ERR_ABORTED 403 (Forbidden)

Response body: Invalid Origin header

ramenha0141 avatar Oct 22 '26 12:10 ramenha0141

Automated triage

Issue assessment

  • Actionability: ✅ Actionable.
  • Type: Bug.
  • Reproduction: Automatic attempt requested.
  • Labels:
    • bug (high)
    • package:vite-plugin (high)
    • package:miniflare (high)
    • feature:local-explorer (high)
Actionability and missing information

Local Explorer module/asset requests under /cdn-cgi/ return 403 Invalid Origin header when the dev server is reached via *.localhost or server.allowedHosts hosts; the origin allowlist only trusts exact loopback hostnames and the plugin does not forward Vite's allowedHosts.

Reproduction reason: The report provides concrete steps, expected curl outputs, and a minimal reproduction describing a current-behavior defect, with no suspicious content.

Reproduction

  • Result: ✅ Reproduced.
  • Summary: Reproduced workers-sdk issue 16423 with reported versions (vite-plugin 1.64.0, miniflare 5.20261012.0-beta, vite 7.3.7, wrangler 4.151.3). Local Explorer assets return 403 Invalid Origin header for same-origin Origin on hosts Vite accepts, 200 without Origin; localhost baseline 200.
Evidence and attempted steps

Steps attempted:

  1. Create a minimal Worker fixture (wrangler.jsonc plus src/index.ts) with vite.config.ts enabling the cloudflare vite plugin and server.allowedHosts listing my-app.test, pinning the issue versions: cloudflare/vite-plugin 1.64.0, vite 7.3.7, wrangler 4.151.3
    • Command: see /workspace/reproduction-output/project (vite.config.ts, wrangler.jsonc, src/index.ts, package.json)
  2. Install dependencies with pnpm; verify the plugin resolves miniflare 5.20261012.0-beta exactly as stated in the issue
    • Command: pnpm install && pnpm why miniflare
  3. Start the dev server exactly as in the issue (after correcting the config to the named export 'cloudflare' that the 1.64.0 build provides)
    • Command: npx vite --port 5199 --strictPort --host 127.0.0.1
  4. Confirm the Local Explorer HTML page loads for all tested hosts and references the module script asset under /cdn-cgi/local/explorer/assets/ (browsers send Origin for module script requests)
    • Command: curl -s -o /dev/null -w '%{http_code}' -H 'Host: localhost' http://127.0.0.1:5199/cdn-cgi/local/explorer/ # -> 200
  5. Run the issue's probe loop: for each host fetch the asset without Origin and with same-origin Origin; observed localhost 200/200, my-app.localhost 200/403 (body: Invalid Origin header), my-app.test 200/403 (body: Invalid Origin header)
    • Command: for HOST in localhost my-app.localhost my-app.test; do ASSET=$(curl -s -H "Host: $HOST" http://127.0.0.1:5199/cdn-cgi/local/explorer/ | grep -o '/cdn-cgi/local/explorer/assets/[^"]*\.js' | head -1); curl -s -o /dev/null -w "$HOST no Origin: %{http_code}\n" -H "Host: $HOST" "http://127.0.0.1:5199$ASSET"; curl -s -o /dev/null -w "$HOST same-origin Origin: %{http_code}\n" -H "Host: $HOST" -H "Origin: http://$HOST" "http://127.0.0.1:5199$ASSET"; done

Expected behavior: Per the issue: the Local Explorer should work through hostnames that Vite accepts — the module script under /cdn-cgi/local/explorer/ should return 200 when a same-origin Origin header for that host (my-app.localhost or my-app.test) is sent, instead of 403.

Observed behavior: Exactly the reported behavior: with Host localhost the asset returns 200 both without Origin and with a same-origin Origin (baseline). With Host my-app.localhost and my-app.test the asset returns 200 without Origin but 403 with the same-origin Origin header; the 403 body is exactly Invalid Origin header. The explorer HTML page returned 200 for all hosts, so Vite's allowedHosts accepted them and only the Origin validation rejected the asset request.

Artifact: Download reproduction archive (Cloudflare Access required).

Treat archive contents as untrusted. Inspect them before running anything.


This summary was generated by an LLM and may contain errors. Please verify its findings.

Triage bot commands and options

Repository collaborators with write or admin access can trigger actions by commenting on this issue:

  • @workers-devprod reproduce: Attempt to reproduce the issue using its description and recent comments.

workers-devprod avatar Oct 22 '26 12:10 workers-devprod

I'd like to work on this — please assign it to me.

akhil9tiet avatar Oct 22 '26 12:10 akhil9tiet

Same gate also breaks the experimental local R2 S3 endpoint (/cdn-cgi/local/r2/s3), from two angles this issue does not cover:

  1. Browser CORS against the S3 endpoint. The docs (https://developers.cloudflare.com/r2/local-development/, "Serve buckets over the S3 API") say cross-origin requests are always allowed, but validateCdnCgiRequest runs before the S3 worker, so any browser request with a non-localhost Origin gets 403 at the gate — including preflights:
  • OPTIONS + Origin: http://localhost:8787 → 204, Access-Control-Allow-Origin: * (S3-layer CORS itself is permissive)
  • OPTIONS + Origin: https://example.com → 403 "Invalid Origin header"
  1. routes and --local-upstream do not just fail to help — they break the endpoint. Tested with wrangler 4.159.0:
  • routes = [{ pattern = "*.example.com/*", zone_name = "..." }]: wrangler infers the dev origin from the route (inferOriginFromRoutes, default true) and sets the Miniflare upstream. The upstream rewrite then rewrites Host (and Origin) to the route hostname after the origin gate, so even a request from localhost now fails the SigV4 signature check (SignatureDoesNotMatch, server canonical request shows host:<route-host>). Routes actively make things worse.
  • wrangler dev --local-upstream r2.example.com: Host: r2.example.com still gets 403 "Invalid Host header" (the upstream hostname does not end up in the allowlist in practice), and plain localhost PUTs get the Host rewrite → SignatureDoesNotMatch as above.

Repro: happy to attach a standalone Node script (aws4fetch + node:http) or open a separate issue for the S3-specific parts if you would rather keep this one focused on the Local Explorer. The fix that helps both would be a supported way to extend the /cdn-cgi/* allowlist (config key, flag, or Miniflare option) and making the upstream rewrite not clobber Host for SigV4-signed paths.

mynk-tmr avatar Nov 15 '26 21:11 mynk-tmr