Skip to content

Add custom CA certificate support and enforce trust_env=False TLS isolation - #77

Open
Rishi-Saraf wants to merge 2 commits into
prooflinedev:mainfrom
Rishi-Saraf:feat/ca-cert-and-tls-isolation
Open

Rishi-Saraf wants to merge 2 commits into
prooflinedev:mainfrom
Rishi-Saraf:feat/ca-cert-and-tls-isolation

Conversation

@Rishi-Saraf

@Rishi-Saraf Rishi-Saraf commented Nov 21, 2026 •

Copy link
Copy Markdown

Problem

  1. Silent trust_env=False bypass in both HTTP transports: ProoflineClient and AsyncProoflineClient explicitly pass trust_env=False to httpx2.Client / httpx2.AsyncClient so ambient environment variables cannot hijack connections or substitute the trust store. However, because custom transports are supplied directly:
    • proofline/_sync_http.py called httpcore2.default_ssl_context() with the default _trust_env=True.
    • proofline/_transport.py called httpx2.AsyncHTTPTransport() with the default trust_env=True.
      As a result, ambient SSL_CERT_FILE or SSL_CERT_DIR variables silently replaced the OS trust store in both sync and async clients.
  2. Missing custom CA root support (CLI parity): Unlike proofline-cli (--ca-cert / PROOFLINE_CA_CERT in src/tls.rs), the Python SDK offered no way for teams behind TLS-inspecting corporate proxies to trust internal root certificates beside system roots.

Approach

  • proofline/_tls.py: Added build_ssl_context(ca_cert=None) to construct httpcore2.default_ssl_context(_trust_env=False) (preserving OS trust roots while ignoring SSL_CERT_FILE and SSL_CERT_DIR) and additively merge PEM root certificates from ca_cert or PROOFLINE_CA_CERT without weakening CERT_REQUIRED or check_hostname.
    • Explicit ca_cert (str | os.PathLike[str]) overrides PROOFLINE_CA_CERT; an empty PROOFLINE_CA_CERT counts as unset.
    • Opens the configured path with O_RDONLY | O_NONBLOCK (POSIX) / O_BINARY (Windows), verifies stat.S_ISREG(os.fstat(fd).st_mode) on the open file descriptor (rejecting directories with IsADirectoryError/OSError and rejecting FIFOs/device nodes without blocking), and bounds reads to 4 MiB (MAX_CA_CERT_BYTES + 1).
    • Decodes PEM bodies strictly via base64.b64decode(..., validate=True) rather than ssl.PEM_cert_to_DER_cert (which silently strips stray non-Base64 punctuation), handles UTF-8 BOM, CRLF, UTF-8 comments, and duplicate certificates across multi-cert PEM bundles, and validates every certificate block individually through OpenSSL so corrupted certs fail closed at construction.
  • proofline/_sync_http.py & proofline/_transport.py: Wired optional ssl_context into Transport, RetryTransport, and AsyncRetryTransport, defaulting both transports to _trust_env=False / trust_env=False.
  • proofline/client.py & README.md: Added keyword-only ca_cert: str | os.PathLike[str] | None = None to both ProoflineClient and AsyncProoflineClient (shared across REST, exports, and MCP calls) and documented the behavior.

Follow-Up Hardening from Proofline Review (b347573)

After running Proofline on the initial commit (4a05d91) on my fork (https://proofline.sh/orgs/github/rishi-saraf/reviews/pr/sdk-python/1/runs/1), I addressed all 6 actionable findings in b347573:

  1. Replaced ssl.PEM_cert_to_DER_cert with strict base64.b64decode(..., validate=True) and added a punctuated-base64.pem regression test (preventing silent stripping of non-Base64 punctuation).
  2. Added O_NONBLOCK + os.fstat(fd) regular-file validation before reading, with a non-blocking FIFO test (os.mkfifo on POSIX + fstat guard on Windows).
  3. Split SSL_CERT_FILE and SSL_CERT_DIR isolation tests and populated a real OpenSSL <subject_hash>.0 directory with an OpenSSL positive control.
  4. Moved TLS handshake in _QuietTlsServer from get_request() (accept thread) into process_request_thread() with a 5s socket timeout.
  5. Added a direct same-leaf positive control (server_hostname="wrong.example.invalid" succeeds, "127.0.0.1" raises ssl.SSLCertVerificationError) to the hostname-verification test.
  6. Added test_read_bounded_stops_at_limit_plus_one proving _read_bounded issues a bounded read(MAX_CA_CERT_BYTES + 1) rather than reading an entire oversized file.

End-to-End Verification

Added tests/test_tls.py (31 tests, zero committed private keys—ephemeral ECDSA X.509 CAs and leaf certificates are generated in tmp_path_factory via openssl and served over a real local ThreadingHTTPServer TLS peer) covering both ProoflineClient and AsyncProoflineClient.

Checks run locally in .venv (pinned requirements-dev.txt):

python -m ruff format --check .
python -m ruff check .
python -m mypy proofline scripts tests/test_tls.py
python scripts/contracts.py
python scripts/generate.py --check
python -m pytest tests/test_tls.py -v
  • ruff format --check .: 225 files already formatted
  • ruff check .: All checks passed
  • mypy proofline scripts tests/test_tls.py: Success: no issues found in 216 source files
  • scripts/contracts.py: Python operation and outcome-vocabulary contracts passed
  • pytest tests/test_tls.py -v: 31 passed (full suite verified at 249 passed)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant