diff --git a/spyne/protocol/soap/soap11.py b/spyne/protocol/soap/soap11.py index 75e1add71..0b2df0561 100644 --- a/spyne/protocol/soap/soap11.py +++ b/spyne/protocol/soap/soap11.py @@ -93,14 +93,23 @@ def _from_soap(in_envelope_xml, xmlids=None, **kwargs): def _parse_xml_string(xml_string, parser, charset=None): xml_string = iter(xml_string) - chunk = next(xml_string) + try: + chunk = next(xml_string) + except StopIteration: + logger_invalid.error("missing body") + raise Fault('Client.XMLSyntaxError', 'Missing body') + if isinstance(chunk, six.binary_type): string = b''.join(chain( (chunk,), xml_string )) else: 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 93af58ae9..6564c7209 100755 --- a/spyne/test/protocol/test_soap11.py +++ b/spyne/test/protocol/test_soap11.py @@ -244,6 +244,41 @@ def test_href(self): # quick and dirty test href reconstruction self.assertEqual(len(payload[0]), 2) + def test_empty_body(self): + # If the soap request has no body, then you get an empty iterable + # as the envelope string. + # This should be treated as a client error. + envelope_string = iter([]) + + with self.assertRaises(Fault) as cm: + _parse_xml_string(envelope_string, + etree.XMLParser(), 'utf8') + 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",