Vite · Plugins

VITE0025Keep every Set-Cookie when copying Fetch response headers

Reports loops that copy Fetch Response headers onto a Node ServerResponse with setHeader, which keeps only the last Set-Cookie.

Why it happens

Dev middleware and plugin servers often bridge a Fetch Response to Node's ServerResponse by iterating response.headers. The Headers iterator combines repeated headers into one comma-separated value, except Set-Cookie: each cookie is yielded as its own entry. res.setHeader replaces any value already set for that name, so every Set-Cookie but the last one is dropped and sessions or CSRF cookies silently go missing. Doctor reports a setHeader call that receives the header name from a for...of or forEach loop over a headers property, except the headers of a request binding such as req or request. It treats Set-Cookie as handled, and skips the loop, when the loop body mentions set-cookie, when the loop body checks the header name in a file that mentions set-cookie, or when the same function mentions set-cookie or getSetCookie after the loop.

Fix

Skip "set-cookie" in the loop and copy the cookies once with res.setHeader("set-cookie", response.headers.getSetCookie()), or append each value with res.appendHeader.

Example

Before

async function writeResponse(res: ServerResponse, response: Response) {
  res.statusCode = response.status
  for (const [name, value] of response.headers) res.setHeader(name, value)
  res.end(await response.text())
}

After

async function writeResponse(res: ServerResponse, response: Response) {
  res.statusCode = response.status
  for (const [name, value] of response.headers) {
    if (name !== "set-cookie") res.setHeader(name, value)
  }
  const cookies = response.headers.getSetCookie()
  if (cookies.length > 0) res.setHeader("set-cookie", cookies)
  res.end(await response.text())
}

Verify the fix

Run only this rule after editing so the report stays focused on the diagnostic you are closing:

pnpm vite-doctor . --framework vite --rules vite/plugin/no-lossy-set-cookie-bridge
Copyright © 2026