Unverified Commit c502308b authored by Nikhil Mehta's avatar Nikhil Mehta Committed by GitHub

fix: cookie override during redirection from server action (#61633)

### What?
Fixes #61611

### Why?
Any one having custom server may be having logic to set cookies during
GET requests too. Currently nextjs in app directory does not allow to do
so but with custom server its very much possible.

### How?
By merging cookies of redirect response and server action POSt response

### Tests
I have added one more test to existing suite and it passing with fix in
place.

![image](https://github.com/vercel/next.js/assets/6815560/858afdbb-c377-49eb-9002-fcbdf06583a4)

### Notes
This bug is reproducible only if developer has custom server on top of
next app but still very probable

---------
Co-authored-by: 's avatarShu Ding <g@shud.in>
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent 2ab4a443
......@@ -13,6 +13,7 @@ export const ipcForbiddenHeaders = [
export const actionsForbiddenHeaders = [
...ipcForbiddenHeaders,
'content-length',
'set-cookie',
]
export const filterReqHeaders = (
......
import { redirect } from 'next/navigation'
import { cookies } from 'next/headers'
async function action() {
'use server'
cookies().set(
'custom-server-action-test-cookie',
'custom-server-action-test-cookie-val'
)
redirect('/another')
}
......
......@@ -18,7 +18,10 @@ async function main() {
try {
const parsedUrl = parse(req.url, true)
const { pathname } = parsedUrl
res.setHeader(
'set-cookie',
'custom-server-test-cookie=custom-server-test-cookie-val'
)
if (pathname.startsWith('/base')) {
await handle(req, res, parsedUrl)
} else {
......
import { join } from 'path'
import { createNextDescribe } from 'e2e-utils'
import { retry } from 'next-test-utils'
import { check, retry } from 'next-test-utils'
import { join } from 'path'
createNextDescribe(
'custom-app-server-action-redirect',
......@@ -35,5 +35,26 @@ createNextDescribe(
// Count should still be 2 as the browser should not have reloaded the page.
expect(await getCount()).toBe('Count: 2')
})
it('redirects with proper cookies set from both redirect response and post respose', async () => {
const browser = await next.browser('/base')
await browser.elementById('submit-server-action-redirect').click()
expect(await browser.waitForElementByCss('#another').text()).toBe(
'Another Page'
)
expect(await browser.url()).toBe(
`http://localhost:${next.appPort}/base/another`
)
await check(
() => browser.eval('document.cookie'),
/custom-server-test-cookie/
)
await check(
() => browser.eval('document.cookie'),
/custom-server-action-test-cookie/
)
})
}
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment