mask_password doesn't return unicode type value if non-ASCII characters exist in the message

Bug #1549202 reported by javeme
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
oslo.utils
Invalid
Undecided
Unassigned

Bug Description

mask_password doesn't return unicode type value[1] if non-ASCII characters exist in the message.

If non-ASCII characters doesn't exist in the message, mask_password returns unicode value:

    >>> from oslo_utils import strutils
    >>> utf8_str = u'test --password Chinese.'.encode('utf8')
    >>> masked = strutils.mask_password(utf8_str)
    >>> type(masked)
    <type 'unicode'>

If non-ASCII characters exist in the message, mask_password returns str value:

    >>> from oslo_utils import strutils
    >>> utf8_str = u'test --password Chinese中文.'.encode('utf8')
    >>> masked = strutils.mask_password(utf8_str)
    >>> type(masked)
    <type 'str'>

The uncertainty of the return type will cause an exception in the following scenario:

    from oslo_serialization import jsonutils
    from oslo_utils import strutils

    def serialize(message_dict):
        utf8_data = jsonutils.dump_as_bytes(message_dict, ensure_ascii=False)
        unicode_data = strutils.mask_password(utf8_data)
        utf8_data = unicode_data.encode('utf8')
        return utf8_data

    # scenario 1: without non-ASCII
    message_dict = {u'name': u'test --password Chinese.'}
    body = serialize(message_dict) # OK

    # scenario 2: with non-ASCII
    message_dict = {u'name': u'test --password Chinese 中文.'}
    body = serialize(message_dict) # UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 30: ordinal not in range(128)

NOTE: There is a way to avoid this problem by converting message to unicode before passed into mask_password:

    strutils.mask_password(six.text_type(utf8_data, 'utf8'))

[1]: https://github.com/openstack/oslo.utils/blob/master/oslo_utils/strutils.py#L233

javeme (javaloveme)
Changed in oslo.utils:
assignee: nobody → javeme (javaloveme)
Revision history for this message
ChangBo Guo(gcb) (glongwave) wrote :

Is there any use case in openstack projects ?

Changed in oslo.utils:
status: New → Incomplete
Revision history for this message
Takashi Kajinami (kajinamit) wrote :

I'm closing this because we haven't heard any response for 7+ years

Changed in oslo.utils:
status: Incomplete → Invalid
assignee: javeme (javaloveme) → nobody
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

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