Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
61b210f8
Commit
61b210f8
authored
Jan 09, 2026
by
Jiachi Liu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
extract helper, only encode,decode on non-ascii
parent
388859ce
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
56 additions
and
15 deletions
+56
-15
response.ts
packages/next/src/server/web/spec-extension/response.ts
+12
-7
utils.ts
packages/next/src/server/web/utils.ts
+14
-8
middleware-non-ascii-headers.test.ts
...re-non-ascii-headers/middleware-non-ascii-headers.test.ts
+30
-0
No files found.
packages/next/src/server/web/spec-extension/response.ts
View file @
61b210f8
...
...
@@ -6,6 +6,7 @@ import {
validateURL
,
encodeHeaderValue
,
decodeHeaderValue
,
containsNonAscii
,
}
from
'../utils'
import
{
ReflectAdapter
}
from
'./adapters/reflect'
...
...
@@ -26,16 +27,20 @@ function handleMiddlewareField(
const
keys
=
[]
const
encodedKeys
=
[]
for
(
const
[
key
,
value
]
of
init
.
request
.
headers
)
{
// First decode any values that were percent-encoded by fromNodeOutgoingHttpHeaders
// to preserve non-ASCII characters, then re-encode for the middleware header
let
finalValue
=
value
// Decode to check if there are non-ASCII characters that need encoding.
// Only apply the decode-encode transformation if the decoded value actually
// contains non-ASCII characters. This avoids changing headers that intentionally
// contain percent-encoded ASCII characters (e.g., %20 for space).
const
decodedValue
=
decodeHeaderValue
(
value
)
const
encodedValue
=
encodeHeaderValue
(
decodedValue
)
headers
.
set
(
'x-middleware-request-'
+
key
,
encodedValue
)
keys
.
push
(
key
)
// Track headers that were encoded (value changed after encoding)
if
(
encodedValue
!==
decodedValue
)
{
if
(
containsNonAscii
(
decodedValue
))
{
finalValue
=
encodeHeaderValue
(
decodedValue
)
encodedKeys
.
push
(
key
)
}
headers
.
set
(
'x-middleware-request-'
+
key
,
finalValue
)
keys
.
push
(
key
)
}
headers
.
set
(
'x-middleware-override-headers'
,
keys
.
join
(
','
))
...
...
packages/next/src/server/web/utils.ts
View file @
61b210f8
...
...
@@ -31,11 +31,7 @@ export function fromNodeOutgoingHttpHeaders(
// Encode non-ASCII characters to prevent corruption by the Headers API.
// The Headers API in edge runtime doesn't properly preserve Latin-1 characters.
// eslint-disable-next-line no-control-regex
if
(
/
[^\u
0000-
\u
007F
]
/
.
test
(
v
))
{
// Encode non-ASCII as percent-encoded UTF-8
v
=
encodeURIComponent
(
v
)
}
v
=
encodeHeaderValue
(
v
)
headers
.
append
(
key
,
v
)
}
...
...
@@ -177,6 +173,18 @@ export function normalizeNextQueryParam(key: string): null | string {
return
null
}
/**
* Checks if a string contains any non-ASCII characters (code points > 127).
* ASCII characters are in the range U+0000 to U+007F.
*
* @param value - The string to check
* @returns true if the string contains non-ASCII characters
*/
export
function
containsNonAscii
(
value
:
string
):
boolean
{
// eslint-disable-next-line no-control-regex
return
/
[^\u
0000-
\u
007F
]
/
.
test
(
value
)
}
/**
* Encodes header values containing non-ASCII characters for safe HTTP transmission.
* HTTP headers must be ASCII-safe, so non-ASCII characters are percent-encoded.
...
...
@@ -185,9 +193,7 @@ export function normalizeNextQueryParam(key: string): null | string {
* @returns The encoded header value
*/
export
function
encodeHeaderValue
(
value
:
string
):
string
{
// Check if the value contains non-ASCII characters
// eslint-disable-next-line no-control-regex
if
(
/
[^\u
0000-
\u
007F
]
/
.
test
(
value
))
{
if
(
containsNonAscii
(
value
))
{
return
encodeURIComponent
(
value
)
}
return
value
...
...
test/e2e/middleware-non-ascii-headers/middleware-non-ascii-headers.test.ts
View file @
61b210f8
...
...
@@ -186,4 +186,34 @@ describe('Middleware Non-ASCII Headers', () => {
const
headers
=
await
res
.
json
()
expect
(
headers
[
'x-city'
]).
toBe
(
'São Paulo'
)
})
// Test for LOCAL scenario: when headers are sent as proper Latin-1 (not Mojibake)
// This happens when:
// - A local proxy (nginx, etc.) adds headers with non-ASCII
// - Node.js fetch/http client sends headers (converts to Latin-1)
// Unlike CDN scenario, these arrive as single-byte Latin-1, not UTF-8 Mojibake
it
(
'should handle Latin-1 encoded headers (local proxy scenario)'
,
async
()
=>
{
// When fetchViaHTTP sends "Montréal", it encodes as Latin-1:
// "é" becomes single byte 0xE9 (not UTF-8's 0xC3 0xA9)
// This simulates a local proxy adding headers
const
res
=
await
fetchViaHTTP
(
next
.
url
,
'/api/headers'
,
{},
{
headers
:
{
'x-city'
:
'Montréal'
,
// Latin-1: é = 0xE9
'x-region'
:
'Île-de-France'
,
'x-country'
:
'Österreich'
,
},
}
)
expect
(
res
.
status
).
toBe
(
200
)
const
headers
=
await
res
.
json
()
// These should be preserved correctly through middleware
expect
(
headers
[
'x-city'
]).
toBe
(
'Montréal'
)
expect
(
headers
[
'x-region'
]).
toBe
(
'Île-de-France'
)
expect
(
headers
[
'x-country'
]).
toBe
(
'Österreich'
)
})
})
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment