Skip to content

findOrCreate docs dangerously wrong #766

Description

@doublemarked

The findOrCreate docs (https://docs.strongloop.com/display/LB/Creating,+updating,+and+deleting+data) state the following:

The where clause used with findOrCreate() is slightly different than that for queries. Omit { where : ... } from the where clause. Simply provide the condition as the first argument.

API docs are similar (https://apidocs.strongloop.com/loopback/#persistedmodel-findorcreate):

Where clause, such as {test: 'me'}. see Where filter.

The problem is that this is demonstrably wrong. The query parameter, at least when used in the common case Model.findOrCreate(query, data, callback) does indeed require a where. Importantly, without the where, the findOrCreate will happily find and return the first record in the collection, without error. This could produce situations of unexpected data exposure, if not worse.

Evidence of it being wrong can be found in the loopback-datasource-juggler tests:
https://github.com/strongloop/loopback-datasource-juggler/blob/master/test/persistence-hooks.suite.js#L457-L474

Analysis: Glancing at dao.js, it seems to me that findOrCreate supports some modes of operation where the query is derived from the data. In this case, no where is required. However, the documentation describes only the common mode of use: Model.findOrCreate(query, data, callback)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions