Skip to content

Commit

Permalink
Implement safe parsing when body is not base64-encoded
Browse files Browse the repository at this point in the history
  • Loading branch information
selectiveduplicate committed Sep 27, 2024
1 parent 924a988 commit bd7d31b
Show file tree
Hide file tree
Showing 8 changed files with 391 additions and 35 deletions.
34 changes: 29 additions & 5 deletions moesif_aws_lambda/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,34 @@ def base64_body(cls, data):
return str(body, "utf-8"), 'base64'
else:
return str(body), 'base64'

def safe_json_parse(self, body):
"""Tries to parse the `body` as JSON safely.
Returns the formatted body and the appropriate `transfer_encoding`.
"""
try:
if isinstance(body, (dict, list)):
# If body is an instance of either a dictionary of list,
# we can return it as is.
return body, None
"""
If body is neither dictionary or list, it has to be one of these types:
- binary data (`bytes` object in Python)
- a string
- a non-string type object like an integer or float
"""
body_str = None
if isinstance(body, bytes):
body_str = body.decode()
else:
body_str = str(body)

# Now we try to parse the string as JSON
json_body = json.loads(body_str)
return json_body, None

except (json.JSONDecodeError, TypeError, ValueError, UnicodeError) as error:
return self.base64_body(body)

def process_body(self, body_wrapper):
"""Function to process body"""
Expand All @@ -220,11 +248,7 @@ def process_body(self, body_wrapper):
body = body_wrapper.get('body')
transfer_encoding = 'base64'
else:
if isinstance(body_wrapper['body'], str):
body = json.loads(body_wrapper.get('body'))
else:
body = body_wrapper.get('body')
transfer_encoding = 'json'
body, transfer_encoding = self.safe_json_parse(body_wrapper.get('body'))
except Exception as e:
return self.base64_body(body_wrapper['body'])

Expand Down
30 changes: 0 additions & 30 deletions moesif_aws_lambda/tests.py

This file was deleted.

