-
-
Notifications
You must be signed in to change notification settings - Fork 34k
gh-131724: Add a new max_response_headers param to HTTP/HTTPSConnection #136814
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a24a88d
gh-131724: Add a new max_headers param to HTTP/HTTPSConnection
aeurielesn 34813ca
Apply review comments
aeurielesn 24d5c4a
Merge branch 'main' into gh-131724
aeurielesn 2ea571e
📜🤖 Added by blurb_it.
blurb-it[bot] ad0523a
Adds docs
aeurielesn b40c126
Apply review comments
aeurielesn f5450dd
Update docs
aeurielesn e93e33b
Update docs
aeurielesn 29b8348
Update Doc/whatsnew/3.15.rst
aeurielesn File filter
Filter by extension
8000Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -209,22 +209,24 @@ def getallmatchingheaders(self, name): | |
| lst.append(line) | ||
| return lst | ||
|
|
||
| def _read_headers(fp): | ||
| def _read_headers(fp, max_headers): | ||
| """Reads potential header lines into a list from a file pointer. | ||
|
|
||
| Length of line is limited by _MAXLINE, and number of | ||
| headers is limited by _MAXHEADERS. | ||
| headers is limited by max_headers. | ||
| """ | ||
| headers = [] | ||
| if max_headers is None: | ||
| max_headers = _MAXHEADERS | ||
| while True: | ||
| line = fp.readline(_MAXLINE + 1) | ||
| if len(line) > _MAXLINE: | ||
| raise LineTooLong("header line") | ||
| headers.append(line) | ||
| if len(headers) > _MAXHEADERS: | ||
| raise HTTPException("got more than %d headers" % _MAXHEADERS) | ||
| if line in (b'\r\n', b'\n', b''): | ||
| break | ||
| headers.append(line) | ||
| if len(headers) > max_headers: | ||
| raise HTTPException(f"got more than {max_headers} headers") | ||
| return headers | ||
|
|
||
| def _parse_header_lines(header_lines, _class=HTTPMessage): | ||
|
CD33
|
@@ -241,10 +243,10 @@ def _parse_header_lines(header_lines, _class=HTTPMessage): | |
| hstring = b''.join(header_lines).decode('iso-8859-1') | ||
| return email.parser.Parser(_class=_class).parsestr(hstring) | ||
|
|
||
| def parse_headers(fp, _class=HTTPMessage): | ||
| def parse_headers(fp, _class=HTTPMessage, *, _max_headers=None): | ||
| """Parses only RFC2822 headers from a file pointer.""" | ||
|
|
||
| headers = _read_headers(fp) | ||
| headers = _read_headers(fp, _max_headers) | ||
| return _parse_header_lines(headers, _class) | ||
|
|
||
|
|
||
|
|
@@ -320,7 +322,7 @@ def _read_status(self): | |
| raise BadStatusLine(line) | ||
| return version, status, reason | ||
|
|
||
| def begin(self): | ||
| def begin(self, *, _max_headers=None): | ||
| if self.headers is not None: | ||
| # we've already started reading the response | ||
| return | ||
|
|
@@ -331,7 +333,7 @@ def begin(self): | |
| if status != CONTINUE: | ||
| break | ||
| # skip the header from the 100 response | ||
| skipped_headers = _read_headers(self.fp) | ||
| skipped_headers = _read_headers(self.fp, _max_headers) | ||
| if self.debuglevel > 0: | ||
| print("headers:", skipped_headers) | ||
| del skipped_headers | ||
|
|
@@ -346,7 +348,9 @@ def begin(self): | |
| else: | ||
| raise UnknownProtocol(version) | ||
|
|
||
| self.headers = self.msg = parse_headers(self.fp) | ||
| self.headers = self.msg = parse_headers( | ||
| self.fp, _max_headers=_max_headers | ||
| ) | ||
|
|
||
| if self.debuglevel > 0: | ||
| for hdr, val in self.headers.items(): | ||
|
|
@@ -864,7 +868,7 @@ def _get_content_length(body, method): | |
| return None | ||
|
|
||
| def __init__(self, host, port=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, | ||
| source_address=None, blocksize=8192): | ||
| source_address=None, blocksize=8192, *, max_response_headers=None): | ||
| self.timeout = timeout | ||
| self.source_address = source_address | ||
| self.blocksize = blocksize | ||
|
|
@@ -877,6 +881,7 @@ def __init__(self, host, port=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, | |
| self._tunnel_port = None | ||
| self._tunnel_headers = {} | ||
| self._raw_proxy_headers = None | ||
| self.max_response_headers = max_response_headers | ||
|
|
||
| (self.host, self.port) = self._get_hostport(host, port) | ||
|
|
||
|
|
@@ -969,7 +974,7 @@ def _tunnel(self): | |
| try: | ||
| (version, code, message) = response._read_status() | ||
|
|
||
| self._raw_proxy_headers = _read_headers(response.fp) | ||
| self._raw_proxy_headers = _read_headers(response.fp, self.max_response_headers) | ||
|
|
||
| if self.debuglevel > 0: | ||
| for header in self._raw_proxy_headers: | ||
|
|
@@ -1426,7 +1431,10 @@ def getresponse(self): | |
|
|
||
| try: | ||
| try: | ||
| response.begin() | ||
| if self.max_response_headers is None: | ||
| response.begin() | ||
| else: | ||
| response.begin(_max_headers=self.max_response_headers) | ||
| except ConnectionError: | ||
| self.close() | ||
| raise | ||
|
|
@@ -1457,10 +1465,12 @@ class HTTPSConnection(HTTPConnection): | |
|
|
||
| def __init__(self, host, port=None, | ||
| *, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, | ||
| source_address=None, context=None, blocksize=8192): | ||
| source_address=None, context=None, blocksize=8192, | ||
| max_response_headers=None): | ||
| super(HTTPSConnection, self).__init__(host, port, timeout, | ||
| source_address, | ||
| blocksize=blocksize) | ||
| blocksize=blocksize, | ||
| max_response_headers=max_response_headers) | ||
| if context is None: | ||
| context = _create_https_context(self._http_vsn) | ||
| self._context = context | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
Misc/NEWS.d/next/Library/2025-07-19-15-40-47.gh-issue-131724.LS59nA.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| In :mod:`http.client`, a new *max_response_headers* keyword-only parameter has been | ||
| added to :class:`~http.client.HTTPConnection` and :class:`~http.client.HTTPSConnection` | ||
| constructors. This parameter sets the maximum number of allowed response headers, | ||
| helping to prevent denial-of-service attacks. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.