From 49ae72182437a6ef87a717e19e3fc6b46757f0fb Mon Sep 17 00:00:00 2001 From: Kai Riemann Date: Thu, 15 Oct 2020 22:16:29 +0200 Subject: [PATCH] [Fast Refresh] Fix crashes caused by rogue getters and Proxies (#20030) --- packages/react-refresh/src/ReactFreshRuntime.js | 14 ++++++++++++-- 1 file changed, 12 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.