[vite-plugin-cloudflare] fix: preserve :authority and port in HTTP/2 requests #15519
🦋 Changeset detected
Latest commit: aad2ccf75c508c53f4f25fb234c51b98d7174a31
The changes in this PR will be included in the next version bump.
<details><summary>This PR includes changesets to release 1 package</summary>
| Name | Type |
| ----------------------- | ----- |
| @cloudflare/vite-plugin | Patch |
</details>
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR
Codeowners approval required for this PR:
<details><summary>Show detailed file reviewers</summary>
</details>
<!-- devin-review-comment {"id": "BUG_pr-review-job-7a6be943511548b3b1d7de6077099aa2_0001", "file_path": "packages/vite-plugin-cloudflare/src/utils.ts", "start_line": 149, "end_line": 150, "side": "RIGHT", "based_on_repo_rules": false, "kind": "bug"} -->
🟡 HTTP/2 authority loses to stale Host
When an HTTP/2 request also carries Host, createRequestForIncomingMessage uses it instead of :authority. A proxy-added Host without the port still breaks redirect origins.
const host =
options?.host ?? getAuthority(req) ?? headers.get("Host") ?? "localhost";
<!-- devin-review-badge-begin -->
<a href="https://cloudflare.devinenterprise.com/review/cloudflare/workers-sdk/pull/15519" target="_blank">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=3">
<img src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=3" alt="Devin Review">
</picture>
</a>
<!-- devin-review-badge-end -->
---
Was this helpful? React with 👍 or 👎 to provide feedback.
Devin Review found 1 potential issue.
2 flags not posted on this PR by your GitHub settings — view them in Devin Review. (Configure)
<!-- devin-review-badge-begin -->
<a href="https://cloudflare.devinenterprise.com/review/cloudflare/workers-sdk/pull/15519" target="_blank">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=3">
<img src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=3" alt="Devin Review">
</picture>
</a>
<!-- devin-review-badge-end -->
<details><summary><b>@cloudflare/autoconfig</b></summary><p>
npm i https://pkg.pr.new/@cloudflare/autoconfig@15519
</p></details>
<details><summary><b>@cloudflare/build-output-utils</b></summary><p>
npm i https://pkg.pr.new/@cloudflare/build-output-utils@15519
</p></details>
<details><summary><b>@cloudflare/codemods</b></summary><p>
npm i https://pkg.pr.new/@cloudflare/codemods@15519
</p></details>
<details><summary><b>@cloudflare/config</b></summary><p>
npm i https://pkg.pr.new/@cloudflare/config@15519
</p></details>
<details><summary><b>create-cloudflare</b></summary><p>
npm i https://pkg.pr.new/create-cloudflare@15519
</p></details>
<details><summary><b>@cloudflare/deploy-helpers</b></summary><p>
npm i https://pkg.pr.new/@cloudflare/deploy-helpers@15519
</p></details>
<details><summary><b>@cloudflare/kv-asset-handler</b></summary><p>
npm i https://pkg.pr.new/@cloudflare/kv-asset-handler@15519
</p></details>
<details><summary><b>miniflare</b></summary><p>
npm i https://pkg.pr.new/miniflare@15519
</p></details>
<details><summary><b>@cloudflare/pages-functions</b></summary><p>
npm i https://pkg.pr.new/@cloudflare/pages-functions@15519
</p></details>
<details><summary><b>@cloudflare/pages-shared</b></summary><p>
npm i https://pkg.pr.new/@cloudflare/pages-shared@15519
</p></details>
<details><summary><b>@cloudflare/unenv-preset</b></summary><p>
npm i https://pkg.pr.new/@cloudflare/unenv-preset@15519
</p></details>
<details><summary><b>@cloudflare/vite-plugin</b></summary><p>
npm i https://pkg.pr.new/@cloudflare/vite-plugin@15519
</p></details>
<details><summary><b>@cloudflare/vitest-plugin</b></summary><p>
npm i https://pkg.pr.new/@cloudflare/vitest-plugin@15519
</p></details>
<details><summary><b>@cloudflare/workers-auth</b></summary><p>
npm i https://pkg.pr.new/@cloudflare/workers-auth@15519
</p></details>
<details><summary><b>@cloudflare/workers-editor-shared</b></summary><p>
npm i https://pkg.pr.new/@cloudflare/workers-editor-shared@15519
</p></details>
<details><summary><b>@cloudflare/workers-utils</b></summary><p>
npm i https://pkg.pr.new/@cloudflare/workers-utils@15519
</p></details>
<details><summary><b>wrangler</b></summary><p>
npm i https://pkg.pr.new/wrangler@15519
</p></details>
_commit: <a href="https://github.com/cloudflare/workers-sdk/runs/101718792638"><code>aad2ccf</code></a>_
cool fixed it
@dario-piotrowicz hey can you look into the PR , let me know if there are any changes to made
@dario-piotrowicz hey can you look into the PR , let me know if there are any changes to made
Sorry I wasn't around, I'm reviewing the PR now 👍
LGTM thanks @devaniketh 🙂
Codeowners reviews satisfied
@dario-piotrowicz thanks can we merge it ?
@dario-piotrowicz thanks can we merge it ?
no worries 🙁 , sure, we can merge this PR as soon as CI goes green 👍
cool all checks passed @dario-piotrowicz
cool all checks passed @dario-piotrowicz
merged 🙂
dario-piotrowicz
devaniketh
Fixes #14931.
When Vite dev server is served over HTTPS with HTTP/2 enabled, browsers communicate using HTTP/2 where the host and port are passed via the
:authoritypseudo-header rather than the HTTP/1.1Hostheader.Previously, request conversion in
createRequestForIncomingMessageomitted pseudo-headers when creating the Web StandardRequest, causingheaders.get("Host")to benulland falling back to"localhost", discarding non-default ports (such as:5173). Consequently,X-Forwarded-Hostwas not set intoMiniflareRequest, breaking OAuth and Clerk handshake redirects.This change:
1. Extracts authority and non-default port from
req.headers[":authority"]andreq.authority.2. Extracts scheme from
req.headers[":scheme"]andreq.scheme.3. Ensures
Hostheader is populated on the createdRequest.4. Falls back to
new URL(request.url).hostintoMiniflareRequestto guaranteeX-Forwarded-Hostpreserves the host and port.---
- [x] Tests included/updated
- [ ] Automated tests not possible - manual testing has been completed as follows:
- [ ] Additional testing not necessary because:
- [ ] Cloudflare docs PR(s):
- [x] Documentation not necessary because: Internal dev server request adaptation fix.
<!-- devin-review-badge-begin -->
---
<a href="https://cloudflare.devinenterprise.com/review/cloudflare/workers-sdk/pull/15519" target="_blank">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=3">
<img src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=3" alt="Devin Review">
</picture>
</a>
<!-- devin-review-badge-end -->