Skip to content

Commit

Permalink
[Fast Refresh] Fix crashes caused by rogue getters and Proxies (#20030)
Browse files Browse the repository at this point in the history
  • Loading branch information
kri committed Oct 15, 2020
1 parent 2eb3181 commit 49ae721
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/react-refresh/src/ReactFreshRuntime.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,16 @@ function cloneSet<T>(set: Set<T>): Set<T> {
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(
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 49ae721

Please sign in to comment.