Commit 61b210f8 authored by Jiachi Liu's avatar Jiachi Liu

extract helper, only encode,decode on non-ascii

parent 388859ce
......@@ -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(','))
......
......@@ -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 (/[^\u0000-\u007F]/.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 /[^\u0000-\u007F]/.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 (/[^\u0000-\u007F]/.test(value)) {
if (containsNonAscii(value)) {
return encodeURIComponent(value)
}
return value
......
......@@ -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')
})
})
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