Skip to content

fix: make synthetic refs non-enumerable to prevent leaking through rest spreads - #64

Merged
RyanCommits merged 6 commits into
fullstorydev:masterfrom
TaduJR:fix/react-19-non-enumerable-synthetic-refs
Apr 13, 2026
Merged

RyanCommits merged 6 commits into
fullstorydev:masterfrom
TaduJR:fix/react-19-non-enumerable-synthetic-refs

Conversation

@TaduJR

@TaduJR TaduJR commented Mar 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

On React 19, @fullstory/babel-plugin-react-native injects synthetic sharedRefWrapper refs into props.ref for every element with FS attributes on iOS. These synthetic refs are enumerable, so they leak through {...rest} spreads in custom components, overwriting legitimate child refs.

This causes broken ref-dependent behavior (focus, scroll-to, etc.) in any component that spreads remaining props without destructuring ref first.

Fix

Inject the synthetic ref as non-enumerable via Object.defineProperty, preserving the existing forwardedRef guard. This preserves FS instrumentation (React's coerceRef reads element.props.ref via direct property access, which works regardless of enumerability) while preventing the ref from appearing in {...rest} spreads.

  • !ref && forwardedRef: skip injection (preserve existing behavior)
  • Otherwise: inject via Object.defineProperty with enumerable: false invisible to {...rest}, visible to coerceRef

@RyanCommits

Copy link
Copy Markdown
Contributor

There are integration tests on https://github.com/fullstorydev/fullstory-react-native that must pass. Please run npx jest --config jest.config.js --clearCache && npm run test:src after you've updated the code in node_modules/@fullstory/babel-plugin-react-native/lib. Right now it fails.

@TaduJR

TaduJR commented Apr 9, 2026

Copy link
Copy Markdown
Contributor Author

Hi @RyanCommits,

I've run the integration tests on fullstory-react-native with the fix applied and all 15 tests pass, including the it.failing test from fullstorydev/fullstory-react-native#149 which now passes.

To enable CI verification via GitHub-based install, I've added:

  • lib/index.js committed to the branch (since lib/ is normally gitignored and devDependencies aren't installed for GitHub deps, so prepare can't build)
  • src/index.js added to the "files" field

These are temporary lib/index.js, .gitignore, and the package.json changes ("prepare" script, "files" addition) should be reverted before merging to master, since they're only needed for GitHub-based installs during CI verification.

Happy to clean those up once you've confirmed the tests pass on your end.

@RyanCommits

RyanCommits commented Apr 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

On React 19, @fullstory/babel-plugin-react-native injects synthetic sharedRefWrapper refs into props.ref for every element with FS attributes on iOS. These synthetic refs are enumerable, so they leak through {...rest} spreads in custom components, overwriting legitimate child refs.

This causes broken ref-dependent behavior (focus, scroll-to, etc.) in any component that spreads remaining props without destructuring ref first.

Fix

When no explicit ref exists, inject the synthetic ref as non-enumerable via Object.defineProperty. This preserves FS instrumentation (React's coerceRef reads element.props.ref via direct property access, which works regardless of enumerability) while preventing the ref from appearing in {...rest} spreads.

  • Existing ref: wrap normally (enumerable) {...props} forwarding preserved
  • No ref: inject synthetic as non-enumerable invisible to {...rest}, visible to coerceRef

Thank you for that. I was running tests locally with the unminified version of your index.js causing test to fail 🤦‍♂️ . It's passing now, which is good.

I see in your change that you've gotten rid of checking for the forwardedRef condition. Is that intentional?

I've tested the following, which passes the tests but preserves the historical logic:

  return `${propsIdentifier} = Object.defineProperty(
    { ...${propsIdentifier} },
    'ref',
    {
      value: (!${propsIdentifier}['ref'] && ${propsIdentifier}['forwardedRef']) ? ${propsIdentifier}['ref'] : ${moduleRef}.applyFSPropertiesWithRef(${propsIdentifier}['ref'], ${hasDynamicAttribute}),
      enumerable: false,
      configurable: true,
    }
  );`;

In your description you only mention the changes to non-enumerable, but removed ${propsIdentifier}['forwardedRef']). Did you do this intentionally?

@RyanCommits

Copy link
Copy Markdown
Contributor

This is actually more surgical, preserving identical logic whilst adding enumerable: false.

return `if (!${propsIdentifier}['ref'] && ${propsIdentifier}['forwardedRef']) {
    ${propsIdentifier} = { ...${propsIdentifier} };
  } else {
    ${propsIdentifier} = Object.defineProperty(
      { ...${propsIdentifier} },
      'ref',
      {
        value: ${moduleRef}.applyFSPropertiesWithRef(${propsIdentifier}['ref'], ${hasDynamicAttribute}),
        enumerable: false,
        configurable: true,
      }
    );
  }`;

@TaduJR

TaduJR commented Apr 11, 2026

Copy link
Copy Markdown
Contributor Author

Hi @RyanCommits,

Yes, removing the forwardedRef check was intentional since it's effectively dead code in React 19, but you're right preserving it is safer.

I've updated the PR with your second suggestion and cleaned up all temporary changes.

@RyanCommits RyanCommits left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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