-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
gh-135993: Fix IPv6 bug in set_ok_port
and return_ok_port
#136076
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?
gh-135993: Fix IPv6 bug in set_ok_port
and return_ok_port
#136076
Conversation
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Sorry for trying to merge the wrong branch. I'm fixing it. Done now, sorry for the noise. |
Misc/NEWS.d/next/Library/2025-06-28-14-10-07.gh-issue-135993.Gmyux9.rst
Outdated
Show resolved
Hide resolved
…myux9.rst Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
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.
Please add a test for empty port (e.g. "acme.com:") and nonnumeric port. I suspect there is an unintentional behavior difference here.
Add also tests for empty and nonnumeric port and for IPv6 address in test_request_port
.
Lib/http/cookiejar.py
Outdated
port = host[i+1:] | ||
match = cut_port_re.search(request.host) | ||
if match: | ||
port = match.group(0).removeprefix(':') | ||
try: |
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 code no longer works, right?
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.
yes. To this code IPv6 is always an unintend. Not only here, there are other parts in http.cookiejar that has this problem
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.
I meant that int(port)
no longer raises ValueError (except the case of very long string), so this code can be removed.
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.
Sure!
for empty port and nonnumeric port, the regex can't match the port and will treat it as DEFAULT_HTTP_PORT(which is normally 80). I think this is intended.
Done. Thanks! |
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.
You have removed wrong code.
Oups, my bad. |
@@ -1075,11 +1069,7 @@ def set_ok_domain(self, cookie, request): | |||
|
|||
def set_ok_port(self, cookie, request): | |||
if cookie.port_specified: | |||
req_port = request_port(request) | |||
if req_port is None: |
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.
I remove this also, since request_port()
will no longer return None.
Note: In fact the original code here is also wrong. the req_port should be DEFAULT_HTTP_PORT instead of 80. nvm, we just remove this logic.
Yet another IPv6 bug fix.
Instead of using the helper functions here, I've found a regex in the code, it looks like this:
This could perfectly solve the bug. caz currently, we are doing this:
And the problem is if the input is IPv6 addr like
[::1]:1234
, the program will treat:1]:1234
as a port and raise a ValueError when trying toint()
it.Now, since we've got the regex, we could use it directly in this func to make sure that port numbers can only be numbers and fix the bug.
Something like this:
I've also added the tests.
cc @picnixz . Looking forward to your review, thank you for your time and effort in advance.
📚 Documentation preview 📚: https://cpython-previews--136076.org.readthedocs.build/