vercel / vercel/next.js

Rewrite does not set the correct x-forwarded-host in Vercel

Open
#67,469 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug Middleware
Dominant language
JavaScript
Stars
142k
Forks
32.4k
Avg merge
2d 14h
Merged PRs (30d)
351

Description

Link to the code that reproduces this issue

https://github.com/hector/middleware-web

To Reproduce

The basic code to trigger this bug is simply to use a rewrite in the middleware of Next.js like this:

return NextResponse.rewrite(url, { request: req });

If you click on this link https://middleware-web.vercel.app/?rewrite=https://request-headers-web.vercel.app/ you are reproducing this issue.

This is what is happening:

Code of https://middleware-web.vercel.app/ is public in this repo https://github.com/hector/middleware-web
Code of https://request-headers-web.vercel.app/ is public in this repo https://github.com/hector/request-headers-web

Current vs. Expected behavior

I would expect to receive the following headers in https://request-headers-web.vercel.app/ server:

  • host: request-headers-web.vercel.app
  • x-forwarded-host: middleware-web.vercel.app

This is what I is actually received (it is printed in the page and can be easily seen):

  • host: request-headers-web.vercel.app
  • x-forwarded-host: request-headers-web.vercel.app

The header x-forwarded-host is not set to the original host before the rewrite.
MDN Reference here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-Host.

Provide environment information
Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 22.6.0: Mon Apr 22 20:51:27 PDT 2024; root:xnu-8796.141.3.705.2~1/RELEASE_ARM64_T6020
  Available memory (MB): 16384
  Available CPU cores: 10
Binaries:
  Node: 20.13.1
  npm: 10.5.2
  Yarn: 1.22.22
  pnpm: 9.3.0
Relevant Packages:
  next: 15.0.0-canary.54 // Latest available version is detected (15.0.0-canary.54).
  eslint-config-next: N/A
  react: 19.0.0-rc-6f23540c7d-20240528
  react-dom: 19.0.0-rc-6f23540c7d-20240528
  typescript: 5.3.3
Next.js Config:
  output: N/A
Which area(s) are affected? (Select all that apply)

Middleware

Which stage(s) are affected? (Select all that apply)

Vercel (Deployed)

Additional context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the middleware-web reproduction and the NextResponse.rewrite call shown in the issue, then trace Next.js middleware rewrite handling for forwarded headers. Compare the deployed request headers with the expected host and x-forwarded-host values; done means the original host is preserved in x-forwarded-host after the rewrite.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, next.js
Domain
backend, web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.