From c196dc16d7528e8ca57145f173df1db642c8d6a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Tue, 10 Aug 2021 21:14:05 +0300 Subject: [PATCH] Fix latin-1 handling in webhook header values --- hc/api/tests/test_notify_webhook.py | 18 ++++++++++++++++++ hc/api/transports.py | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/hc/api/tests/test_notify_webhook.py b/hc/api/tests/test_notify_webhook.py index 7d14e9d9..156b6fe3 100644 --- a/hc/api/tests/test_notify_webhook.py +++ b/hc/api/tests/test_notify_webhook.py @@ -359,3 +359,21 @@ class NotifyWebhookTestCase(BaseTestCase): args, kwargs = mock_request.call_args self.assertEqual(kwargs["headers"]["X-Foo"], "bār") + + @patch("hc.api.transports.requests.request") + def test_webhooks_handle_latin1_in_headers(self, mock_request): + definition = { + "method_down": "GET", + "url_down": "http://foo.com", + "headers_down": {"X-Foo": "½"}, + "body_down": "", + } + + self._setup_data(json.dumps(definition)) + self.check.save() + + self.channel.notify(self.check) + args, kwargs = mock_request.call_args + + self.assertEqual(kwargs["headers"]["X-Foo"], "½") + diff --git a/hc/api/transports.py b/hc/api/transports.py index 930933ff..178b4a2b 100644 --- a/hc/api/transports.py +++ b/hc/api/transports.py @@ -223,7 +223,7 @@ class Webhook(HttpTransport): result = replace(template, ctx) if latin1: # Replace non-latin-1 characters with XML character references. - result = result.encode("latin-1", "xmlcharrefreplace").decode() + result = result.encode("latin-1", "xmlcharrefreplace").decode("latin-1") return result