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(api): drop Content-Length from 204 responses (#107981)

Matt Brooker9e8ba77d0cf5
diff --git a/posthog/api/test/test_authentication.py b/posthog/api/test/test_authentication.py
index d06c2e381441..99322fba51fb 100644
--- a/posthog/api/test/test_authentication.py
+++ b/posthog/api/test/test_authentication.py
@@ -1609,7 +1609,7 @@ class TestPasswordResetAPI(APIBaseTest):
response = self.client.post("/api/reset/", {"email": self.CONFIG_EMAIL})
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
self.assertEqual(response.content.decode(), "")
- self.assertEqual(response.headers["Content-Length"], "0")
+ self.assertNotIn("Content-Length", response.headers)
user: User = User.objects.get(email=self.CONFIG_EMAIL)
self.assertEqual(
diff --git a/posthog/middleware.py b/posthog/middleware.py
index 84d5672812f5..7bd7f3f4c30d 100644
--- a/posthog/middleware.py
+++ b/posthog/middleware.py
@@ -1213,7 +1213,7 @@ class AutoLogoutImpersonateMiddleware:
class Fix204Middleware:
"""
- Remove the 'Content-Type' and 'X-Content-Type-Options: nosniff' headers and set content to empty string for HTTP 204 response (and only those).
+ Remove the 'Content-Type', 'Content-Length' and 'X-Content-Type-Options: nosniff' headers and set content to empty string for HTTP 204 response (and only those).
"""
def __init__(self, get_response):
@@ -1224,7 +1224,8 @@ class Fix204Middleware:
if response.status_code == 204:
response.content = b""
- for h in ["Content-Type", "X-Content-Type-Options"]:
+ # Envoy rejects a 204 that has a non-zero Content-Length, then retries the request.
+ for h in ["Content-Type", "Content-Length", "X-Content-Type-Options"]:
response.headers.pop(h, None)
return response
diff --git a/posthog/test/test_middleware.py b/posthog/test/test_middleware.py
index 13f42212edcf..1672a33d2c62 100644
--- a/posthog/test/test_middleware.py
+++ b/posthog/test/test_middleware.py
@@ -31,6 +31,7 @@ from posthog.api.test.test_organization import create_organization
from posthog.api.test.test_team import create_team
from posthog.middleware import (
ActivityLoggingMiddleware,
+ Fix204Middleware,
ManagedProxyClientIPMiddleware,
SignedClientIPOutcome,
per_request_logging_context_middleware,
@@ -397,6 +398,19 @@ class TestManagedProxyClientIPMiddleware(SimpleTestCase):
)
+class TestFix204Middleware(SimpleTestCase):
+ def test_no_content_response_has_no_body_or_content_length(self) -> None:
+ def get_response(request: HttpRequest) -> HttpResponse:
+ response = HttpResponse(b'{"ok": true}', status=204, content_type="application/json")
+ response.headers["Content-Length"] = str(len(response.content))
+ return response
+
+ response = Fix204Middleware(get_response)(RequestFactory().post("/"))
+
+ assert response.content == b""
+ assert "Content-Length" not in response.headers
+
+
class TestAutoProjectMiddleware(APIBaseTest):
# How many queries are made in the base app
# On Cloud there's an additional multi_tenancy_organizationbilling query