fix: sanitize Django tracing headers#538
Open
dustinbyrne wants to merge 1 commit intomainfrom
Open
Conversation
Contributor
Prompt To Fix All With AIThis is a comment left during a code review.
Path: posthog/test/test_client.py
Line: 2425-2477
Comment:
**Prefer parameterised tests**
The two new window-ID tests duplicate the shape of the existing `test_set_context_session_with_capture` / `test_set_context_session_override_in_capture` pair. Per the team's preference, these four tests could be collapsed into one parameterized test covering `(context_value, explicit_override, expected_result)`, removing the duplicated mock setup, `new_context`, `capture`, and assertion boilerplate.
**Context Used:** Do not attempt to comment on incorrect alphabetica... ([source](https://app.greptile.com/review/custom-context?memory=instruction-0))
How can I resolve this? If you propose a fix, please make it concise.
---
This is a comment left during a code review.
Path: posthog/test/integrations/test_middleware.py
Line: 144-208
Comment:
**Prefer parameterised tests**
`test_extract_tags_sanitizes_tracing_headers` and `test_extract_tags_ignores_non_string_tracing_headers` test per-header sanitization rules (control-char stripping, type rejection) but use two independent test methods each building a full `MockRequest`. A single `@parameterized.expand` over `(header_name, raw_value, expected_context_getter, expected_result)` tuples would cover each sanitization rule individually, give more precise failure messages, and avoid the multi-assertion-per-test pattern that hides which header triggered a failure.
**Context Used:** Do not attempt to comment on incorrect alphabetica... ([source](https://app.greptile.com/review/custom-context?memory=instruction-0))
How can I resolve this? If you propose a fix, please make it concise.Reviews (1): Last reviewed commit: "feat: add context window id support" | Re-trigger Greptile |
Contributor
posthog-python Compliance ReportDate: 2026-04-28 17:26:10 UTC ✅ All Tests Passed!30/30 tests passed Capture Tests✅ 29/29 tests passed View Details
Feature_Flags Tests✅ 1/1 tests passed View Details
|
turnipdabeets
approved these changes
Apr 27, 2026
ioannisj
approved these changes
Apr 27, 2026
marandaneto
reviewed
Apr 28, 2026
6c7bfff to
5bdd7a6
Compare
marandaneto
approved these changes
Apr 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
💡 Motivation and Context
Django middleware extracts PostHog tracing headers from incoming requests and uses them to set context values. These headers are user-controlled, so sanitize them before they enter context/event properties.
This keeps the existing session/distinct-id behavior, but now strips control characters, trims whitespace, caps header length, ignores non-string values, and falls back to the request user when a distinct-id header sanitizes to empty.
This brings us to parity with how we currently handle these headers in PostHog Django
💚 How did you test it?
uv run pytest posthog/test/integrations/test_middleware.py -q📝 Checklist
If releasing new changes
sampo addto generate a changeset filereleaselabel to the PR