Fix latin-1 handling in webhook header values

This commit is contained in:
Pēteris Caune 2021-08-10 21:14:05 +03:00
parent b43612806f
commit c196dc16d7
No known key found for this signature in database
GPG Key ID: E28D7679E9A9EDE2
2 changed files with 19 additions and 1 deletions

View File

@ -359,3 +359,21 @@ class NotifyWebhookTestCase(BaseTestCase):
args, kwargs = mock_request.call_args args, kwargs = mock_request.call_args
self.assertEqual(kwargs["headers"]["X-Foo"], "bār") 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"], "½")

View File

@ -223,7 +223,7 @@ class Webhook(HttpTransport):
result = replace(template, ctx) result = replace(template, ctx)
if latin1: if latin1:
# Replace non-latin-1 characters with XML character references. # 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 return result