Skip to content

Commit ea2e2c5

Browse files
mhorowitzfacebook-github-bot
authored andcommitted
Make the __DEV__ argument check more robust
Summary: JSON.stringify will convert a function to null, but folly::dynamic in the native code can't represent a JS function. Props do sometimes have functions, but don't permit them everywhere. Reviewed By: johnislarry Differential Revision: D6541878 fbshipit-source-id: b2a9d3ba7899dfb98a6a2ada3aa91a26549fdd94
1 parent ee521f9 commit ea2e2c5

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

Libraries/BatchedBridge/MessageQueue.js

+34-2
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,40 @@ class MessageQueue {
215215
this._queue[METHOD_IDS].push(methodID);
216216

217217
if (__DEV__) {
218-
// Any params sent over the bridge should be encodable as JSON
219-
JSON.stringify(params);
218+
// Validate that parameters passed over the bridge are
219+
// folly-convertible. As a special case, if a prop value is a
220+
// function it is permitted here, and special-cased in the
221+
// conversion.
222+
const isValidArgument = val => {
223+
const t = typeof val;
224+
if (
225+
t === 'undefined' ||
226+
t === 'null' ||
227+
t === 'boolean' ||
228+
t === 'number' ||
229+
t === 'string'
230+
) {
231+
return true;
232+
}
233+
if (t === 'function' || t !== 'object') {
234+
return false;
235+
}
236+
if (Array.isArray(val)) {
237+
return val.every(isValidArgument);
238+
}
239+
for (const k in val) {
240+
if (typeof val[k] !== 'function' && !isValidArgument(val[k])) {
241+
return false;
242+
}
243+
}
244+
return true;
245+
};
246+
247+
invariant(
248+
isValidArgument(params),
249+
'%s is not usable as a native method argument',
250+
params,
251+
);
220252

221253
// The params object should not be mutated after being queued
222254
deepFreezeAndThrowOnMutationInDev((params: any));

0 commit comments

Comments
 (0)