bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
|
6613.1.1
by Vincent Ladeuil
Use ssl module for the match_hostname function |
1 |
# Copyright (C) 2011, 2012, 2013, 2016 Canonical Ltd
|
|
6238.2.10
by Jelmer Vernooij
Add more tests for ssl.ca_certs option. |
2 |
#
|
3 |
# This program is free software; you can redistribute it and/or modify
|
|
4 |
# it under the terms of the GNU General Public License as published by
|
|
5 |
# the Free Software Foundation; either version 2 of the License, or
|
|
6 |
# (at your option) any later version.
|
|
7 |
#
|
|
8 |
# This program is distributed in the hope that it will be useful,
|
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 |
# GNU General Public License for more details.
|
|
12 |
#
|
|
13 |
# You should have received a copy of the GNU General Public License
|
|
14 |
# along with this program; if not, write to the Free Software
|
|
15 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
16 |
||
17 |
"""Tests for the SSL support in the urllib HTTP transport.
|
|
18 |
||
19 |
"""
|
|
20 |
||
|
6238.2.19
by Vincent Ladeuil
Just hack the tests until they pass. |
21 |
import os |
|
6613.1.5
by Vincent Ladeuil
Help python2.6 compatibility. |
22 |
import sys |
|
6238.2.10
by Jelmer Vernooij
Add more tests for ssl.ca_certs option. |
23 |
|
|
6624
by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes') |
24 |
from .. import ( |
|
6238.2.19
by Vincent Ladeuil
Just hack the tests until they pass. |
25 |
config, |
26 |
trace, |
|
|
6613.1.1
by Vincent Ladeuil
Use ssl module for the match_hostname function |
27 |
)
|
|
6624
by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes') |
28 |
from .. import tests |
|
7296.2.1
by Jelmer Vernooij
Integrate _urllib2_wrappers. |
29 |
from ..transport.http import ssl, opt_ssl_ca_certs |
|
6613.1.5
by Vincent Ladeuil
Help python2.6 compatibility. |
30 |
|
31 |
||
32 |
class CaCertsConfigTests(tests.TestCaseInTempDir): |
|
|
6238.2.10
by Jelmer Vernooij
Add more tests for ssl.ca_certs option. |
33 |
|
|
6238.2.19
by Vincent Ladeuil
Just hack the tests until they pass. |
34 |
def get_stack(self, content): |
35 |
return config.MemoryStack(content.encode('utf-8')) |
|
36 |
||
|
6238.2.10
by Jelmer Vernooij
Add more tests for ssl.ca_certs option. |
37 |
def test_default_exists(self): |
|
6437.25.1
by Vincent Ladeuil
Provide default paths for ca certs for supported platforms |
38 |
"""Check that the default we provide exists for the tested platform.""" |
|
6238.2.19
by Vincent Ladeuil
Just hack the tests until they pass. |
39 |
stack = self.get_stack("") |
|
6437.25.1
by Vincent Ladeuil
Provide default paths for ca certs for supported platforms |
40 |
self.assertPathExists(stack.get('ssl.ca_certs')) |
|
6238.2.10
by Jelmer Vernooij
Add more tests for ssl.ca_certs option. |
41 |
|
42 |
def test_specified(self): |
|
43 |
self.build_tree(['cacerts.pem']) |
|
44 |
path = os.path.join(self.test_dir, "cacerts.pem") |
|
|
6238.2.19
by Vincent Ladeuil
Just hack the tests until they pass. |
45 |
stack = self.get_stack("ssl.ca_certs = %s\n" % path) |
|
6614.1.3
by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual. |
46 |
self.assertEqual(path, stack.get('ssl.ca_certs')) |
|
6238.2.10
by Jelmer Vernooij
Add more tests for ssl.ca_certs option. |
47 |
|
48 |
def test_specified_doesnt_exist(self): |
|
|
6437.25.6
by Vincent Ladeuil
Feedback from mgz. |
49 |
stack = self.get_stack('') |
50 |
# Disable the default value mechanism to force the behavior we want
|
|
|
7296.2.1
by Jelmer Vernooij
Integrate _urllib2_wrappers. |
51 |
self.overrideAttr(opt_ssl_ca_certs, 'default', |
|
6437.25.6
by Vincent Ladeuil
Feedback from mgz. |
52 |
os.path.join(self.test_dir, u"nonexisting.pem")) |
53 |
self.warnings = [] |
|
|
6613.1.1
by Vincent Ladeuil
Use ssl module for the match_hostname function |
54 |
|
|
6437.25.6
by Vincent Ladeuil
Feedback from mgz. |
55 |
def warning(*args): |
56 |
self.warnings.append(args[0] % args[1:]) |
|
57 |
self.overrideAttr(trace, 'warning', warning) |
|
|
6614.1.3
by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual. |
58 |
self.assertEqual(None, stack.get('ssl.ca_certs')) |
|
6437.25.6
by Vincent Ladeuil
Feedback from mgz. |
59 |
self.assertLength(1, self.warnings) |
60 |
self.assertContainsRe(self.warnings[0], |
|
61 |
"is not valid for \"ssl.ca_certs\"") |
|
|
6238.2.10
by Jelmer Vernooij
Add more tests for ssl.ca_certs option. |
62 |
|
63 |
||
|
6613.1.5
by Vincent Ladeuil
Help python2.6 compatibility. |
64 |
class CertReqsConfigTests(tests.TestCaseInTempDir): |
|
6238.2.10
by Jelmer Vernooij
Add more tests for ssl.ca_certs option. |
65 |
|
66 |
def test_default(self): |
|
|
7045.4.33
by Jelmer Vernooij
Fix https_urllib tests. |
67 |
stack = config.MemoryStack(b"") |
|
6614.1.3
by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual. |
68 |
self.assertEqual(ssl.CERT_REQUIRED, stack.get("ssl.cert_reqs")) |
|
6238.2.10
by Jelmer Vernooij
Add more tests for ssl.ca_certs option. |
69 |
|
70 |
def test_from_string(self): |
|
|
7045.4.33
by Jelmer Vernooij
Fix https_urllib tests. |
71 |
stack = config.MemoryStack(b"ssl.cert_reqs = none\n") |
|
6614.1.3
by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual. |
72 |
self.assertEqual(ssl.CERT_NONE, stack.get("ssl.cert_reqs")) |
|
7045.4.33
by Jelmer Vernooij
Fix https_urllib tests. |
73 |
stack = config.MemoryStack(b"ssl.cert_reqs = required\n") |
|
6614.1.3
by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual. |
74 |
self.assertEqual(ssl.CERT_REQUIRED, stack.get("ssl.cert_reqs")) |
|
7045.4.33
by Jelmer Vernooij
Fix https_urllib tests. |
75 |
stack = config.MemoryStack(b"ssl.cert_reqs = invalid\n") |
|
6737
by Jelmer Vernooij
Merge lp:~jelmer/brz/move-errors-config. |
76 |
self.assertRaises(config.ConfigOptionValueError, stack.get, |
77 |
"ssl.cert_reqs") |
|
|
6238.2.11
by Jelmer Vernooij
add basic tests for match_hostname. |
78 |
|
79 |
||
|
6613.1.5
by Vincent Ladeuil
Help python2.6 compatibility. |
80 |
class MatchHostnameTests(tests.TestCase): |
81 |
||
82 |
def setUp(self): |
|
83 |
super(MatchHostnameTests, self).setUp() |
|
84 |
if sys.version_info < (2, 7, 9): |
|
85 |
raise tests.TestSkipped( |
|
86 |
'python version too old to provide proper'
|
|
87 |
' https hostname verification') |
|
|
6238.2.11
by Jelmer Vernooij
add basic tests for match_hostname. |
88 |
|
89 |
def test_no_certificate(self): |
|
|
6238.2.19
by Vincent Ladeuil
Just hack the tests until they pass. |
90 |
self.assertRaises(ValueError, |
|
6613.1.1
by Vincent Ladeuil
Use ssl module for the match_hostname function |
91 |
ssl.match_hostname, {}, "example.com") |
|
6238.2.11
by Jelmer Vernooij
add basic tests for match_hostname. |
92 |
|
|
6573.1.1
by Andrew Starr-Bochicchio
Fix possible abuse of _urllib2_wrappers.match_hostname() for denial of service using certificates with many wildcards (CVE-2013-2099). |
93 |
def test_wildcards_in_cert(self): |
94 |
def ok(cert, hostname): |
|
|
6613.1.1
by Vincent Ladeuil
Use ssl module for the match_hostname function |
95 |
ssl.match_hostname(cert, hostname) |
96 |
||
97 |
def not_ok(cert, hostname): |
|
98 |
self.assertRaises( |
|
99 |
ssl.CertificateError, |
|
100 |
ssl.match_hostname, cert, hostname) |
|
|
6573.1.1
by Andrew Starr-Bochicchio
Fix possible abuse of _urllib2_wrappers.match_hostname() for denial of service using certificates with many wildcards (CVE-2013-2099). |
101 |
|
102 |
# Python Issue #17980: avoid denials of service by refusing more than
|
|
103 |
# one wildcard per fragment.
|
|
|
7206.3.4
by Jelmer Vernooij
Fix match_hostname test. |
104 |
if sys.version_info[:2] >= (3, 7): |
|
7206.3.7
by Jelmer Vernooij
Update comment about ssl. |
105 |
# Python 3.7 dropped support for partial wildcards, see
|
106 |
# https://docs.python.org/3/whatsnew/3.7.html#ssl
|
|
|
7206.3.4
by Jelmer Vernooij
Fix match_hostname test. |
107 |
not_ok({'subject': ((('commonName', 'a*b.com'),),)}, 'axxb.com') |
108 |
else: |
|
109 |
ok({'subject': ((('commonName', 'a*b.com'),),)}, 'axxb.com') |
|
|
6613.1.1
by Vincent Ladeuil
Use ssl module for the match_hostname function |
110 |
not_ok({'subject': ((('commonName', 'a*b.co*'),),)}, 'axxb.com') |
111 |
not_ok({'subject': ((('commonName', 'a*b*.com'),),)}, 'axxbxxc.com') |
|
|
6573.1.1
by Andrew Starr-Bochicchio
Fix possible abuse of _urllib2_wrappers.match_hostname() for denial of service using certificates with many wildcards (CVE-2013-2099). |
112 |
|
|
6238.2.11
by Jelmer Vernooij
add basic tests for match_hostname. |
113 |
def test_no_valid_attributes(self): |
|
6613.1.1
by Vincent Ladeuil
Use ssl module for the match_hostname function |
114 |
self.assertRaises(ssl.CertificateError, ssl.match_hostname, |
|
6238.2.19
by Vincent Ladeuil
Just hack the tests until they pass. |
115 |
{"Problem": "Solved"}, "example.com") |
|
6238.2.11
by Jelmer Vernooij
add basic tests for match_hostname. |
116 |
|
117 |
def test_common_name(self): |
|
118 |
cert = {'subject': ((('commonName', 'example.com'),),)} |
|
|
6238.2.19
by Vincent Ladeuil
Just hack the tests until they pass. |
119 |
self.assertIs(None, |
|
6613.1.1
by Vincent Ladeuil
Use ssl module for the match_hostname function |
120 |
ssl.match_hostname(cert, "example.com")) |
121 |
self.assertRaises(ssl.CertificateError, ssl.match_hostname, |
|
|
6238.2.19
by Vincent Ladeuil
Just hack the tests until they pass. |
122 |
cert, "example.org") |