zopefoundation / zope.publisher

Map requests from HTTP/WebDAV clients, web browsers, XML-RPC and FTP clients onto Python objects
Other
3 stars 13 forks source link

BrowserRequest fails to decode URL-encoded form values with charset="UTF-8" #65

Closed cjwatson closed 2 years ago

cjwatson commented 3 years ago

Following finally upgrading all of Launchpad's appservers to Python 3, https://bugs.launchpad.net/launchpad/+bug/1937345 now reports a problem with submitting URL-encoded form data containing non-ISO-8859-1 values. For Content-Type: application/x-www-form-urlencoded; charset=UTF-8 and QUERY_STRING=:ws.op=newMessage&content=%E2%80%9Ccomment%E2%80%9D, the traceback looks like this:

Traceback (most recent call last):
  File "/home/cjwatson/src/canonical/launchpad/git/review/env/lib/python3.5/site-packages/zope/publisher/publish.py", line 139, in publish
    request.processInputs()
  File "/home/cjwatson/src/canonical/launchpad/git/review/env/lib/python3.5/site-packages/zope/publisher/browser.py", line 379, in processInputs
    self.__processItem(key, item)
  File "/home/cjwatson/src/canonical/launchpad/git/review/env/lib/python3.5/site-packages/zope/publisher/browser.py", line 452, in __processItem
    item = self._decode(item)
  File "/home/cjwatson/src/canonical/launchpad/git/review/lib/lp/services/webapp/servers.py", line 697, in _decode
    text = super(LaunchpadBrowserRequest, self)._decode(text)
  File "/home/cjwatson/src/canonical/launchpad/git/review/env/lib/python3.5/site-packages/zope/publisher/browser.py", line 284, in _decode
    text = text.encode('latin-1')
UnicodeEncodeError: 'latin-1' codec can't encode character '\u201c' in position 0: ordinal not in range(256)

(There's a bit of Launchpad in this traceback, but it's only some fallback code and is in practice irrelevant here.)

zope.publisher plays a complicated game of core wars with the underlying libraries it uses to decode form input, presumably for ancient historical reasons. I didn't quite dare to touch that part of the code significantly when converting zope.publisher to multipart, but I think it's going to be necessary to revamp that in order to fix this bug. There are two fundamental problems:

I've reproduced this in zope.publisher's test suite, and should be able to come up with a PR soon.