fix(iframe-manager): prevent memory leak for sandboxed iframes without allow-scripts#135
Closed
Dhruv317 wants to merge 1 commit intoPostHog:mainfrom
Closed
fix(iframe-manager): prevent memory leak for sandboxed iframes without allow-scripts#135Dhruv317 wants to merge 1 commit intoPostHog:mainfrom
Dhruv317 wants to merge 1 commit intoPostHog:mainfrom
Conversation
…t allow-scripts For sandboxed iframes that don't have `allow-scripts`, rrweb cannot inject scripts into the iframe, so `attachIframe()` / `onIframeLoad` never fires. This means `attachedIframes` Map is never populated for these iframes. When the iframe is removed from the DOM, `removeIframeById()` tries to find the iframe element via: 1. `attachedIframes.get(id)` - fails (never populated) 2. `mirror.getNode(id)` - fails (mirror was already cleaned up) As a result, `crossOriginIframeMap` and `iframes` WeakMaps are never cleaned, causing the iframe element and its contentWindow to be retained in memory. This fix populates `attachedIframes` immediately in `addIframe()` with a null content placeholder. If `attachIframe()` is called later (for non-sandboxed iframes), it updates the content. This ensures `removeIframeById()` can always find the element reference for cleanup. Fixes memory leak reported at: PostHog/posthog-js#2418 Co-authored-by: Cursor <cursoragent@cursor.com>
Author
|
Closing for now - need to verify the fix locally first. Will reopen once tested. |
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.
Problem
For sandboxed iframes that don't have
allow-scripts, rrweb cannot inject scripts into the iframe, soattachIframe()/onIframeLoadnever fires. This means theattachedIframesMap is never populated for these iframes.When the iframe is removed from the DOM,
removeIframeById()tries to find the iframe element via:attachedIframes.get(id)- fails (never populated)mirror.getNode(id)- fails (mirror was already cleaned up by the mutation observer beforeremoveIframeByIdis called)As a result,
crossOriginIframeMapandiframesWeakMaps are never cleaned, causing the iframe element and its contentWindow to be retained in memory indefinitely.Solution
Populate
attachedIframesimmediately inaddIframe()with a null content placeholder. IfattachIframe()is called later (for non-sandboxed iframes), it updates the content. This ensuresremoveIframeById()can always find the element reference for cleanup.Test plan
allow-scriptsattachIframe()updates content correctly afteraddIframe()Related
Fixes PostHog/posthog-js#2418
Made with Cursor