Skip to content

Commit

Permalink
callFunction argument validation (realm#4626)
Browse files Browse the repository at this point in the history
* Add regression test

* Add validation for bson array.
  • Loading branch information
takameyer committed Jun 9, 2022
1 parent 55265ea commit bf2f459
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/js_user.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@
#include <realm/object-store/sync/sync_session.hpp>
#include <realm/object-store/sync/sync_user.hpp>
#include <realm/object-store/sync/app.hpp>
#include <realm/object-store/util/bson/bson.hpp>
#include "js_types.hpp"
#include "platform.hpp"


namespace realm {
namespace js {

Expand Down Expand Up @@ -409,6 +411,7 @@ void UserClass<T>::call_function(ContextType ctx, ObjectType this_object, Argume
auto user = get_internal<T, UserClass<T>>(ctx, this_object);

auto name = Value::validated_to_string(ctx, args[0], "name");

auto stringified_ejson_args = Value::validated_to_string(ctx, args[1], "args");
auto service = Value::is_undefined(ctx, args[2])
? util::none
Expand All @@ -417,6 +420,10 @@ void UserClass<T>::call_function(ContextType ctx, ObjectType this_object, Argume

auto bson_args = String::to_bson(stringified_ejson_args);

if (bson_args.type() != realm::bson::Bson::Type::Array) {
throw TypeErrorException("args", "array", stringified_ejson_args);
}

user->m_app->call_function(
user->m_user, name, static_cast<const bson::BsonArray&>(bson_args), service,
Function::wrap_callback_result_first(ctx, this_object, callback,
Expand Down
3 changes: 3 additions & 0 deletions tests/js/user-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,9 @@ module.exports = {

const err = await TestCase.assertThrowsAsync(async () => await user.functions.error());
TestCase.assertEqual(err.message, "function not found: 'error'");

// Regression tests for invalid inputs to function (non array)
TestCase.assertThrowsAsync(async () => await user.callFunction("sumFunc", 123));
},

async testMongoClient() {
Expand Down

0 comments on commit bf2f459

Please sign in to comment.