Skip to content
Myr

fix(aws-lambda): sanitize non-ASCII header values to prevent ByteString errors (#4437)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(aws-lambda): sanitize non-ASCII header values to prevent ByteString errors * ignore eslint errors * simplify and use `encodeURIComponent`

authored by Yusuke Wada · committed by GitHub · parent e3bf8c8 · Browse files · 2 files+37 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+37 added−4 removed

  • tests:+17 added−0 removed
  • other:+20 added−4 removed

2 files+37 added−4 removed

modified:src/adapter/aws-lambda/handler.test.ts+17 added
287 unmodified lines
288 header1: 'value1',289 header2: 'value1,value2',290 })291 })292293 describe('non-ASCII header value processing', () => {294 it('Should encode non-ASCII header values with encodeURIComponent', async () => {295 const event: LambdaEvent = {296 ...baseV1Event,297 headers: {298 'x-city': '炎', // Non-ASCII character299 },300 }301302 const processor = getProcessor(event)303 const request = processor.createRequest(event)304305 const xCity = request.headers.get('x-city') ?? ''306 expect(decodeURIComponent(xCity)).toBe('炎')307 })308 })309})
modified:src/adapter/aws-lambda/handler.ts+20 added-4 removed
7 unmodified lines
8 Handler,9 LambdaContext,10} from './types'1112function sanitizeHeaderValue(value: string): string {13 // Check if the value contains non-ASCII characters (char codes > 127)14 // eslint-disable-next-line no-control-regex15 const hasNonAscii = /[^\x00-\x7F]/.test(value)16 if (!hasNonAscii) {17 return value18 }19 return encodeURIComponent(value)20}2122export type LambdaEvent = APIGatewayProxyEvent | APIGatewayProxyEventV2 | ALBProxyEvent2324// When calling HTTP API or Lambda directly through function urls25export interface APIGatewayProxyEventV2 {
397 unmodified lines
423 this.getCookies(event, headers)424 if (event.headers) {425 for (const [k, v] of Object.entries(event.headers)) {426 if (v) {417 headers.set(k, v)427 headers.set(k, sanitizeHeaderValue(v))428 }429 }430 }431 if (event.multiValueHeaders) {432 for (const [k, values] of Object.entries(event.multiValueHeaders)) {433 if (values) {434 // avoid duplicating already set headers435 const foundK = headers.get(k)426 values.forEach((v) => (!foundK || !foundK.includes(v)) && headers.append(k, v))436 values.forEach((v) => {437 const sanitizedValue = sanitizeHeaderValue(v)438 return (439 (!foundK || !foundK.includes(sanitizedValue)) && headers.append(k, sanitizedValue)440 )441 })442 }443 }444 }445 return headers
16 unmodified lines
462 if (event.multiValueHeaders) {463 for (const [key, values] of Object.entries(event.multiValueHeaders)) {464 if (values && Array.isArray(values)) {465 // https://www.rfc-editor.org/rfc/rfc9110.html#name-common-rules-for-defining-f451 headers.set(key, values.join('; '))466 const sanitizedValue = sanitizeHeaderValue(values.join('; '))467 headers.set(key, sanitizedValue)468 }469 }470 } else {471 for (const [key, value] of Object.entries(event.headers ?? {})) {472 if (value) {457 headers.set(key, value)473 headers.set(key, sanitizeHeaderValue(value))474 }475 }476 }477 return headers
100 unmodified lines