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): catch untriaged bucket settings in CI (#81610)

Tom Piccirello6ffe4b033c8e
diff --git a/products/warehouse_sources/backend/models/util.py b/products/warehouse_sources/backend/models/util.py
index 835ad58a457c..c1489e2fb9d1 100644
--- a/products/warehouse_sources/backend/models/util.py
+++ b/products/warehouse_sources/backend/models/util.py
@@ -711,6 +711,82 @@ _NOT_OUR_STORAGE = (
)
+# Every Django setting naming a bucket the ClickHouse node role can read, so a table's
+# url_pattern must never resolve to one - see _posthog_owned_bucket_names below.
+# test_bucket_settings_are_all_triaged in test_util.py enumerates every "*_BUCKET" setting that
+# exists and fails if one isn't listed here or in _BUCKET_SETTINGS_NOT_READABLE_BY_THE_NODE_ROLE,
+# so a newly added "*_BUCKET" setting can't silently miss this check the way this one originally
+# did. BUCKET_PATH and BUCKET_URL are the two names here that don't follow that suffix, so they
+# have to stay listed by hand; the test can't discover either on its own. BUCKET_URL is the
+# warehouse pipelines' actual storage root (an `s3://bucket` URI, not a bare name - see the
+# s3:// handling in _posthog_owned_bucket_names) and is configured independently of
+# DATAWAREHOUSE_BUCKET/BUCKET_PATH, so a deployment that points it at a different bucket was
+# reachable through a table's url_pattern until this line was added.
+#
+# CLICKHOUSE_BACKUPS_BUCKET, DICTIONARY_STAGING_S3_BUCKET, IDENTITY_MATCHING_S3_BUCKET,
+# NOTEBOOKS_FRAME_STORE_S3_BUCKET, OBJECT_STORAGE_EXTERNAL_WEB_ANALYTICS_BUCKET, and
+# QUERY_LOG_ARCHIVE_EXPORT_S3_BUCKET are here because each is read or written by ClickHouse's own
+# `s3(...)` / `BACKUP ... TO S3(...)` with no explicit access key in the query, the same credential-less
+# shape the original vulnerability exploited - not because a customer's url_pattern can reach them today.
+#
+# DICTIONARY_STAGING_S3_BUCKET and NOTEBOOKS_FRAME_STORE_S3_BUCKET both fall back to
+# OBJECT_STORAGE_BUCKET, so they widen this set only on a deployment that points either one at a
+# bucket of its own, which cloud does for frames. Both writers (posthog/dags/common/staged_dictionary.py
+# and notebooks' frame_materialize.py) omit credentials whenever their own endpoint setting is empty,
+# and it is empty on prod, so ClickHouse reaches the object through its node role.
+#
+# BATCH_EXPORT_INTERNAL_STAGING_BUCKET is the same shape: internal_stage.py's get_s3_function_call
+# omits credentials whenever _get_s3_credentials() returns None, which it does on every cloud
+# deployment (_uses_object_storage_endpoint() is false there) - "we omit credentials and ClickHouse
+# uses the default credential provider chain" per that function's own docstring. That every team's
+# batch-export data lands there under aioboto3 elsewhere doesn't change that ClickHouse itself
+# writes there keylessly first.
+_POSTHOG_OWNED_BUCKET_SETTING_NAMES = (
+ "BATCH_EXPORT_INTERNAL_STAGING_BUCKET",
+ "BUCKET_PATH",
+ "BUCKET_URL",
+ "CLICKHOUSE_BACKUPS_BUCKET",
+ "DATAWAREHOUSE_BUCKET",
+ "DICTIONARY_STAGING_S3_BUCKET",
+ "IDENTITY_MATCHING_S3_BUCKET",
+ "NOTEBOOKS_FRAME_STORE_S3_BUCKET",
+ "OBJECT_STORAGE_BUCKET",
+ "OBJECT_STORAGE_EXTERNAL_WEB_ANALYTICS_BUCKET",
+ "QUERY_LOG_ARCHIVE_EXPORT_S3_BUCKET",
+ "SESSION_RECORDING_V2_S3_BUCKET",
+)
+
+# "*_BUCKET" settings checked and found to be read only through a Python process's own boto3/aioboto3
+# client (a different IAM identity than the ClickHouse node role) or to have no reader in this
+# codebase at all - never through ClickHouse's `s3(...)`/`BACKUP...S3(...)`, the credential-less
+# access pattern that makes a bucket reachable by the node role. Each reason names what was actually
+# checked; "no reader found" means exactly that, not a claim that none exists in a non-Python service.
+#
+# This registry can only ever cover buckets a Django setting names. A bucket the node role can read
+# but whose name comes from elsewhere - a database row, a control-plane API call - is invisible to
+# it by construction; posthog/dags/events_backfill_to_duckling.py resolves a per-organization bucket
+# that way, with the same "ClickHouse uses its EC2 instance role, no credentials needed" shape. The
+# load-bearing control against that class is TableSerializer requiring a credential whenever
+# url_pattern is set or changed (products/data_warehouse/backend/presentation/views/table.py), not
+# this list - this registry only backstops the credential-less-by-design tables PostHog's own code
+# creates (self-managed uploads, pipeline syncs), where the URL is known in advance.
+_BUCKET_SETTINGS_NOT_READABLE_BY_THE_NODE_ROLE = {
+ "AGENT_BUNDLES_S3_BUCKET": "no reader found in this codebase; defaults to OBJECT_STORAGE_BUCKET",
+ "AI_BLOB_S3_BUCKET": "read via posthog.storage.object_storage (boto3), by ai_observability/backend/api/ai_blob.py",
+ "BATCH_EXPORTS_FILE_DOWNLOAD_BUCKET": "written via a pre-signed URL over an assumed STS role, and read via aioboto3 - never by ClickHouse",
+ "BILLING_USAGE_REPORTS_S3_BUCKET": "read via posthog.storage.object_storage (boto3), by posthog/temporal/usage_report/storage.py",
+ "DAGSTER_AI_EVALS_S3_BUCKET": "read via boto3 (s3.get_client()) by products/posthog_ai/dags/utils.py",
+ "DAGSTER_FAVICONS_S3_BUCKET": "read via boto3 (s3.get_client()) by products/web_analytics/dags/cache_favicons.py",
+ "DAGSTER_S3_BUCKET": "read via Dagster's own S3Resource (boto3), the pickle io-manager's storage",
+ "INBOX_RANKING_DATASET_S3_BUCKET": "read via boto3 by products/signals/dags/inbox_ranking/common.py",
+ "MANAGED_MIGRATIONS_TRIAL_S3_BUCKET": "read via boto3 by products/managed_migrations/backend/trial_storage.py",
+ "POSTHOG_JS_S3_BUCKET": "read via boto3 by posthog/models/js_snippet_versioning.py",
+ "QUERY_CACHE_S3_BUCKET": "no reader found in this codebase; defaults to OBJECT_STORAGE_BUCKET",
+ "REPLAY_MESSAGE_TOO_LARGE_SAMPLE_BUCKET": "no reader found in this codebase",
+ "VIDEO_SEGMENT_CLUSTERING_S3_BUCKET": "no reader found in this codebase; defaults to OBJECT_STORAGE_BUCKET",
+}
+
+
def _posthog_owned_bucket_names() -> set[str]:
"""Buckets PostHog owns, so a table must never be pointed at one.
@@ -718,14 +794,18 @@ def _posthog_owned_bucket_names() -> set[str]:
table carries no credential. These are the buckets that role can reach, and they hold every
team's data, so reading one across the API would cross the tenant boundary.
"""
- names = {
- settings.DATAWAREHOUSE_BUCKET,
- settings.BUCKET_PATH,
- settings.OBJECT_STORAGE_BUCKET,
- settings.SESSION_RECORDING_V2_S3_BUCKET,
- }
- # A couple of these settings are configured as `bucket/prefix`; only the bucket identifies storage.
- return {str(name).strip("/").split("/", 1)[0] for name in names if name}
+ owned: set[str] = set()
+ for setting_name in _POSTHOG_OWNED_BUCKET_SETTING_NAMES:
+ value = getattr(settings, setting_name, None)
+ if not value:
+ continue
+ value = str(value)
+ # BUCKET_URL is a full `s3://bucket` URI rather than a bare bucket name; the rest are
+ # sometimes configured as `bucket/prefix`, where only the leading segment is the bucket.
+ bucket = urlparse(value).netloc if value.startswith("s3://") else value.strip("/").split("/", 1)[0]
+ if bucket:
+ owned.add(bucket)
+ return owned
def _validate_url_pattern_is_not_posthog_storage(
diff --git a/products/warehouse_sources/backend/tests/test_util.py b/products/warehouse_sources/backend/tests/test_util.py
index 15bf86c31163..6b7e41e6b611 100644
--- a/products/warehouse_sources/backend/tests/test_util.py
+++ b/products/warehouse_sources/backend/tests/test_util.py
@@ -3,6 +3,7 @@ import ipaddress
from posthog.test.base import BaseTest
from unittest.mock import patch
+from django.conf import settings
from django.test import SimpleTestCase, override_settings
from parameterized import parameterized
@@ -11,6 +12,8 @@ from products.warehouse_sources.backend.models.credential import DataWarehouseCr
from products.warehouse_sources.backend.models.external_data_source import ExternalDataSource
from products.warehouse_sources.backend.models.table import DataWarehouseTable
from products.warehouse_sources.backend.models.util import (
+ _BUCKET_SETTINGS_NOT_READABLE_BY_THE_NODE_ROLE,
+ _POSTHOG_OWNED_BUCKET_SETTING_NAMES,
get_view_or_table_by_name,
reconstruct_ordered_columns,
validate_warehouse_table_url_pattern,
@@ -50,6 +53,11 @@ class TestReconstructOrderedColumns(SimpleTestCase):
BUCKET_PATH="ph-warehouse",
OBJECT_STORAGE_BUCKET="ph-objects",
SESSION_RECORDING_V2_S3_BUCKET="ph-replay",
+ CLICKHOUSE_BACKUPS_BUCKET="ph-ch-backups",
+ IDENTITY_MATCHING_S3_BUCKET="ph-identity-matching",
+ OBJECT_STORAGE_EXTERNAL_WEB_ANALYTICS_BUCKET="ph-web-analytics",
+ QUERY_LOG_ARCHIVE_EXPORT_S3_BUCKET="ph-query-log-archive",
+ BATCH_EXPORT_INTERNAL_STAGING_BUCKET="ph-batch-export-staging",
)
class TestValidateWarehouseTableUrlPattern(SimpleTestCase):
@parameterized.expand(
@@ -68,6 +76,19 @@ class TestValidateWarehouseTableUrlPattern(SimpleTestCase):
# Buckets other than the warehouse one are just as reachable from the ClickHouse node.
("object_storage_bucket", "https://s3.us-east-1.amazonaws.com/ph-objects/exports/x.csv"),
("session_replay_bucket", "https://ph-replay.s3.eu-central-1.amazonaws.com/x.json"),
+ # These four are also read or written by ClickHouse's own s3()/BACKUP...S3() with no
+ # explicit credentials - the same shape that made the original vulnerability exploitable.
+ ("clickhouse_backups_bucket", "https://s3.us-east-1.amazonaws.com/ph-ch-backups/db/table/full-x/"),
+ ("identity_matching_bucket", "https://ph-identity-matching.s3.us-east-1.amazonaws.com/x.parquet"),
+ ("web_analytics_bucket", "https://ph-web-analytics.s3.us-east-1.amazonaws.com/team_1/data.native"),
+ ("query_log_archive_bucket", "https://ph-query-log-archive.s3.amazonaws.com/day=2026-01-01/data.parquet"),
+ # ClickHouse writes every team's staged batch-export data here with no explicit
+ # credentials whenever the deployment is cloud (internal_stage.py's own docstring:
+ # "we omit credentials and ClickHouse uses the default credential provider chain").
+ (
+ "batch_export_staging_bucket",
+ "https://ph-batch-export-staging.s3.us-east-1.amazonaws.com/some-run/export_0.arrow",
+ ),
]
)
def test_rejects_urls_that_address_posthog_storage(self, _name: str, url_pattern: str) -> None:
@@ -76,6 +97,18 @@ class TestValidateWarehouseTableUrlPattern(SimpleTestCase):
assert not is_valid
assert "internal storage" in error_message
+ @override_settings(BUCKET_URL="s3://ph-modeling-storage")
+ def test_rejects_a_bucket_url_that_diverges_from_datawarehouse_bucket(self) -> None:
+ # BUCKET_URL is configured independently of DATAWAREHOUSE_BUCKET/BUCKET_PATH (see
+ # s3_proxy.warehouse_bucket_host) and is the actual storage root every warehouse-pipeline
+ # write path uses, so a deployment where it names a different bucket must still be blocked.
+ is_valid, error_message = validate_warehouse_table_url_pattern(
+ "https://ph-modeling-storage.s3.us-east-1.amazonaws.com/team_1_model_x/modeling/y"
+ )
+
+ assert not is_valid
+ assert "internal storage" in error_message
+
@parameterized.expand(
[
("brace_expansion", "https://s3.us-east-1.amazonaws.com/{ph-warehouse,acme}/x.csv"),
@@ -116,6 +149,12 @@ class TestValidateWarehouseTableUrlPattern(SimpleTestCase):
"https://s3.us-east-1.amazonaws.com/ph-warehouse%2Ffile_uploads/team_2/x.csv",
),
("path_style_encoded_char_in_bucket_position", "https://s3.us-east-1.amazonaws.com/acme%2dexports/x.csv"),
+ # Decoding this reveals an owned bucket ("ph-warehouse") that the undecoded string
+ # doesn't match, so this must be rejected on the encoding alone, not on a bucket lookup.
+ (
+ "encoded_first_character_of_an_owned_bucket",
+ "https://s3.us-east-1.amazonaws.com/%70h-warehouse/x.parquet",
+ ),
]
)
def test_rejects_percent_encoding_in_the_bucket_position(self, _name: str, url_pattern: str) -> None:
@@ -201,6 +240,34 @@ class TestValidateWarehouseTableUrlPattern(SimpleTestCase):
assert not is_valid, error_message
+class TestBucketSettingsAreAllTriaged(SimpleTestCase):
+ def test_every_bucket_setting_is_either_owned_or_excluded_with_a_reason(self) -> None:
+ # A setting a future PR adds without following the "*_BUCKET" suffix (like BUCKET_PATH
+ # today) won't be caught here - it has to be added to _POSTHOG_OWNED_BUCKET_SETTING_NAMES
+ # by hand. What this catches is the drift that made the original check incomplete: a new
+ # "*_BUCKET" setting landing without anyone deciding whether the node role can read it.
+ existing_bucket_settings = {name for name in dir(settings) if name.isupper() and name.endswith("_BUCKET")}
+ triaged = set(_POSTHOG_OWNED_BUCKET_SETTING_NAMES) | set(_BUCKET_SETTINGS_NOT_READABLE_BY_THE_NODE_ROLE)
+
+ untriaged = existing_bucket_settings - triaged
+ assert not untriaged, (
+ f"{sorted(untriaged)} aren't triaged in products/warehouse_sources/backend/models/util.py. "
+ "Add each to _POSTHOG_OWNED_BUCKET_SETTING_NAMES if the ClickHouse node role can read it, "
+ "or to _BUCKET_SETTINGS_NOT_READABLE_BY_THE_NODE_ROLE with a reason if it can't."
+ )
+
+ def test_owned_and_excluded_lists_do_not_overlap(self) -> None:
+ overlap = set(_POSTHOG_OWNED_BUCKET_SETTING_NAMES) & set(_BUCKET_SETTINGS_NOT_READABLE_BY_THE_NODE_ROLE)
+ assert not overlap, f"{sorted(overlap)} listed as both node-role-readable and not"
+
+ def test_every_exclusion_has_a_non_empty_reason(self) -> None:
+ # The setting-is-triaged test above only checks _BUCKET_SETTINGS_NOT_READABLE_BY_THE_NODE_ROLE's
+ # keys, so an empty or whitespace-only reason would still count as "triaged" there - this is
+ # what actually enforces that every exclusion documents the access-path check it stands on.
+ blank = {name for name, reason in _BUCKET_SETTINGS_NOT_READABLE_BY_THE_NODE_ROLE.items() if not reason.strip()}
+ assert not blank, f"{sorted(blank)} are excluded with no reason given"
+
+
class TestGetViewOrTableByName(BaseTest):
def _create_warehouse_table(self, *, name, url_pattern, source=None, credential=None) -> DataWarehouseTable:
return DataWarehouseTable.objects.create(