Commit b02ac1bd authored by Jiachi Liu's avatar Jiachi Liu

try consistent encode/decode

parent 6f028440
......@@ -943,23 +943,17 @@ export default abstract class Server<
isNodeNextResponse(res) ? res.originalResponse : res
)
// In minimal mode (Vercel), decode headers that were percent-encoded by middleware
// to preserve non-ASCII characters during transport
// In minimal mode (Vercel), decode percent-encoded header values.
// Middleware encodes non-ASCII characters for safe HTTP transport,
// and we decode them here to restore the original values.
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)
}
for (const key of Object.keys(req.headers)) {
const value = req.headers[key]
if (typeof value === 'string') {
req.headers[key] = decodeHeaderValue(value)
} else if (Array.isArray(value)) {
req.headers[key] = value.map(decodeHeaderValue)
}
// Clean up the marker header
delete req.headers['x-middleware-request-encoded']
}
}
......
......@@ -45,7 +45,6 @@ const INTERNAL_HEADERS = [
'x-middleware-set-cookie',
'x-middleware-skip',
'x-middleware-override-headers',
'x-middleware-request-encoded',
'x-middleware-next',
'x-now-route-matches',
'x-matched-path',
......
......@@ -5,8 +5,6 @@ import {
toNodeOutgoingHttpHeaders,
validateURL,
encodeHeaderValue,
decodeHeaderValue,
containsNonAscii,
} from '../utils'
import { ReflectAdapter } from './adapters/reflect'
......@@ -25,30 +23,14 @@ function handleMiddlewareField(
}
const keys = []
const encodedKeys = []
for (const [key, value] of init.request.headers) {
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)
if (containsNonAscii(decodedValue)) {
finalValue = encodeHeaderValue(decodedValue)
encodedKeys.push(key)
}
headers.set('x-middleware-request-' + key, finalValue)
// Encode non-ASCII characters for safe HTTP header transmission.
// If value is already percent-encoded (all ASCII), it passes through unchanged.
headers.set('x-middleware-request-' + key, encodeHeaderValue(value))
keys.push(key)
}
headers.set('x-middleware-override-headers', keys.join(','))
// Add a marker header listing which headers have percent-encoded values
// This allows the server to decode them in minimal mode (Vercel)
if (encodedKeys.length > 0) {
headers.set('x-middleware-request-encoded', encodedKeys.join(','))
}
}
}
......
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