-
-
Notifications
You must be signed in to change notification settings - Fork 33.8k
gh-142533: Prevent CRLF injection in HTTP headers #142605
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
6d53fd6
5e56689
6d661e8
2f0605a
1c0d5d7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Reject CR/LF in header names/values in `http.server` and `wsgiref.headers` to prevent CRLF injection attacks.
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,12 +35,14 @@ def __init__(self, headers=None): | |
| self._headers = headers | ||
| if __debug__: | ||
| for k, v in headers: | ||
| self._convert_string_type(k) | ||
| self._convert_string_type(v) | ||
| self._validate_header_string(k) | ||
| self._validate_header_string(v) | ||
|
|
||
| def _convert_string_type(self, value): | ||
| """Convert/check value type.""" | ||
| def _validate_header_string(self, value): | ||
| """Validate header type and value.""" | ||
| if type(value) is str: | ||
| if '\r' in value or '\n' in value: | ||
| raise ValueError('Invalid header name/value: contains CR or LF') | ||
| return value | ||
|
||
| raise AssertionError("Header names/values must be" | ||
| " of type str (got {0})".format(repr(value))) | ||
|
|
@@ -53,14 +55,15 @@ def __setitem__(self, name, val): | |
| """Set the value of a header.""" | ||
| del self[name] | ||
| self._headers.append( | ||
| (self._convert_string_type(name), self._convert_string_type(val))) | ||
| (self._validate_header_string(name), | ||
| self._validate_header_string(val))) | ||
|
|
||
| def __delitem__(self,name): | ||
| """Delete all occurrences of a header, if present. | ||
|
|
||
| Does *not* raise an exception if the header is missing. | ||
| """ | ||
| name = self._convert_string_type(name.lower()) | ||
| name = self._validate_header_string(name.lower()) | ||
|
||
| self._headers[:] = [kv for kv in self._headers if kv[0].lower() != name] | ||
|
|
||
| def __getitem__(self,name): | ||
|
|
@@ -87,13 +90,13 @@ def get_all(self, name): | |
| fields deleted and re-inserted are always appended to the header list. | ||
| If no fields exist with the given name, returns an empty list. | ||
| """ | ||
| name = self._convert_string_type(name.lower()) | ||
| name = self._validate_header_string(name.lower()) | ||
| return [kv[1] for kv in self._headers if kv[0].lower()==name] | ||
|
|
||
|
|
||
| def get(self,name,default=None): | ||
| """Get the first header value for 'name', or return 'default'""" | ||
| name = self._convert_string_type(name.lower()) | ||
| name = self._validate_header_string(name.lower()) | ||
| for k,v in self._headers: | ||
| if k.lower()==name: | ||
| return v | ||
|
|
@@ -148,8 +151,8 @@ def setdefault(self,name,value): | |
| and value 'value'.""" | ||
| result = self.get(name) | ||
| if result is None: | ||
| self._headers.append((self._convert_string_type(name), | ||
| self._convert_string_type(value))) | ||
| self._headers.append((self._validate_header_string(name), | ||
| self._validate_header_string(value))) | ||
| return value | ||
| else: | ||
| return result | ||
|
|
@@ -172,13 +175,13 @@ def add_header(self, _name, _value, **_params): | |
| """ | ||
| parts = [] | ||
| if _value is not None: | ||
| _value = self._convert_string_type(_value) | ||
| _value = self._validate_header_string(_value) | ||
| parts.append(_value) | ||
| for k, v in _params.items(): | ||
| k = self._convert_string_type(k) | ||
| k = self._validate_header_string(k) | ||
| if v is None: | ||
| parts.append(k.replace('_', '-')) | ||
| else: | ||
| v = self._convert_string_type(v) | ||
| v = self._validate_header_string(v) | ||
| parts.append(_formatparam(k.replace('_', '-'), v)) | ||
| self._headers.append((self._convert_string_type(_name), "; ".join(parts))) | ||
| self._headers.append((self._validate_header_string(_name), "; ".join(parts))) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Reject CR/LF in HTTP headers in `http.server` and `wsgiref.headers` to prevent | ||
| CRLF injection attacks. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that this is debug-only check. It can be completely omitted.