Skip to content
Myr

support extraction of certificate bundle from a zip archive

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Arthur Vigil · committed by Arthur Vigil · parent 81b6341 · Browse files · 5 files+71 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+71 added−5 removed

  • tests:+29 added−1 removed
  • other:+42 added−4 removed

5 files+71 added−5 removed

modified:requests/adapters.py+4 added-4 removed
26 unmodified lines
27from urllib3.exceptions import ResponseError2829from .models import Response30from .compat import urlparse, basestring31from .utils import (DEFAULT_CA_BUNDLE_PATH, get_encoding_from_headers,32 prepend_scheme_if_needed, get_auth_from_url, urldefragauth,33 select_proxy)31from .utils import (DEFAULT_CA_BUNDLE_PATH, extract_zipped_paths,32 get_encoding_from_headers, prepend_scheme_if_needed,33 get_auth_from_url, urldefragauth, select_proxy)34from .structures import CaseInsensitiveDict35from .cookies import extract_cookies_to_jar36from .exceptions import (ConnectionError, ConnectTimeout, ReadTimeout, SSLError,37 ProxyError, RetryError, InvalidSchema)
180 unmodified lines
218 if verify is not True:219 cert_loc = verify220221 if not cert_loc:222 cert_loc = DEFAULT_CA_BUNDLE_PATH222 cert_loc = extract_zipped_paths(DEFAULT_CA_BUNDLE_PATH)223224 if not cert_loc or not os.path.exists(cert_loc):225 raise IOError("Could not find a suitable TLS CA certificate bundle, "226 "invalid path: {0}".format(cert_loc))
299 unmodified lines
modified:requests/utils.py+34 added
16 unmodified lines
17import re18import socket19import struct20import sys21import tempfile22import warnings23import zipfile2425from .__version__ import __version__26from . import certs27# to_native_string is unused here, but imported here for backwards compatibility
189 unmodified lines
217 name[-1] != '>'):218 return os.path.basename(name)219220221def extract_zipped_paths(path):222 """Replace nonexistant paths that look like they refer to a member of a zip223 archive with the location of an extracted copy of the target, or else224 just return the provided path unchanged.225 """226 if os.path.exists(path):227 # this is already a valid path, no need to do anything further228 return path229230 # find the first valid part of the provided path and treat that as a zip archive231 # assume the rest of the path is the name of a member in the archive232 archive, member = os.path.split(path)233 while archive and not os.path.exists(archive):234 archive, prefix = os.path.split(archive)235 member = '/'.join([prefix, member])236237 if not zipfile.is_zipfile(archive):238 return path239240 zip_file = zipfile.ZipFile(archive)241 if member not in zip_file.namelist():242 return path243244 # we have a valid zip archive and a valid member of that archive245 tmp = tempfile.gettempdir()246 extracted_path = os.path.join(tmp, *member.split('/'))247 if not os.path.exists(extracted_path):248 extracted_path = zip_file.extract(member, path=tmp)249250 return extracted_path251252253def from_key_val_list(value):254 """Take an object and test to see if it can be represented as a255 dictionary. Unless it can not be represented as such, return an256 OrderedDict, e.g.,
686 unmodified lines
modified:tests/test_utils.py+29 added-1 removed
1# -*- coding: utf-8 -*-23import os4import copy5import filecmp6from io import BytesIO7import zipfile89import pytest10from requests import compat11from requests.cookies import RequestsCookieJar12from requests.structures import CaseInsensitiveDict13from requests.utils import (12 address_in_network, dotted_netmask,14 address_in_network, dotted_netmask, extract_zipped_paths,15 get_auth_from_url, get_encoding_from_headers,16 get_encodings_from_content, get_environ_proxies,17 guess_filename, guess_json_utf, is_ipv4_address,18 is_valid_cidr, iter_slices, parse_dict_header,
238 unmodified lines
257 assert result == value258 assert isinstance(result, expected_type)259260261class TestExtractZippedPaths:262263 @pytest.mark.parametrize(264 'path', (265 '/',266 __file__,267 pytest.__file__,268 '/etc/invalid/location',269 ))270 def test_unzipped_paths_unchanged(self, path):271 assert path == extract_zipped_paths(path)272273 def test_zipped_paths_extracted(self, tmpdir):274 zipped_py = tmpdir.join('test.zip')275 with zipfile.ZipFile(zipped_py.strpath, 'w') as f:276 f.write(__file__)277278 _, name = os.path.splitdrive(__file__)279 zipped_path = os.path.join(zipped_py.strpath, name.lstrip(r'\/'))280 extracted_path = extract_zipped_paths(zipped_path)281282 assert extracted_path != zipped_path283 assert os.path.exists(extracted_path)284 assert filecmp.cmp(extracted_path, __file__)285286287class TestContentEncodingDetection:288289 def test_none(self):290 encodings = get_encodings_from_content('')
422 unmodified lines
modified:AUTHORS.rst+1 added
177 unmodified lines
178- Ryan Pineo (`@ryanpineo <https://github.com/ryanpineo>`_)179- Ed Morley (`@edmorley <https://github.com/edmorley>`_)180- Matt Liu <liumatt@gmail.com> (`@mlcrazy <https://github.com/mlcrazy>`_)181- Taylor Hoff <primdevs@protonmail.com> (`@PrimordialHelios <https://github.com/PrimordialHelios>`_)182- Arthur Vigil (`@ahvigil <https://github.com/ahvigil>`_)
modified:HISTORY.rst+3 added
11 unmodified lines
1213**Bugfixes**1415- Parsing empty ``Link`` headers with ``parse_header_links()`` no longer return one bogus entry16- Fixed issue where loading the default certificate bundle from a zip archive17 would raise an ``IOError``1819202.18.4 (2017-08-15)21+++++++++++++++++++2223**Improvements**
1518 unmodified lines