Skip to content

Commit

Permalink
Fix actions redirect handling (#49483)
Browse files Browse the repository at this point in the history
This ensures we properly strip conflicting headers from being forwarded
when handling the redirect request/response in actions.

x-ref: [slack
thread](https://vercel.slack.com/archives/C042LHPJ1NX/p1683218335368759)

Fixes: 

```sh
failed to get redirect response TypeError: fetch failed
    at Object.fetch (node:internal/deps/undici/undici:11413:11) {
  cause: RequestContentLengthMismatchError: Request body length does not match content-length header
      at write (node:internal/deps/undici/undici:9907:41)
      at _resume (node:internal/deps/undici/undici:9885:33)
      at resume (node:internal/deps/undici/undici:9787:7)
      at connect (node:internal/deps/undici/undici:9776:7) {
    code: 'UND_ERR_REQ_CONTENT_LENGTH_MISMATCH'
  }
}
```
  • Loading branch information
ijjk committed May 8, 2023
1 parent b20d5f2 commit 5ca02a8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
10 changes: 7 additions & 3 deletions packages/next/src/server/app-render/action-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { StaticGenerationStore } from '../../client/components/static-generation
import { FlightRenderResult } from './flight-render-result'
import { ActionResult } from './types'
import { ActionAsyncStorage } from '../../client/components/action-async-storage'
import { filterReqHeaders, forbiddenHeaders } from '../lib/server-ipc/utils'

function nodeToWebReadableStream(nodeReadable: import('stream').Readable) {
if (process.env.NEXT_RUNTIME !== 'edge') {
Expand Down Expand Up @@ -90,10 +91,10 @@ function getForwardedHeaders(
})

// Merge request and response headers
const mergedHeaders = {
const mergedHeaders = filterReqHeaders({
...nodeHeadersToRecord(requestHeaders),
...nodeHeadersToRecord(responseHeaders),
}
}) as Record<string, string>

// Merge cookies
const mergedCookies = requestCookies.split('; ').concat(setCookies).join('; ')
Expand Down Expand Up @@ -177,13 +178,16 @@ async function createRedirectRenderResult(
})
// copy the headers from the redirect response to the response we're sending
for (const [key, value] of response.headers) {
res.setHeader(key, value)
if (!forbiddenHeaders.includes(key)) {
res.setHeader(key, value)
}
}

return new FlightRenderResult(response.body!)
}
} catch (err) {
// we couldn't stream the redirect response, so we'll just do a normal redirect
console.error(`failed to get redirect response`, err)
}
}
return new RenderResult(JSON.stringify({}))
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/server/lib/server-ipc/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const forbiddenHeaders = [
export const forbiddenHeaders = [
'accept-encoding',
'content-length',
'keepalive',
Expand Down

0 comments on commit 5ca02a8

Please sign in to comment.