Skip to content

Commit

Permalink
Handle bad encoding of SOAP body as client error
Browse files Browse the repository at this point in the history
  • Loading branch information
sashawood committed Aug 14, 2022
1 parent 59ddbd0 commit 9d419c3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
6 changes: 5 additions & 1 deletion spyne/protocol/soap/soap11.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
23 changes: 23 additions & 0 deletions spyne/test/protocol/test_soap11.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ['''
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tns="http://tempuri.org/"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<tns:myResponse xsi:type="tns:myResponse">
<myResult href="bad-encoding-é#id1" />
</tns:myResponse>
</soap:Body>
</soap:Envelope>
'''.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",
Expand Down

0 comments on commit 9d419c3

Please sign in to comment.