: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(custom): accept an already-parsed manifest object in _assemble_manifest (#74624)
Tom Owersb21458f9afef
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/custom/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/custom/source.pyindex 7825597ad45d..58c99758d57b 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/custom/source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/custom/source.py@@ -606,13 +606,18 @@ def manifest_request_hosts(manifest_json: Any) -> frozenset[str]:the stored secret must not be able to redirect it to a server they control.Returns an empty set for anything unparseable — the caller treats "no hosts"as "nothing new", and a malformed manifest is rejected elsewhere.++ Accepts both a JSON string and an already-parsed object — the same two shapes+ `_assemble_manifest` takes — so a dict manifest can never slip past the gate as+ "no hosts". Read-only: the object is inspected in place, never mutated."""- if not isinstance(manifest_json, str):- return frozenset()- try:- manifest = json.loads(manifest_json)- except json.JSONDecodeError:- return frozenset()+ if isinstance(manifest_json, str):+ try:+ manifest = json.loads(manifest_json)+ except json.JSONDecodeError:+ return frozenset()+ else:+ manifest = manifest_jsonif not isinstance(manifest, dict):return frozenset()@@ -905,10 +910,20 @@ class CustomSource(SimpleSource[CustomSourceConfig]):API layer can redact them. This rebuilds the full config the RESTengine consumes."""- try:- manifest = json.loads(config.manifest_json)- except json.JSONDecodeError as exc:- raise ManifestValidationError(f"Manifest is not valid JSON: {exc.msg} (line {exc.lineno}, col {exc.colno})")+ raw: Any = config.manifest_json+ # `manifest_json` is declared as a JSON string, but the create/validate API can hand us an+ # already-parsed object when a client submits the manifest as JSON rather than a JSON-encoded+ # string. Accept both — deep-copy the object so the in-place secret injection below never+ # writes credentials back into the caller's (persisted) config.+ if isinstance(raw, str):+ try:+ manifest = json.loads(raw)+ except json.JSONDecodeError as exc:+ raise ManifestValidationError(+ f"Manifest is not valid JSON: {exc.msg} (line {exc.lineno}, col {exc.colno})"+ )+ else:+ manifest = copy.deepcopy(raw)# Structural validation only — no resource-graph checks. This runs on# every sync and schema listing of already-stored manifests, so a# graph problem on one resource must not take down the source's otherdiff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/custom/test_custom_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/custom/test_custom_source.pyindex c788ff08f07e..0d96da342a75 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/custom/test_custom_source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/custom/test_custom_source.py@@ -411,6 +411,24 @@ class TestCustomSourceAssembleManifest(SimpleTestCase):manifest = source._assemble_manifest(config)assert manifest["client"]["base_url"] == "https://api.example.com"+ def test_accepts_already_parsed_manifest_object(self):+ # The create/validate API can hand us the manifest as an already-parsed object rather than a+ # JSON string; that used to reach json.loads(dict) and raise an uncaught TypeError.+ source = CustomSource()+ config = CustomSourceConfig(manifest_json=_minimal_manifest()) # type: ignore[arg-type]+ manifest = source._assemble_manifest(config)+ assert manifest["client"]["base_url"] == "https://api.example.com"++ def test_does_not_mutate_manifest_object_when_injecting_secrets(self):+ # A dict manifest is deep-copied before secret injection so credentials never leak back into+ # the caller's (persisted, redacted) config.+ stored = _minimal_manifest()+ source = CustomSource()+ config = CustomSourceConfig(manifest_json=stored, auth_token="ya29.secret") # type: ignore[arg-type]+ assembled = source._assemble_manifest(config)+ assert assembled["client"]["auth"]["token"] == "ya29.secret"+ assert "token" not in stored["client"]["auth"]+@parameterized.expand([("bearer", {"type": "bearer"}, {"auth_token": "ya29.secret"}, "token", "ya29.secret"),@@ -1546,6 +1564,16 @@ class TestManifestRequestHosts(SimpleTestCase):def test_unparseable_returns_empty(self, _name, raw):assert manifest_request_hosts(raw) == frozenset()+ def test_parsed_object_manifest_reports_hosts(self):+ # _assemble_manifest accepts a dict manifest, so the re-entry gate must extract hosts from a+ # dict too — otherwise an update PATCHing a dict manifest that retargets a new host slips past+ # the gate as "no hosts" and the stored credential is sent to the new host without re-entry.+ manifest = {+ "client": {"base_url": "https://api.example.com"},+ "resources": [{"name": "r", "endpoint": {"path": "https://attacker.example.net/data"}}],+ }+ assert manifest_request_hosts(manifest) == frozenset({"api.example.com", "attacker.example.net"})+def test_oauth2_token_url_host_is_tracked(self):# The token endpoint receives the stored client_secret, so its host must be in the# re-entry set — otherwise an editor who can't read the secret could repoint token_url