In Lib/wsgiref/headers.py, Headers.setitem deletes the existing
header before validating the new name and value:
def __setitem__(self, name, val):
"""Set the value of a header."""
del self[name]
self._headers.append(
(self._convert_string_type(name, name=True),
self._convert_string_type(val, name=False)))
If _convert_string_type raises (for example when val is not a string),
the old header has already been removed and the new one is never added,
so the header disappears entirely.
Reproduction:
from wsgiref.headers import Headers
h = Headers([("Content-Type", "text/html")])
try:
h["Content-Type"] = 123
except AssertionError:
pass
print(h.get("Content-Type"))
# Expected: 'text/html'
# Actual: None
The fix is to validate the new name and value before deleting the old
header.
Linked PRs
In Lib/wsgiref/headers.py, Headers.setitem deletes the existing
header before validating the new name and value:
If _convert_string_type raises (for example when val is not a string),
the old header has already been removed and the new one is never added,
so the header disappears entirely.
Reproduction:
The fix is to validate the new name and value before deleting the old
header.
Linked PRs