Skip to content

bpo-23033: consider wildcard in left most segment only for domain names #937

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 10 commits into from
Nov 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Doc/library/ssl.rst
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,10 @@ Certificate handling
Matching of IP addresses, when present in the subjectAltName field
of the certificate, is now supported.

.. versionchanged:: 3.7
Allow wildcard when it is the leftmost and the only character
in that segment.

.. function:: cert_time_to_seconds(cert_time)

Return the time in seconds since the Epoch, given the ``cert_time``
Expand Down
9 changes: 7 additions & 2 deletions Lib/ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ class CertificateError(ValueError):
pass


def _dnsname_match(dn, hostname, max_wildcards=1):
def _dnsname_match(dn, hostname):
"""Matching according to RFC 6125, section 6.4.3

http://tools.ietf.org/html/rfc6125#section-6.4.3
Expand All @@ -233,7 +233,12 @@ def _dnsname_match(dn, hostname, max_wildcards=1):
leftmost, *remainder = dn.split(r'.')

wildcards = leftmost.count('*')
if wildcards > max_wildcards:
if wildcards == 1 and len(leftmost) > 1:
# Only match wildcard in leftmost segment.
raise CertificateError(
"wildcard can only be present in the leftmost segment: " + repr(dn))

if wildcards > 1:
# Issue #17980: avoid denials of service by refusing more
# than one wildcard per fragment. A survey of established
# policy among SSL implementations showed it to be a
Expand Down
13 changes: 7 additions & 6 deletions Lib/test/test_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,10 +512,11 @@ def fail(cert, hostname):
fail(cert, 'Xa.com')
fail(cert, '.a.com')

# only match one left-most wildcard
# only match wildcards when they are the only thing
# in left-most segment
cert = {'subject': ((('commonName', 'f*.com'),),)}
ok(cert, 'foo.com')
ok(cert, 'f.com')
fail(cert, 'foo.com')
fail(cert, 'f.com')
fail(cert, 'bar.com')
fail(cert, 'foo.a.com')
fail(cert, 'bar.foo.com')
Expand Down Expand Up @@ -552,8 +553,8 @@ def fail(cert, hostname):
# are supported.
idna = 'www*.pythön.org'.encode("idna").decode("ascii")
cert = {'subject': ((('commonName', idna),),)}
ok(cert, 'www.pythön.org'.encode("idna").decode("ascii"))
ok(cert, 'www1.pythön.org'.encode("idna").decode("ascii"))
fail(cert, 'www.pythön.org'.encode("idna").decode("ascii"))
fail(cert, 'www1.pythön.org'.encode("idna").decode("ascii"))
fail(cert, 'ftp.pythön.org'.encode("idna").decode("ascii"))
fail(cert, 'pythön.org'.encode("idna").decode("ascii"))

Expand Down Expand Up @@ -637,7 +638,7 @@ def fail(cert, hostname):
# Issue #17980: avoid denials of service by refusing more than one
# wildcard per fragment.
cert = {'subject': ((('commonName', 'a*b.com'),),)}
ok(cert, 'axxb.com')
fail(cert, 'axxb.com')
cert = {'subject': ((('commonName', 'a*b.co*'),),)}
fail(cert, 'axxb.com')
cert = {'subject': ((('commonName', 'a*b*.com'),),)}
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,7 @@ Nathan Paul Simons
Guilherme Simões
Adam Simpkins
Ravi Sinha
Mandeep Singh
Janne Sinkkonen
Ng Pheng Siong
Yann Sionneau
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Wildcard is now supported in hostname when it is one and only character in
the left most segment of hostname in second argument of
:meth:`ssl.match_hostname`. Patch by Mandeep Singh.