From dfb6a403357d5ac2c0ea87aa6e5a4f947de342f8 Mon Sep 17 00:00:00 2001 From: Kai Riemann <53490304+kri@users.noreply.github.com> Date: Sat, 17 Oct 2020 18:47:52 +0200 Subject: [PATCH] [Fast Refresh] Fix crashes caused by rogue Proxies (#20030) (#20039) --- .../react-refresh/src/ReactFreshRuntime.js | 14 +++++++++++-- .../src/__tests__/ReactFresh-test.js | 21 +++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/packages/react-refresh/src/ReactFreshRuntime.js b/packages/react-refresh/src/ReactFreshRuntime.js index e6f0fb70ae5c6..cd5d5a6716bf8 100644 --- a/packages/react-refresh/src/ReactFreshRuntime.js +++ b/packages/react-refresh/src/ReactFreshRuntime.js @@ -178,6 +178,16 @@ function cloneSet(set: Set): Set { return clone; } +// This is a safety mechanism to protect against rogue getters and Proxies. +function getProperty(object, property) { + try { + return object[property]; + } catch (err) { + // Intentionally ignore. + return undefined; + } +} + export function performReactRefresh(): RefreshUpdate | null { if (!__DEV__) { throw new Error( @@ -322,7 +332,7 @@ export function register(type: any, id: string): void { // Visit inner types because we might not have registered them. if (typeof type === 'object' && type !== null) { - switch (type.$$typeof) { + switch (getProperty(type, '$$typeof')) { case REACT_FORWARD_REF_TYPE: register(type.render, id + '$render'); break; @@ -676,7 +686,7 @@ export function isLikelyComponentType(type: any): boolean { } case 'object': { if (type != null) { - switch (type.$$typeof) { + switch (getProperty(type, '$$typeof')) { case REACT_FORWARD_REF_TYPE: case REACT_MEMO_TYPE: // Definitely React components. diff --git a/packages/react-refresh/src/__tests__/ReactFresh-test.js b/packages/react-refresh/src/__tests__/ReactFresh-test.js index 3b6f69b13e318..da73032520c64 100644 --- a/packages/react-refresh/src/__tests__/ReactFresh-test.js +++ b/packages/react-refresh/src/__tests__/ReactFresh-test.js @@ -3612,11 +3612,26 @@ describe('ReactFresh', () => { const useStore = () => {}; expect(ReactFreshRuntime.isLikelyComponentType(useStore)).toBe(false); expect(ReactFreshRuntime.isLikelyComponentType(useTheme)).toBe(false); + const rogueProxy = new Proxy( + {}, + { + get(target, property) { + throw new Error(); + }, + }, + ); + expect(ReactFreshRuntime.isLikelyComponentType(rogueProxy)).toBe(false); // These seem like function components. const Button = () => {}; expect(ReactFreshRuntime.isLikelyComponentType(Button)).toBe(true); expect(ReactFreshRuntime.isLikelyComponentType(Widget)).toBe(true); + const ProxyButton = new Proxy(Button, { + get(target, property) { + return target[property]; + }, + }); + expect(ReactFreshRuntime.isLikelyComponentType(ProxyButton)).toBe(true); const anon = (() => () => {})(); anon.displayName = 'Foo'; expect(ReactFreshRuntime.isLikelyComponentType(anon)).toBe(true); @@ -3624,8 +3639,14 @@ describe('ReactFresh', () => { // These seem like class components. class Btn extends React.Component {} class PureBtn extends React.PureComponent {} + const ProxyBtn = new Proxy(Btn, { + get(target, property) { + return target[property]; + }, + }); expect(ReactFreshRuntime.isLikelyComponentType(Btn)).toBe(true); expect(ReactFreshRuntime.isLikelyComponentType(PureBtn)).toBe(true); + expect(ReactFreshRuntime.isLikelyComponentType(ProxyBtn)).toBe(true); expect( ReactFreshRuntime.isLikelyComponentType( createReactClass({render() {}}),