download

Client set check_hostname = False for staging. What breaks in production?

Security~10 minReviewed 3 Dec 2026Free · No sign-in

Question

A billing worker hit CERTIFICATE_VERIFY_FAILED in staging because staging uses an internal CA. To unblock the deploy, someone added ctx.check_hostname = False and ctx.verify_mode = ssl.CERT_NONE to the shared TLS client.

  1. Why does disabling certificate or hostname checks open production traffic to active man-in-the-middle (MitM) interception, even over HTTPS?
  2. Fix build_tls_context(cafile=None) with ssl.create_default_context so production validates CA chains, hostnames, and TLS 1.2+, while staging loads its internal CA via cafile.
  3. If you restore CERT_REQUIRED but keep check_hostname = False, why is the client still unsafe?
"CODE · PYTHON"
import ssl


def build_tls_context(cafile=None):
    ctx = ssl.create_default_context()
    # Workaround for staging self-signed certificate.
    ctx.check_hostname = False
    ctx.verify_mode = ssl.CERT_NONE
    return ctx
Show hints (3)
  1. "ENCRYPTION VERSUS IDENTITY"

    TLS encrypts bytes on the wire, yet without checking the certificate chain and hostname, you can hold an encrypted tunnel directly with an active attacker.

  2. "CA CHAIN PLUS HOSTNAME"

    Keeping CERT_REQUIRED without check_hostname = True accepts any publicly trusted certificate, including a free certificate issued to attacker.example.

  3. "TRUST THE STAGING CA"

    Keep ssl.create_default_context(cafile=cafile) with CERT_REQUIRED, check_hostname = True, and TLSv1_2 minimum, passing the staging CA bundle path only in staging config.

Show checked explanation

Encryption without authentication lets an active network attacker terminate TLS with any certificate, read API keys, and tamper with billing payloads. Even with CERT_REQUIRED, leaving check_hostname = False accepts any CA-signed certificate for attacker.example. Keep ssl.create_default_context(cafile=cafile) with CERT_REQUIRED, check_hostname = True, and TLS 1.2+, loading the private CA bundle via cafile in staging.

"SOLUTION · PYTHON"
import ssl


def build_tls_context(cafile=None):
    ctx = ssl.create_default_context(cafile=cafile)
    ctx.verify_mode = ssl.CERT_REQUIRED
    ctx.check_hostname = True
    ctx.minimum_version = ssl.TLSVersion.TLSv1_2
    return ctx


ctx = build_tls_context()
print("verify_mode:", ctx.verify_mode.name)
print("check_hostname:", ctx.check_hostname)
print("min_version:", ctx.minimum_version.name)
"OUTPUT"
verify_mode: CERT_REQUIRED
check_hostname: True
min_version: TLSv1_2

Author-checked on CPython 3.12.10 (OpenSSL 3.0.16) with live loopback 127.0.0.1 TLS 1.3 handshakes using ephemeral synthetic P-256 test CA and leaf certificates for api.example and attacker.example; official Python ssl, OWASP TLS, and RFC links checked. Checked by an editor other than the author.

Walkthrough

HTTPS relies on two certificate checks during every TLS handshake. First, the certificate chain must sign up to a trusted Certificate Authority (verify_mode = ssl.CERT_REQUIRED) and be within its validity dates. Second, the certificate's Subject Alternative Name (SAN) must match the exact hostname you meant to reach (check_hostname = True with server_hostname="api.example").

client configuntrusted self-signed certCA-signed attacker.example certCA-signed api.example cert
Starter (CERT_NONE, check_hostname=False)Accepted (MitM)Accepted (MitM)Accepted
CERT_REQUIRED, check_hostname=FalseRejectedAccepted (MitM)Accepted
Fixed (cafile=None, system store)RejectedRejectedRejected (staging CA unknown)
Fixed (cafile=staging_ca.pem)RejectedRejected (Hostname mismatch)Accepted (TLSv1.3)

Why CERT_REQUIRED alone is still broken. Anyone can get a free, publicly trusted CA certificate for attacker.example in seconds. On a compromised Wi-Fi link, rogue proxy, or hijacked BGP/DNS route, that attacker intercepts traffic bound for api.example and presents their valid attacker.example certificate. Because the CA chain is valid, a client with check_hostname = False completes the handshake and sends Authorization: Bearer ... straight to the attacker.

Fix staging by trusting the staging CA, never by turning checks off. Pass cafile="/etc/ssl/staging-ca.pem" through environment config (such as REQUESTS_CA_BUNDLE or SSL_CERT_FILE). Notice an important Python detail: ssl.create_default_context(cafile=path) trusts only the CAs in path, isolating internal traffic from public CAs. If one client must call both public APIs and an internal CA, call ssl.create_default_context() first and then ctx.load_verify_locations(cafile=path) so system CAs stay loaded too. Never trust the staging CA in production.

Common mistakes

  • Fixing CERTIFICATE_VERIFY_FAILED with verify=False, CERT_NONE, or NODE_TLS_REJECT_UNAUTHORIZED=0. That turns off server authentication for every call.
  • Setting verify_mode = ssl.CERT_REQUIRED while leaving check_hostname = False. Any publicly signed certificate for attacker.example still passes.
  • Wrapping a socket without passing server_hostname=host to ctx.wrap_socket. Python needs server_hostname for both TLS SNI and hostname validation.
  • Bundling a staging root CA into production containers. A leaked staging CA key could then sign valid certificates for any domain your production worker calls.

Follow-up questions

  • "When should you use mutual TLS (mTLS) instead of standard one-way TLS?" Use mTLS for service-to-service or internal API calls where the server also needs cryptographic proof of the caller's identity. Both sides present short-lived workload certificates (for example via SPIFFE or a service mesh) and validate each other's CA chain and identity.
  • "Why have most browsers and backend teams moved away from static certificate pinning?" Static leaf-certificate pins cause sudden outages whenever a server rotates keys early, switches CAs, or replaces an expiring certificate before clients update. Prefer standard CA validation, short-lived certificates, Certificate Transparency monitoring, and mTLS for controlled internal links.
  • "Does Python's ssl.create_default_context() already require TLS 1.2 or newer?" Yes on modern OpenSSL builds in Python 3.12, where minimum_version defaults to TLSv1_2 or higher. Setting ctx.minimum_version = ssl.TLSVersion.TLSv1_2 explicitly guarantees an older OS OpenSSL policy cannot silently allow TLS 1.0 or 1.1.

What a strong answer covers

Distinguishes TLS encryption from server authentication and explains why CERT_REQUIRED without check_hostname still falls to attacker.example certs. Uses ssl.create_default_context(cafile=...) with TLS 1.2+ for staging CAs, and weighs mTLS against brittle cert pinning.

Open in interactive practice deck →

Original OfferVibe question. Python ssl.create_default_context · OWASP Transport Layer Security Cheat Sheet · RFC 9525 Service Identity in TLS · RFC 8446 TLS 1.3.
© 2026 OfferVibe · Text CC BY-NC-SA 4.0 · code MITFree to share and adapt with credit, not for commercial use. Code samples are free for any use.