Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #1340 (closed defect: fixed)

Opened 11 years ago

Last modified 10 years ago

Raising IdentityFailure fails if a unicode string was posted

Reported by: noam Owned by: 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 Download (1.2 KB) - added by noam 11 years ago.
Diff of decodingfilter.py

Change History

comment:1 Changed 11 years ago by noam

  • Owner set to anonymous
  • Component changed from unassigned to CherryPy

comment:2 Changed 11 years ago by alberto

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

Thanks

Alberto

comment:3 Changed 11 years ago 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).

comment:4 Changed 11 years ago by alberto

  • Milestone changed from 1.0.2 to 1.0.3

comment:5 Changed 11 years ago 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

comment:6 Changed 11 years ago by noam

Try the following patch and say if the problem remains.

Changed 11 years ago by noam

  • attachment diff Download added

Diff of decodingfilter.py

comment:7 Changed 11 years ago by faide

  • Milestone changed from 1.0.3 to 1.1

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

comment:8 Changed 11 years ago 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.

comment:9 Changed 10 years ago by faide

  • Status changed from new to closed
  • Resolution set to fixed

same as #1598 and fixed...

Note: See TracTickets for help on using tickets.