Install or point Python’s SSL context to a current root CA bundle using certifi (ssl.create_default_context(cafile=certifi.where())), run the macOS Install Certificates.command script if using the official python macOS installer, or pass your corporate proxy’s root .pem certificate to cafile=—avoiding ssl._create_unverified_context() in production because disabling certificate verification exposes HTTPS traffic to man-in-the-middle attacks.
Basic Example
import ssl
import urllib.request
import certifi
url = "[https://example.com](https://example.com)"
# 1. Recommended (cross-platform): Build an SSLContext backed by Mozilla's `certifi` CA bundle
ssl_ctx = ssl.create_default_context(cafile=certifi.where())
with urllib.request.urlopen(url, context=ssl_ctx, timeout=10) as response:
print(response.status)
# Output: 200
# 2. Corporate proxy / internal self-signed CA: Point `cafile` to your organization's root .pem
corp_ctx = ssl.create_default_context(cafile="/path/to/corporate-root-ca.pem")
with urllib.request.urlopen("[https://internal.corp.local](https://internal.corp.local)", context=corp_ctx, timeout=10) as resp:
print(resp.status)
Key Details & Edge Cases
- Why
urllib.request.urlopen()fails on macOS or minimal Docker containers:- Unlike
requests(which bundlescertifiautomatically), Python’s standard libraryurllibrelies on the operating system’s OpenSSL trust store (ssl.get_default_verify_paths()). - macOS (
pythoninstaller): Official macOS Python builds ship with their own OpenSSL and an empty certificate directory until you run/Applications/Python\ 3.x/Install\ Certificates.command(orpip install --upgrade certifi). - Linux / Docker (
python:3.x-slim): Minimal container images often lack theca-certificatesOS package; install it viaapt-get update && apt-get install -y ca-certificatesor passcafile=certifi.where().
- Unlike
- Fix globally via environment variables without changing application code (
SSL_CERT_FILE):- Python’s
ssl.create_default_context()automatically honors theSSL_CERT_FILEandSSL_CERT_DIRenvironment variables:export SSL_CERT_FILE=$(python -c "import certifi; print(certifi.where())") - On corporate networks with TLS-inspecting firewalls (Zscaler, Netskope, Cloudflare WARP), export your corporate root
.pempath toSSL_CERT_FILE—or installpip-system-certs(pip install pip-system-certs) so Python automatically trusts the Windows/macOS OS Certificate Store.
- Python’s
- Check your system clock if valid public sites (like
google.comorgithub.com) fail verification:- TLS certificates have strict
Not BeforeandNot Aftertimestamps. An inaccurate system clock on a VM, container, or embedded device causes OpenSSL to reject completely valid certificates withCERTIFICATE_VERIFY_FAILED.
- TLS certificates have strict
- Never ship
ssl._create_unverified_context()(orctx.verify_mode = ssl.CERT_NONE) to production:- Bypassing verification removes all server identity guarantees and leaves credentials, API keys, and tokens vulnerable to passive interception. Restrict unverified contexts strictly to throwaway local debugging against
localhost.
- Bypassing verification removes all server identity guarantees and leaves credentials, API keys, and tokens vulnerable to passive interception. Restrict unverified contexts strictly to throwaway local debugging against
Comparison of Fixes for SSL: CERTIFICATE_VERIFY_FAILED in urllib
| Fix / Approach | Scope | Security Level | Best Used When |
|---|---|---|---|
ssl.create_default_context(cafile=certifi.where()) (Recommended) |
Per-request / module | High (validates against Mozilla root CAs) | Cross-platform Python scripts, macOS, or slim Docker containers missing OS CA roots. |
macOS Install Certificates.command |
Entire Python installation | High (links certifi into Python’s OpenSSL) |
Fixing python macOS builds permanently for all stdlib urllib scripts. |
SSL_CERT_FILE=/path/to/ca.pem / pip-system-certs |
Process / environment-wide | High (trusts corporate/OS root store) | Corporate HTTPS proxies, internal CAs, or CI runners where you shouldn’t modify source code. |
ssl._create_unverified_context() |
Per-request | None (vulnerable to MITM attacks) | Temporary local testing against self-signed localhost dev servers only. |
You must log in or register to comment.
