Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perform hasOwnProperty check in Relay Flight #20220

Merged
merged 1 commit into from
Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ export function processErrorChunk(
};
}

const hasOwnProperty = Object.prototype.hasOwnProperty;

function convertModelToJSON(
request: Request,
parent: {+[key: string]: ReactModel} | $ReadOnlyArray<ReactModel>,
Expand All @@ -107,12 +109,14 @@ function convertModelToJSON(
} else {
const jsonObj: {[key: string]: JSONValue} = {};
for (const nextKey in json) {
jsonObj[nextKey] = convertModelToJSON(
request,
json,
nextKey,
json[nextKey],
);
if (hasOwnProperty.call(json, nextKey)) {
jsonObj[nextKey] = convertModelToJSON(
request,
json,
nextKey,
json[nextKey],
);
}
}
return jsonObj;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,23 @@ describe('ReactFlightDOMRelay', () => {
const model = readThrough(transport);
expect(model).toEqual(14);
});

it('should warn in DEV if a class instance polyfill is passed to a host component', () => {
function Bar() {}

function Foo() {}
Foo.prototype = Object.create(Bar.prototype);
// This is enumerable which some polyfills do.
Foo.prototype.constructor = Foo;
Foo.prototype.method = function() {};

expect(() => {
const transport = [];
ReactDOMFlightRelayServer.render(<input value={new Foo()} />, transport);
readThrough(transport);
}).toErrorDev(
'Only plain objects can be passed to client components from server components. ',
{withoutStack: true},
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ export function processErrorChunk(
};
}

const hasOwnProperty = Object.prototype.hasOwnProperty;

function convertModelToJSON(
request: Request,
parent: {+[key: string]: ReactModel} | $ReadOnlyArray<ReactModel>,
Expand All @@ -107,12 +109,14 @@ function convertModelToJSON(
} else {
const jsonObj: {[key: string]: JSONValue} = {};
for (const nextKey in json) {
jsonObj[nextKey] = convertModelToJSON(
request,
json,
nextKey,
json[nextKey],
);
if (hasOwnProperty.call(json, nextKey)) {
jsonObj[nextKey] = convertModelToJSON(
request,
json,
nextKey,
json[nextKey],
);
}
}
return jsonObj;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,26 @@ describe('ReactFlightNativeRelay', () => {
nativeFabricUIManager.__dumpHierarchyForJestTestsOnly(),
).toMatchSnapshot();
});

it('should warn in DEV if a class instance polyfill is passed to a host component', () => {
function Bar() {}

function Foo() {}
Foo.prototype = Object.create(Bar.prototype);
// This is enumerable which some polyfills do.
Foo.prototype.constructor = Foo;
Foo.prototype.method = function() {};

expect(() => {
const transport = [];
ReactNativeFlightRelayServer.render(
<input value={new Foo()} />,
transport,
);
readThrough(transport);
}).toErrorDev(
'Only plain objects can be passed to client components from server components. ',
{withoutStack: true},
);
});
});