Files
awx/awx/api/parsers.py
Ryan Petrello d97ff57cda prohibit API payloads that represent something other than a JSON object
The JSON serializer for our API uses ``json.loads``, which permits *any*
valid JSON (including bare integers, boolean values, etc).  Lots of our
code, however, assumes that inbound JSON content will be a dict.

see: #4756
2017-02-28 13:23:03 -05:00

35 lines
1.1 KiB
Python

# Python
from collections import OrderedDict
import json
# Django
from django.conf import settings
from django.utils import six
from django.utils.translation import ugettext_lazy as _
# Django REST Framework
from rest_framework import parsers
from rest_framework.exceptions import ParseError
class JSONParser(parsers.JSONParser):
"""
Parses JSON-serialized data, preserving order of dictionary keys.
"""
def parse(self, stream, media_type=None, parser_context=None):
"""
Parses the incoming bytestream as JSON and returns the resulting data.
"""
parser_context = parser_context or {}
encoding = parser_context.get('encoding', settings.DEFAULT_CHARSET)
try:
data = stream.read().decode(encoding)
obj = json.loads(data, object_pairs_hook=OrderedDict)
if not isinstance(obj, dict):
raise ParseError(_('JSON parse error - not a JSON object'))
return obj
except ValueError as exc:
raise ParseError(_('JSON parse error - %s') % six.text_type(exc))