Skip to content

Commit 7ca95dc

Browse files
committed
Avoid logging async request payloads
1 parent b9fb7fc commit 7ca95dc

2 files changed

Lines changed: 55 additions & 2 deletions

File tree

‎posthog/async_request.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ async def async_post(
9494
url = trimmed_host + cast(str, path)
9595
body["api_key"] = api_key
9696
data: str | bytes = json.dumps(body, cls=DatetimeSerializer)
97-
log.debug("making async request: %s to url: %s", data, url)
97+
log.debug("making async request to url: %s", url)
9898
headers = {"Content-Type": "application/json", "User-Agent": USER_AGENT}
9999
if gzip:
100100
try:
@@ -167,7 +167,7 @@ def _process_async_response(
167167

168168
try:
169169
payload = _response_json(res)
170-
log.debug("received response: %s", payload)
170+
log.debug("received response with status: %s", res.status_code)
171171
raise APIError(res.status_code, payload["detail"], retry_after=retry_after)
172172
except (KeyError, ValueError):
173173
raise APIError(res.status_code, _response_text(res), retry_after=retry_after)

‎posthog/test/test_async_request.py‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from __future__ import annotations
2+
3+
import logging
4+
5+
import pytest
6+
7+
from posthog.async_request import async_post, _process_async_response
8+
from posthog.request import APIError
9+
10+
11+
class FakeAsyncClient:
12+
async def post(self, *args, **kwargs):
13+
return FakeResponse(200, {"ok": True})
14+
15+
16+
class FakeResponse:
17+
def __init__(self, status_code, payload):
18+
self.status_code = status_code
19+
self._payload = payload
20+
self.headers = {}
21+
self.text = str(payload)
22+
23+
def json(self):
24+
return self._payload
25+
26+
27+
@pytest.mark.asyncio
28+
async def test_async_post_debug_log_does_not_include_payload_or_api_key(caplog):
29+
caplog.set_level(logging.DEBUG, logger="posthog")
30+
31+
await async_post(
32+
"test-secret-key",
33+
"https://example.com",
34+
"/batch/",
35+
client=FakeAsyncClient(),
36+
batch=[{"properties": {"password": "super-secret"}}],
37+
)
38+
39+
logs = caplog.text
40+
assert "making async request to url: https://example.com/batch/" in logs
41+
assert "test-secret-key" not in logs
42+
assert "super-secret" not in logs
43+
44+
45+
def test_process_async_response_debug_log_does_not_include_response_payload(caplog):
46+
caplog.set_level(logging.DEBUG, logger="posthog")
47+
48+
with pytest.raises(APIError):
49+
_process_async_response(FakeResponse(400, {"detail": "password=secret"}), "ok")
50+
51+
logs = caplog.text
52+
assert "received response with status: 400" in logs
53+
assert "password=secret" not in logs

0 commit comments

Comments
 (0)