How do you resolve "SSL: CERTIFICATE_VERIFY_FAILED" errors in Python urllib? | A1Lab@MZeeshanM to PythonEnglish • 3 days ago

How do you resolve "SSL: CERTIFICATE_VERIFY_FAILED" errors in Python urllib?

message-square
0

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.org 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 bundles certifi automatically), Python’s standard library urllib relies on the operating system’s OpenSSL trust store (ssl.get_default_verify_paths()).
    • macOS (python.org installer): Official macOS Python builds ship with their own OpenSSL and an empty certificate directory until you run /Applications/Python\ 3.x/Install\ Certificates.command (or pip install --upgrade certifi).
    • Linux / Docker (python:3.x-slim): Minimal container images often lack the ca-certificates OS package; install it via apt-get update && apt-get install -y ca-certificates or pass cafile=certifi.where().
  • Fix globally via environment variables without changing application code (SSL_CERT_FILE):
    • Python’s ssl.create_default_context() automatically honors the SSL_CERT_FILE and SSL_CERT_DIR environment 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 .pem path to SSL_CERT_FILE—or install pip-system-certs (pip install pip-system-certs) so Python automatically trusts the Windows/macOS OS Certificate Store.
  • Check your system clock if valid public sites (like google.com or github.com) fail verification:
    • TLS certificates have strict Not Before and Not After timestamps. An inaccurate system clock on a VM, container, or embedded device causes OpenSSL to reject completely valid certificates with CERTIFICATE_VERIFY_FAILED.
  • Never ship ssl._create_unverified_context() (or ctx.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.

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.org 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.