Ticket #1340 (closed defect: fixed)

Opened 1 year ago

Last modified 2 months ago

Raising IdentityFailure fails if a unicode string was posted

Reported by: noam Assigned to: anonymous
Priority: normal Milestone: 1.5
Component: CherryPy Version: 1.0
Severity: normal Keywords:
Cc:

Description

Hello,

I have a page called addMessage, which adds a message. It is the action of a form with a textarea. In the method, I checked if the user is authorized to add a message, and if not, raised identity.IdentityFailure?. Unfortunately, I got the following error:

500 Internal error

The server encountered an unexpected condition which prevented it from fulfilling the request.

Page handler: 'ordinal not in range(128)'
Traceback (most recent call last):
  File "/home/noam/lib/python/CherryPy-2.2.1-py2.4.egg/cherrypy/_cphttptools.py", line 103, in _run
    applyFilters('before_main')
  File "/home/noam/lib/python/CherryPy-2.2.1-py2.4.egg/cherrypy/filters/__init__.py", line 151, in applyFilters
    method()
  File "/home/noam/lib/python/CherryPy-2.2.1-py2.4.egg/cherrypy/filters/decodingfilter.py", line 31, in before_main
    self.decode(enc)
  File "/home/noam/lib/python/CherryPy-2.2.1-py2.4.egg/cherrypy/filters/decodingfilter.py", line 54, in decode
    decodedParams[key] = value.decode(enc)
  File "encodings/utf_8.py", line 16, in decode
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)

After some fiddling around, it turned out that cherrypy tried to decode a parameter (which was the message text), but it was already decoded. So, I changed cherrypy/filters/decodingfilter.py:54 from

                decodedParams[key] = value.decode(enc)

to:

                if isinstance(value, unicode):
                    decodedParams[key] = value
                else:
                    decodedParams[key] = value.decode(enc)

and now it works.

I'm not sure if it's a good fix, or if something more fundamental should be changed.

Have a good day, Noam

Attachments

diff (1.2 kB) - added by noam on 05/03/07 02:35:14.
Diff of decodingfilter.py

Change History

03/27/07 12:46:50 changed by noam

  • owner set to anonymous.
  • component changed from unassigned to CherryPy.

04/06/07 17:18:05 changed by alberto

Can you please provide some test code so I can reproduce this?

Thanks

Alberto

04/13/07 06:15:16 changed by noam

Yes.

Create a new project with tg-admin quickstart, with identity enabled.

Add to welcome.kid somewhere:

  <form action="/addMessage" method="POST">
  <textarea name="text"/>
  <input type="submit"/>
  </form>

Add to controllers.py, as a method of Root:

    @expose()
    def addMessage(self, text):
        raise identity.IdentityFailure, 'not allowed'

Start the server, and write at the textbox some text which is not in English, and press submit. You'll get the error I reported (hopefully).

05/01/07 12:57:01 changed by alberto

  • milestone changed from 1.0.2 to 1.0.3.

05/03/07 00:42:45 changed by guest

I have what appears to be the same exact problem. But when I did the above fix, I now get this error message:

Traceback (most recent call last):
  File "/usr/lib/python2.4/site-packages/CherryPy-2.2.1-py2.4.egg/cherrypy/_cphttptools.py", line 103, in _run
    applyFilters('before_main')
  File "/usr/lib/python2.4/site-packages/CherryPy-2.2.1-py2.4.egg/cherrypy/filters/__init__.py", line 151, in applyFilters
    method()
  File "/usr/lib/python2.4/site-packages/CherryPy-2.2.1-py2.4.egg/cherrypy/filters/decodingfilter.py", line 31, in before_main
    self.decode(enc)
  File "/usr/lib/python2.4/site-packages/CherryPy-2.2.1-py2.4.egg/cherrypy/filters/decodingfilter.py", line 54, in decode
    if isinstance(value, unicode):
UnboundLocalError: local variable 'value' referenced before assignment

05/03/07 02:34:40 changed by noam

Try the following patch and say if the problem remains.

05/03/07 02:35:14 changed by noam

  • attachment diff added.

Diff of decodingfilter.py

07/01/07 17:56:04 changed by faide

  • milestone changed from 1.0.3 to 1.1.

Maybe this should be proposed as a patch for the Cherrypy maintenance team ?

11/16/07 08:13:59 changed by aalbrecht

I can confirm this bug. It only happens if an identity error occurs. Then the decode method of CherryPy's DecodingFilter is called twice and {{kwargs}} are already decoded for the second time.

The patch submitted by noam fixes this issue.

It's reproducable with the simple example given in #1598.

06/06/08 01:45:37 changed by faide

  • status changed from new to closed.
  • resolution set to fixed.

same as #1598 and fixed...