Skip to content

parse_authorization_header raises UnicodeDecodeError, not returning None #1816

@ArchibaldArt

Description

@ArchibaldArt

werkzeug.http.parse_authorization_header() docstring states:

The return value is either None if the header was invalid or not given

But in case of passing string improperly formatted (i.e. not properly UTF-8 encoded) it raises UnicodeDecodeError exception:

>>> import base64
>>> from werkzeug.http import parse_authorization_header
>>> value = 'Basic ' + base64.b64encode(b'\xffser:pass').decode()
>>> parse_authorization_header(value)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/py3.8/lib/python3.8/site-packages/werkzeug/http.py", line 610, in parse_authorization_header
    "username": to_unicode(username, _basic_auth_charset),
  File "/py3.8/lib/python3.8/site-packages/werkzeug/_compat.py", line 219, in to_unicode
    return x.decode(charset, errors)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

Am I right that exception handling should be added in parse_authorization_header()?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions