fix(autocapture): guard null shadow-root host in parent walks#3481
Draft
posthog[bot] wants to merge 1 commit intomainfrom
Draft
fix(autocapture): guard null shadow-root host in parent walks#3481posthog[bot] wants to merge 1 commit intomainfrom
posthog[bot] wants to merge 1 commit intomainfrom
Conversation
Shadow root host can be null if the host element has been detached or GC'd while autocapture is iterating ancestors. Previously the loops in autocapturePropertiesForElement and getElementAndParentsForElement would push the null host into the element list and then dereference its parentNode on the next iteration, throwing "Cannot read properties of null (reading 'parentNode')". Bail out of the walk when host is missing, and apply the same defensive null-curEl check to shouldCaptureElement. Generated-By: PostHog Code Task-Id: be7aa73a-cf19-4b43-b9b5-54f88d9bba05
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
📝 No Changeset FoundThis PR doesn't include a changeset. A changeset is required to release a new version. How to add a changesetRun this command and follow the prompts: pnpm changesetRemember: Never use |
Contributor
Contributor
|
Size Change: +306 B (0%) Total Size: 6.95 MB
ℹ️ View Unchanged
|
Contributor
|
This PR hasn't seen activity in a week! Should it be merged, closed, or further worked on? If you want to keep it open, post a comment or remove the |
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.
Summary
A latent null-deref in autocapture's shadow-DOM parent walk surfaced once on a developer's localhost session as
TypeError: Cannot read properties of null (reading 'parentNode')(error tracking issue 019dcd7b-1f9b-7bc2-a130-a6acaba01f64, posthog-js 1.372.1). When a shadow root has been detached and its host element is null, the walks inautocapturePropertiesForElementandgetElementAndParentsForElementwould push the null host into the element list and then dereference itsparentNodeon the next iteration.Changes
packages/browser/src/autocapture.ts(autocapturePropertiesForElement): bail out of the parent walk when the shadow root'shostis null instead of pushing null and continuing.packages/browser/src/autocapture-utils.ts(getElementAndParentsForElement): same null-host guard.packages/browser/src/autocapture-utils.ts(shouldCaptureElement): defensive null-curEl check on the parent walk so any null ancestor terminates cleanly.shouldCaptureElementandshouldCaptureDomEvent.Test plan
pnpm test:unit—autocapture.test.ts(85) +autocapture-utils.test.ts(52) pass; new tests cover the detached-shadow-root pathpnpm lintclean for changed filesCreated with PostHog Code