loopback icon indicating copy to clipboard operation
loopback copied to clipboard

server crashes from uncaught exception thrown by _initProperties when passing string for Array field in mongodb

Open kaizhu256 opened this issue 12 years ago • 8 comments

see the model schema and my post api request below.

/* /common/models/Content.json */
{
  "base": "PersistedModel",
  "name": "Content",
  "properties": {
    "alias": [{ "type": "string" }],
    "categories": [{ "type": "string" }],
    "contexts": [{ "type": "string" }],
    "context_settings": { "type": "object" },
    "title": { "type": "string" },
    "type": { "type": "string" },
    "translations": { "type": "object" },
    "widgets": [{ "type": "object" }]
  }
}

/ * POST /Contents example that crashes server */
{"widgets":"this string will crash the server when it attempts to cast it to an array"}

here is the stack output:

/Users/kaizhu/src/swagger-demo/node_modules/loopback-connector-mongodb/node_modules/mongodb/lib/mongodb/connection/base.js:246
        throw message;
              ^
Error: could not create List from JSON string: "this string will crash the server when it attempts to cast it to an array"
    at new List (/Users/kaizhu/src/swagger-demo/node_modules/loopback-datasource-juggler/lib/list.js:16:13)
    at List (/Users/kaizhu/src/swagger-demo/node_modules/loopback-datasource-juggler/lib/list.js:9:12)
    at ModelConstructor.Object.defineProperty.set [as widgets] (/Users/kaizhu/src/swagger-demo/node_modules/loopback-datasource-juggler/lib/model-builder.js:406:43)
    at ModelConstructor.ModelBaseClass._initProperties (/Users/kaizhu/src/swagger-demo/node_modules/loopback-datasource-juggler/lib/model.js:139:17)
    at ModelConstructor.ModelBaseClass (/Users/kaizhu/src/swagger-demo/node_modules/loopback-datasource-juggler/lib/model.js:42:8)
    at ModelConstructor (/Users/kaizhu/src/swagger-demo/node_modules/loopback-datasource-juggler/lib/model-builder.js:169:22)
    at ModelConstructor (/Users/kaizhu/src/swagger-demo/node_modules/loopback-datasource-juggler/lib/model-builder.js:169:22)
    at new ModelConstructor (/Users/kaizhu/src/swagger-demo/node_modules/loopback-datasource-juggler/lib/model-builder.js:169:22)
    at Function.upsert (/Users/kaizhu/src/swagger-demo/node_modules/loopback-datasource-juggler/lib/dao.js:277:14)
    at SharedMethod.invoke (/Users/kaizhu/src/swagger-demo/node_modules/loopback/node_modules/strong-remoting/lib/shared-method.js:224:17)
    at HttpContext.invoke (/Users/kaizhu/src/swagger-demo/node_modules/loopback/node_modules/strong-remoting/lib/http-context.js:253:12)
    at /Users/kaizhu/src/swagger-demo/node_modules/loopback/node_modules/strong-remoting/lib/remote-objects.js:477:9
    at execStack (/Users/kaizhu/src/swagger-demo/node_modules/loopback/node_modules/strong-remoting/lib/remote-objects.js:348:7)
    at /Users/kaizhu/src/swagger-demo/node_modules/loopback/lib/application.js:319:13

my current fix is to override _initProperties to call it in a try-catch block, but this is far from ideal:

  // bug - catch uncaught exceptions in remote api
  mainApp.loopback_datastore_juggler.ModelBaseClass.prototype._initProperties0 = mainApp.loopback_datastore_juggler.ModelBaseClass.prototype._initProperties;
  mainApp.loopback_datastore_juggler.ModelBaseClass.prototype._initProperties = function (data, options) {
    try {
      debugPrint('aa');
      this._initProperties0(data, options);
    } catch (errorCaught) {
      debugPrint(errorCaught.stack);
      //!! throw errorCaught;
      debugPrint('bb');
      //!! next(errorCaught);
    }
  };

the best fix i can think of is to enclose the next() call in a try-catch block in /Users/kaizhu/src/swagger-demo/node_modules/loopback/node_modules/strong-remoting/lib/remote-objects.js:348:7

  function execStack(err) {
    if(err) return next(err);

    var cur = stack.shift();

    if(cur) {
      cur.call(scope, ctx, execStack, method);
    } else {
-      next();
+     try { next() } catch (errorCaught) { next(errorCaught); }
    }
  }

  return execStack();
};

kaizhu256 avatar Oct 30 '14 01:10 kaizhu256

What version of strong-remoting are you using? Is it possible this issue is a duplicate of https://github.com/strongloop/strong-remoting/pull/119?

bajtos avatar Nov 03 '14 19:11 bajtos

+1, It is not the bug of the strong-remoting.

2014-11-04 3:05 GMT+08:00 Miroslav Bajtoš [email protected]:

What version of strong-remoting are you using? Is it possible this issue is a duplicate of strongloop/strong-remoting#119 https://github.com/strongloop/strong-remoting/pull/119?

— Reply to this email directly or view it on GitHub https://github.com/strongloop/loopback/issues/709#issuecomment-61529775.

Spark-star avatar Nov 04 '14 03:11 Spark-star

hmm, yes, it looks related. has strongloop/strong-remoting#119 patch been pushed to npm? if so, then i can test if it still crashes

kaizhu256 avatar Nov 04 '14 05:11 kaizhu256

strongloop/strong-remoting/pull/119 was released in 2.7.1. In case the problem still persist, would you mind submitting a pull request to strong-remoting fixing the problem?

bajtos avatar Nov 04 '14 07:11 bajtos

I tried it , but the server also crashed when I pass an object to a property that should be an array , I used the strong-remoting 2.8.1. the error is : Error: Items must be an array: {"url":"http://upai.com/544e394f97f5d6de1a0c5cd7.jpg"} would you check it again? Thx

Spark-star avatar Nov 05 '14 03:11 Spark-star

@PradnyaBaviskar will be looking into this issue in the next few days.

It should be possible to reproduce the problem of unguarded cur.call by adding a test to test/rest.test.js with the following code:

      var method = givenSharedStaticMethod();
      objects.before(method.name, function(ctx, next) {
        // important: move of the initial tick where 
        // express eventually catches all errors
        process.nextTick(next);
      });
      objects.before(method.name, function(ctx, next) {
        throw new Error('test error');
      });

      request(app).get(method.url)
         // verify error was caught and reported in the response

bajtos avatar Jan 28 '15 17:01 bajtos

@Spark-star strongloop/strong-remoting#172 fixed the crash you mentioned in your comment. Please upgrade to [email protected] and let us know if it works for you now.

bajtos avatar Feb 03 '15 15:02 bajtos

This issue should be fixed by https://github.com/strongloop/strong-remoting/pull/172 and https://github.com/strongloop/loopback-datasource-juggler/pull/430.

bajtos avatar Feb 05 '15 11:02 bajtos