Skip to content

Commit

Permalink
🐛 fix unquote issue with querystring handling. (#1264)
Browse files Browse the repository at this point in the history
* 🐛 fix unquote issue with querystring handling.

* 🎨 fix flake8

* 📝 Add comment regarding query string handling for clarity.

---------

Co-authored-by: shane <[email protected]>
  • Loading branch information
monkut and storkwrangler authored Sep 7, 2023
1 parent 7fba630 commit f0b51ac
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
32 changes: 31 additions & 1 deletion tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2768,7 +2768,37 @@ def test_wsgi_query_string_unquoted(self):
"requestContext": {},
}
request = create_wsgi_request(event)
self.assertEqual(request["QUERY_STRING"], "a=A,B&b=C#D")
expected = "a=A%2CB&b=C%23D" # unencoded result: "a=A,B&b=C#D"
self.assertEqual(request["QUERY_STRING"], expected)

def test_wsgi_query_string_ampersand_unencoded(self):
event = {
"body": None,
"headers": {},
"pathParameters": {},
"path": "/path/path1",
"httpMethod": "GET",
"queryStringParameters": {
"test": "M&M",
},
"requestContext": {},
}
request = create_wsgi_request(event)
self.assertEqual(request["QUERY_STRING"], "test=M%26M")

def test_wsgi_query_string_with_encodechars(self):
event = {
"body": None,
"headers": {},
"pathParameters": {},
"path": "/path/path1",
"httpMethod": "GET",
"queryStringParameters": {"query": "Jane&John", "otherquery": "B", "test": "hello+m.te&how&are&you"},
"requestContext": {},
}
request = create_wsgi_request(event)
expected = "query=Jane%26John&otherquery=B&test=hello%2Bm.te%26how%26are%26you"
self.assertEqual(request["QUERY_STRING"], expected)


if __name__ == "__main__":
Expand Down
5 changes: 4 additions & 1 deletion zappa/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ def create_wsgi_request(
# we have to check for the existence of one and then fall back to the
# other.

# Assumes that the lambda event provides the unencoded string as
# the value in "queryStringParameters"/"multiValueQueryStringParameters"
# The QUERY_STRING value provided to WSGI expects the query string to be properly urlencoded.
# See https://github.com/zappa/Zappa/issues/1227 for discussion of this behavior.
if "multiValueQueryStringParameters" in event_info:
query = event_info["multiValueQueryStringParameters"]
query_string = urlencode(query, doseq=True) if query else ""
else:
query = event_info.get("queryStringParameters", {})
query_string = urlencode(query) if query else ""
query_string = unquote(query_string)

if context_header_mappings:
for key, value in context_header_mappings.items():
Expand Down

0 comments on commit f0b51ac

Please sign in to comment.