Fix: wrap cross-origin property access in try/catch in ReactPerforman…#36485
Fix: wrap cross-origin property access in try/catch in ReactPerforman…#36485KrishnaVipul14 wants to merge 1 commit into
Conversation
…ceTrackProperties
|
Hi @KrishnaVipul14! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
Fix SecurityError crash when cross-origin Window is passed as a prop (DEV build)
Summary
Fixes a bug where passing a cross-origin Window object (e.g. iframe.contentWindow
from an iframe with srcDoc="") as a React component prop crashes the entire fiber
tree in DEV builds, making the UI permanently unresponsive.
Problem
In React 19.2 DEV builds, the performance logger (logComponentRender) walks all
component props via addObjectToProperties and addValueToProperties in
ReactPerformanceTrackProperties.js.
When a prop contains a cross-origin Window object, the browser throws a SecurityError
on any property access. Because there was no try/catch, this error escaped the commit
phase and corrupted the work-in-progress fiber tree. Every render after that threw:
...and the app became completely frozen — no clicks, no input, nothing worked.
Root Cause
Two unguarded operations in ReactPerformanceTrackProperties.js:
for (const key in object) — throws SecurityError immediately when object is a
cross-origin Window because property enumeration is blocked by the browser.
Object.getPrototypeOf(value) — also throws SecurityError on cross-origin objects.
Fix
File changed:
packages/react-reconciler/src/ReactPerformanceTrackProperties.js
Change 1 — addObjectToProperties:
Wrapped the for...in loop in a try/catch. If SecurityError is thrown, pushes a
[cross-origin object] placeholder into properties and returns early. Fiber tree
is never touched.
Change 2 — addValueToProperties:
Wrapped Object.getPrototypeOf(value) in a try/catch. If it throws, objectName
silently stays as 'Object' and execution continues normally.
How To Reproduce
function App() {
const iframeRef = useRef(null);
const [win, setWin] = useState(null);
useEffect(() => {
setWin(iframeRef.current?.contentWindow ?? null);
}, []);
return (
<>
<iframe ref={iframeRef} srcDoc="
hi
" title="x" /></>
);
}
function Child({ win }) {
return
}
Before This Fix
After This Fix
Notes
this PR guards the two remaining unprotected call sites in the same file
conversions, embedded notebook editors and dashboards all hit this bug
when storing Window or DOM refs in component state
Test
Tested manually with Vite + React 19.2.5 in Chrome.
Before patch: app freezes on mount.
After patch: app renders correctly, no errors in console.