Skip to content

Commit

Permalink
Check that the argument to EJSON.parse is a string.
Browse files Browse the repository at this point in the history
Some Android browser versions of JSON.parse can crash when passed null
(https://code.google.com/p/android/issues/detail?id=11973), so it's
better not to pass on a non-string argument to JSON.parse.

Thanks to @raix for raising the issue in meteor#1401.
  • Loading branch information
awwx authored and glasser committed Sep 12, 2013
1 parent 00a70b7 commit 4714f89
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/ejson/ejson.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ EJSON.stringify = function (item) {
};

EJSON.parse = function (item) {
if (typeof item !== 'string')
throw new Error("EJSON.parse argument should be a string");
return EJSON.fromJSONValue(JSON.parse(item));
};

Expand Down
8 changes: 8 additions & 0 deletions packages/ejson/ejson_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,11 @@ Tinytest.add("ejson - clone", function (test) {
};
testCloneArgs(1, 2, "foo", [4]);
});

Tinytest.add("ejson - parse", function (test) {
test.equal(EJSON.parse("[1,2,3]"), [1,2,3]);
test.throws(
function () { EJSON.parse(null) },
/argument should be a string/
);
});

0 comments on commit 4714f89

Please sign in to comment.