CherryPy 始终使用 ISO-8859-1 解码 Basic Auth

CherryPy allways decodes Basic Auth with ISO-8859-1

有没有办法配置 cherrypy 以正确解码 utf-8 编码的身份验证字符串?

更新

issue #1680 中记录了已知限制。

在问题解决之前,CherryPy 将无法识别 UTF-8 编码的 Basic-Auth 数据。

原题

我在 name/passwords 使用变音字符的基本身份验证时遇到问题。似乎没有办法让 http 客户端发布 ISO-8859-1(cherrypy 会理解)name:password 或配置 cherrypy 使用 utf-8 解码身份验证字符串。

使用 Python 3.6 和 CherryPy 13.1.0:

import cherrypy

class SimpleWebpage(object):
    @cherrypy.expose
    def index(self):
        return "<html><head></head><body>Authenticated</body></html>"

def dummy_validate(realm, username, password):
    print("realm: {realm!r}, username: {username!r}, password: {password!r}".format_map(locals()))
    return True

cherrypy.tree.mount(SimpleWebpage(), '/',
                    {'/': {'tools.auth_basic.checkpassword': dummy_validate,
                           'tools.auth_basic.on': True,
                           'tools.auth_basic.realm': 'MY_REALM',}})

cherrypy.config.update({'tools.sessions.on': True,})

cherrypy.engine.autoreload.unsubscribe()
cherrypy.engine.start()
cherrypy.engine.block()

使用以下参数调用 curl:

curl -u 'Céline:motörhead' -i -X GET http://127.0.0.1:8080/

将从 cherrypy 控制台提供以下输出:

[28/Dec/2017:15:52:57] ENGINE Bus STARTING
[28/Dec/2017:15:52:57] ENGINE Serving on http://127.0.0.1:8080
[28/Dec/2017:15:52:57] ENGINE Bus STARTED
realm: 'MY_REALM', username: 'Céline', password: 'motörhead'
127.0.0.1 - C\xc3\x83\xc2\xa9line [28/Dec/2017:15:53:18] "GET / HTTP/1.1" 200 52 "" "Mozilla/5.0 (Windows; U; MSIE 9.0; WIndows NT 9.0; en-US))"

在 cygwin 上使用 curl 7.56.1 (i686-pc-cygwin) 进行测试,在 redhat6 上使用 curl 7.19.7 (x86_64-redhat-linux-gnu) 进行测试。我还使用 google-chrome 63.0.3239.108 对其进行了测试,结果完全相同。

Kludge

def decode_utf8(s):
    s_bytes = bytes([ord(c) for c in s])
    return s_bytes.decode('utf-8')

def dummy_validate(realm, username, password):
    username = decode_utf8(username)
    password = decode_utf8(password)
    print("realm: {realm!r}, username: {username!r}, password: {password!r}".format_map(locals()))
    return True

使用此代码将为我提供 google-chrome 和 curl 的正确结果。但它不适用于(作为示例)Windows 10 上的 Firefox 57.0.2(32 位),它发送 ISO-8851-15 编码的字符串。

此外,这并没有修复 cherrypy.request.login 值。

更新(2018 年 4 月 22 日):

因为 CherryPy v14.2.0 auth_basic and auth_digest tools support RFC 7617 在 HTTP 客户端(浏览器)支持的范围内,在某些情况下往往会发送损坏的数据。

旧答案:

由于 @webKnjaZ 已在评论中批准,这是一个需要在 CherryPy 或 cheroot 中解决的错误。

我认为问题已得到解答。有关错误的进一步进展可以在相应的 CherryPy-Issue.

上进行跟踪