Encoding Error while using SessionAuth tool

67 views
Skip to first unread message

Derek Litz

unread,
Mar 19, 2012, 1:51:03 PM3/19/12
to cherrypy-users
I provided a method to override the check_username_and_password
method, and after doing so upon error I got.

mod_wsgi (pid=7625): Exception occurred processing WSGI script
TypeError: sequence of byte string values expected, value of type
unicode found

I tried using tools.encode.on: True and tools.encode.encoding: 'utf-8'
in my conf, but that did not fix the problem.

I noticed in the tool the response body was being set directly so I
changed line 319 to:

response.body = body
response.body = body.encode('utf-8')

and that fixed the problem.

I noticed https://bitbucket.org/cherrypy/cherrypy/wiki/UpgradeTo32,
which seems like it may be related. Perhaps someone with better
comprehension of the matters could shed some light for me?

1) Why didn't using tools.encode in my configuration work? Is it
because the tool is setting the response body directly?
2) Is this a bug, poor implementation (of the tool), or user error?
3) Is there a way for me to fix this problem I'm encountering, besides
rewriting/overriding what I need to in the Session Auth tool?

Julien Cigar

unread,
Mar 20, 2012, 4:57:44 AM3/20/12
to cherryp...@googlegroups.com

Did you already tried with: "tools.encode.text_only = False" ?

--
No trees were killed in the creation of this message.
However, many electrons were terribly inconvenienced.

jcigar.vcf

Derek Litz

unread,
Mar 20, 2012, 10:30:00 AM3/20/12
to cherryp...@googlegroups.com
Just tried it with:

conf = {'/': {
                'tools.encode.on': True,
                'tools.encode.encoding': 'utf-8',
                'tools.encode.text_only': False,
                ...

Still getting the same error from mod_wsgi


 sequence of byte string values expected, value of type unicode found

What does setting text_only option True or False do?

Julien Cigar

unread,
Mar 20, 2012, 10:33:57 AM3/20/12
to cherryp...@googlegroups.com
On 03/20/2012 15:30, Derek Litz wrote:
> Just tried it with:
>
> conf = {'/': {
> 'tools.encode.on': True,
> 'tools.encode.encoding': 'utf-8',
> 'tools.encode.text_only': False,
> ...
>
> Still getting the same error from mod_wsgi
>
> sequence of byte string values expected, value of type unicode found
>
>
> What does setting text_only option True or False do?
>


by default the encoding tool encodes only "text/*", so it may fail with
"application/xml" or ...
I filled a ticket about that some time ago:
https://bitbucket.org/cherrypy/cherrypy/issue/1123/responseencoder-replace-text_only-with


> On Monday, March 19, 2012 12:51:03 PM UTC-5, Derek Litz wrote:
>
> I provided a method to override the check_username_and_password
> method, and after doing so upon error I got.
>
> mod_wsgi (pid=7625): Exception occurred processing WSGI script
> TypeError: sequence of byte string values expected, value of type
> unicode found
>
> I tried using tools.encode.on: True and tools.encode.encoding:
> 'utf-8'
> in my conf, but that did not fix the problem.
>
> I noticed in the tool the response body was being set directly so I
> changed line 319 to:
>
> response.body = body
> response.body = body.encode('utf-8')
>
> and that fixed the problem.
>
> I noticed https://bitbucket.org/cherrypy/cherrypy/wiki/UpgradeTo32

> <https://bitbucket.org/cherrypy/cherrypy/wiki/UpgradeTo32>,


> which seems like it may be related. Perhaps someone with better
> comprehension of the matters could shed some light for me?
>
> 1) Why didn't using tools.encode in my configuration work? Is it
> because the tool is setting the response body directly?
> 2) Is this a bug, poor implementation (of the tool), or user error?
> 3) Is there a way for me to fix this problem I'm encountering,
> besides
> rewriting/overriding what I need to in the Session Auth tool?
>

> --
> You received this message because you are subscribed to the Google
> Groups "cherrypy-users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/cherrypy-users/-/IDLAc1slNS0J.
> To post to this group, send email to cherryp...@googlegroups.com.
> To unsubscribe from this group, send email to
> cherrypy-user...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/cherrypy-users?hl=en.

jcigar.vcf

Derek Litz

unread,
Mar 20, 2012, 4:36:00 PM3/20/12
to cherryp...@googlegroups.com
Ok, I just noticed the tool DOES encode the body line 301 using the function ntob imported from cherrypy._cpcompat.

def login_screen(self, from_page='..', username='', error_msg='', **kwargs):
        return ntob("""<html><body>
Message: %(error_msg)s
<form method="post" action="do_login">
    Login: <input type="text" name="username" value="%(username)s" size="10" /><br />
    Password: <input type="password" name="password" size="10" /><br />
    <input type="hidden" name="from_page" value="%(from_page)s" /><br />
    <input type="submit" />
</form>
</body></html>""" % {'from_page': from_page, 'username': username,
                     'error_msg': error_msg}, "utf-8")

Just doing a quick test I can see the ntob function DOES NOT return a byte string as the documentation states: 'Return the given native string as a byte string in the given encoding.'

ntob('asfd')
Out[90]: 'asfd' <--- BAD

ntob(u'asdf')
Out[91]: u'asdf' <---- BAD

u'asdf'.encode('utf-8')
Out[92]: 'asdf' <--- GOOD

Python3

>>> 'asdfasdf'.encode('utf-8')
b'asdfasdf' <--- GOOD

>>> from cherrypy._cpcompat import basestring, ntob, md5, set
>>> ntob('asdf')
b'asdf' <--- GOOD

So this may only affect python2.x it seems.  Could someone confirm?


On Monday, March 19, 2012 12:51:03 PM UTC-5, Derek Litz wrote:

Derek Litz

unread,
Mar 20, 2012, 4:42:01 PM3/20/12
to cherryp...@googlegroups.com
hmm, ignore the first bad mis-marked it.  Also, I noticed the doc say it takes a 'native' string and encodes it, perhaps that is the issue?  The unicode string is coming from the form submission for username and password.

Robert Brewer

unread,
Mar 20, 2012, 5:45:28 PM3/20/12
to cherryp...@googlegroups.com

Good catch. That function should probably fail loudly if it receives a non-native string arg. A ticket would be appreciated.

 

 

Robert Brewer

fuma...@aminus.org

 

--

You received this message because you are subscribed to the Google Groups "cherrypy-users" group.

To view this discussion on the web visit https://groups.google.com/d/msg/cherrypy-users/-/VT6BM4TJhbsJ.

Derek Litz

unread,
Mar 20, 2012, 11:06:40 PM3/20/12
to cherryp...@googlegroups.com
No, problem. https://bitbucket.org/cherrypy/cherrypy/issue/1132/cherrypy_cpcompatntob-fails-silently-on


On Monday, March 19, 2012 12:51:03 PM UTC-5, Derek Litz wrote:
Reply all
Reply to author
Forward
0 new messages