Opened 19 minutes ago
#37523 assigned Bug
HttpResponse crashes when reason is a lazy string or reason_phrase is a read-only property
| Reported by: | Mariano Ignacio Baragiola | Owned by: | Mariano Ignacio Baragiola |
|---|---|---|---|
| Component: | HTTP handling | Version: | 6.1 |
| Severity: | Release blocker | Keywords: | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
Since 53645750412efa1e9013004040db328bd515e0f1 (#37100, backported to 6.1 as d7b3fed6a1), HttpResponseBase.__init__() sets the reason through the reason_phrase setter, and the setter runs _control_chars_re.search(value). Two cases that worked in Django 6.0 now fail.
- A lazy string as
reasonraisesTypeError:
from django.http import HttpResponse from django.utils.translation import gettext_lazy HttpResponse(status=418, reason=gettext_lazy("Custom reason"))
TypeError: expected string or bytes-like object, got '__proxy__'
The same happens with response.reason_phrase = gettext_lazy("...").
- A subclass that defines
reason_phraseas a getter-only property raisesAttributeErrorwhen created:
from django.http import HttpResponse class CustomResponse(HttpResponse): @property def reason_phrase(self): return "Custom reason" CustomResponse(status=418)
AttributeError: property 'reason_phrase' of 'CustomResponse' object has no setter
Expected: both work as in 6.0, and control characters are still rejected (lazy strings included).
Affected: 6.1.0 to 6.1.3 and main. Regression in 53645750412efa1e9013004040db328bd515e0f1.
Suggested fix: run the control character check on str(value) when the value is not None, and have __init__() run the check and set _reason_phrase directly instead of going through the setter.
AI disclosure: I found and reproduced this with Claude Code (Anthropic).