Choosing between a custom CA bundle and --trusted-host for pip installs behind an intercepting proxy
0 reputation · 12 Jul 2023, 11:09 UTC
0 reputation · 12 Jul 2023, 11:09 UTC
The goal is to decide how to configure pip for successful package installation from PyPI when an intercepting proxy causes TLS handshake failures due to re‑signed certificates.
When a custom CA bundle is supplied via --cert or PIP_CERT alongside the system trust store, the documentation does not specify whether pip prioritizes the explicit bundle or merges it with the store, creating ambiguous validation outcomes. Furthermore, if the bundle lacks the proxy's signing certificate, it is unclear whether pip falls back to the system store or aborts the handshake, which influences the choice between providing a bundle and adding the host to --trusted-host.
Does pip give precedence to an explicitly supplied --cert bundle over the system trust store when both are present? If the custom bundle does not contain the proxy's CA, does pip fall back to the system store or fail the TLS verification?
Pip gives precedence to an explicitly supplied --cert / PIP_CERT bundle over the system trust store, and it does not fall back to the system store when the bundle is used.
If you run pip with --cert or PIP_CERT set, pip uses that bundle as the sole trust anchor for TLS verification of the index and package URLs. The system trust store is ignored for that connection. If the bundle does not contain the proxy’s signing CA, the TLS handshake fails and the install aborts. There is no silent fallback to the system store.
--trusted-host disables certificate validation entirely for the named host. It will make the install succeed even with a missing or untrusted proxy CA, but it removes TLS verification for that host.
Confirmed behavior to plan around:
Likely explanation and assumptions:
The precedence is implemented in pip’s use of urllib3/requests where a custom CA file replaces the default certifi/system bundle. This is the expected behavior for modern pip releases. The exact internal handling can vary with the bundled TLS library and platform, so treat the above as the operational assumption until verified in your environment. Behavior prior to recent pip releases may differ.
openssl s_client -connect pypi.org:443 -CAfile /path/to/bundle.pem -servername pypi.org < /dev/null
Look for verification OK in the output.
pip install --cert /path/to/bundle.pem <package>
Or set the environment variable for the session:
PIP_CERT=/path/to/bundle.pem pip install <package>
pip debug
Check the Certificates section. With PIP_CERT set, pip will report the explicit path.
Avoid --trusted-host unless the proxy is fully trusted and you accept the risk of MITM. If you must use it, scope it to the minimal host and index URL.
Is the proxy’s signing CA already present in the system trust store on the machine running pip? If yes, omitting --cert will work and you can rely on system trust. If no, you must supply a bundle that contains the proxy CA, because pip will not fall back once --cert is provided and will fail if the CA is missing from the bundle.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.