|
1 | 1 | import functools |
2 | 2 | import gc |
3 | 3 | import secrets |
| 4 | +import ssl |
4 | 5 | from importlib import resources |
5 | 6 | from importlib.metadata import version |
6 | 7 | from pathlib import Path |
@@ -65,18 +66,37 @@ def _setup_httpx_client( |
65 | 66 | ca_bundle: Path | None = None, |
66 | 67 | disable_tls_verify: bool | None = False, |
67 | 68 | ): |
68 | | - headers = {"User-Agent": f"{APP_NAME}/{APP_VERSION}"} |
| 69 | + return httpx.Client( |
| 70 | + base_url=api_url, |
| 71 | + headers={ |
| 72 | + "User-Agent": f"{APP_NAME}/{APP_VERSION}", |
| 73 | + }, |
| 74 | + verify=self._create_ssl_context( |
| 75 | + ca_bundle=ca_bundle, disable_tls_verify=disable_tls_verify |
| 76 | + ), |
| 77 | + ) |
| 78 | + |
| 79 | + def _create_ssl_context( |
| 80 | + self, |
| 81 | + ca_bundle: Path | None, |
| 82 | + disable_tls_verify: bool | None, |
| 83 | + ) -> ssl.SSLContext: |
| 84 | + |
69 | 85 | if disable_tls_verify: |
70 | | - return httpx.Client(base_url=api_url, headers=headers, verify=False) # noqa: S501 (TLS certificate validation disabled) |
| 86 | + context = ssl.create_default_context() |
| 87 | + context.check_hostname = False |
| 88 | + context.verify_mode = ssl.CERT_NONE |
| 89 | + return context |
71 | 90 |
|
72 | 91 | if ca_bundle is not None: |
73 | 92 | ca = ca_bundle.expanduser() |
74 | 93 | if not ca.exists(): |
75 | 94 | raise errors.InvalidCABundle |
76 | 95 |
|
77 | | - return httpx.Client(base_url=api_url, headers=headers, verify=str(ca)) |
| 96 | + return ssl.create_default_context(cafile=str(ca)) |
| 97 | + |
78 | 98 | with resources.path(keys, "ca.pem") as ca: |
79 | | - return httpx.Client(base_url=api_url, headers=headers, verify=str(ca)) |
| 99 | + return ssl.create_default_context(cafile=str(ca)) |
80 | 100 |
|
81 | 101 | def _load_key(self, key_name: str, path: Path | None = None): |
82 | 102 | if path is not None: |
|
0 commit comments