From d67aeb18b0f82611a148dabc037a405c6471182f Mon Sep 17 00:00:00 2001 From: spurreiter Date: Tue, 8 May 2018 11:40:38 +0200 Subject: [PATCH] fix: normalize include with boolean or number - On include = true or include = 1 the lib crashes with "TypeError: includes.forEach is not a function". - checking for boolean and number type and return empty array. - fix: include symbol and function in normalization - fix: review changes; adding debug stmt + early exit for tests --- lib/include.js | 5 +++++ test/include.test.js | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/lib/include.js b/lib/include.js index 117c6e4d..fab83559 100644 --- a/lib/include.js +++ b/lib/include.js @@ -16,6 +16,8 @@ const uniq = utils.uniq; const idName = utils.idName; const debug = require('debug')('loopback:include'); +const DISALLOWED_TYPES = ['boolean', 'number', 'symbol', 'function']; + /*! * Normalize the include to be an array * @param include @@ -47,6 +49,9 @@ function normalizeInclude(include) { newInclude = newInclude.concat(subIncludes); } return newInclude; + } else if (DISALLOWED_TYPES.includes(typeof include)) { + debug('Ignoring invalid "include" value of type %s:', typeof include, include); + return []; } else { return include; } diff --git a/test/include.test.js b/test/include.test.js index edd2b474..21462b67 100644 --- a/test/include.test.js +++ b/test/include.test.js @@ -1254,6 +1254,43 @@ describe('include', function() { }); }); + it('should not throw on fetch User if include is boolean equals true', function(done) { + User.find({include: true}, function(err, users) { + if (err) return done(err); + should.exist(users); + users.should.not.be.empty(); + done(); + }); + }); + + it('should not throw on fetch User if include is number', function(done) { + User.find({include: 1}, function(err, users) { + if (err) return done(err); + should.exist(users); + users.should.not.be.empty(); + done(); + }); + }); + + it('should not throw on fetch User if include is symbol', function(done) { + User.find({include: Symbol('include')}, function(err, users) { + if (err) return done(err); + should.exist(users); + users.should.not.be.empty(); + done(); + }); + }); + + it('should not throw on fetch User if include is function', function(done) { + const include = () => {}; + User.find({include}, function(err, users) { + if (err) return done(err); + should.exist(users); + users.should.not.be.empty(); + done(); + }); + }); + // Not implemented correctly, see: loopback-datasource-juggler/issues/166 // fixed by DB optimization it('should support include scope on hasAndBelongsToMany', function(done) {