serverless-express icon indicating copy to clipboard operation
serverless-express copied to clipboard

Double encoding of query

Open karlismelderis opened this issue 5 years ago • 3 comments

seems that query is encoded one more time when hitting express. I had to add this peace of code before passing event to module:

exports.handler = (event, context) => {
    Object.keys(event.queryStringParameters).forEach((key) => {
        // eslint-disable-next-line no-param-reassign
        event.queryStringParameters[key] = decodeURIComponent(event.queryStringParameters[key]);
    });
    awsServerlessExpress.proxy(server, event, context);
};

karlismelderis avatar Mar 19 '19 15:03 karlismelderis

Thanks. I'll need to verify this and fix as part of v4.

brettstack avatar May 10 '19 17:05 brettstack

Hi, just saw this. I think it's the same problem I described in #241 .

@karlismelderis were you using Application Load Balancer by any chance?

nabilfreeman avatar Jul 01 '19 15:07 nabilfreeman

I'd just like to chime in here and say I'm also running into this as an issue. In additional to the decoding, it's important to note that the + character has special behaviour in query strings - it decodes to a space character, but not if you use decodeURIComponent, so in the example above you'll still be getting + characters back when you shouldn't.

Here's the solution I've been using, which effectively rebuilds the whole URL and then parses it again using the querystring module:

const querystring = require('querystring');

exports.handler = (event, context) => {
  const qs = Object.keys(event.queryStringParameters).map(key => key + '=' + event.queryStringParameters[key]).join('&');
  event.queryStringParameters = querystring.parse(qs);

  ase.proxy(server, event, context);
};

That said, I'm pretty sure the behaviour of not decoding keys and values is a bug, both because of how it's inconsistent with API Gateway, and how it can't cope with certain characters (e.g. ?foo%25bar=baz apparently decodes as { foo: "" } which is obviously wrong) so perhaps this needs to be addressed at a higher level inside the ALB-Lambda handler itself rather than inside this module

jbt avatar Aug 21 '19 15:08 jbt