Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit de9ac6c

Browse files
committed
Issue #14780: urllib.request.urlopen() now has a cadefault argument to use the default certificate store.
Initial patch by James Oakley.
1 parent 5d95318 commit de9ac6c

5 files changed

Lines changed: 30 additions & 7 deletions

File tree

Doc/library/urllib.request.rst

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ authentication, redirections, cookies and more.
1616
The :mod:`urllib.request` module defines the following functions:
1717

1818

19-
.. function:: urlopen(url, data=None[, timeout], *, cafile=None, capath=None)
19+
.. function:: urlopen(url, data=None[, timeout], *, cafile=None, capath=None, cadefault=True)
2020

2121
Open the URL *url*, which can be either a string or a
2222
:class:`Request` object.
@@ -53,9 +53,15 @@ The :mod:`urllib.request` module defines the following functions:
5353
point to a directory of hashed certificate files. More information can
5454
be found in :meth:`ssl.SSLContext.load_verify_locations`.
5555

56+
The *cadefault* parameter specifies whether to fall back to loading a
57+
default certificate store defined by the underlying OpenSSL library if the
58+
*cafile* and *capath* parameters are omitted. This will only work on
59+
some non-Windows platforms.
60+
5661
.. warning::
57-
If neither *cafile* nor *capath* is specified, an HTTPS request
58-
will not do any verification of the server's certificate.
62+
If neither *cafile* nor *capath* is specified, and *cadefault* is False,
63+
an HTTPS request will not do any verification of the server's
64+
certificate.
5965

6066
This function returns a file-like object that works as a :term:`context manager`,
6167
with two additional methods from the :mod:`urllib.response` module
@@ -92,6 +98,9 @@ The :mod:`urllib.request` module defines the following functions:
9298
.. versionadded:: 3.2
9399
*data* can be an iterable object.
94100

101+
.. versionchanged:: 3.3
102+
*cadefault* was added.
103+
95104
.. function:: install_opener(opener)
96105

97106
Install an :class:`OpenerDirector` instance as the default global opener.

Lib/test/test_urllib2_localnet.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,13 @@ def test_https_with_cafile(self):
474474
self.urlopen("https://localhost:%s/bizarre" % handler.port,
475475
cafile=CERT_fakehostname)
476476

477+
def test_https_with_cadefault(self):
478+
handler = self.start_https_server(certfile=CERT_localhost)
479+
# Self-signed cert should fail verification with system certificate store
480+
with self.assertRaises(urllib.error.URLError) as cm:
481+
self.urlopen("https://localhost:%s/bizarre" % handler.port,
482+
cadefault=True)
483+
477484
def test_sending_headers(self):
478485
handler = self.start_server()
479486
req = urllib.request.Request("http://localhost:%s/" % handler.port,

Lib/urllib/request.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,19 @@
135135

136136
_opener = None
137137
def urlopen(url, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
138-
*, cafile=None, capath=None):
138+
*, cafile=None, capath=None, cadefault=False):
139139
global _opener
140-
if cafile or capath:
140+
if cafile or capath or cadefault:
141141
if not _have_ssl:
142142
raise ValueError('SSL support not available')
143143
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
144144
context.options |= ssl.OP_NO_SSLv2
145-
if cafile or capath:
145+
if cafile or capath or cadefault:
146146
context.verify_mode = ssl.CERT_REQUIRED
147-
context.load_verify_locations(cafile, capath)
147+
if cafile or capath:
148+
context.load_verify_locations(cafile, capath)
149+
else:
150+
context.set_default_verify_paths()
148151
check_hostname = True
149152
else:
150153
check_hostname = False

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,7 @@ Nigel O'Brian
746746
John O'Connor
747747
Kevin O'Connor
748748
Tim O'Malley
749+
James Oakley
749750
Jon Oberheide
750751
Pascal Oberndoerfer
751752
Jeffrey Ollie

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ Core and Builtins
3434
Library
3535
-------
3636

37+
- Issue #14780: urllib.request.urlopen() now has a ``cadefault`` argument
38+
to use the default certificate store. Initial patch by James Oakley.
39+
3740
- Issue #14829: Fix bisect and range() indexing with large indices
3841
(>= 2 ** 32) under 64-bit Windows.
3942

0 commit comments

Comments
 (0)