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(clickhouse): redact credentials from logged query repr (#63608)

Lucas Ricoy228739f7feb0
diff --git a/posthog/clickhouse/cluster.py b/posthog/clickhouse/cluster.py
index 4b91ad5fab4a..150349d5a96c 100644
--- a/posthog/clickhouse/cluster.py
+++ b/posthog/clickhouse/cluster.py
@@ -1,5 +1,6 @@
from __future__ import annotations
+import re
import abc
import time
import logging
@@ -565,6 +566,25 @@ def get_cluster(
)
+# Masks inline credentials (e.g. dictionary `SOURCE(CLICKHOUSE(... PASSWORD '…'))` or
+# `CREATE USER … IDENTIFIED BY '…'`) so they never reach logs. ClickHouse needs the password in
+# the source SQL for dictionary reloads to authenticate, so we redact at the logging boundary
+# rather than dropping it from the query.
+_SQL_SECRET_RE = re.compile(r"(?i)\b(PASSWORD|IDENTIFIED\s+WITH\s+\S+\s+BY|IDENTIFIED\s+BY)\s+'(?:[^']|'')*'")
+
+
+def redact_sql_secrets(sql: str) -> str:
+ return _SQL_SECRET_RE.sub(r"\1 '[REDACTED]'", sql)
+
+
+def _redact_parameters(parameters: Any) -> Any:
+ if isinstance(parameters, Mapping):
+ return {k: ("[REDACTED]" if "password" in str(k).lower() else v) for k, v in parameters.items()}
+ if isinstance(parameters, list):
+ return [_redact_parameters(p) for p in parameters]
+ return parameters
+
+
@dataclass
class Query:
query: str
@@ -575,11 +595,13 @@ class Query:
return client.execute(self.query, self.parameters, settings=self.settings)
def __repr__(self) -> str:
+ query = redact_sql_secrets(self.query)
if self.parameters and isinstance(self.parameters, list):
- params_repr = f"{self.parameters[:50]!r} (showing first 50 out of {len(self.parameters)} parameters)"
+ shown = _redact_parameters(self.parameters[:50])
+ params_repr = f"{shown!r} (showing first 50 out of {len(self.parameters)} parameters)"
else:
- params_repr = f"{self.parameters!r}"
- return f"Query(query={self.query!r}, parameters={params_repr}, settings={self.settings!r})"
+ params_repr = f"{_redact_parameters(self.parameters)!r}"
+ return f"Query(query={query!r}, parameters={params_repr}, settings={self.settings!r})"
@dataclass
diff --git a/posthog/clickhouse/test/test_cluster.py b/posthog/clickhouse/test/test_cluster.py
index 67d25a0bbf5d..d71de9ea91f2 100644
--- a/posthog/clickhouse/test/test_cluster.py
+++ b/posthog/clickhouse/test/test_cluster.py
@@ -22,6 +22,7 @@ from posthog.clickhouse.cluster import (
RetryPolicy,
T,
get_cluster,
+ redact_sql_secrets,
)
from posthog.models.event.sql import EVENTS_DATA_TABLE
@@ -714,3 +715,45 @@ def test_alter_mutation_force_parameter(cluster: ClickhouseCluster) -> None:
# Should have more mutations after using force=True
for host in mutations_count_before:
assert mutations_count_after[host][0][0] > mutations_count_before[host][0][0]
+
+
+ "sql,expected",
+ [
+ (
+ "SOURCE(CLICKHOUSE(TABLE 'web_bot_definition' USER 'default' PASSWORD 'sup3r-s3cret'))",
+ "SOURCE(CLICKHOUSE(TABLE 'web_bot_definition' USER 'default' PASSWORD '[REDACTED]'))",
+ ),
+ # case-insensitive keyword
+ ("source(clickhouse(password 'pw'))", "source(clickhouse(password '[REDACTED]'))"),
+ # CREATE USER
+ ("CREATE USER bob IDENTIFIED BY 'hunter2'", "CREATE USER bob IDENTIFIED BY '[REDACTED]'"),
+ (
+ "CREATE USER bob IDENTIFIED WITH sha256_password BY 'hunter2'",
+ "CREATE USER bob IDENTIFIED WITH sha256_password BY '[REDACTED]'",
+ ),
+ # no secret -> unchanged
+ ("SELECT * FROM events WHERE name = 'password'", "SELECT * FROM events WHERE name = 'password'"),
+ ],
+)
+def test_redact_sql_secrets(sql, expected):
+ assert redact_sql_secrets(sql) == expected
+
+
+def test_query_repr_redacts_inline_password():
+ rendered = repr(Query("SOURCE(CLICKHOUSE(TABLE 't' PASSWORD 'sup3r-s3cret'))"))
+ assert "sup3r-s3cret" not in rendered
+ assert "[REDACTED]" in rendered
+
+
+def test_query_repr_redacts_password_parameter():
+ rendered = repr(Query("SOURCE(CLICKHOUSE(DB %(db)s PASSWORD %(password)s))", {"db": "posthog", "password": "pw"}))
+ assert "pw" not in rendered
+ assert "'db': 'posthog'" in rendered
+ assert "'password': '[REDACTED]'" in rendered
+
+
+def test_query_repr_redacts_password_in_list_parameters():
+ rendered = repr(Query("INSERT INTO t VALUES", [{"db": "posthog", "password": "pw"}]))
+ assert "pw" not in rendered
+ assert "'password': '[REDACTED]'" in rendered