Skip to content

fix(rrweb-plugin-console-record): bind wrapped console methods to the logger#1904

Open
yashrao2607 wants to merge 2 commits into
rrweb-io:mainfrom
yashrao2607:fix/1772-console-record-illegal-invocation
Open

fix(rrweb-plugin-console-record): bind wrapped console methods to the logger#1904
yashrao2607 wants to merge 2 commits into
rrweb-io:mainfrom
yashrao2607:fix/1772-console-record-illegal-invocation

Conversation

@yashrao2607

Copy link
Copy Markdown

Summary

getRecordConsolePlugin patches each console method with an arrow function that calls original.apply(this, args). Arrow functions ignore the call-site receiver entirely and use their lexically enclosing this — here that's the replace() function, which is invoked as a plain function call, so this is undefined in this module's strict mode. In other words this was never the logger, regardless of how the wrapped method was actually called.

Native console implementations can throw TypeError: Illegal invocation when a method is applied with the wrong receiver. That's consistent with the reported behavior: importing this plugin silently crashed a Chrome extension's content script (isolated-world consoles appear to enforce this more strictly than a page's main-world console, which is likely why this wasn't caught earlier).

Fixed by binding explicitly to _logger (the actual logger object passed into replace()), both in the normal call path and in the internal-error fallback (catch block), which had the identical bug (original('rrweb logger error:', ...), an unbound plain call).

Test plan

  • Added test/this-binding.test.ts: patches a fake logger whose method records this, calls it, and asserts this is the logger instance. Verified this test fails against the pre-fix code (this === undefined) and passes after the fix.
  • yarn vitest run for this package: all existing tests (including the puppeteer-based test/index.test.ts integration tests) still pass.
  • tsc -noEmit clean.

Fixes #1772

The wrapped console method was an arrow function, so `original.apply(this, args)`
never used the logger as `this` — arrow functions ignore the call-site
receiver entirely and use the enclosing (non-method) call's `this`, which is
undefined in this module's strict mode. Native console implementations can
throw "Illegal invocation" when called with the wrong receiver, which is
what crashed the reported Chrome extension content script. Bind explicitly
to `_logger` instead, in both the normal call path and the internal-error
fallback that has the same issue.

Fixes rrweb-io#1772
@changeset-bot

changeset-bot Bot commented Jul 17, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 45c77df

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 22 packages
Name Type
@rrweb/rrweb-plugin-console-record Patch
rrweb Patch
rrweb-snapshot Patch
rrdom Patch
rrdom-nodejs Patch
rrweb-player Patch
@rrweb/all Patch
@rrweb/replay Patch
@rrweb/record Patch
@rrweb/types Patch
@rrweb/packer Patch
@rrweb/utils Patch
@rrweb/browser-client Patch
@rrweb/web-extension Patch
rrvideo Patch
@rrweb/rrweb-plugin-console-replay Patch
@rrweb/rrweb-plugin-sequential-id-record Patch
@rrweb/rrweb-plugin-sequential-id-replay Patch
@rrweb/rrweb-plugin-canvas-webrtc-record Patch
@rrweb/rrweb-plugin-canvas-webrtc-replay Patch
@rrweb/rrweb-plugin-network-record Patch
@rrweb/rrweb-plugin-network-replay Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
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.

[Bug]: Importing getRecordConsolePlugin version 2.0.0-alpha.18, and referencing it crashes chrome extension content script.

1 participant