Repository navigation
Add custom CA certificate support and enforce trust_env=False TLS isolation - #77
Open
Rishi-Saraf wants to merge 2 commits into
Open
Rishi-Saraf wants to merge 2 commits into
Rishi-Saraf wants to merge 2 commits into
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
trust_env=Falsebypass in both HTTP transports:ProoflineClientandAsyncProoflineClientexplicitly passtrust_env=Falsetohttpx2.Client/httpx2.AsyncClientso ambient environment variables cannot hijack connections or substitute the trust store. However, because custom transports are supplied directly:proofline/_sync_http.pycalledhttpcore2.default_ssl_context()with the default_trust_env=True.proofline/_transport.pycalledhttpx2.AsyncHTTPTransport()with the defaulttrust_env=True.As a result, ambient
SSL_CERT_FILEorSSL_CERT_DIRvariables silently replaced the OS trust store in both sync and async clients.proofline-cli(--ca-cert/PROOFLINE_CA_CERTinsrc/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: Addedbuild_ssl_context(ca_cert=None)to constructhttpcore2.default_ssl_context(_trust_env=False)(preserving OS trust roots while ignoringSSL_CERT_FILEandSSL_CERT_DIR) and additively merge PEM root certificates fromca_certorPROOFLINE_CA_CERTwithout weakeningCERT_REQUIREDorcheck_hostname.ca_cert(str | os.PathLike[str]) overridesPROOFLINE_CA_CERT; an emptyPROOFLINE_CA_CERTcounts as unset.O_RDONLY | O_NONBLOCK(POSIX) /O_BINARY(Windows), verifiesstat.S_ISREG(os.fstat(fd).st_mode)on the open file descriptor (rejecting directories withIsADirectoryError/OSErrorand rejecting FIFOs/device nodes without blocking), and bounds reads to 4 MiB (MAX_CA_CERT_BYTES + 1).base64.b64decode(..., validate=True)rather thanssl.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 optionalssl_contextintoTransport,RetryTransport, andAsyncRetryTransport, defaulting both transports to_trust_env=False/trust_env=False.proofline/client.py&README.md: Added keyword-onlyca_cert: str | os.PathLike[str] | None = Noneto bothProoflineClientandAsyncProoflineClient(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 inb347573:ssl.PEM_cert_to_DER_certwith strictbase64.b64decode(..., validate=True)and added apunctuated-base64.pemregression test (preventing silent stripping of non-Base64 punctuation).O_NONBLOCK+os.fstat(fd)regular-file validation before reading, with a non-blocking FIFO test (os.mkfifoon POSIX +fstatguard on Windows).SSL_CERT_FILEandSSL_CERT_DIRisolation tests and populated a real OpenSSL<subject_hash>.0directory with an OpenSSL positive control._QuietTlsServerfromget_request()(accept thread) intoprocess_request_thread()with a 5s socket timeout.server_hostname="wrong.example.invalid"succeeds,"127.0.0.1"raisesssl.SSLCertVerificationError) to the hostname-verification test.test_read_bounded_stops_at_limit_plus_oneproving_read_boundedissues a boundedread(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 intmp_path_factoryviaopenssland served over a real localThreadingHTTPServerTLS peer) covering bothProoflineClientandAsyncProoflineClient.Checks run locally in
.venv(pinnedrequirements-dev.txt):ruff format --check .: 225 files already formattedruff check .: All checks passedmypy proofline scripts tests/test_tls.py: Success: no issues found in 216 source filesscripts/contracts.py: Python operation and outcome-vocabulary contracts passedpytest tests/test_tls.py -v: 31 passed (full suite verified at 249 passed)