Skip to content

fix(snapshot): Clear leftover nodes when document.open() is skipped - #350

Merged
gggritso merged 5 commits into
sentry-v2from
fix/rebuild-doctype-placement
Nov 10, 2026
Merged

gggritso merged 5 commits into
sentry-v2from
fix/rebuild-doctype-placement

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

The replay player crashes with a HierarchyRequestError while it appends the full snapshot's doctype. The error seems to be from trying to jam multiple DOCTYPE declarations into the same iframe.

Before building a document, buildNodeWithSN calls doc.close(); doc.open() and expects doc.open() to clear the iframe. Browsers skip open() once the document's parser has been aborted (for example by window.stop()). In that case the previous rebuild stays in the iframe, and appending the new doctype runs into the old <doctype> + <html>.

The fix removes any children left over after open(), so the rebuild always starts from an empty document.

Fixes REPLAY-1031.

via George Gritsouk.

--

View Junior Session [Sentry]

…d doctype

Full snapshots whose doctype comes after <html>, or that carry a second
doctype (including the one written for BackCompat documents), made
rebuild() throw a HierarchyRequestError and crash the replayer. Insert the
doctype before the root element, or replace the existing one, instead.

Co-Authored-By: George Gritsouk <george.gritsouk@sentry.io>
@linear-code

linear-code Bot commented Oct 28, 2026

Copy link
Copy Markdown

REPLAY-1031

@gggritso
gggritso marked this pull request as draft October 28, 2026 18:57
The previous doctype-placement change assumed a snapshot with the doctype
after <html>, but a crashing replay has a normal [doctype, html] snapshot.
Both production HierarchyRequestError messages reproduce instead when
document.open() is a no-op because the iframe document's parser was
aborted: the previous rebuild stays in place and the new doctype can't be
appended. Remove any leftover children after open() and revert the
doctype-placement change.

Co-Authored-By: George Gritsouk <george.gritsouk@sentry.io>
@sentry-junior sentry-junior Bot changed the title fix(snapshot): Avoid HierarchyRequestError when rebuilding a misplaced doctype fix(snapshot): Clear leftover nodes when document.open() is skipped Oct 28, 2026
sentry-junior Bot and others added 2 commits November 6, 2026 21:23
The existing unit test stubs document.open(). This adds puppeteer tests
that abort the iframe document's parser the way browsers do, with
window.stop() and with a link click that never commits a new document
(a 204), check that open() is now a no-op, and then rebuild. Both fail
with "Can't insert a doctype after the root element" without the fix.

Also drop the changeset, since sentry-v2 no longer uses changesets.

Co-Authored-By: George Gritsouk <george.gritsouk@sentry.io>
@gggritso gggritso changed the title fix(snapshot): Clear leftover nodes when document.open() is skipped fix(snapshot): Clear leftover nodes when document.open() is skipped Nov 6, 2026
Comment thread packages/rrweb-snapshot/src/rebuild.ts Resolved
Address review feedback: replace the parameterized abort triggers with
two explicit test cases that share setup helpers, drop the leftover
headless comment, and say in rebuild.ts that open() doesn't throw when it
skips clearing the document.
@gggritso

gggritso commented Nov 6, 2026

Copy link
Copy Markdown
Member

@cursor review

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 21ee16c. Configure here.

@gggritso
gggritso marked this pull request as ready for review November 6, 2026 21:59

@logaretm logaretm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, I will handle the compat mode leftover in a separate PR

// once the document's parser was aborted (e.g. by window.stop()), open()
// returns without throwing and without clearing the previous rebuild,
// which would make the appends below throw a HierarchyRequestError
while (doc.firstChild) {

@logaretm logaretm Nov 9, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: This fixes the crash, but leaves out some stale state like compatMode.

It's a very rare case, I had an agent try it in Chromium, after window.stop() a quirks snapshot stays CSS1Compat, and a standards one stays BackCompat if the previous rebuild was quirks.

I can PR that separately once this one merges, probably will try to replace the entire iframe.

@gggritso
gggritso merged commit 49fe3a9 into sentry-v2 Nov 10, 2026
21 checks passed
@gggritso
gggritso deleted the fix/rebuild-doctype-placement branch November 10, 2026 03:46
gggritso added a commit to getsentry/sentry that referenced this pull request Nov 12, 2026
…130930)

Bumps the Sentry JavaScript SDK from 11.7.0 to
[11.12.0](https://github.com/getsentry/sentry-javascript/releases/tag/11.12.0),
mainly to pick up rrweb
[2.46.0](https://github.com/getsentry/rrweb/releases/tag/2.46.0)
(getsentry/sentry-javascript#26241).

rrweb 2.46.0 fixes:
- Clear leftover nodes when `document.open()` is skipped
(getsentry/rrweb#350)
- Don't record the default value of cleared inputs (getsentry/rrweb#349)
- Keep adopted stylesheets on seek when their first host was removed
(getsentry/rrweb#352)
- Don't throw when tearing down a locked `adoptedStyleSheets` patch
(getsentry/rrweb#351)

Changes:
- `@sentry/browser`, `@sentry/core`, `@sentry/node`, `@sentry/react`:
11.7.0 → 11.12.0 across `static/app`, `static/gsApp`, `static/gsAdmin`,
`static/packages/scraps`, and `tests/js`.
- `@sentry/rrweb`, `@sentry/rrweb-player`, `@sentry/rrweb-snapshot`:
2.45.0 → 2.46.0, so the replay player uses the same rrweb as the SDK.
- `@sentry/rrweb-types`: 2.44.0 → 2.46.0. This drops the duplicate
`@sentry/rrweb-snapshot@2.44.0` / `@sentry/rrweb-types@2.44.0` entries
from the lockfile.

<!-- junior-request-attribution:start -->
via **George Gritsouk**.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:slack%3AD0AR76A9F5G%3A1794502210.473359 -->

Closes REPLAY-1029
Closes REPLAY-1019

--

[View
Session](https://junior-prod.sentry.dev/conversations/slack%3AD0AR76A9F5G%3A1794502210.473359)
[[Sentry]](https://sentry.sentry.io/explore/conversations/slack%3AD0AR76A9F5G%3A1794502210.473359/?project=4510944073809921)

<!-- junior-session-footer:end -->

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: George Gritsouk <george.gritsouk@sentry.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants