Skip to content

Fix unquote IndexError on empty string input#3771

Open
bysiber wants to merge 1 commit intoencode:masterfrom
bysiber:fix/unquote-empty-string-crash
Open

Fix unquote IndexError on empty string input#3771
bysiber wants to merge 1 commit intoencode:masterfrom
bysiber:fix/unquote-empty-string-crash

Conversation

@bysiber
Copy link

@bysiber bysiber commented Feb 20, 2026

unquote('') in _utils.py raises IndexError because it accesses value[0] without checking string length first.

This can occur when parsing digest auth WWW-Authenticate headers containing parameters with empty unquoted values (e.g. realm= instead of realm="").

`unquote('')` raises `IndexError` because it accesses `value[0]`
without checking the string length first. This can occur when parsing
digest auth WWW-Authenticate headers containing parameters with empty
unquoted values (e.g. `realm=` instead of `realm=""`).

def unquote(value: str) -> str:
return value[1:-1] if value[0] == value[-1] == '"' else value
if len(value) >= 2 and value[0] == value[-1] == '"':
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if len(value) >= 2 and value[0] == value[-1] == '"':
if value and value[0] == value[-1] == '"':

to avoid a global lookup, a function call and a comparison...?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants