Commit 5ee6ebb0 authored by Jiachi Liu's avatar Jiachi Liu

refactor the flow, add tests

parent 41b8e8ea
......@@ -111,7 +111,7 @@ import {
import { BaseServerSpan } from './lib/trace/constants'
import { I18NProvider } from './lib/i18n-provider'
import { sendResponse } from './send-response'
import { normalizeNextQueryParam, decodeHeaderValue } from './web/utils'
import { normalizeNextQueryParam } from './web/utils'
import {
HTML_CONTENT_TYPE_HEADER,
JSON_CONTENT_TYPE_HEADER,
......@@ -944,24 +944,21 @@ export default abstract class Server<
)
// In minimal mode (Vercel), decode headers that were percent-encoded by middleware
// to preserve non-ASCII characters during transport. Only decode headers explicitly
// marked as encoded to avoid affecting other headers with intentional percent-encoding.
// to preserve non-ASCII characters during transport.
if (this.minimalMode) {
const encodedHeadersList = req.headers['x-middleware-request-encoded']
if (typeof encodedHeadersList === 'string') {
const encodedHeaders = encodedHeadersList.split(',')
for (const headerName of encodedHeaders) {
const trimmedName = headerName.trim()
const headerValue = req.headers[trimmedName]
if (typeof headerValue === 'string') {
req.headers[trimmedName] = decodeHeaderValue(headerValue)
} else if (Array.isArray(headerValue)) {
req.headers[trimmedName] = headerValue.map(decodeHeaderValue)
const encodedHeaders = req.headers['x-middleware-request-encoded']
if (typeof encodedHeaders === 'string') {
for (const key of encodedHeaders.split(',')) {
const headerKey = key.trim()
const value = req.headers[headerKey]
if (typeof value === 'string' && /%[0-9A-Fa-f]{2}/.test(value)) {
try {
req.headers[headerKey] = decodeURIComponent(value)
} catch {}
}
}
// Clean up the marker header
delete req.headers['x-middleware-request-encoded']
}
delete req.headers['x-middleware-request-encoded']
}
const urlParts = (req.url || '').split('?', 1)
......
......@@ -14,7 +14,7 @@ import { getCloneableBody } from '../../body-streams'
import { filterReqHeaders, ipcForbiddenHeaders } from '../server-ipc/utils'
import { stringifyQuery } from '../../server-route-utils'
import { formatHostname } from '../format-hostname'
import { toNodeOutgoingHttpHeaders, decodeHeaderValue } from '../../web/utils'
import { toNodeOutgoingHttpHeaders } from '../../web/utils'
import { isAbortError } from '../../pipe-readable'
import { getHostname } from '../../../shared/lib/get-hostname'
import {
......@@ -608,16 +608,10 @@ export function getResolveRoutes(
}
}
// Update or add headers.
// Update or add headers (without decoding yet).
for (const key of overriddenHeaders.keys()) {
const valueKey = 'x-middleware-request-' + key
const encodedValue = middlewareHeaders[valueKey]
const newValue =
encodedValue == null
? undefined
: typeof encodedValue === 'string'
? decodeHeaderValue(encodedValue)
: encodedValue.map(decodeHeaderValue)
const newValue = middlewareHeaders[valueKey]
const oldValue = req.headers[key]
if (oldValue !== newValue) {
......@@ -626,7 +620,24 @@ export function getResolveRoutes(
delete middlewareHeaders[valueKey]
}
// Clean up the encoded headers marker
// Decode only headers that were explicitly marked as encoded.
// This preserves intentional percent-encoding (like %2F, %20) in other headers.
const encodedHeaders =
middlewareHeaders['x-middleware-request-encoded']
if (typeof encodedHeaders === 'string') {
for (const key of encodedHeaders.split(',')) {
const headerKey = key.trim()
const value = req.headers[headerKey]
if (
typeof value === 'string' &&
/%[0-9A-Fa-f]{2}/.test(value)
) {
try {
req.headers[headerKey] = decodeURIComponent(value)
} catch {}
}
}
}
delete middlewareHeaders['x-middleware-request-encoded']
}
......
......@@ -18,6 +18,7 @@ export function fromNodeOutgoingHttpHeaders(
nodeHeaders: OutgoingHttpHeaders
): Headers {
const headers = new Headers()
for (let [key, value] of Object.entries(nodeHeaders)) {
const values = Array.isArray(value) ? value : [value]
for (let v of values) {
......@@ -26,16 +27,11 @@ export function fromNodeOutgoingHttpHeaders(
v = v.toString()
}
// Try to recover from Mojibake (UTF-8 bytes interpreted as Latin-1)
// Try to recover from Mojibake (UTF-8 bytes interpreted as Latin-1).
// This can happen when CDN/proxy sends UTF-8 header bytes but Node.js
// HTTP parser interprets them as Latin-1.
v = decodeMojibake(v)
// Encode non-ASCII characters for safe HTTP header transport.
// HTTP headers can be corrupted at multiple points:
// 1. HTTP/2 expects UTF-8; Latin-1 bytes get replaced with replacement chars
// 2. Some runtimes may not preserve non-ASCII in header values
// Percent-encoding ensures safe ASCII-only transmission.
v = encodeHeaderValue(v)
headers.append(key, v)
}
}
......@@ -213,7 +209,6 @@ export function encodeHeaderValue(value: string): string {
* @returns The decoded header value
*/
export function decodeHeaderValue(value: string): string {
// Check if value contains percent-encoded sequences
if (/%[0-9A-Fa-f]{2}/.test(value)) {
try {
return decodeURIComponent(value)
......
/**
* App Route handler that:
* 1. Reads request headers (tests NextRequest header conversion)
* 2. Sets response headers with non-ASCII values (tests outgoing header handling)
*/
export async function GET(request) {
// Read incoming request headers
const requestHeaders = {}
for (const [key, value] of request.headers.entries()) {
if (key.startsWith('x-')) {
requestHeaders[key] = value
}
}
// Create response with non-ASCII header values
const response = new Response(JSON.stringify(requestHeaders), {
headers: {
'Content-Type': 'application/json',
// Set non-ASCII response headers to verify they work
'x-response-city': 'Montréal',
'x-response-country': 'Österreich',
},
})
return response
}
export default function RootLayout({ children }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return <div>Test page</div>
}
......@@ -35,6 +35,7 @@ describe('Middleware Non-ASCII Headers', () => {
next = await createNext({
files: {
pages: new FileRef(join(__dirname, 'app/pages')),
app: new FileRef(join(__dirname, 'app/app')),
'next.config.js': new FileRef(join(__dirname, 'app/next.config.js')),
'middleware.js': new FileRef(join(__dirname, 'app/middleware.js')),
},
......@@ -265,4 +266,74 @@ describe('Middleware Non-ASCII Headers', () => {
expect(headers['x-number']).toBe('12345')
expect(headers['x-special']).toBe('test_value-123')
})
// ============================================================
// App Route Handler Tests
// These test fromNodeOutgoingHttpHeaders in NextRequest adapter
// ============================================================
describe('App Route Handler', () => {
// App Route handlers should see properly decoded headers (same as middleware)
// fromNodeOutgoingHttpHeaders applies mojibake recovery without encoding
it('should read non-ASCII request headers correctly in route handler', async () => {
// Test that App Route handlers can read mojibake headers correctly
// fromNodeOutgoingHttpHeaders decodes mojibake but doesn't encode by default
const res = await fetchViaHTTP(
next.url,
'/api/route-headers',
{},
{
headers: {
'x-city': toMojibake('Montréal'),
'x-country': toMojibake('Österreich'),
},
}
)
expect(res.status).toBe(200)
const body = await res.json()
// Headers should be decoded correctly (mojibake recovered to UTF-8)
expect(body['x-city']).toBe('Montréal')
expect(body['x-country']).toBe('Österreich')
})
// Response headers with non-ASCII values arrive as mojibake
// (UTF-8 bytes interpreted as Latin-1 by HTTP transport)
it('should show that response headers with non-ASCII arrive as mojibake', async () => {
// Test that route handlers setting non-ASCII response headers
// have those headers arrive as mojibake to the client
const res = await fetchViaHTTP(next.url, '/api/route-headers', {}, {})
expect(res.status).toBe(200)
// Response headers with non-ASCII values get mangled during HTTP transport
// UTF-8 bytes are interpreted as Latin-1 (mojibake)
const cityHeader = res.headers.get('x-response-city')
const countryHeader = res.headers.get('x-response-country')
// These arrive as mojibake: "Montréal" → "Montréal", "Österreich" → "Ãsterreich"
expect(cityHeader).toBe(toMojibake('Montréal'))
expect(countryHeader).toBe(toMojibake('Österreich'))
})
it('should preserve intentional percent-encoding in route handler', async () => {
// Test that intentional percent-encoding is not double-encoded or decoded
const res = await fetchViaHTTP(
next.url,
'/api/route-headers',
{},
{
headers: {
'x-path': '/hello%2Fworld',
'x-query': 'foo%20bar',
},
}
)
expect(res.status).toBe(200)
const body = await res.json()
// Intentional percent-encoding should be preserved
expect(body['x-path']).toBe('/hello%2Fworld')
expect(body['x-query']).toBe('foo%20bar')
})
})
})
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