Commit 05c44d52 authored by Jiachi Liu's avatar Jiachi Liu

still need encoded keys list

parent 505ef11a
......@@ -943,19 +943,24 @@ export default abstract class Server<
isNodeNextResponse(res) ? res.originalResponse : res
)
// 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.
// Note: decodeHeaderValue is safe - it only decodes valid percent-encoded
// sequences and returns the original value if decoding fails.
// 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.
if (this.minimalMode) {
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)
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)
}
}
// Clean up the marker header
delete req.headers['x-middleware-request-encoded']
}
}
......
......@@ -45,6 +45,7 @@ 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',
......
......@@ -23,14 +23,24 @@ function handleMiddlewareField(
}
const keys = []
const encodedKeys = []
for (const [key, value] of init.request.headers) {
// 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))
const encodedValue = encodeHeaderValue(value)
headers.set('x-middleware-request-' + key, encodedValue)
keys.push(key)
// Track headers that were actually encoded (value changed)
if (encodedValue !== value) {
encodedKeys.push(key)
}
}
headers.set('x-middleware-override-headers', keys.join(','))
// Add marker for headers that were percent-encoded, so server knows which to decode
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