Improper certificate validation

Improper certificate validation

Description

A TLS client must check that the server certificate chains to a trusted certificate authority (CA) and matches the destination hostname.

Potential impact

Without these checks, someone on the network path can impersonate the server, read sensitive requests or alter responses.

Remediation

Keep the library’s certificate checks enabled. If you use a private CA, add it to the trusted CA store and make sure the server presents the correct certificate chain.

Examples

Use the CA file and client certificate paths for your environment. When a client certificate is required, keep checking the server’s chain and hostname as well.

Standard Library - SSL

Before

python
# Unsafe ssl code
import ssl

unsafe_ctx_1 = ssl._create_stdlib_context()
unsafe_ctx_2 = ssl._create_unverified_context()

unsafe_ctx_3 = ssl.create_default_context()
unsafe_ctx_3.check_hostname = False
unsafe_ctx_3.verify_mode = ssl.CERT_NONE

After

# Safe ssl code
import ssl

safe_ctx_1 = ssl.create_default_context()
safe_ctx_1.verify_mode = ssl.CERT_REQUIRED

safe_ctx_2 = ssl._create_default_https_context()

The calls to ssl._create_stdlib_context() and ssl._create_unverified_context() here skip verification. The third configuration disables hostname checking before setting ssl.CERT_NONE. Use ssl.create_default_context() and pass the destination hostname as server_hostname when wrapping the socket.

OpenSSL

Before

python
# Unsafe OpenSSL code
from OpenSSL import SSL

unsafe_ctx_1 = SSL.Context(SSL.TLSv1_2_METHOD)

unsafe_ctx_2 = SSL.Context(SSL.TLSv1_2_METHOD)
unsafe_ctx_2.set_verify(SSL.VERIFY_NONE)

After

python
# Safe OpenSSL code
from OpenSSL import SSL

ctx = SSL.Context(SSL.TLSv1_2_METHOD)
ctx.set_default_verify_paths()
ctx.set_verify(SSL.VERIFY_PEER)

Leaving verification unset or using SSL.VERIFY_NONE skips the server chain check. Load trusted roots with set_default_verify_paths() and verify the chain with SSL.VERIFY_PEER. Check the hostname separately. A custom callback must reject verification failures. VERIFY_FAIL_IF_NO_PEER_CERT applies to servers that require a client certificate.

Requests

Before

python
# Unsafe Requests code
import requests

requests.get('https://example.com', verify=False)

After

python
# Safe Requests code
import requests

requests.get('https://example.com')

To trust a private CA, use its CA bundle.

python
# Safe Request code with self-signed certificate
import requests

requests.get('https://private.com', verify="/path/to/private_cert")

Explanation

Requests verifies server certificates by default. Remove verify=False or set verify=True. For a private CA, pass the path to its trusted CA bundle as verify.

HTTPX

Before

python
# Unsafe HTTPX code
import httpx

httpx.get('https://example.com', verify=False)

After

python
# Safe HTTPX code
import httpx

httpx.get('https://example.com')

For a private CA, pass a context that loads its certificate.

python
# Safe HTTPX code with a trusted private CA
import ssl
import httpx

ctx = ssl.create_default_context(cafile="/path/to/private_cert")
httpx.get('https://private.com', verify=ctx)

If the server also requires client authentication, load the client certificate and private key into the context that verifies the server.

python
import ssl
import httpx

ctx = ssl.create_default_context()
ctx.load_cert_chain(certfile="path/to/client.pem")
client = httpx.Client(verify=ctx)
response = client.get("https://example.org")

Explanation

HTTPX verifies the server by default. Remove verify=False. For a private CA, pass the SSLContext as verify. If the client also needs to identify itself, load its credentials with load_cert_chain().

aiohttp

Before

python
# Unsafe aiohttp code
import aiohttp

async def main():
    async with aiohttp.ClientSession() as session:
        async with session.get("https://example.com", ssl=False):
            pass
        async with session.get("https://legacy.example.com", verify_ssl=False):
            pass

After

python
# Safe aiohttp code
import aiohttp

async def main():
    async with aiohttp.ClientSession() as session:
        async with session.get("https://example.com"):
            pass

Explanation

Remove ssl=False or the older verify_ssl=False to restore aiohttp’s certificate checks. Pass a trusted ssl.SSLContext if you need a different trust store. The async with statement sends the request and manages the response.

urllib3

Before

python
# Unsafe urllib3 code
import urllib3

http = urllib3.PoolManager(cert_reqs='CERT_NONE')
http.request('GET', 'https://example.com')

After

python
# Safe urllib3 code
import urllib3

http = urllib3.PoolManager(cert_reqs='CERT_REQUIRED', ca_certs='/path/to/ca-bundle.crt')
http.request('GET', 'https://example.com')

Change cert_reqs from CERT_NONE to CERT_REQUIRED to enable verification. For a private CA, set ca_certs to the path of its trusted CA bundle.

References