-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
bpo-37463: match_hostname requires quad-dotted IPv4 #14499
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -327,12 +327,22 @@ def _inet_paton(ipname): | |
Supports IPv4 addresses on all platforms and IPv6 on platforms with IPv6 | ||
support. | ||
""" | ||
# inet_aton() also accepts strings like '1' | ||
if ipname.count('.') == 3: | ||
try: | ||
return _socket.inet_aton(ipname) | ||
except OSError: | ||
pass | ||
# inet_aton() also accepts strings like '1', '127.1', some also trailing | ||
# data like '127.0.0.1 whatever'. | ||
try: | ||
addr = _socket.inet_aton(ipname) | ||
except OSError: | ||
# not an IPv4 address | ||
pass | ||
else: | ||
if _socket.inet_ntoa(addr) == ipname: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason not to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just notice that you're explicitly testing trailing whitespace below. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The |
||
# only accept injective ipnames | ||
return addr | ||
else: | ||
# refuse for short IPv4 notation and additional trailing data | ||
raise ValueError( | ||
"{!r} is not a quad-dotted IPv4 address.".format(ipname) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW, on Windows if you pass an invalid address you get There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some implementations of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick: usually, Python error messages don't end with a dot. (Same comment for the 3 error messages.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm following the style of the surrounding code. The exception messages are full sentences and end with a full stop. |
||
) | ||
|
||
try: | ||
return _socket.inet_pton(_socket.AF_INET6, ipname) | ||
|
@@ -346,14 +356,15 @@ def _inet_paton(ipname): | |
raise ValueError("{!r} is not an IPv4 address.".format(ipname)) | ||
|
||
|
||
def _ipaddress_match(ipname, host_ip): | ||
def _ipaddress_match(cert_ipaddress, host_ip): | ||
"""Exact matching of IP addresses. | ||
|
||
RFC 6125 explicitly doesn't define an algorithm for this | ||
(section 1.7.2 - "Out of Scope"). | ||
""" | ||
# OpenSSL may add a trailing newline to a subjectAltName's IP address | ||
ip = _inet_paton(ipname.rstrip()) | ||
# OpenSSL may add a trailing newline to a subjectAltName's IP address, | ||
# commonly woth IPv6 addresses. Strip off trailing \n. | ||
ip = _inet_paton(cert_ipaddress.rstrip()) | ||
return ip == host_ip | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
ssl.match_hostname() no longer accepts IPv4 addresses with additional text | ||
after the address and only quad-dotted notation without trailing | ||
whitespaces. Some inet_aton() implementations ignore whitespace and all data | ||
after whitespace, e.g. '127.0.0.1 whatever'. |
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.
_ipaddress_match() is documented as "Exact matching of IP addresses." but is explicitly strips trailing spaces. Maybe just add a sentence to explain that in its docstring?
_inet_paton() disallow leading and trailing whitespaces: maybe explain that in its docstring?
_inet_paton() allows short form of IPv6 addresses, but not for IPv4 address. Is it a deliberate choice? Again, maybe document it in the docstring.
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 don't think it is useful to add more doc strings to internal helper methods for a deprecated feature.
ssl.match_hostname
is deprecated and I plan to drop it in 3.9 or 3.10.The
strip
in_ipaddress_match()
is an implementation detail that works around a quirk in OpenSSL. The IP address may sometimes contain a newline. IIRC IPv6 addresses only.Yes, that is deliberate. Short notation of IPv4 addresses is an uncommon and rarely used feature. However short notation of IPv6 is pretty much the default. The behavior also reflects the behavior of the old implementation with ipaddress module.