Mixing str and unicode leads to a UnicodeDecodeError

Bug #1428706 reported by Cyril Roelandt
16
This bug affects 2 people
Affects Status Importance Assigned to Milestone
keystonemiddleware
Fix Released
Undecided
Unassigned
python-keystoneclient
Invalid
Undecided
Cyril Roelandt

Bug Description

In keystoneclient/middleware/s3token.py, the following code:

class S3Token(object):
    ...
    def __call__(self, environ, start_response):
        ...
        environ['PATH_INFO'] = environ['PATH_INFO'].replace(account,
                                                            new_tenant_name)

sometimes leads to the following error:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 49: ordinal not in range(128).

This happens because "environ['PATH_INFO']" and "account" have type "str", while "new_tenant_name" is unicode. When dealing with unicode characters, we end up doing something like:

>>> x = "fooébar"
>>> x.replace('o', u'u')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 3: ordinal not in range(128)

The "new_tenant_name" is Unicode because keystone returns the tenant id as a Unicode string. We should encode it before calling str.replace().

You may find a reproducible test case involving Swift and Swift3 at https://bugs.launchpad.net/swift3/+bug/1333600 .

description: updated
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to python-keystoneclient (master)

Fix proposed to branch: master
Review: https://review.openstack.org/161762

Changed in python-keystoneclient:
assignee: nobody → Cyril Roelandt (cyril-roelandt)
status: New → In Progress
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Change abandoned on python-keystoneclient (master)

Change abandoned by Cyril Roelandt (<email address hidden>) on branch: master
Review: https://review.openstack.org/161762
Reason: The relevant code is now part of keystonemiddleware.

Revision history for this message
Charles Hsu (charles0126) wrote :

Hi Cyril,

I opened a bug in keystonemiddleware.
https://bugs.launchpad.net/keystonemiddleware/+bug/1465525

And do you want to summit a patch to keystonemiddleware? Or I can help this?

Revision history for this message
Cyril Roelandt (cyril-roelandt) wrote :

There is already a patch https://review.openstack.org/#/c/179777/ :)

Revision history for this message
Charles Hsu (charles0126) wrote :

@Cyril, That's awesome! Thanks!

Revision history for this message
Steve Martinelli (stevemar) wrote :

looks like this was fixed in keystonemiddleware, marking as fix-released

Changed in python-keystoneclient:
status: In Progress → Invalid
Changed in keystonemiddleware:
status: New → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Duplicates of this bug

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.