Skip to content

Conversation

@sethmlarson
Copy link
Contributor

@sethmlarson sethmlarson commented Jan 21, 2026

In #143917 we were overzealous, HTAB (0x09) is allowed in header values but not header names.

self._convert_string_type(v)

def _convert_string_type(self, value):
def _convert_string_type(self, value, *, name=False):
Copy link
Member

Choose a reason for hiding this comment

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

It may be safer to always require the name parameter:

Suggested change
def _convert_string_type(self, value, *, name=False):
def _convert_string_type(self, value, *, name):

"""Convert/check value type."""
if type(value) is str:
if _control_chars_re.search(value):
if (_name_disallowed_re if name else _value_disallowed_re).search(value):
Copy link
Member

Choose a reason for hiding this comment

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

nitpick: I would prefer to write this code on two lines for better readability:

Suggested change
if (_name_disallowed_re if name else _value_disallowed_re).search(value):
regex = (_name_disallowed_re if name else _value_disallowed_re)
if regex.search(value):

headers = Headers()
self.assertRaises(ValueError, headers.__setitem__, f"key{c0}", "val")
self.assertRaises(ValueError, headers.add_header, f"key{c0}", "val", param="param")
# HTAB is allowed in values, not names.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# HTAB is allowed in values, not names.
# HTAB (\x09) is allowed in values, not names.

@vstinner
Copy link
Member

vstinner commented Feb 3, 2026

See also PR gh-144371 which rejects control characters in Lib/wsgiref/handlers.py.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants