okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
cd9745050
main
1"""#3722: HTTPS downloads check the server against the operating system's certificates (truststore), fall back to certifi, and a 2certificate that still fails gives a plain explanation, never the raw urlopen text and never a way to skip the check.""" 3 4import ssl 5import sys 6import types 7import unittest 8import urllib .error 9from pathlib import Path 10from unittest import mock 11 12ROOT = Path (__file__ ).resolve ().parents [2 ] 13sys .path .insert (0 ,str (ROOT )) 14from strata_control import tls # noqa: E402 15 16RAW = ("<urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate " 17"(_ssl.c:1010)>" ) 18 19 20def cert_error (): 21inner = ssl .SSLCertVerificationError (1 ,"[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate" ) 22return urllib .error .URLError (inner ) 23 24 25class Install (unittest .TestCase ): 26def setUp (self ): 27self .mode = tls .mode 28self .context = ssl ._create_default_https_context 29tls .mode = None 30self .addCleanup (self .restore ) 31 32def restore (self ): 33tls .mode = self .mode 34ssl ._create_default_https_context = self .context 35 36def test_truststore_is_used_when_present (self ): 37fake = types .SimpleNamespace (calls = 0 ) 38fake .inject_into_ssl = lambda :setattr (fake ,"calls" ,fake .calls + 1 ) 39with mock .patch .dict (sys .modules , {"truststore" :fake }): 40self .assertEqual (tls .install (),"truststore" ) 41self .assertEqual (tls .install (),"truststore" )# again: nothing more happens 42self .assertEqual (fake .calls ,1 ) 43 44def test_certifi_is_the_fallback_when_truststore_is_missing (self ): 45seen = {} 46fake_certifi = types .SimpleNamespace (where = lambda :"bundle.pem" ) 47with mock .patch .dict (sys .modules , {"truststore" :None ,"certifi" :fake_certifi }), \ 48mock .patch .object (ssl ,"create_default_context" ,lambda cafile = None ,** k :seen .setdefault ("cafile" ,cafile )): 49self .assertEqual (tls .install (),"certifi" ) 50ssl ._create_default_https_context () 51self .assertEqual (seen ["cafile" ],"bundle.pem" ) 52 53def test_python_default_stays_when_neither_exists (self ): 54with mock .patch .dict (sys .modules , {"truststore" :None ,"certifi" :None }): 55self .assertEqual (tls .install (),"default" ) 56self .assertIs (ssl ._create_default_https_context ,self .context ) 57 58def test_the_pins_name_truststore_in_requirements_and_every_lock (self ): 59self .assertIn ("truststore==" , (ROOT / "requirements.txt" ).read_text ()) 60for lock in (ROOT / "packaging" / "locks" ).glob ("runtime-*-cp312.txt" ): 61text = lock .read_text () 62self .assertRegex (text ,r"truststore==\S+ \\r?\n\s+--hash=sha256:[0-9a-f]{64}" ,lock .name ) 63self .assertIn ("certifi==" ,text )# the fallback stays 64 65 66class Plain (unittest .TestCase ): 67def test_a_failed_certificate_is_recognised_however_it_is_wrapped (self ): 68self .assertTrue (tls .is_certificate_error (cert_error ())) 69self .assertTrue (tls .is_certificate_error (OSError (RAW ))) 70wrapped = RuntimeError ("x" ) 71wrapped .__cause__ = cert_error () 72self .assertTrue (tls .is_certificate_error (wrapped )) 73self .assertFalse (tls .is_certificate_error (urllib .error .URLError (ConnectionRefusedError ()))) 74self .assertFalse (tls .is_certificate_error (None )) 75 76def test_the_message_is_plain_and_never_offers_to_skip_the_check (self ): 77text = tls .explain ("github.com" ) 78for word in ("github.com" ,"antivirus" ,"proxy" ,"date and time" ,"Windows Update" ): 79self .assertIn (word ,text ) 80for raw in ("urlopen" ,"CERTIFICATE_VERIFY_FAILED" ,"_ssl.c" ,"issuer" ): 81self .assertNotIn (raw ,text ) 82self .assertRegex (text ,r"will not skip the check" ) 83self .assertNotRegex (text .lower (),r"verify\s*=\s*false|insecure|disable (the )?verification|turn off (the )?(certificate|verification)" ) 84 85def test_setup_urlopen_raises_the_plain_problem (self ): 86import setup 87request = types .SimpleNamespace (full_url = "https://github.com/x/y" ) 88with mock .patch .object (setup .acquisition ,"require" ,lambda * a ,** k :None ), \ 89mock .patch .object (setup .netpolicy ,"urlopen" ,side_effect = cert_error ()): 90with self .assertRaises (tls .CertificateProblem )as cm : 91setup ._urlopen (request ) 92self .assertIn ("github.com" ,str (cm .exception )) 93self .assertNotIn ("CERTIFICATE_VERIFY_FAILED" ,str (cm .exception )) 94 95def test_setup_urlopen_leaves_other_errors_alone (self ): 96import setup 97refused = urllib .error .URLError (ConnectionRefusedError ()) 98with mock .patch .object (setup .acquisition ,"require" ,lambda * a ,** k :None ), \ 99mock .patch .object (setup .netpolicy ,"urlopen" ,side_effect = refused ): 100with self .assertRaises (urllib .error .URLError ): 101setup ._urlopen (types .SimpleNamespace (full_url = "https://github.com/x" )) 102 103def test_the_update_reader_gives_the_plain_message (self ): 104from strata_control import transfer 105with mock .patch .object (transfer ,"check_url_policy" ,lambda url :None ), \ 106mock .patch .object (transfer ._opener ,"open" ,side_effect = cert_error ()): 107with self .assertRaises (transfer .TransferError )as cm : 108transfer .read_small ("https://updates.example.org/feed.json" ) 109self .assertIn ("antivirus" ,str (cm .exception )) 110self .assertNotIn ("urlopen" ,str (cm .exception )) 111 112 113if __name__ == "__main__" : 114unittest .main ()