From 5bd244725737ff08a0783e95d2990bb61a3800aa Mon Sep 17 00:00:00 2001 From: David Moreau Simard Date: Tue, 19 Nov 2019 16:40:14 -0500 Subject: [PATCH] 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 --- ara/server/settings.py | 1 + ara/ui/views.py | 4 ++++ tests/integration/smoke.yaml | 2 +- 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/ara/server/settings.py b/ara/server/settings.py index e24f45f6..f7734628 100644 --- a/ara/server/settings.py +++ b/ara/server/settings.py @@ -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) diff --git a/ara/ui/views.py b/ara/ui/views.py index ab3925eb..8add9ce4 100644 --- a/ara/ui/views.py +++ b/ara/ui/views.py @@ -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}) diff --git a/tests/integration/smoke.yaml b/tests/integration/smoke.yaml index e4e4b3dd..23017337 100644 --- a/tests/integration/smoke.yaml +++ b/tests/integration/smoke.yaml @@ -65,6 +65,6 @@ - untag - verytag - - name: Echo a binary string + - name: Echo the �abc binary string command: echo -e '\x80abc' changed_when: false