Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Security and hardening

Python rejects a self-signed certificate in the chain

ErrorSSL: CERTIFICATE_VERIFY_FAILED

Short answer

Python could not build a trusted path from the server certificate to a root it knows. A company proxy, a private CA or a self-signed server certificate is in the chain. Give Python the CA file that signed it, through verify=, REQUESTS_CA_BUNDLE or an SSL context. Do not set verify=False outside local tests.

  • Applies to Python 3.13, requests 2.32, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print which CA files requests and the ssl module trust right now. The extra CA you need is missing from these.

    python -c "from requests.utils import DEFAULT_CA_BUNDLE_PATH; print(DEFAULT_CA_BUNDLE_PATH)"
    python -c "import ssl; print(ssl.get_default_verify_paths())"
  2. 2Match what you see, then jump to its fix

    1. 1The failing call uses requests, and a browser on the same network trusts the siterequests does not know your private or proxy CAFix
    2. 2The variable is exported and plain requests.get works, but code using Session.send or PreparedRequest still failsREQUESTS_CA_BUNDLE is set but ignored by a prepared requestFix
    3. 3No requests in the traceback; the error comes from urllib, http.client or a client library that takes an SSL contextCode that uses the ssl module or urllib needs its own contextFix

What does the error look like?

  • SSL: CERTIFICATE_VERIFY_FAILED
  • certificate verify failed: self signed certificate in certificate chain (_ssl.c:992)

Cause 1 of 3

requests does not know your private or proxy CA

1Check

requests verifies against the certifi bundle, not the operating system store. A TLS-inspecting proxy or private CA is missing from it.

python -c "import certifi; print(certifi.where())"

2Fix

Get the root CA certificate as a PEM file from whoever runs the proxy or server, and pass its path. verify takes a path to a CA bundle; setting REQUESTS_CA_BUNDLE does the same for the whole process.

import requests
r = requests.get("https://api.example.com", verify="/etc/ssl/private-ca/root.pem")

# or for the whole process
# shell: export REQUESTS_CA_BUNDLE=/etc/ssl/private-ca/root.pem

3Verify

The same request now returns a response.

python -c "import requests; print(requests.get('https://api.example.com').status_code)"

Confirmed by requests.readthedocs.io

Did this fix it?

Cause 2 of 3

REQUESTS_CA_BUNDLE is set but ignored by a prepared request

1Check

The requests docs say the prepared-request flow does not read environment settings, so a self-signed certificate named in REQUESTS_CA_BUNDLE is not taken into account.

grep -rn "prepare_request\|Request(" --include=*.py .

2Fix

Merge the environment settings into the session before sending.

s = requests.Session()
prepped = s.prepare_request(requests.Request("GET", url))
settings = s.merge_environment_settings(prepped.url, {}, None, None, None)
resp = s.send(prepped, **settings)

3Verify

The request returns instead of raising SSLError.

python your_script.py

Confirmed by requests.readthedocs.io

Did this fix it?

Cause 3 of 3

Code that uses the ssl module or urllib needs its own context

1Check

ssl.create_default_context() trusts the system defaults. Look at where those point.

python -c "import ssl; print(ssl.get_default_verify_paths())"

2Fix

Build a context that loads your CA file and pass it to the call. Verification stays on, with CERT_REQUIRED.

import ssl, urllib.request
ctx = ssl.create_default_context(cafile="/etc/ssl/private-ca/root.pem")
urllib.request.urlopen("https://api.example.com", context=ctx)

3Verify

The call returns a response.

python -c "import ssl; print(ssl.create_default_context(cafile='/etc/ssl/private-ca/root.pem').verify_mode)"

Confirmed by docs.python.org

Did this fix it?

How do I stop it coming back?

  • Keep the private CA as a file in your repo's deployment config, not as verify=False in code.
  • Update certifi regularly, as the requests docs recommend.
  • Use verify=False only in a throwaway local test; it makes the connection open to man-in-the-middle attacks.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

npm rejects the certificate it gets from a company proxy

npm ERR! request to https://registry.npmjs.org/express failed, reason: self signed certificate in certificate chain

A proxy that inspects HTTPS traffic re-signs it with the company's own root certificate, which Node does not know, so npm refuses the connection.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.