: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(data-warehouse): stop blocking source edits on an unchanged host field (#99886)
Tom Owers10cd76157899
diff --git a/products/warehouse_sources/backend/presentation/views/external_data_source.py b/products/warehouse_sources/backend/presentation/views/external_data_source.pyindex 1a1ad1886f27..c888974762cd 100644--- a/products/warehouse_sources/backend/presentation/views/external_data_source.py+++ b/products/warehouse_sources/backend/presentation/views/external_data_source.py@@ -548,21 +548,34 @@ _SSH_TUNNEL_CONNECTION_FIELDS = ("enabled", "host", "port")_CONNECTION_TARGET_FIELDS = ("host", "instance_url")-def ssh_tunnel_connection_changed(existing: Any, incoming: Any) -> bool:- """True if the SSH tunnel's connection target (enabled/host/port) changed.+def _coerce_connection_target(value: Any) -> str:+ """Normalize a connection-target value for comparison.Scalars are coerced to strings to ignore type drift between stored values(often strings) and JSON-parsed input (bools/ints). Only `None` collapses to ""— `or ""` would also swallow falsy-but-meaningful values like `False` and 0,making stored "False" falsely diverge from JSON `false`."""+ return "" if value is None else str(value)+++def connection_target_changed(existing: Any, incoming: Any) -> bool:+ """True if a named connection-target field actually moved to a different target.++ An unset field and a blank one name the same (absent) target, so collapsing them keeps the+ gate off an edit that changes nothing: the edit form submits a blank for every declared field+ the stored source never had, and treating that as a retarget blocks the whole form behind a+ credential re-entry that does not apply.+ """+ return _coerce_connection_target(existing) != _coerce_connection_target(incoming)+++def ssh_tunnel_connection_changed(existing: Any, incoming: Any) -> bool:+ """True if the SSH tunnel's connection target (enabled/host/port) changed."""existing = existing if isinstance(existing, dict) else {}incoming = incoming if isinstance(incoming, dict) else {}- def _coerce(value: Any) -> str:- return "" if value is None else str(value)-- return any(_coerce(existing.get(key)) != _coerce(incoming.get(key)) for key in _SSH_TUNNEL_CONNECTION_FIELDS)+ return any(connection_target_changed(existing.get(key), incoming.get(key)) for key in _SSH_TUNNEL_CONNECTION_FIELDS)# Nested containers that keep their secrets one level down, not at the top level: the@@ -1326,7 +1339,8 @@ class ExternalDataSourceSerializers(UserAccessControlSerializerMixin, serializer# both the generic `host` field and source-specific URL fields like ServiceNow's# `instance_url`, so a stored credential can't be redirected to a new host.connection_host_changed = any(- field in incoming_job_inputs and incoming_job_inputs[field] != existing_job_inputs.get(field)+ field in incoming_job_inputs+ and connection_target_changed(existing_job_inputs.get(field), incoming_job_inputs[field])for field in _CONNECTION_TARGET_FIELDS)@@ -1334,7 +1348,8 @@ class ExternalDataSourceSerializers(UserAccessControlSerializerMixin, serializer# `okta_domain`, Freshdesk's `subdomain`). Changing one would send the preserved credential# to a new host — the same exfiltration risk as a `host` change — so require re-entry too.connection_host_changed = connection_host_changed or any(- field in incoming_job_inputs and incoming_job_inputs[field] != existing_job_inputs.get(field)+ field in incoming_job_inputs+ and connection_target_changed(existing_job_inputs.get(field), incoming_job_inputs[field])for field in source.connection_host_fields)diff --git a/products/warehouse_sources/backend/tests/api/test_external_data_source.py b/products/warehouse_sources/backend/tests/api/test_external_data_source.pyindex 4c5d376c5758..45507c02e2e0 100644--- a/products/warehouse_sources/backend/tests/api/test_external_data_source.py+++ b/products/warehouse_sources/backend/tests/api/test_external_data_source.py@@ -6464,6 +6464,62 @@ class TestExternalDataSource(APIBaseTest):assert source.job_inputs["password"] == "original_password"mock_validate_credentials.assert_not_called()+ @parameterized.expand(+ [+ (+ "blank_for_a_field_the_source_never_had",+ "AppleSearchAds",+ "products.warehouse_sources.backend.temporal.data_imports.sources.apple_search_ads.source.AppleSearchAdsSource.validate_credentials",+ {+ "source_type": "AppleSearchAds",+ "org_id": "4242",+ "client_id": "cid",+ "apple_team_id": "tid",+ "key_id": "kid",+ "private_key": "pem",+ },+ {"org_id": "4242", "ad_account_id": ""},+ "private_key",+ ),+ (+ "stored_value_arriving_as_a_number",+ "Freshdesk",+ "products.warehouse_sources.backend.temporal.data_imports.sources.freshdesk.source.FreshdeskSource.validate_credentials",+ {"source_type": "Freshdesk", "subdomain": "12345", "api_key": "original_key"},+ {"subdomain": 12345},+ "api_key",+ ),+ ]+ )+ def test_update_without_a_real_connection_target_change_is_allowed(+ self, _name, source_type, validate_path, stored_job_inputs, incoming_job_inputs, secret_field+ ):+ # The edit form resubmits every declared field, so a field the stored source never had+ # arrives blank, and an encrypted-JSON scalar can come back as a number. Neither points the+ # stored secret anywhere new, so neither may gate the edit behind a credential re-entry —+ # that would leave the source uneditable, including its sync settings.+ source = ExternalDataSource.objects.create(+ team_id=self.team.pk,+ source_id=str(uuid.uuid4()),+ connection_id=str(uuid.uuid4()),+ destination_id=str(uuid.uuid4()),+ source_type=source_type,+ created_by=self.user,+ prefix=f"test_noop_{source_type.lower()}",+ job_inputs=stored_job_inputs,+ )++ with patch(validate_path, return_value=(True, None)):+ response = self.client.patch(+ f"/api/environments/{self.team.pk}/external_data_sources/{source.pk}/",+ data={"job_inputs": incoming_job_inputs, "auto_sync_new_schemas": True},+ )++ assert response.status_code == 200, response.json()+ source.refresh_from_db()+ assert source.job_inputs[secret_field] == stored_job_inputs[secret_field]+ assert source.auto_sync_new_schemas is True+@patch("products.warehouse_sources.backend.temporal.data_imports.sources.freshdesk.source.FreshdeskSource.validate_credentials",return_value=(True, None),