api: Fix UnicodeEncodeError when parsing and rendering results

This is an attempt to prevent encoding errors when posting results or
when rendering them in the API and built-in interface.

Setting UNICODE_JSON [1] to false allows the callback to post results'
content and allows the API browsing interface to render the result.

The built-in UI had a similar problem but isn't fixed by the
UNICODE_JSON because we are using a Response object so use
"surrogateescape" instead.

Related: https://github.com/ansible-community/ara/issues/48

[1]: https://www.django-rest-framework.org/api-guide/settings/#unicode_json

Change-Id: I48bcef440a1ee9c8574fdd24d5c05ef0f13ca666
This commit is contained in:
David Moreau Simard 2019-11-19 16:40:14 -05:00
parent a87bd88437
commit 5bd2447257
No known key found for this signature in database
GPG Key ID: 938880DAFC753E80
3 changed files with 6 additions and 1 deletions

View File

@ -230,6 +230,7 @@ REST_FRAMEWORK = {
"DEFAULT_AUTHENTICATION_CLASSES": ("rest_framework.authentication.BasicAuthentication",),
"DEFAULT_PERMISSION_CLASSES": ("ara.api.auth.APIAccessPermission",),
"TEST_REQUEST_DEFAULT_FORMAT": "json",
"UNICODE_JSON": False,
}
ARA_SETTINGS = os.getenv("ARA_SETTINGS", DEFAULT_SETTINGS)

View File

@ -1,3 +1,5 @@
import codecs
from rest_framework import generics
from rest_framework.renderers import TemplateHTMLRenderer
from rest_framework.response import Response
@ -90,6 +92,8 @@ class Result(generics.RetrieveAPIView):
template_name = "result.html"
def get(self, request, *args, **kwargs):
# Results can contain a wide array of non-ascii or binary characters, escape them
codecs.register_error("strict", codecs.lookup_error("surrogateescape"))
result = self.get_object()
serializer = serializers.DetailedResultSerializer(result)
return Response({"result": serializer.data})

View File

@ -65,6 +65,6 @@
- untag
- verytag
- name: Echo a binary string
- name: Echo the <20>abc binary string
command: echo -e '\x80abc'
changed_when: false