[Fixes #14244] Do not return WWW-Authenticate inside API exception response#14265
Conversation
There was a problem hiding this comment.
Code Review
This pull request modifies the custom exception handler to remove the 'WWW-Authenticate' header from 401 Unauthorized responses. However, calling .pop() on response.headers will raise an AttributeError because HttpHeaders in Django is not a mutable mapping. It is recommended to safely remove the header using the del operator on the response object directly.
| if response.status_code == 401: | ||
| response.headers.pop('WWW-Authenticate', None) |
There was a problem hiding this comment.
In Django, response.headers returns an instance of HttpHeaders which inherits from CaseInsensitiveMapping (a subclass of collections.abc.Mapping, not MutableMapping). Because of this, it does not implement the pop() method, and calling response.headers.pop('WWW-Authenticate', None) will raise an AttributeError: 'HttpHeaders' object has no attribute 'pop'.
To safely and compatibly remove the header in Django, use the standard response.has_header() check and the del operator on the response object directly.
| if response.status_code == 401: | |
| response.headers.pop('WWW-Authenticate', None) | |
| if response.status_code == 401 and response.has_header('WWW-Authenticate'): | |
| del response['WWW-Authenticate'] |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #14265 +/- ##
=======================================
Coverage 74.92% 74.93%
=======================================
Files 975 975
Lines 59902 59912 +10
Branches 8157 8159 +2
=======================================
+ Hits 44884 44894 +10
Misses 13194 13194
Partials 1824 1824 🚀 New features to boost your workflow:
|
Checklist
For all pull requests:
The following are required only for core and extension modules (they are welcomed, but not required, for contrib modules):
Submitting the PR does not require you to check all items, but by the time it gets merged, they should be either satisfied or inapplicable.