diff --git a/spyne/protocol/soap/soap11.py b/spyne/protocol/soap/soap11.py index 7bc702338..0b2df0561 100644 --- a/spyne/protocol/soap/soap11.py +++ b/spyne/protocol/soap/soap11.py @@ -105,7 +105,11 @@ def _parse_xml_string(xml_string, parser, charset=None): string = ''.join(chain( (chunk,), xml_string )) if charset: - string = string.decode(charset) + try: + string = string.decode(charset) + except UnicodeDecodeError as e: + logger_invalid.error("%r in string %r", e, string) + raise Fault('Client.XMLSyntaxError', str(e)) try: try: diff --git a/spyne/test/protocol/test_soap11.py b/spyne/test/protocol/test_soap11.py index 0a0fb29d6..6564c7209 100755 --- a/spyne/test/protocol/test_soap11.py +++ b/spyne/test/protocol/test_soap11.py @@ -256,6 +256,29 @@ def test_empty_body(self): self.assertEqual(cm.exception.faultcode, 'Client.XMLSyntaxError') self.assertEqual(cm.exception.faultstring, 'Missing body') + def test_bad_encoding(self): + # Encode string with non-ascii characters as Latin-1, so that later + # when decoding as utf-8 you will get an decode error. + # This should result in a client error. + envelope_string = [''' + + + + + + + +'''.encode('latin-1')] + + with self.assertRaises(Fault) as cm: + _parse_xml_string(envelope_string, + etree.XMLParser(), 'utf8') + self.assertEqual(cm.exception.faultcode, 'Client.XMLSyntaxError') + self.assertIn("'utf-8' codec can't decode byte", + cm.exception.faultstring) + def test_namespaces(self): m = ComplexModel.produce( namespace="some_namespace",