-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
bpo-39603: Prevent header injection in http methods #18485
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
bpo-39603: Prevent header injection in http methods #18485
Conversation
Codecov Report
@@ Coverage Diff @@
## master #18485 +/- ##
=========================================
Coverage 82.12% 82.12%
=========================================
Files 1955 1955
Lines 588958 583810 -5148
Branches 44428 44449 +21
=========================================
- Hits 483663 479453 -4210
+ Misses 95652 94713 -939
- Partials 9643 9644 +1
Continue to review full report at Codecov.
|
Should I write some tests??! |
…cpython into fix-http-header-injection
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.
This looks correct to me, however I'm far from an expert in the http
module, so I'd appreciate a second review.
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.
This looks good to me. By providing an _validate
method, it enables subclasses an escape hatch to suppress or customize the validation.
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.
So this basically disallows control characters. It still allows spaces, which seems questionable, and non-ASCII bytes, which seems even more questionable (though probably disallowed by the ASCII encoding requirement), as well as ASCII punctuation characters (which I'm not sure would be useful). Could we just match the entire verb against r"\A\w+\Z"
? Or are there use cases for having punctuation or spaces in the HTTP verb( method)?
@@ -1086,6 +1090,7 @@ def putrequest(self, method, url, skip_host=False, | |||
raise CannotSendRequest(self.__state) | |||
|
|||
# Save the method for use later in the response phase | |||
self._validate_method(method) |
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.
Placing the validation right behind this comment is somewhat misleading -- the comment explains the assignment, but the validation is not just for the assignment -- its primary use is for the construction of the request
variable a few lines nelow (L1099 in this version). I recommend moving this up and placing blank lines around it.
@gvanrossum Thank you for the review! |
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.
Thanks. I'm approving this. The security issue is fixed by this, if people want to screw around with weird verbs I guess the server will just reject it.
Sorry, I can't merge this PR. Reason: |
3 similar comments
Sorry, I can't merge this PR. Reason: |
Sorry, I can't merge this PR. Reason: |
Sorry, I can't merge this PR. Reason: |
reject control chars in http method in http.client.putrequest to prevent http header injection (cherry picked from commit 8ca8a2e) Co-authored-by: AMIR <31338382+amiremohamadi@users.noreply.github.com>
GH-21537 is a backport of this pull request to the 3.8 branch. |
GH-21538 is a backport of this pull request to the 3.7 branch. |
GH-21539 is a backport of this pull request to the 3.6 branch. |
reject control chars in http method in http.client.putrequest to prevent http header injection (cherry picked from commit 8ca8a2e) Co-authored-by: AMIR <31338382+amiremohamadi@users.noreply.github.com>
reject control chars in http method in http.client.putrequest to prevent http header injection (cherry picked from commit 8ca8a2e) Co-authored-by: AMIR <31338382+amiremohamadi@users.noreply.github.com>
Thank you, everyone - who got involved with review of this patch. The review comments that were mentioned in bpo ticket seems to have been addressed. The changes look good to me. |
reject control chars in http method in http.client.putrequest to prevent http header injection
reject control chars in http method in http.client.putrequest to prevent http header injection
reject control chars in http method in http.client.putrequest to prevent http header injection
I created a backport to 3.5, can someone please review it? PR #21946. |
00354 # Reject control chars in HTTP method in httplib.putrequest to prevent HTTP header injection Backported from Python 3.5-3.10 (and adjusted for py2's single-module httplib): - https://bugs.python.org/issue39603 - python#18485 (3.10) - python#21946 (3.5) Co-authored-by: AMIR <31338382+amiremohamadi@users.noreply.github.com>
reject control chars in http method in http.client.putrequest to prevent http header injection
00354 # Reject control chars in HTTP method in httplib.putrequest to prevent HTTP header injection Backported from Python 3.5-3.10 (and adjusted for py2's single-module httplib): - https://bugs.python.org/issue39603 - python#18485 (3.10) - python#21946 (3.5) Co-authored-by: AMIR <31338382+amiremohamadi@users.noreply.github.com>
00354 # Reject control chars in HTTP method in httplib.putrequest to prevent HTTP header injection Backported from Python 3.5-3.10 (and adjusted for py2's single-module httplib): - https://bugs.python.org/issue39603 - python#18485 (3.10) - python#21946 (3.5) Co-authored-by: AMIR <31338382+amiremohamadi@users.noreply.github.com>
00354 # Reject control chars in HTTP method in httplib.putrequest to prevent HTTP header injection Backported from Python 3.5-3.10 (and adjusted for py2's single-module httplib): - https://bugs.python.org/issue39603 - python#18485 (3.10) - python#21946 (3.5) Co-authored-by: AMIR <31338382+amiremohamadi@users.noreply.github.com>
reject control chars in http method in http.client.putrequest to prevent http header injection
https://bugs.python.org/issue39603
Automerge-Triggered-By: @gvanrossum