From 8cb4206103e730e61a3acb38fceba4cc9c355d91 Mon Sep 17 00:00:00 2001 From: fredericgermain Date: Wed, 9 Jun 2021 22:17:49 +0200 Subject: [PATCH] fix: auto-unescape query parameters on ALB (#219, #241) (#393) --- src/event-sources/aws/alb.js | 38 +++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/event-sources/aws/alb.js b/src/event-sources/aws/alb.js index 9edf1fc7..508588d8 100644 --- a/src/event-sources/aws/alb.js +++ b/src/event-sources/aws/alb.js @@ -1,6 +1,42 @@ +const url = require('url') const { getRequestValuesFromEvent, getMultiValueHeaders } = require('../utils') -const getRequestValuesFromAlbEvent = ({ event }) => getRequestValuesFromEvent({ event }) +function getPathWithQueryStringUseUnescapeParams ({ + event, + // NOTE: Use `event.pathParameters.proxy` if available ({proxy+}); fall back to `event.path` + path = (event.pathParameters && event.pathParameters.proxy && `/${event.pathParameters.proxy}`) || event.path, + // NOTE: Strip base path for custom domains + stripBasePath = '', + replaceRegex = new RegExp(`^${stripBasePath}`) +}) { + const query = {} + // decode everything back into utf-8 text. + if (event.multiValueQueryStringParameters) { + for (const key in event.multiValueQueryStringParameters) { + const formattedKey = decodeURIComponent(key) + query[formattedKey] = event.multiValueQueryStringParameters[key].map(value => decodeURIComponent(value)) + } + } else { + for (const key in event.queryStringParameters) { + const formattedKey = decodeURIComponent(key) + query[formattedKey] = decodeURIComponent(event.queryStringParameters[key]) + } + } + + return url.format({ + pathname: path.replace(replaceRegex, ''), + query + }) +} + +const getRequestValuesFromAlbEvent = ({ event }) => { + const values = getRequestValuesFromEvent({ + event, + path: getPathWithQueryStringUseUnescapeParams({ event }) + }) + return values +} + const getResponseToAlb = ({ statusCode, body,