fix: chain beforeSend hooks - #697
Open
posthog[bot] wants to merge 5 commits into
Open
Conversation
…of dropping `buildEvent()` in `PostHogStateless` called `beforeSend.run(postHogEvent)` on every loop iteration instead of `beforeSend.run(eventChecked)`, so with 2+ registered hooks each one transformed the *original* event rather than the previous hook's output — mutations from an earlier hook were invisible to later hooks. A throwing hook also dropped the event outright (`return null`) instead of falling back to the last good value. Both bugs affect `posthog-android` and `posthog-server`, which share this code via `PostHogStateless`. Generated-By: PostHog Desktop Task-Id: ea027967-6134-4b68-bcce-ef6a81e507b0
Contributor
posthog-android Compliance ReportDate: 2026-08-19 11:25:11 UTC ✅ All Tests Passed!46/46 tests passed Capture Tests✅ 29/29 tests passed View Details
Feature_Flags Tests✅ 17/17 tests passed View Details
|
marandaneto
marked this pull request as ready for review
August 18, 2026 13:59
Member
Member
|
@PostHog/team-client-libraries pls review if you think this is the right approach |
| "posthog-server": patch | ||
| --- | ||
|
|
||
| Fix `beforeSend` hook chaining in `PostHogStateless.buildEvent()`: when multiple hooks are registered, each hook now receives the previous hook's output instead of the original, pre-chain event, so a mutation made by one hook is visible to the next. Hooks that throw continue to drop the event to avoid enqueueing a potentially unsafe, partially processed payload. Affects both `posthog-android` and `posthog-server`, which share this code path. |
Contributor
There was a problem hiding this comment.
nit: worth trimming this?
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
Fixes
beforeSendhook chaining in the sharedPostHogStateless.buildEvent()path used by bothposthog-androidandposthog-server.When multiple hooks were registered, every hook received the original event instead of the previous hook's returned event. Each hook now receives the running value, so replacement events and mutations compose in registration order.
If a hook throws, the SDK logs the error and drops the event. This preserves the safer existing behavior: sending an event after a failed redaction or filtering hook could leak PII or enqueue a partially processed payload.
💚 How did you test it?
make checkFormat../gradlew :posthog:test --tests 'com.posthog.PostHogBeforeSendTest' --tests 'com.posthog.PostHogStatelessTest.beforeSend error handling does not crash'.📝 Checklist
If releasing new changes
pnpm changesetto generate a changeset file