Skip to content

Commit 5addf8c

Browse files
authored
Merge pull request #106 from sapcc/handle_json_string
handle payload json string to dict, type check
2 parents 2361acf + 7ae2293 commit 5addf8c

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

auditmiddleware/_api.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,26 @@ def _get_action_from_payload(self, request, res_spec, res_id):
776776
"""Determine the CADF action from the payload."""
777777
try:
778778
payload = request.json
779+
780+
# Handle case where payload is a string (JSON-encoded)
781+
if isinstance(payload, str):
782+
try:
783+
payload = json.loads(payload)
784+
except json.JSONDecodeError:
785+
self._log.warning(
786+
"Invalid JSON string payload for path: %s",
787+
request.path)
788+
return None
789+
779790
if payload:
791+
# Type checking for payload
792+
if not isinstance(payload, dict):
793+
self._log.warning(
794+
"Unexpected payload type %s for path: %s",
795+
type(payload), request.path
796+
)
797+
return None
798+
780799
rest_action = next(iter(payload))
781800
# check for individual mapping of action
782801
action = res_spec.custom_actions.get(rest_action)

test-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ coverage!=4.4,>=4.0 # Apache-2.0
1111
fixtures>=3.0.0 # Apache-2.0/BSD
1212
mock>=3.0.0 # BSD
1313
oslotest>=3.8.0 # Apache-2.0
14+
pytz==2024.2 # MIT
1415
requests-mock>=1.2.0 # Apache-2.0
1516
#stevedore>=1.20.0 # Apache-2.0
1617
stestr>=2.0.0 # Apache-2.0

0 commit comments

Comments
 (0)