fix(autocapture): guard against bare DocumentFragment in parent walker#3450
Draft
posthog[bot] wants to merge 2 commits intomainfrom
Draft
fix(autocapture): guard against bare DocumentFragment in parent walker#3450posthog[bot] wants to merge 2 commits intomainfrom
posthog[bot] wants to merge 2 commits intomainfrom
Conversation
The parent traversal in autocapturePropertiesForElement and getElementAndParentsForElement treated every DocumentFragment as a ShadowRoot and unconditionally jumped to `.host`. A plain DocumentFragment (from createDocumentFragment, <template>.content, or detached sub-trees) has no host, so curEl became undefined and the next iteration threw `TypeError: Cannot read properties of null (reading 'parentNode')` in the unhandled autocapture click path. Narrow the shadow-root branch to ShadowRoot by checking `.host` before following it, and break out of the loop when there is no host — mirroring the existing getParentElement guard. Also switch the shouldCaptureElement walk to getParentElement so it can't fall through to a cast on a non-Element parent. Generated-By: PostHog Code Task-Id: 2de12b43-6969-46f8-81e4-1fd0c73333c3
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
Contributor
|
Size Change: +123 B (0%) Total Size: 6.79 MB
ℹ️ View Unchanged
|
Generated-By: PostHog Code Task-Id: 2de12b43-6969-46f8-81e4-1fd0c73333c3
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
autocapturePropertiesForElement(autocapture.ts:157-167) andgetElementAndParentsForElement(autocapture-utils.ts:240-259) treated everyDocumentFragmentas aShadowRootand unconditionally jumped to(parent as any).host. A bareDocumentFragment— fromdocument.createDocumentFragment(),<template>.content, or detached sub-trees — has no.host, socurElbecameundefinedand the next iteration threwTypeError: Cannot read properties of null (reading 'parentNode')..hostbefore following it, andbreakout of the loop when it isn't there — mirroring the existinggetParentElementguard.shouldCaptureElementwalk to usegetParentElementso it can't fall through to a cast on a non-Elementparent.Context
Surfaced via PostHog error tracking issue
019da8d1-114c-7a13-b375-9aa292331c87on posthog-js v1.369.2 (1 event / 1 affected user, first seen 2026-04-20, Chrome on localhost:3000). The raw volume is tiny and_captureEventis already wrapped intry/catch, so the user-visible impact is contained — but the null-deref pattern is real and worth fixing defensively before it shows up in a production surface where the catch isn't in place (e.g. callers ofautocapturePropertiesForElementindead-clicks-autocapture.ts).Test plan
pnpm test:unit— newshouldCaptureElement/shouldCaptureDomEvent/_captureEventcases cover a target parented to a bareDocumentFragmentpnpm typecheckpnpm lint/pnpm prettier:checkon changed files<template>content orcreateDocumentFragment()subtrees once builtCreated with PostHog Code