From 10d70bfe77b0bd1a140ee3334e6a997d18f960f7 Mon Sep 17 00:00:00 2001 From: Athanasius Date: Fri, 7 Jan 2022 16:45:37 +0000 Subject: [PATCH] Gateway: Properly report 'not compressed, badly form-encoded' to uploaders * This code worked if the request was *properly* form-encoded, with a 'data' key whose value was a valid message. * It failed to detect where the request was form-encoded, with without a 'data' key. It would just assume 'not form-encoded' in that case, then fail later on JSON parsing. Thus, re-use the `urlparse.parse_qs()` check for form-encoded format. This passes: 1. Properly, `data` key, form-encoded with valid value is fully JSON parsed, schema checked and accepted. 2. *NOT* compressed *or* form-encoded valid message is properly parsed and accepted. 2. Uncompressed, form-encoded, but no `data` key correctly returns the same error status and body as the compressed+form-encoded+no data key path. --- src/eddn/Gateway.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/eddn/Gateway.py b/src/eddn/Gateway.py index 90e8ac1..091088c 100644 --- a/src/eddn/Gateway.py +++ b/src/eddn/Gateway.py @@ -164,13 +164,28 @@ def get_decompressed_message(): logger.debug('Request is *NOT* form-encoded') else: - # Uncompressed request. Bottle handles all of the parsing of the - # POST key/vals, or un-encoded body. - data_key = request.forms.get('data') - if data_key: - # This is a form-encoded POST. Support the silly people. - message_body = data_key + logger.debug('Content-Encoding indicates *not* compressed...') + + form_enc_parsed = urlparse.parse_qs(request.body.read()) + if form_enc_parsed: + logger.debug('Request is form-encoded') + + # Uncompressed request. Bottle handles all of the parsing of the + # POST key/vals, or un-encoded body. + data_key = request.forms.get('data') + if data_key: + logger.debug('form-encoded POST request detected...') + # This is a form-encoded POST. Support the silly people. + message_body = data_key + + else: + raise MalformedUploadError( + "No 'data' POST key/value found. Check your POST key " + "name for spelling, and make sure you're passing a value." + ) + else: + logger.debug('Plain POST request detected...') # This is a non form-encoded POST body. message_body = request.body.read()