Empty file.
69 changes: 69 additions & 0 deletions moesif_aws_lambda/tests/event_body_dict.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"version": "2.0",
"routeKey": "$default",
"rawPath": "/path/to/resource",
"rawQueryString": "parameter1=value1&parameter1=value2&parameter2=value",
"cookies": [
"cookie1",
"cookie2"
],
"headers": {
"Header1": "value1",
"Header2": "value1,value2"
},
"queryStringParameters": {
"parameter1": "value1,value2",
"parameter2": "value"
},
"requestContext": {
"accountId": "123456789012",
"apiId": "api-id",
"authentication": {
"clientCert": {
"clientCertPem": "CERT_CONTENT",
"subjectDN": "www.example.com",
"issuerDN": "Example issuer",
"serialNumber": "a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1",
"validity": {
"notBefore": "May 28 12:30:02 2019 GMT",
"notAfter": "Aug 5 09:36:04 2021 GMT"
}
}
},
"authorizer": {
"jwt": {
"claims": {
"claim1": "value1",
"claim2": "value2"
},
"scopes": [
"scope1",
"scope2"
]
}
},
"domainName": "id.execute-api.us-east-1.amazonaws.com",
"domainPrefix": "id",
"http": {
"method": "POST",
"path": "/path/to/resource",
"protocol": "HTTP/1.1",
"sourceIp": "192.168.0.1/32",
"userAgent": "agent"
},
"requestId": "id",
"routeKey": "$default",
"stage": "$default",
"time": "27/Sep/2024:09:06:03 +0000",
"timeEpoch": 1727427963
},
"body": {"foo": "bar"},
"pathParameters": {
"parameter1": "value1"
},
"isBase64Encoded": true,
"stageVariables": {
"stageVariable1": "value1",
"stageVariable2": "value2"
}
}
69 changes: 69 additions & 0 deletions moesif_aws_lambda/tests/event_body_int.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"version": "2.0",
"routeKey": "$default",
"rawPath": "/path/to/resource",
"rawQueryString": "parameter1=value1&parameter1=value2&parameter2=value",
"cookies": [
"cookie1",
"cookie2"
],
"headers": {
"Header1": "value1",
"Header2": "value1,value2"
},
"queryStringParameters": {
"parameter1": "value1,value2",
"parameter2": "value"
},
"requestContext": {
"accountId": "123456789012",
"apiId": "api-id",
"authentication": {
"clientCert": {
"clientCertPem": "CERT_CONTENT",
"subjectDN": "www.example.com",
"issuerDN": "Example issuer",
"serialNumber": "a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1",
"validity": {
"notBefore": "May 28 12:30:02 2019 GMT",
"notAfter": "Aug 5 09:36:04 2021 GMT"
}
}
},
"authorizer": {
"jwt": {
"claims": {
"claim1": "value1",
"claim2": "value2"
},
"scopes": [
"scope1",
"scope2"
]
}
},
"domainName": "id.execute-api.us-east-1.amazonaws.com",
"domainPrefix": "id",
"http": {
"method": "POST",
"path": "/path/to/resource",
"protocol": "HTTP/1.1",
"sourceIp": "192.168.0.1/32",
"userAgent": "agent"
},
"requestId": "id",
"routeKey": "$default",
"stage": "$default",
"time": "27/Sep/2024:09:06:03 +0000",
"timeEpoch": 1727427963
},
"body": 10,
"pathParameters": {
"parameter1": "value1"
},
"isBase64Encoded": true,
"stageVariables": {
"stageVariable1": "value1",
"stageVariable2": "value2"
}
}
69 changes: 69 additions & 0 deletions moesif_aws_lambda/tests/event_body_json.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"version": "2.0",
"routeKey": "$default",
"rawPath": "/path/to/resource",
"rawQueryString": "parameter1=value1&parameter1=value2&parameter2=value",
"cookies": [
"cookie1",
"cookie2"
],
"headers": {
"Header1": "value1",
"Header2": "value1,value2"
},
"queryStringParameters": {
"parameter1": "value1,value2",
"parameter2": "value"
},
"requestContext": {
"accountId": "123456789012",
"apiId": "api-id",
"authentication": {
"clientCert": {
"clientCertPem": "CERT_CONTENT",
"subjectDN": "www.example.com",
"issuerDN": "Example issuer",
"serialNumber": "a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1",
"validity": {
"notBefore": "May 28 12:30:02 2019 GMT",
"notAfter": "Aug 5 09:36:04 2021 GMT"
}
}
},
"authorizer": {
"jwt": {
"claims": {
"claim1": "value1",
"claim2": "value2"
},
"scopes": [
"scope1",
"scope2"
]
}
},
"domainName": "id.execute-api.us-east-1.amazonaws.com",
"domainPrefix": "id",
"http": {
"method": "POST",
"path": "/path/to/resource",
"protocol": "HTTP/1.1",
"sourceIp": "192.168.0.1/32",
"userAgent": "agent"
},
"requestId": "id",
"routeKey": "$default",
"stage": "$default",
"time": "27/Sep/2024:09:06:03 +0000",
"timeEpoch": 1727427963
},
"body": "{'foo': 'bar'}",
"pathParameters": {
"parameter1": "value1"
},
"isBase64Encoded": true,
"stageVariables": {
"stageVariable1": "value1",
"stageVariable2": "value2"
}
}
69 changes: 69 additions & 0 deletions moesif_aws_lambda/tests/event_body_valid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"version": "2.0",
"routeKey": "$default",
"rawPath": "/path/to/resource",
"rawQueryString": "parameter1=value1&parameter1=value2&parameter2=value",
"cookies": [
"cookie1",
"cookie2"
],
"headers": {
"Header1": "value1",
"Header2": "value1,value2"
},
"queryStringParameters": {
"parameter1": "value1,value2",
"parameter2": "value"
},
"requestContext": {
"accountId": "123456789012",
"apiId": "api-id",
"authentication": {
"clientCert": {
"clientCertPem": "CERT_CONTENT",
"subjectDN": "www.example.com",
"issuerDN": "Example issuer",
"serialNumber": "a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1",
"validity": {
"notBefore": "May 28 12:30:02 2019 GMT",
"notAfter": "Aug 5 09:36:04 2021 GMT"
}
}
},
"authorizer": {
"jwt": {
"claims": {
"claim1": "value1",
"claim2": "value2"
},
"scopes": [
"scope1",
"scope2"
]
}
},
"domainName": "id.execute-api.us-east-1.amazonaws.com",
"domainPrefix": "id",
"http": {
"method": "POST",
"path": "/path/to/resource",
"protocol": "HTTP/1.1",
"sourceIp": "192.168.0.1/32",
"userAgent": "agent"
},
"requestId": "id",
"routeKey": "$default",
"stage": "$default",
"time": "27/Sep/2024:09:06:03 +0000",
"timeEpoch": 1727427963
},
"body": "eyJ0ZXN0IjoiYm9keSJ9",
"pathParameters": {
"parameter1": "value1"
},
"isBase64Encoded": true,
"stageVariables": {
"stageVariable1": "value1",
"stageVariable2": "value2"
}
}
Loading

0 comments on commit bd7d31b

Please sign in to comment.