From 567414f1afba3d546f6b10b1f1a39bd0994af2d5 Mon Sep 17 00:00:00 2001 From: "posthog[bot]" <206114724+posthog[bot]@users.noreply.github.com> Date: Sun, 6 Dec 2026 07:08:15 +0000 Subject: [PATCH] fix(remote-config): decode a payload the endpoint sends as a JSON string The remote config endpoint answers with a JSON string holding the payload's JSON text, so `get_remote_config_payload()` returned that text instead of the payload. Decode a string result once more, and return a string that is not JSON unchanged, on both the sync and the async client. Generated-By: PostHog Desktop Task-Id: 82bede78-1bb4-4e30-80ee-9d585c4740d3 --- .../remote-config-payload-second-decode.md | 5 ++ posthog/_async_request.py | 3 +- posthog/request.py | 16 ++++- posthog/test/test_remote_config_paths.py | 68 +++++++++++++++++++ 4 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 .sampo/changesets/remote-config-payload-second-decode.md diff --git a/.sampo/changesets/remote-config-payload-second-decode.md b/.sampo/changesets/remote-config-payload-second-decode.md new file mode 100644 index 000000000..b7bc7fb82 --- /dev/null +++ b/.sampo/changesets/remote-config-payload-second-decode.md @@ -0,0 +1,5 @@ +--- +pypi/posthog: patch +--- + +`get_remote_config_payload()` now returns the decoded payload when the endpoint answers with a JSON string holding the payload's JSON text, instead of returning that text. A decoded string is parsed as JSON once more, and a string that is not JSON comes back unchanged. The same applies to `AsyncClient.get_remote_config_payload()`. Callers that worked around this with their own `json.loads()` on the returned string should drop it. diff --git a/posthog/_async_request.py b/posthog/_async_request.py index 6cbccf09b..bf414d20d 100644 --- a/posthog/_async_request.py +++ b/posthog/_async_request.py @@ -13,6 +13,7 @@ QuotaLimitError, USER_AGENT, normalize_host, + _decode_remote_config_payload, _remote_config_path, ) from .utils import remove_trailing_slash @@ -114,7 +115,7 @@ async def async_flags( def _process_remote_config_response(response: Any) -> Any: _process_response(response) - return response.json() + return _decode_remote_config_payload(response.json()) async def async_remote_config( diff --git a/posthog/request.py b/posthog/request.py index 133576d42..d27a608d2 100644 --- a/posthog/request.py +++ b/posthog/request.py @@ -385,6 +385,20 @@ def _remote_config_path(key: str) -> str: return f"/api/projects/@current/feature_flags/{quote(key, safe='')}/remote_config" +def _decode_remote_config_payload(data: Any) -> Any: + """Decode the second layer of a remote config response. + + The endpoint answers with a JSON string holding the payload's JSON text, so a + decoded string is parsed once more. A string that is not JSON is returned as is. + """ + if not isinstance(data, str): + return data + try: + return json.loads(data) + except ValueError: + return data + + def remote_config( personal_api_key: str, project_api_key: str, @@ -399,7 +413,7 @@ def remote_config( host, timeout, ) - return response.data + return _decode_remote_config_payload(response.data) def get( diff --git a/posthog/test/test_remote_config_paths.py b/posthog/test/test_remote_config_paths.py index 97ddcbda3..befc7ae0a 100644 --- a/posthog/test/test_remote_config_paths.py +++ b/posthog/test/test_remote_config_paths.py @@ -105,3 +105,71 @@ def test_remote_config_rejects_empty_and_dot_segments( "configured": True } assert len(requests) == 1 + + +@pytest.fixture +def remote_config_body_server(): + state = {"body": b"{}"} + + class Handler(BaseHTTPRequestHandler): + def do_GET(self): + body = state["body"] + self.send_response(200) + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", str(len(body))) + self.end_headers() + self.wfile.write(body) + + def log_message(self, *args): + pass + + server = ThreadingHTTPServer(("127.0.0.1", 0), Handler) + thread = threading.Thread( + target=lambda: server.serve_forever(poll_interval=0.01), daemon=True + ) + thread.start() + try: + yield f"http://127.0.0.1:{server.server_port}", state + finally: + server.shutdown() + server.server_close() + thread.join() + + +@pytest.mark.parametrize("client_type", ["sync", "async"]) +@pytest.mark.parametrize( + "body, payload", + [ + ("{}", {}), + ('{"foo":["bar","baz"]}', {"foo": ["bar", "baz"]}), + ('"{ \\"foo\\":[\\"bar\\",\\"baz\\"]}"', {"foo": ["bar", "baz"]}), + ('"[1,2]"', [1, 2]), + ('"123"', 123), + ('["{\\"a\\":1}"]', ['{"a":1}']), + ], +) +def test_remote_config_body_decodes_to_the_payload( + remote_config_body_server, client_type, body, payload +): + host, state = remote_config_body_server + state["body"] = body.encode() + + assert read_payload(client_type, host, "test-flag") == payload + + +@pytest.mark.parametrize("client_type", ["sync", "async"]) +@pytest.mark.parametrize( + "body, payload", + [ + ('"hello world"', "hello world"), + ('"{broken"', "{broken"), + ('""', ""), + ], +) +def test_remote_config_string_that_is_not_json_comes_back_unchanged( + remote_config_body_server, client_type, body, payload +): + host, state = remote_config_body_server + state["body"] = body.encode() + + assert read_payload(client_type, host, "test-flag") == payload