Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5791cb6

Browse files
ztannerijjk
andauthored
[Backport v14] add additional x-middleware-set-cookie filtering (#75561) (#75870)
Backports:-#75561-#73482---------Co-authored-by: JJ Kasper <jj@jjsweb.site>
1 parent8129a61 commit5791cb6

File tree

13 files changed

+83
-0
lines changed

13 files changed

+83
-0
lines changed

‎packages/next/src/server/lib/router-server.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import { getNextPathnameInfo } from '../../shared/lib/router/utils/get-next-path
4141
import{getHostname}from'../../shared/lib/get-hostname'
4242
import{detectDomainLocale}from'../../shared/lib/i18n/detect-domain-locale'
4343
import{normalizedAssetPrefix}from'../../shared/lib/normalized-asset-prefix'
44+
import{filterInternalHeaders}from'./server-ipc/utils'
4445

4546
constdebug=setupDebug('next:router-server:main')
4647
constisNextFont=(pathname:string|null)=>
@@ -149,6 +150,11 @@ export async function initialize(opts: {
149150
require('./render-server')astypeofimport('./render-server')
150151

151152
constrequestHandlerImpl:WorkerRequestHandler=async(req,res)=>{
153+
// internal headers should not be honored by the request handler
154+
if(!process.env.NEXT_PRIVATE_TEST_HEADERS){
155+
filterInternalHeaders(req.headers)
156+
}
157+
152158
if(
153159
!opts.minimalMode&&
154160
config.i18n&&

‎packages/next/src/server/lib/router-utils/resolve-routes.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,14 @@ export function getResolveRoutes(
589589
){
590590
continue
591591
}
592+
593+
// for set-cookie, the header shouldn't be added to the response
594+
// as it's only needed for the request to the middleware function.
595+
if(key==='x-middleware-set-cookie'){
596+
req.headers[key]=value
597+
continue
598+
}
599+
592600
if(value){
593601
resHeaders[key]=value
594602
req.headers[key]=value

‎packages/next/src/server/lib/server-ipc/utils.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,26 @@ export const filterReqHeaders = (
3636
}
3737
returnheadersasRecord<string,undefined|string|string[]>
3838
}
39+
40+
// These are headers that are only used internally and should
41+
// not be honored from the external request
42+
constINTERNAL_HEADERS=[
43+
'x-middleware-rewrite',
44+
'x-middleware-redirect',
45+
'x-middleware-set-cookie',
46+
'x-middleware-skip',
47+
'x-middleware-override-headers',
48+
'x-middleware-next',
49+
'x-now-route-matches',
50+
'x-matched-path',
51+
]
52+
53+
exportconstfilterInternalHeaders=(
54+
headers:Record<string,undefined|string|string[]>
55+
)=>{
56+
for(constheaderinheaders){
57+
if(INTERNAL_HEADERS.includes(header)){
58+
deleteheaders[header]
59+
}
60+
}
61+
}

‎packages/next/src/server/send-response.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ export async function sendResponse(
2525

2626
// Copy over the response headers.
2727
response.headers?.forEach((value,name)=>{
28+
// `x-middleware-set-cookie` is an internal header not needed for the response
29+
if(name.toLowerCase()==='x-middleware-set-cookie'){
30+
return
31+
}
32+
2833
// The append handling is special cased for `set-cookie`.
2934
if(name.toLowerCase()==='set-cookie'){
3035
// TODO: (wyattjoh) replace with native response iteration when we can upgrade undici

‎test/e2e/app-dir/app-middleware/app-middleware.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,18 @@ createNextDescribe(
174174
awaitbrowser.deleteCookies()
175175
})
176176

177+
it('should omit internal headers for middleware cookies',async()=>{
178+
constresponse=awaitnext.fetch('/rsc-cookies/cookie-options')
179+
expect(response.status).toBe(200)
180+
expect(response.headers.get('x-middleware-set-cookie')).toBeNull()
181+
182+
constresponse2=awaitnext.fetch('/cookies/api')
183+
expect(response2.status).toBe(200)
184+
expect(response2.headers.get('x-middleware-set-cookie')).toBeNull()
185+
expect(response2.headers.get('set-cookie')).toBeDefined()
186+
expect(response2.headers.get('set-cookie')).toContain('example')
187+
})
188+
177189
it('should respect cookie options of merged middleware cookies',async()=>{
178190
constbrowser=awaitnext.browser('/rsc-cookies/cookie-options')
179191

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import{NextResponse}from'next/server'
2+
3+
exportfunctionGET(){
4+
constresponse=newNextResponse()
5+
response.cookies.set({
6+
name:'example',
7+
value:'example',
8+
})
9+
10+
returnresponse
11+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import{cookies}from'next/headers'
2+
3+
exportdefaultasyncfunctionPage(){
4+
constcookieLength=(awaitcookies()).size
5+
return<divid="cookies">cookies:{cookieLength}</div>
6+
}

‎test/integration/required-server-files-ssr-404/test/index.test.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ describe('Required Server Files', () => {
4444
}
4545
awaitfs.rename(join(appDir,'pages'),join(appDir,'pages-bak'))
4646

47+
process.env.NEXT_PRIVATE_TEST_HEADERS='1'
4748
nextApp=nextServer({
4849
conf:{},
4950
dir:appDir,
@@ -57,6 +58,7 @@ describe('Required Server Files', () => {
5758
console.log(`Listening at ::${appPort}`)
5859
})
5960
afterAll(async()=>{
61+
deleteprocess.env.NEXT_PRIVATE_TEST_HEADERS
6062
if(server)server.close()
6163
awaitfs.rename(join(appDir,'pages-bak'),join(appDir,'pages'))
6264
})

‎test/production/standalone-mode/required-server-files/required-server-files-app.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ describe('required server files app router', () => {
2525
})=>{
2626
// test build against environment with next support
2727
process.env.NOW_BUILDER=nextEnv ?'1' :''
28+
process.env.NEXT_PRIVATE_TEST_HEADERS='1'
2829

2930
next=awaitcreateNext({
3031
files:{
@@ -96,6 +97,7 @@ describe('required server files app router', () => {
9697
awaitsetupNext({nextEnv:true,minimalMode:true})
9798
})
9899
afterAll(async()=>{
100+
deleteprocess.env.NEXT_PRIVATE_TEST_HEADERS
99101
awaitnext.destroy()
100102
if(server)awaitkillApp(server)
101103
})

‎test/production/standalone-mode/required-server-files/required-server-files-i18n.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ describe('required server files i18n', () => {
2424

2525
beforeAll(async()=>{
2626
letwasmPkgIsAvailable=false
27+
process.env.NEXT_PRIVATE_TEST_HEADERS='1'
2728

2829
constres=awaitnodeFetch(
2930
`https://registry.npmjs.com/@next/swc-wasm-nodejs/-/swc-wasm-nodejs-${
@@ -128,6 +129,7 @@ describe('required server files i18n', () => {
128129
)
129130
})
130131
afterAll(async()=>{
132+
deleteprocess.env.NEXT_PRIVATE_TEST_HEADERS
131133
awaitnext.destroy()
132134
if(server)awaitkillApp(server)
133135
})

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp