From d6fb95353ef4ff733139ad45a208bdcbbf1b3edc Mon Sep 17 00:00:00 2001 From: Tyler James Harden Date: Tue, 11 Jul 2017 10:58:46 -0400 Subject: [PATCH] Fixes non UTF-8 surrogateescapes (#612) Fixes non UTF-8 surrogateescapes Surrogate escapes in Unicode (non UTF-8 encoding) will be properly escaped with backslashes when encountered, versus breaking the transport layer. Fixes #611 --- elasticsearch/transport.py | 2 +- test_elasticsearch/test_transport.py | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/elasticsearch/transport.py b/elasticsearch/transport.py index 012c5a3c..dc8cd891 100644 --- a/elasticsearch/transport.py +++ b/elasticsearch/transport.py @@ -292,7 +292,7 @@ class Transport(object): if body is not None: try: - body = body.encode('utf-8') + body = body.encode('utf-8', 'surrogatepass') except (UnicodeDecodeError, AttributeError): # bytes/str - no need to re-encode pass diff --git a/test_elasticsearch/test_transport.py b/test_elasticsearch/test_transport.py index 74f52fd4..328325c1 100644 --- a/test_elasticsearch/test_transport.py +++ b/test_elasticsearch/test_transport.py @@ -105,6 +105,13 @@ class TestTransport(TestCase): self.assertEquals(1, len(t.get_connection().calls)) self.assertEquals(('GET', '/', None, body), t.get_connection().calls[0][0]) + def test_body_surrogates_replaced_encoded_into_bytes(self): + t = Transport([{}], connection_class=DummyConnection) + + t.perform_request('GET', '/', body='你好\uda6a') + self.assertEquals(1, len(t.get_connection().calls)) + self.assertEquals(('GET', '/', None, b'\xe4\xbd\xa0\xe5\xa5\xbd\xed\xa9\xaa'), t.get_connection().calls[0][0]) + def test_kwargs_passed_on_to_connections(self): t = Transport([{'host': 'google.com'}], port=123) self.assertEquals(1, len(t.connection_pool.connections))