Unverified Commit ceebc676 authored by Janka Uryga's avatar Janka Uryga Committed by GitHub

prevent cookies from being set in the wrong phase (#71094)

makes the object returned by `cookies() check if the phase we're
currently in allows mutations.

- `(await cookies()).set(...)` will no longer be allowed in a render
that follows an action (previously, this'd work, because
`actionStore.isAction` is still true during the render!)
- `unstable_after(async () => (await cookies()).set(...))` will error
- `const store = await cookies(); unstable_after(() => store.set(...))`
will now also error

this affects a couple places that mutate cookies in a way that we don't
let userspace code do, like draftmode. to support this, i've added
`workUnitStore.userspaceMutableCookies` and made *only that one* check
for invalid accesses. that's the one we return from `cookies()` in a
mutable context. the rest of the codebase can continue using
`workUnitStore.mutableCookies` without worrying about phase checks.
parent eaf467e9
...@@ -43,6 +43,7 @@ import { fromNodeOutgoingHttpHeaders } from '../web/utils' ...@@ -43,6 +43,7 @@ import { fromNodeOutgoingHttpHeaders } from '../web/utils'
import { selectWorkerForForwarding } from './action-utils' import { selectWorkerForForwarding } from './action-utils'
import { isNodeNextRequest, isWebNextRequest } from '../base-http/helpers' import { isNodeNextRequest, isWebNextRequest } from '../base-http/helpers'
import { RedirectStatusCode } from '../../client/components/redirect-status-code' import { RedirectStatusCode } from '../../client/components/redirect-status-code'
import { synchronizeMutableCookies } from '../async-storage/request-store'
function formDataFromSearchQueryString(query: string) { function formDataFromSearchQueryString(query: string) {
const searchParams = new URLSearchParams(query) const searchParams = new URLSearchParams(query)
...@@ -459,6 +460,15 @@ export async function handleAction({ ...@@ -459,6 +460,15 @@ export async function handleAction({
) )
} }
const finalizeAndGenerateFlight: GenerateFlight = (...args) => {
// When we switch to the render phase, cookies() will return
// `workUnitStore.cookies` instead of `workUnitStore.userspaceMutableCookies`.
// We want the render to see any cookie writes that we performed during the action,
// so we need to update the immutable cookies to reflect the changes.
synchronizeMutableCookies(requestStore)
return generateFlight(...args)
}
requestStore.phase = 'action' requestStore.phase = 'action'
// When running actions the default is no-store, you can still `cache: 'force-cache'` // When running actions the default is no-store, you can still `cache: 'force-cache'`
...@@ -548,7 +558,7 @@ export async function handleAction({ ...@@ -548,7 +558,7 @@ export async function handleAction({
return { return {
type: 'done', type: 'done',
result: await generateFlight(req, ctx, { result: await finalizeAndGenerateFlight(req, ctx, {
actionResult: promise, actionResult: promise,
// if the page was not revalidated, we can skip the rendering the flight tree // if the page was not revalidated, we can skip the rendering the flight tree
skipFlight: !workStore.pathWasRevalidated, skipFlight: !workStore.pathWasRevalidated,
...@@ -841,7 +851,7 @@ export async function handleAction({ ...@@ -841,7 +851,7 @@ export async function handleAction({
requestStore, requestStore,
}) })
actionResult = await generateFlight(req, ctx, { actionResult = await finalizeAndGenerateFlight(req, ctx, {
actionResult: Promise.resolve(returnVal), actionResult: Promise.resolve(returnVal),
// if the page was not revalidated, or if the action was forwarded from another worker, we can skip the rendering the flight tree // if the page was not revalidated, or if the action was forwarded from another worker, we can skip the rendering the flight tree
skipFlight: !workStore.pathWasRevalidated || actionWasForwarded, skipFlight: !workStore.pathWasRevalidated || actionWasForwarded,
...@@ -916,7 +926,7 @@ export async function handleAction({ ...@@ -916,7 +926,7 @@ export async function handleAction({
} }
return { return {
type: 'done', type: 'done',
result: await generateFlight(req, ctx, { result: await finalizeAndGenerateFlight(req, ctx, {
skipFlight: false, skipFlight: false,
actionResult: promise, actionResult: promise,
}), }),
......
...@@ -38,8 +38,11 @@ export type RequestStore = { ...@@ -38,8 +38,11 @@ export type RequestStore = {
} }
readonly headers: ReadonlyHeaders readonly headers: ReadonlyHeaders
readonly cookies: ReadonlyRequestCookies // This is mutable because we need to reassign it when transitioning from the action phase to the render phase.
// The cookie object itself is deliberately read only and thus can't be updated.
cookies: ReadonlyRequestCookies
readonly mutableCookies: ResponseCookies readonly mutableCookies: ResponseCookies
readonly userspaceMutableCookies: ResponseCookies
readonly draftMode: DraftModeProvider readonly draftMode: DraftModeProvider
readonly isHmrRefresh?: boolean readonly isHmrRefresh?: boolean
readonly serverComponentsHmrCache?: ServerComponentsHmrCache readonly serverComponentsHmrCache?: ServerComponentsHmrCache
......
...@@ -13,6 +13,8 @@ import { ...@@ -13,6 +13,8 @@ import {
import { import {
MutableRequestCookiesAdapter, MutableRequestCookiesAdapter,
RequestCookiesAdapter, RequestCookiesAdapter,
responseCookiesToRequestCookies,
wrapWithMutableAccessCheck,
type ReadonlyRequestCookies, type ReadonlyRequestCookies,
} from '../web/spec-extension/adapters/request-cookies' } from '../web/spec-extension/adapters/request-cookies'
import { ResponseCookies, RequestCookies } from '../web/spec-extension/cookies' import { ResponseCookies, RequestCookies } from '../web/spec-extension/cookies'
...@@ -165,6 +167,7 @@ function createRequestStoreImpl( ...@@ -165,6 +167,7 @@ function createRequestStoreImpl(
headers?: ReadonlyHeaders headers?: ReadonlyHeaders
cookies?: ReadonlyRequestCookies cookies?: ReadonlyRequestCookies
mutableCookies?: ResponseCookies mutableCookies?: ResponseCookies
userspaceMutableCookies?: ResponseCookies
draftMode?: DraftModeProvider draftMode?: DraftModeProvider
} = {} } = {}
...@@ -202,6 +205,9 @@ function createRequestStoreImpl( ...@@ -202,6 +205,9 @@ function createRequestStoreImpl(
return cache.cookies return cache.cookies
}, },
set cookies(value: ReadonlyRequestCookies) {
cache.cookies = value
},
get mutableCookies() { get mutableCookies() {
if (!cache.mutableCookies) { if (!cache.mutableCookies) {
const mutableCookies = getMutableCookies( const mutableCookies = getMutableCookies(
...@@ -215,6 +221,15 @@ function createRequestStoreImpl( ...@@ -215,6 +221,15 @@ function createRequestStoreImpl(
} }
return cache.mutableCookies return cache.mutableCookies
}, },
get userspaceMutableCookies() {
if (!cache.userspaceMutableCookies) {
const userspaceMutableCookies = wrapWithMutableAccessCheck(
this.mutableCookies
)
cache.userspaceMutableCookies = userspaceMutableCookies
}
return cache.userspaceMutableCookies
},
get draftMode() { get draftMode() {
if (!cache.draftMode) { if (!cache.draftMode) {
cache.draftMode = new DraftModeProvider( cache.draftMode = new DraftModeProvider(
...@@ -234,3 +249,10 @@ function createRequestStoreImpl( ...@@ -234,3 +249,10 @@ function createRequestStoreImpl(
(globalThis as any).__serverComponentsHmrCache, (globalThis as any).__serverComponentsHmrCache,
} }
} }
export function synchronizeMutableCookies(store: RequestStore) {
// TODO: does this need to update headers as well?
store.cookies = RequestCookiesAdapter.seal(
responseCookiesToRequestCookies(store.mutableCookies)
)
}
import { import {
type ReadonlyRequestCookies, type ReadonlyRequestCookies,
type ResponseCookies, type ResponseCookies,
areCookiesMutableInCurrentPhase,
RequestCookiesAdapter, RequestCookiesAdapter,
} from '../web/spec-extension/adapters/request-cookies' } from '../web/spec-extension/adapters/request-cookies'
import { RequestCookies } from '../web/spec-extension/cookies' import { RequestCookies } from '../web/spec-extension/cookies'
...@@ -16,7 +17,6 @@ import { ...@@ -16,7 +17,6 @@ import {
trackDynamicDataInDynamicRender, trackDynamicDataInDynamicRender,
} from '../app-render/dynamic-rendering' } from '../app-render/dynamic-rendering'
import { getExpectedRequestStore } from '../app-render/work-unit-async-storage.external' import { getExpectedRequestStore } from '../app-render/work-unit-async-storage.external'
import { actionAsyncStorage } from '../app-render/action-async-storage.external'
import { StaticGenBailoutError } from '../../client/components/static-generation-bailout' import { StaticGenBailoutError } from '../../client/components/static-generation-bailout'
import { makeResolvedReactPromise } from './utils' import { makeResolvedReactPromise } from './utils'
import { makeHangingPromise } from '../dynamic-rendering-utils' import { makeHangingPromise } from '../dynamic-rendering-utils'
...@@ -113,22 +113,16 @@ export function cookies(): Promise<ReadonlyRequestCookies> { ...@@ -113,22 +113,16 @@ export function cookies(): Promise<ReadonlyRequestCookies> {
} }
// cookies is being called in a dynamic context // cookies is being called in a dynamic context
const actionStore = actionAsyncStorage.getStore()
const requestStore = getExpectedRequestStore(callingExpression) const requestStore = getExpectedRequestStore(callingExpression)
let underlyingCookies: ReadonlyRequestCookies let underlyingCookies: ReadonlyRequestCookies
// The current implementation of cookies will return Response cookies if (areCookiesMutableInCurrentPhase(requestStore)) {
// for a server action during the render phase of a server action.
// This is not correct b/c the type of cookies during render is ReadOnlyRequestCookies
// where as the type of cookies during action is ResponseCookies
// This was found because RequestCookies is iterable and ResponseCookies is not
if (actionStore?.isAction || actionStore?.isAppRoute) {
// We can't conditionally return different types here based on the context. // We can't conditionally return different types here based on the context.
// To avoid confusion, we always return the readonly type here. // To avoid confusion, we always return the readonly type here.
underlyingCookies = underlyingCookies =
requestStore.mutableCookies as unknown as ReadonlyRequestCookies requestStore.userspaceMutableCookies as unknown as ReadonlyRequestCookies
} else { } else {
underlyingCookies = requestStore.cookies underlyingCookies = requestStore.cookies
} }
......
import { RequestCookies } from '../cookies' import {
workUnitAsyncStorage,
type RequestStore,
} from '../../../app-render/work-unit-async-storage.external'
import { RequestCookies, ResponseCookies } from '../cookies'
import { import {
ReadonlyRequestCookiesError, ReadonlyRequestCookiesError,
RequestCookiesAdapter, RequestCookiesAdapter,
MutableRequestCookiesAdapter,
wrapWithMutableAccessCheck,
} from './request-cookies' } from './request-cookies'
describe('RequestCookiesAdapter', () => { describe('RequestCookiesAdapter', () => {
...@@ -52,3 +58,97 @@ describe('RequestCookiesAdapter', () => { ...@@ -52,3 +58,97 @@ describe('RequestCookiesAdapter', () => {
expect(sealed.get('bar')).toEqual(undefined) expect(sealed.get('bar')).toEqual(undefined)
}) })
}) })
describe('MutableRequestCookiesAdapter', () => {
it('supports chained set calls and preserves wrapping', () => {
const headers = new Headers({})
const underlyingCookies = new RequestCookies(headers)
const onUpdateCookies = jest.fn<void, [string[]]>()
const wrappedCookies = MutableRequestCookiesAdapter.wrap(
underlyingCookies,
onUpdateCookies
)
const returned = wrappedCookies.set('foo', '1').set('bar', '2')
expect(returned).toBe(wrappedCookies)
expect(onUpdateCookies).toHaveBeenCalledWith([
expect.stringContaining('foo=1'),
])
expect(onUpdateCookies).toHaveBeenCalledWith([
expect.stringContaining('foo=1'),
expect.stringContaining('bar=2'),
])
})
it('supports chained delete calls and preserves wrapping', () => {
const headers = new Headers({})
const underlyingCookies = new RequestCookies(headers)
underlyingCookies.set('foo', '1').set('bar', '2')
const onUpdateCookies = jest.fn<void, [string[]]>()
const wrappedCookies = MutableRequestCookiesAdapter.wrap(
underlyingCookies,
onUpdateCookies
)
const returned = wrappedCookies.delete('foo').delete('bar')
expect(returned).toBe(wrappedCookies)
expect(onUpdateCookies).toHaveBeenCalledWith([
expect.stringContaining('foo=;'),
])
expect(onUpdateCookies).toHaveBeenCalledWith([
expect.stringContaining('foo=;'),
expect.stringContaining('bar=;'),
])
})
})
describe('wrapWithMutableAccessCheck', () => {
const createMockRequestStore = (phase: RequestStore['phase']) =>
({ type: 'request', phase }) as RequestStore
it('prevents setting cookies in the render phase', () => {
const requestStore = createMockRequestStore('action')
workUnitAsyncStorage.run(requestStore, () => {
const headers = new Headers({})
const underlyingCookies = new ResponseCookies(headers)
const wrappedCookies = wrapWithMutableAccessCheck(underlyingCookies)
// simulate changing phases
requestStore.phase = 'render'
const EXPECTED_ERROR =
/Cookies can only be modified in a Server Action or Route Handler\./
expect(() => {
wrappedCookies.set('foo', '1')
}).toThrow(EXPECTED_ERROR)
expect(wrappedCookies.get('foo')).toBe(undefined)
})
})
it('prevents deleting cookies in the render phase', () => {
const requestStore = createMockRequestStore('action')
workUnitAsyncStorage.run(requestStore, () => {
const headers = new Headers({})
const underlyingCookies = new ResponseCookies(headers)
const wrappedCookies = wrapWithMutableAccessCheck(underlyingCookies)
wrappedCookies.set('foo', '1')
// simulate changing phases
requestStore.phase = 'render'
const EXPECTED_ERROR =
/Cookies can only be modified in a Server Action or Route Handler\./
expect(() => {
wrappedCookies.delete('foo')
}).toThrow(EXPECTED_ERROR)
expect(wrappedCookies.get('foo')?.value).toEqual('1')
})
})
})
import type { RequestCookies } from '../cookies' import { RequestCookies } from '../cookies'
import { ResponseCookies } from '../cookies' import { ResponseCookies } from '../cookies'
import { ReflectAdapter } from './reflect' import { ReflectAdapter } from './reflect'
import { workAsyncStorage } from '../../../app-render/work-async-storage.external' import { workAsyncStorage } from '../../../app-render/work-async-storage.external'
import {
getExpectedRequestStore,
type RequestStore,
} from '../../../app-render/work-unit-async-storage.external'
/** /**
* @internal * @internal
...@@ -63,6 +67,10 @@ export function getModifiedCookieValues( ...@@ -63,6 +67,10 @@ export function getModifiedCookieValues(
return modified return modified
} }
type SetCookieArgs =
| [key: string, value: string, cookie?: Partial<ResponseCookie>]
| [options: ResponseCookie]
export function appendMutableCookies( export function appendMutableCookies(
headers: Headers, headers: Headers,
mutableCookies: ResponseCookies mutableCookies: ResponseCookies
...@@ -128,7 +136,7 @@ export class MutableRequestCookiesAdapter { ...@@ -128,7 +136,7 @@ export class MutableRequestCookiesAdapter {
} }
} }
return new Proxy(responseCookies, { const wrappedCookies = new Proxy(responseCookies, {
get(target, prop, receiver) { get(target, prop, receiver) {
switch (prop) { switch (prop) {
// A special symbol to get the modified cookie values // A special symbol to get the modified cookie values
...@@ -144,29 +152,86 @@ export class MutableRequestCookiesAdapter { ...@@ -144,29 +152,86 @@ export class MutableRequestCookiesAdapter {
) )
try { try {
target.delete(...args) target.delete(...args)
return wrappedCookies
} finally { } finally {
updateResponseCookies() updateResponseCookies()
} }
} }
case 'set': case 'set':
return function ( return function (...args: SetCookieArgs) {
...args:
| [key: string, value: string, cookie?: Partial<ResponseCookie>]
| [options: ResponseCookie]
) {
modifiedCookies.add( modifiedCookies.add(
typeof args[0] === 'string' ? args[0] : args[0].name typeof args[0] === 'string' ? args[0] : args[0].name
) )
try { try {
return target.set(...args) target.set(...args)
return wrappedCookies
} finally { } finally {
updateResponseCookies() updateResponseCookies()
} }
} }
default: default:
return ReflectAdapter.get(target, prop, receiver) return ReflectAdapter.get(target, prop, receiver)
} }
}, },
}) })
return wrappedCookies
}
}
export function wrapWithMutableAccessCheck(
responseCookies: ResponseCookies
): ResponseCookies {
const wrappedCookies = new Proxy(responseCookies, {
get(target, prop, receiver) {
switch (prop) {
case 'delete':
return function (...args: [string] | [ResponseCookie]) {
ensureCookiesAreStillMutable('cookies().delete')
target.delete(...args)
return wrappedCookies
}
case 'set':
return function (...args: SetCookieArgs) {
ensureCookiesAreStillMutable('cookies().set')
target.set(...args)
return wrappedCookies
}
default:
return ReflectAdapter.get(target, prop, receiver)
}
},
})
return wrappedCookies
}
export function areCookiesMutableInCurrentPhase(requestStore: RequestStore) {
return requestStore.phase === 'action'
}
/** Ensure that cookies() starts throwing on mutation
* if we changed phases and can no longer mutate.
*
* This can happen when going:
* 'render' -> 'after'
* 'action' -> 'render'
* */
function ensureCookiesAreStillMutable(callingExpression: string) {
const requestStore = getExpectedRequestStore(callingExpression)
if (!areCookiesMutableInCurrentPhase(requestStore)) {
// TODO: maybe we can give a more precise error message based on callingExpression?
throw new ReadonlyRequestCookiesError()
}
}
export function responseCookiesToRequestCookies(
responseCookies: ResponseCookies
): RequestCookies {
const requestCookies = new RequestCookies(new Headers())
for (const cookie of responseCookies.getAll()) {
requestCookies.set(cookie)
} }
return requestCookies
} }
...@@ -248,15 +248,16 @@ describe.each(runtimes)('unstable_after() in %s runtime', (runtimeValue) => { ...@@ -248,15 +248,16 @@ describe.each(runtimes)('unstable_after() in %s runtime', (runtimeValue) => {
const cookie1 = await browser.elementById('cookie').text() const cookie1 = await browser.elementById('cookie').text()
expect(cookie1).toEqual('Cookie: null') expect(cookie1).toEqual('Cookie: null')
const cliOutputIndex = next.cliOutput.length
try { try {
await browser.elementByCss('button[type="submit"]').click() await browser.elementByCss('button[type="submit"]').click()
await retry(async () => { await retry(async () => {
const cookie1 = await browser.elementById('cookie').text() const cookie1 = await browser.elementById('cookie').text()
expect(cookie1).toEqual('Cookie: "action"') expect(cookie1).toEqual('Cookie: "action"')
// const newLogs = next.cliOutput.slice(cliOutputIndex) const newLogs = next.cliOutput.slice(cliOutputIndex)
// // after() from action // // after() from action
// expect(newLogs).toContain(EXPECTED_ERROR) expect(newLogs).toMatch(EXPECTED_ERROR)
}) })
} finally { } finally {
await browser.eval('document.cookie = "testCookie=;path=/;max-age=-1"') await browser.eval('document.cookie = "testCookie=;path=/;max-age=-1"')
......
import { unstable_after as after } from 'next/server'
import { cookies } from 'next/headers'
import * as React from 'react'
export const dynamic = 'force-dynamic'
async function action() {
'use server'
after(async () => {
const cookieStore = await cookies()
cookieStore.set('illegalCookie', 'too-late-for-that')
})
}
export default async function Page() {
return (
<>
<form action={action}>
<button type="submit">Submit</button>
</form>
</>
)
}
import { unstable_after as after } from 'next/server'
import { cookies } from 'next/headers'
import * as React from 'react'
export const dynamic = 'force-dynamic'
async function action() {
'use server'
const cookieStore = await cookies()
after(async () => {
cookieStore.set('illegalCookie', 'too-late-for-that')
})
}
export default async function Page() {
return (
<>
<form action={action}>
<button type="submit">Submit</button>
</form>
</>
)
}
import { cookies } from 'next/headers'
import * as React from 'react'
export const dynamic = 'force-dynamic'
async function action() {
'use server'
// make sure we return an updated version of the page in the response
;(await cookies()).set('pleaseRenderThePage', Date.now() + '')
}
export default async function Page() {
const timestamp = Date.now()
const cookieStore = await cookies()
const canSetCookies = (() => {
try {
cookieStore.set('illegalCookie', 'i-love-side-effects-in-render')
return true
} catch (err) {
// we want assert on the error message, so don't swallow the error
console.error(err)
return false
}
})()
return (
<>
<div id="timestamp">{timestamp}</div>
<div id="canSetCookies">{JSON.stringify(canSetCookies)}</div>
<form action={action}>
<button type="submit">Submit</button>
</form>
</>
)
}
import * as React from 'react'
export default function Page() {
return <>This page does nothing, everything happens in middleware</>
}
import * as React from 'react'
export default function Page() {
return <>This page does nothing, everything happens in middleware</>
}
import * as React from 'react'
export default async function Page() {
return <div>Redirect target</div>
}
import { cookies } from 'next/headers'
import { unstable_after as after } from 'next/server'
export async function POST() {
after(async () => {
const cookieStore = await cookies()
cookieStore.set('illegalCookie', 'too-late-for-that')
})
return new Response('')
}
import { cookies } from 'next/headers'
import { unstable_after as after } from 'next/server'
export async function POST() {
const cookieStore = await cookies()
after(async () => {
cookieStore.set('illegalCookie', 'too-late-for-that')
})
return new Response('')
}
import * as React from 'react'
export default function AppLayout({ children }) {
return (
<html>
<head></head>
<body>{children}</body>
</html>
)
}
/* eslint-env jest */
import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
describe('setting cookies', () => {
const { next, isNextDeploy, skipped } = nextTestSetup({
files: __dirname,
})
if (skipped) return
let currentCliOutputIndex = 0
beforeEach(() => {
resetCliOutput()
})
const getCliOutput = () => {
if (next.cliOutput.length < currentCliOutputIndex) {
// cliOutput shrank since we started the test, so something (like a `sandbox`) reset the logs
currentCliOutputIndex = 0
}
return next.cliOutput.slice(currentCliOutputIndex)
}
const resetCliOutput = () => {
currentCliOutputIndex = next.cliOutput.length
}
const EXPECTED_ERROR =
/Cookies can only be modified in a Server Action or Route Handler\./
const EXPECTED_ERROR_IN_AFTER =
/An error occurred in a function passed to `unstable_after\(\)`: .+?: Cookies can only be modified in a Server Action or Route Handler\./
describe('stops cookie mutations when changing phases', () => {
it('from an action to a page render', async () => {
const path = '/cookies/action-to-render'
const session = await next.browser(path)
const timestamp1 = await session.elementById('timestamp').text()
// .set() should throw during render
expect(await session.elementById('canSetCookies').text()).toEqual('false')
if (!isNextDeploy) {
expect(getCliOutput()).toMatch(EXPECTED_ERROR)
}
// no cookie should be set
expect(await session.eval('document.cookie')).not.toInclude(
'illegalCookie'
)
resetCliOutput()
// trigger an action
await session.elementByCss('[type="submit"]').click()
// wait for page to update as a result
await retry(async () => {
const timestamp2 = await session.elementById('timestamp').text()
expect(timestamp2).not.toEqual(timestamp1)
})
// .set() should throw during render
expect(await session.elementById('canSetCookies').text()).toEqual('false')
if (!isNextDeploy) {
expect(getCliOutput()).toMatch(EXPECTED_ERROR)
}
// no cookie should be set
expect(await session.eval('document.cookie')).not.toInclude(
'illegalCookie'
)
})
// these tests inspect CLI logs to see what happened in unstable_after,
// so they won't work in deploy mode
if (!isNextDeploy) {
it.each([
{
title: 'from an action to unstable_after',
path: '/cookies/action-to-after',
},
{
title: 'from an action to unstable_after via closure',
path: '/cookies/action-to-after/via-closure',
},
])('$title', async ({ path }) => {
const session = await next.browser(path)
// trigger an action
await session.elementByCss('[type="submit"]').click()
await retry(async () => {
// the .set() in unstable_after should error
expect(getCliOutput()).toMatch(EXPECTED_ERROR_IN_AFTER)
})
// no cookie should be set
expect(await session.eval('document.cookie')).not.toInclude(
'illegalCookie'
)
})
it.each([
{
title: 'from a route handler to unstable_after',
path: '/cookies/route-handler-to-after',
},
{
title: 'from a route handler to unstable_after via closure',
path: '/cookies/route-handler-to-after/via-closure',
},
])('$title', async ({ path }) => {
const response = await next.fetch(path, { method: 'POST' })
await response.text()
expect(response.status).toBe(200)
// no cookie should be set
expect(response.headers.get('set-cookie')).toBe(null)
// the .set() in unstable_after should error
await retry(async () => {
expect(getCliOutput()).toMatch(EXPECTED_ERROR_IN_AFTER)
})
})
it.each([
{
title: 'from middleware to unstable_after',
path: '/cookies/middleware-to-after',
},
{
title: 'from middleware to unstable_after via closure',
path: '/cookies/middleware-to-after/via-closure',
},
])('$title', async ({ path }) => {
const response = await next.fetch(path)
await response.text()
expect(response.status).toBe(200)
// no cookie should be set
expect(response.headers.get('set-cookie')).toBe(null)
// the .set() in unstable_after should error
await retry(async () => {
expect(getCliOutput()).toMatch(EXPECTED_ERROR_IN_AFTER)
})
})
}
})
})
import { cookies } from 'next/headers'
import { unstable_after as after, type NextRequest } from 'next/server'
export async function middleware(request: NextRequest) {
const url = new URL(request.url)
if (url.pathname === '/cookies/middleware-to-after') {
after(async () => {
const cookieStore = await cookies()
cookieStore.set('illegalCookie', 'too-late-for-that')
})
} else if (url.pathname === '/cookies/middleware-to-after/via-closure') {
const cookieStore = await cookies()
after(async () => {
cookieStore.set('illegalCookie', 'too-late-for-that')
})
}
}
export const config = {
matcher: ['/cookies/middleware-to-after/:path*'],
}
/** @type {import('next').NextConfig} */
module.exports = {
experimental: {
after: true,
},
}
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