Skip to content
Myr

fix(aws-lambda): preserve percent-encoded values in query strings (#4372)

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

authored by Yusuke Wada · committed by GitHub · parent 67f2c8d · Browse files · 2 files+131 added−111 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+131 added−111 removed

  • tests:+126 added−109 removed
  • other:+5 added−2 removed

2 files+131 added−111 removed

modified:src/adapter/aws-lambda/handler.test.ts+126 added-109 removed
1import type { LambdaEvent } from './handler'2import { getProcessor, isContentEncodingBinary, defaultIsContentTypeBinary } from './handler'34// Base event objects to reduce duplication5const baseV1Event: LambdaEvent = {6 version: '1.0',7 resource: '/my/path',8 path: '/my/path',9 httpMethod: 'GET',10 headers: {},11 multiValueHeaders: {},12 queryStringParameters: {},13 requestContext: {14 accountId: '123456789012',15 apiId: 'id',16 authorizer: { claims: null, scopes: null },17 domainName: 'id.execute-api.us-east-1.amazonaws.com',18 domainPrefix: 'id',19 extendedRequestId: 'request-id',20 httpMethod: 'GET',21 identity: {22 sourceIp: '192.0.2.1',23 userAgent: 'user-agent',24 clientCert: {25 clientCertPem: 'CERT_CONTENT',26 subjectDN: 'www.example.com',27 issuerDN: 'Example issuer',28 serialNumber: 'a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1',29 validity: {30 notBefore: 'May 28 12:30:02 2019 GMT',31 notAfter: 'Aug 5 09:36:04 2021 GMT',32 },33 },34 },35 path: '/my/path',36 protocol: 'HTTP/1.1',37 requestId: 'id=',38 requestTime: '04/Mar/2020:19:15:17 +0000',39 requestTimeEpoch: 1583349317135,40 resourcePath: '/my/path',41 stage: '$default',42 },43 pathParameters: {},44 stageVariables: {},45 body: null,46 isBase64Encoded: false,47}4849const baseV2Event: LambdaEvent = {50 version: '2.0',51 routeKey: '$default',52 rawPath: '/my/path',53 rawQueryString: '',54 cookies: [],55 headers: {},56 queryStringParameters: {},57 requestContext: {58 accountId: '123456789012',59 apiId: 'api-id',60 authentication: null,61 authorizer: {},62 domainName: 'id.execute-api.us-east-1.amazonaws.com',63 domainPrefix: 'id',64 http: {65 method: 'POST',66 path: '/my/path',67 protocol: 'HTTP/1.1',68 sourceIp: '192.0.2.1',69 userAgent: 'agent',70 },71 requestId: 'id',72 routeKey: '$default',73 stage: '$default',74 time: '12/Mar/2020:19:03:58 +0000',75 timeEpoch: 1583348638390,76 },77 body: null,78 pathParameters: {},79 isBase64Encoded: false,80 stageVariables: {},81}8283describe('isContentTypeBinary', () => {84 it('Should determine whether it is binary', () => {85 expect(defaultIsContentTypeBinary('image/png')).toBe(true)86 expect(defaultIsContentTypeBinary('font/woff2')).toBe(true)
20 unmodified lines
107 expect(isContentEncodingBinary('unknown')).toBe(false)108 })109})11032describe('EventProcessor.createResult with contentTypsAsBinary', () => {33 const event: LambdaEvent = {34 version: '1.0',35 resource: '/my/path',36 path: '/my/path',37 httpMethod: 'GET',38 headers: {},39 multiValueHeaders: {},40 queryStringParameters: {},41 requestContext: {42 accountId: '123456789012',43 apiId: 'id',44 authorizer: { claims: null, scopes: null },45 domainName: 'id.execute-api.us-east-1.amazonaws.com',46 domainPrefix: 'id',47 extendedRequestId: 'request-id',48 httpMethod: 'GET',49 identity: {50 sourceIp: '192.0.2.1',51 userAgent: 'user-agent',52 clientCert: {53 clientCertPem: 'CERT_CONTENT',54 subjectDN: 'www.example.com',55 issuerDN: 'Example issuer',56 serialNumber: 'a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1',57 validity: {58 notBefore: 'May 28 12:30:02 2019 GMT',59 notAfter: 'Aug 5 09:36:04 2021 GMT',60 },61 },62 },63 path: '/my/path',64 protocol: 'HTTP/1.1',65 requestId: 'id=',66 requestTime: '04/Mar/2020:19:15:17 +0000',67 requestTimeEpoch: 1583349317135,68 resourcePath: '/my/path',69 stage: '$default',70 },71 pathParameters: {},72 stageVariables: {},73 body: null,74 isBase64Encoded: false,75 }111describe('EventProcessor.createResult with contentTypesAsBinary', () => {112 const event = baseV1Event113114 it('Should encode as base64 when content-type is in contentTypesAsBinary array', async () => {115 const processor = getProcessor(event)116 const response = new Response('test content', {
53 unmodified lines
170 })171})172173describe('EventProcessor.createRequest', () => {174 it('Should preserve percent-encoded values in query string for version 1.0', () => {175 const event: LambdaEvent = {176 ...baseV1Event,177 // API Gateway provides decoded values178 multiValueQueryStringParameters: {179 path: ['/book/{bookId}/'], // Originally %7BbookId%7D180 name: ['John Doe'], // Originally John%20Doe181 tag: ['日本語'], // Originally %E6%97%A5%E6%9C%AC%E8%AA%9E182 },183 }184185 const processor = getProcessor(event)186 const request = processor.createRequest(event)187188 // URL should contain properly encoded values189 expect(request.url).toEqual(190 'https://id.execute-api.us-east-1.amazonaws.com/my/path?path=%2Fbook%2F%7BbookId%7D%2F&name=John%20Doe&tag=%E6%97%A5%E6%9C%AC%E8%AA%9E'191 )192 })193194 it('Should handle special characters correctly in queryStringParameters for version 1.0', () => {195 const event: LambdaEvent = {196 ...baseV1Event,197 queryStringParameters: {198 'key with spaces': 'value with spaces',199 'special!@#$%^&*()': 'chars!@#$%^&*()',200 equals: 'a=b=c',201 ampersand: 'a&b&c',202 },203 }204205 const processor = getProcessor(event)206 const request = processor.createRequest(event)207208 // Verify the URL is properly encoded209 const url = new URL(request.url)210 expect(url.searchParams.get('key with spaces')).toBe('value with spaces')211 expect(url.searchParams.get('special!@#$%^&*()')).toBe('chars!@#$%^&*()')212 expect(url.searchParams.get('equals')).toBe('a=b=c')213 expect(url.searchParams.get('ampersand')).toBe('a&b&c')214 })215216 it('Should return valid Request object from version 1.0 API Gateway event', () => {217 const event: LambdaEvent = {139 version: '1.0',140 resource: '/my/path',141 path: '/my/path',142 httpMethod: 'GET',218 ...baseV1Event,219 headers: {220 'content-type': 'application/json',221 header1: 'value1',222 header2: 'value1',
10 unmodified lines
233 multiValueQueryStringParameters: {234 parameter1: ['value1', 'value2'],235 parameter2: ['value'],236 },161 requestContext: {162 accountId: '123456789012',163 apiId: 'id',164 authorizer: {165 claims: null,166 scopes: null,167 },168 domainName: 'id.execute-api.us-east-1.amazonaws.com',169 domainPrefix: 'id',170 extendedRequestId: 'request-id',171 httpMethod: 'GET',172 identity: {173 sourceIp: '192.0.2.1',174 userAgent: 'user-agent',175 clientCert: {176 clientCertPem: 'CERT_CONTENT',177 subjectDN: 'www.example.com',178 issuerDN: 'Example issuer',179 serialNumber: 'a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1',180 validity: {181 notBefore: 'May 28 12:30:02 2019 GMT',182 notAfter: 'Aug 5 09:36:04 2021 GMT',183 },184 },185 },186 path: '/my/path',187 protocol: 'HTTP/1.1',188 requestId: 'id=',189 requestTime: '04/Mar/2020:19:15:17 +0000',190 requestTimeEpoch: 1583349317135,191 resourcePath: '/my/path',192 stage: '$default',193 },194 pathParameters: {},195 stageVariables: {},196 body: null,197 isBase64Encoded: false,237 }238239 const processor = getProcessor(event)240 const request = processor.createRequest(event)241242 expect(request.method).toEqual('GET')243 // Note: Values are now properly encoded244 expect(request.url).toEqual(245 'https://id.execute-api.us-east-1.amazonaws.com/my/path?parameter1=value1&parameter1=value2&parameter2=value'246 )247 expect(Object.fromEntries(request.headers)).toEqual({
4 unmodified lines
252 })253254 it('Should return valid Request object from version 2.0 API Gateway event', () => {255 const event: LambdaEvent = {216 version: '2.0',217 routeKey: '$default',218 rawPath: '/my/path',256 ...baseV2Event,257 rawQueryString: 'parameter1=value1&parameter1=value2&parameter2=value',258 cookies: ['cookie1', 'cookie2'],259 headers: {260 'content-type': 'application/json',
3 unmodified lines
264 queryStringParameters: {265 parameter1: 'value1,value2',266 parameter2: 'value',267 },230 requestContext: {231 accountId: '123456789012',232 apiId: 'api-id',233 authentication: null,234 authorizer: {},235 domainName: 'id.execute-api.us-east-1.amazonaws.com',236 domainPrefix: 'id',237 http: {238 method: 'POST',239 path: '/my/path',240 protocol: 'HTTP/1.1',241 sourceIp: '192.0.2.1',242 userAgent: 'agent',243 },244 requestId: 'id',245 routeKey: '$default',246 stage: '$default',247 time: '12/Mar/2020:19:03:58 +0000',248 timeEpoch: 1583348638390,249 },268 body: 'Hello from Lambda',269 pathParameters: {270 parameter1: 'value1',271 },254 isBase64Encoded: false,272 stageVariables: {273 stageVariable1: 'value1',274 stageVariable2: 'value2',275 },
17 unmodified lines
modified:src/adapter/aws-lambda/handler.ts+5 added-2 removed
381 unmodified lines
382 }383384 protected getQueryString(event: Exclude<LambdaEvent, APIGatewayProxyEventV2>): string {385 // In the case of gateway Integration either queryStringParameters or multiValueQueryStringParameters can be present not both386 // API Gateway passes decoded values, so we need to re-encode them to preserve the original URL387 if (event.multiValueQueryStringParameters) {388 return Object.entries(event.multiValueQueryStringParameters || {})389 .filter(([, value]) => value)389 .map(([key, value]) => `${key}=${value.join(`&${key}=`)}`)390 .map(([key, values]) =>391 values.map((value) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`).join('&')392 )393 .join('&')394 } else {395 return Object.entries(event.queryStringParameters || {})396 .filter(([, value]) => value)394 .map(([key, value]) => `${key}=${value}`)397 .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value || '')}`)398 .join('&')399 }400 }401
160 unmodified lines