Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(warehouse-sources): check tls hostnames on chdb reads (#122866)

Felipe Rosa de Almeida743a42796c0d
diff --git a/products/warehouse_sources/backend/tests/test_table.py b/products/warehouse_sources/backend/tests/test_table.py
index 58118c920925..c5aea52e2b97 100644
--- a/products/warehouse_sources/backend/tests/test_table.py
+++ b/products/warehouse_sources/backend/tests/test_table.py
@@ -1,4 +1,10 @@
+import ssl
+import datetime
+import threading
+import contextlib
import subprocess
+import socketserver
+from pathlib import Path
from typing import Any
import pytest
@@ -9,6 +15,10 @@ from django.core.exceptions import ValidationError
from django.test import SimpleTestCase
from clickhouse_driver.errors import ServerException
+from cryptography import x509
+from cryptography.hazmat.primitives import hashes, serialization
+from cryptography.hazmat.primitives.asymmetric import ec
+from cryptography.x509.oid import NameOID
from parameterized import parameterized
from posthog.hogql.database.direct_clickhouse_table import DirectClickHouseTable
@@ -415,6 +425,29 @@ class TestChdbEngineChoiceByFormat:
assert mock_sync_execute.called is not uses_chdb
+def _test_certificate(
+ name: str,
+ key: ec.EllipticCurvePrivateKey,
+ signing_key: ec.EllipticCurvePrivateKey,
+ issuer: x509.Name | None = None,
+) -> x509.Certificate:
+ subject = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, name)])
+ now = datetime.datetime.now(datetime.UTC)
+ builder = (
+ x509.CertificateBuilder()
+ .subject_name(subject)
+ .issuer_name(issuer or subject)
+ .public_key(key.public_key())
+ .serial_number(x509.random_serial_number())
+ .not_valid_before(now - datetime.timedelta(hours=1))
+ .not_valid_after(now + datetime.timedelta(hours=1))
+ .add_extension(x509.BasicConstraints(ca=issuer is None, path_length=None), critical=True)
+ )
+ if issuer is not None:
+ builder = builder.add_extension(x509.SubjectAlternativeName([x509.DNSName(name)]), critical=False)
+ return builder.sign(signing_key, hashes.SHA256())
+
+
class TestRunChdbQuery:
def test_hung_query_is_killed_and_raises_instead_of_blocking(self) -> None:
# Real subprocess: chdb import alone exceeds the timeout, so this exercises the
@@ -444,6 +477,54 @@ class TestRunChdbQuery:
assert child_env["https_proxy"] == "http://web-1:[email protected]:4750/"
assert mock_run.call_args.kwargs["input"] == "SET s3_max_redirects = 0; SELECT 1"
+ def test_read_rejects_a_certificate_issued_for_another_host(
+ self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
+ ) -> None:
+ # The proxy answers every CONNECT with a certificate the test CA issued for other.invalid. An engine
+ # that checks the chain but not the hostname accepts that certificate and reads the body.
+ ca_key = ec.generate_private_key(ec.SECP256R1())
+ ca = _test_certificate("Test CA", ca_key, ca_key)
+ leaf_key = ec.generate_private_key(ec.SECP256R1())
+ leaf = _test_certificate("other.invalid", leaf_key, ca_key, issuer=ca.subject)
+ (tmp_path / "ca.pem").write_bytes(ca.public_bytes(serialization.Encoding.PEM))
+ (tmp_path / "leaf.pem").write_bytes(
+ leaf.public_bytes(serialization.Encoding.PEM)
+ + leaf_key.private_bytes(
+ serialization.Encoding.PEM, serialization.PrivateFormat.PKCS8, serialization.NoEncryption()
+ )
+ )
+ tls_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
+ tls_context.load_cert_chain(tmp_path / "leaf.pem")
+
+ class TlsTerminatingProxy(socketserver.BaseRequestHandler):
+ def handle(self) -> None:
+ request = b""
+ while b"\r\n\r\n" not in request:
+ chunk = self.request.recv(4096)
+ if not chunk:
+ return
+ request += chunk
+ self.request.sendall(b"HTTP/1.1 200 Connection established\r\n\r\n")
+ with contextlib.suppress(OSError), tls_context.wrap_socket(self.request, server_side=True) as tls:
+ tls.recv(65536)
+ tls.sendall(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nok")
+
+ with socketserver.ThreadingTCPServer(("127.0.0.1", 0), TlsTerminatingProxy) as proxy:
+ threading.Thread(target=proxy.serve_forever, daemon=True).start()
+ monkeypatch.setenv("HTTPS_PROXY", f"http://127.0.0.1:{proxy.server_address[1]}")
+ monkeypatch.delenv("NO_PROXY", raising=False)
+ monkeypatch.delenv("no_proxy", raising=False)
+ monkeypatch.setenv("SSL_CERT_FILE", str(tmp_path / "ca.pem"))
+ try:
+ # Retries back off on each failed handshake and would outlast run_chdb_query's timeout.
+ with pytest.raises(RuntimeError, match="Unacceptable certificate from wrong.invalid"):
+ run_chdb_query(
+ "SET s3_retry_attempts = 0; "
+ "SELECT * FROM s3('https://wrong.invalid/bucket/data.csv', 'RawBLOB')"
+ )
+ finally:
+ proxy.shutdown()
+
@pytest.mark.parametrize(
"stderr",
[
diff --git a/pyproject.toml b/pyproject.toml
index 2b13d8c4c764..9bfc4cb62b46 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -168,6 +168,8 @@ dependencies = [
"zxcvbn==4.5.0",
"pyyaml~=6.0.2",
"chdb==4.3.0",
+ # chdb allows any chdb-core>=26.7.0, and engines before 26.9.0 accept a TLS certificate issued for another host.
+ "chdb-core==26.9.0",
"elevenlabs~=2.1.0",
"django-oauth-toolkit~=3.2.0",
"azure-ai-projects~=1.0.0b11",
diff --git a/uv.lock b/uv.lock
index 4d98ccda080a..cd5ce98cf328 100644
--- a/uv.lock
+++ b/uv.lock
@@ -1027,17 +1027,17 @@ wheels = [
[[package]]
name = "chdb-core"
-version = "26.7.0"
+version = "26.9.0"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "pandas" },
{ name = "pyarrow" },
]
wheels = [
- { url = "https://files.pythonhosted.org/packages/71/00/307d0ed8bbf14f3e42b018c589b84d2b4bb99222c2ce54ba26c7a71b057a/chdb_core-26.7.0-cp39-abi3-macosx_10_15_x86_64.whl", hash = "sha256:7134aa30087a54b1bc45b7893d34d1719b976f22dc7624f783ffec926d40db1d", size = 120845633, upload-time = "2026-08-13T04:22:50.054Z" },
- { url = "https://files.pythonhosted.org/packages/41/4e/06524356d7bda9257a12a11860a4713f706d388330249d74a8e542e53953/chdb_core-26.7.0-cp39-abi3-macosx_11_0_arm64.whl", hash = "sha256:191124104076e21581de6a4d1335eb0c87c41e2521243f84600fa9205fbc94d1", size = 103982428, upload-time = "2026-08-12T23:34:33.474Z" },
- { url = "https://files.pythonhosted.org/packages/54/ee/1f3bfcf04b8d5ec265236412f95d3a27a7c586a12363775e7cc34ff962da/chdb_core-26.7.0-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:3afa4b054ab815197149378d947e0409b0e4017baf87a5dc8d9b7c6571f99893", size = 136524494, upload-time = "2026-08-12T23:16:10.072Z" },
- { url = "https://files.pythonhosted.org/packages/dd/79/8dbea41ea83f486e5824d081232892c69cfd8522539f578a620e082a7810/chdb_core-26.7.0-cp39-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:659c36f2f2fe910c3f2b0bcc230c84242cb97d9588aed1359bf7e84566d01f32", size = 175224786, upload-time = "2026-08-12T22:28:28.183Z" },
+ { url = "https://files.pythonhosted.org/packages/9b/1d/66071789c0b550ec32ba493f28ef43398ecb52931874d92c040d4ab70788/chdb_core-26.9.0-cp39-abi3-macosx_10_15_x86_64.whl", hash = "sha256:0d24d78969f7ab41d5303c148b7cf64880fa017507bbed9c9b8799b29c26723f", size = 133441224, upload-time = "2026-09-28T14:49:56.5Z" },
+ { url = "https://files.pythonhosted.org/packages/68/c4/b68a3c3dd2de33a7cbe64b718af5c1143e9ed127550dac180495b1174a20/chdb_core-26.9.0-cp39-abi3-macosx_11_0_arm64.whl", hash = "sha256:bc2d2baedc038ba04be59d97d9ded4f87a3fbe05c3820f93d90e508b1e541ad0", size = 115400237, upload-time = "2026-09-29T06:25:14.87Z" },
+ { url = "https://files.pythonhosted.org/packages/9c/43/3f1b4e3c0eb9e5960b2273ba35d1a8291ead5bb109af79523ac5b950c65b/chdb_core-26.9.0-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:46148d3fc1edd6d6f701922be0f18e2aa6e60349e418b430e69bbb87ab6b95a9", size = 161109244, upload-time = "2026-09-28T13:21:13.918Z" },
+ { url = "https://files.pythonhosted.org/packages/e2/60/543811d41d856a8a42b665d0e966d6ee518bc0b3cb487345acf812131ff0/chdb_core-26.9.0-cp39-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:707e2ec3fe0f7953bac97942eaeb7a1ed1d66b1e56ecef3f0aa2130f61f6e735", size = 181534334, upload-time = "2026-09-28T12:24:50.563Z" },
]
[[package]]
@@ -5907,6 +5907,7 @@ dependencies = [
{ name = "celery-redbeat" },
{ name = "certifi" },
{ name = "chdb" },
+ { name = "chdb-core" },
{ name = "circular-dict" },
{ name = "click" },
{ name = "clickhouse-connect" },
@@ -6206,6 +6207,7 @@ requires-dist = [
{ name = "celery-redbeat", specifier = "==2.3.3" },
{ name = "certifi", specifier = "==2026.10.19" },
{ name = "chdb", specifier = "==4.3.0" },
+ { name = "chdb-core", specifier = "==26.9.0" },
{ name = "circular-dict", specifier = "==1.9" },
{ name = "click", specifier = "~=8.0" },
{ name = "clickhouse-connect", specifier = "==0.10.0" },