Skip to content

Commit 25435ce

Browse files
author
Hugo Osvaldo Barrera
authored
Merge pull request pimutils#903 from pimutils/fix-old-ssl-tests
Fix SSL tests failing due to old weak MDs
2 parents 59b6e24 + 1f6cc6f commit 25435ce

File tree

5 files changed

+74
-13
lines changed

5 files changed

+74
-13
lines changed

.builds/archlinux.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ packages:
1717
# Test dependencies:
1818
- python-hypothesis
1919
- python-pytest-cov
20-
- python-pytest-localserver
20+
- python-pytest-httpserver
21+
- python-trustme
2122
sources:
2223
- https://github.com/pimutils/vdirsyncer
2324
environment:

CHANGELOG.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ Version 0.19.0
1818
- Add a new "showconfig" status. This prints *some* configuration values as
1919
JSON. This is intended to be used by external tools and helpers that interact
2020
with ``vdirsyncer``.
21+
- Update TLS-related tests that were failing due to weak MDs.
22+
- ``pytest-httpserver`` and ``trustme`` are now required for tests.
23+
- ``pytest-localserver`` is no longer required for tests.
2124

2225
Version 0.18.0
2326
==============

test-requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
hypothesis>=5.0.0,<7.0.0
22
pytest
33
pytest-cov
4-
pytest-localserver
4+
pytest-httpserver
5+
trustme

tests/system/conftest.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import ssl
2+
3+
import pytest
4+
import trustme
5+
6+
7+
@pytest.fixture(scope="session")
8+
def ca():
9+
return trustme.CA()
10+
11+
12+
@pytest.fixture(scope="session")
13+
def localhost_cert(ca):
14+
return ca.issue_cert("localhost")
15+
16+
17+
@pytest.fixture(scope="session")
18+
def httpserver_ssl_context(localhost_cert):
19+
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
20+
21+
crt = localhost_cert.cert_chain_pems[0]
22+
key = localhost_cert.private_key_pem
23+
with crt.tempfile() as crt_file, key.tempfile() as key_file:
24+
context.load_cert_chain(crt_file, key_file)
25+
26+
return context

tests/system/utils/test_main.py

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import click_log
55
import pytest
66
import requests
7+
from cryptography import x509
8+
from cryptography.hazmat.primitives import hashes
79

810
from vdirsyncer import http
911
from vdirsyncer import utils
@@ -38,27 +40,55 @@ def _fingerprints_broken():
3840
return broken_urllib3
3941

4042

43+
def fingerprint_of_cert(cert, hash=hashes.SHA256):
44+
return x509.load_pem_x509_certificate(cert.bytes()).fingerprint(hash()).hex()
45+
46+
4147
@pytest.mark.skipif(
4248
_fingerprints_broken(), reason="https://github.com/shazow/urllib3/issues/529"
4349
)
44-
@pytest.mark.parametrize(
45-
"fingerprint",
46-
[
47-
"94:FD:7A:CB:50:75:A4:69:82:0A:F8:23:DF:07:FC:69:3E:CD:90:CA",
48-
"19:90:F7:23:94:F2:EF:AB:2B:64:2D:57:3D:25:95:2D",
49-
],
50+
@pytest.mark.parametrize("hash_algorithm", [hashes.MD5, hashes.SHA256])
51+
def test_request_ssl_leaf_fingerprint(httpserver, localhost_cert, hash_algorithm):
52+
fingerprint = fingerprint_of_cert(localhost_cert.cert_chain_pems[0], hash_algorithm)
53+
54+
# We have to serve something:
55+
httpserver.expect_request("/").respond_with_data("OK")
56+
url = f"https://{httpserver.host}:{httpserver.port}/"
57+
58+
http.request("GET", url, verify=False, verify_fingerprint=fingerprint)
59+
with pytest.raises(requests.exceptions.ConnectionError) as excinfo:
60+
http.request("GET", url, verify_fingerprint=fingerprint)
61+
62+
with pytest.raises(requests.exceptions.ConnectionError) as excinfo:
63+
http.request(
64+
"GET",
65+
url,
66+
verify=False,
67+
verify_fingerprint="".join(reversed(fingerprint)),
68+
)
69+
assert "Fingerprints did not match" in str(excinfo.value)
70+
71+
72+
@pytest.mark.skipif(
73+
_fingerprints_broken(), reason="https://github.com/shazow/urllib3/issues/529"
5074
)
51-
def test_request_ssl_fingerprints(httpsserver, fingerprint):
52-
httpsserver.serve_content("") # we need to serve something
75+
@pytest.mark.xfail(reason="Not implemented")
76+
@pytest.mark.parametrize("hash_algorithm", [hashes.MD5, hashes.SHA256])
77+
def test_request_ssl_ca_fingerprint(httpserver, ca, hash_algorithm):
78+
fingerprint = fingerprint_of_cert(ca.cert_pem)
79+
80+
# We have to serve something:
81+
httpserver.expect_request("/").respond_with_data("OK")
82+
url = f"https://{httpserver.host}:{httpserver.port}/"
5383

54-
http.request("GET", httpsserver.url, verify=False, verify_fingerprint=fingerprint)
84+
http.request("GET", url, verify=False, verify_fingerprint=fingerprint)
5585
with pytest.raises(requests.exceptions.ConnectionError) as excinfo:
56-
http.request("GET", httpsserver.url, verify_fingerprint=fingerprint)
86+
http.request("GET", url, verify_fingerprint=fingerprint)
5787

5888
with pytest.raises(requests.exceptions.ConnectionError) as excinfo:
5989
http.request(
6090
"GET",
61-
httpsserver.url,
91+
url,
6292
verify=False,
6393
verify_fingerprint="".join(reversed(fingerprint)),
6494
)

0 commit comments

Comments
 (0)