Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Xmlrpc: accept data:str|bytes and require method:str (python#12734)
Declared <https://github.com/python/cpython/blob/main/Lib/xmlrpc/server.py#L622> Inside [`do_POST()`](https://github.com/python/cpython/blob/main/Lib/xmlrpc/server.py#L493) `data: bytes`, where `decode_request_content()` only handles `gzip` compression. The `result` is then written to `self.wfile`, which uses `bytes`. But for [CGI](https://github.com/python/cpython/blob/main/Lib/xmlrpc/server.py#L636) `str` is used: [`handle_xmlrpc(self, request_text)`](https://github.com/python/cpython/blob/main/Lib/xmlrpc/server.py#L642) calls the argument `request_text`, which is read from `sys.stdin` as type `str` and then passed to `_marshaled_dispatch()`, which internally calls [`xmlrpc.client.loads()`](https://github.com/python/cpython/blob/main/Lib/xmlrpc/server.py#L257) to parse the XML using Expat, which accepts both `str` and `bytes`; the later defaults to encoding `utf-8`, but other encodings can be used when explicitly specified: >>> xmlrpc.client.loads('<params><param><value><string>ä</string></value></param></params>') (('ä',), None) >>> xmlrpc.client.loads('<params><param><value><string>ä</string></value></param></params>'.encode("utf-8")) (('ä',), None) >>> xmlrpc.client.loads('<params><param><value><string>ä</string></value></param></params>'.encode("iso-8859-1")) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python3.11/xmlrpc/client.py", line 1029, in loads p.feed(data) File "/usr/lib/python3.11/xmlrpc/client.py", line 451, in feed self._parser.Parse(data, False) xml.parsers.expat.ExpatError: not well-formed (invalid token): line 3, column 15 >>> xmlrpc.client.loads('<?xml version="1.0" encoding="utf-8"?><params><param><value><string>ä</string></value></param></params>'.encode("iso-8859-1")) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python3.11/xmlrpc/client.py", line 1029, in loads p.feed(data) File "/usr/lib/python3.11/xmlrpc/client.py", line 451, in feed self._parser.Parse(data, False) xml.parsers.expat.ExpatError: not well-formed (invalid token): line 1, column 68 >>> xmlrpc.client.loads('<?xml version="1.0" encoding="iso-8859-1"?><params><param><value><string>ä</string></value></param></params>'.encode("iso-8859-1")) (('ä',), None) Signed-off-by: Philipp Hahn <[email protected]> Reviewed-By: Jelle Zijlstra <[email protected]>
- Loading branch information