MongoDB: operator object in em.upsert() data is used as the update filter
Description
em.upsert() and em.upsertMany() build the filter of the update from the values of the unique properties (or of onConflictFields) found in the entity data. With the MongoDB driver, a value that is an object with operator keys such as $ne or $in is interpreted as a query operator instead of a literal value, so the upsert matches and updates existing documents.
Impact
Applications using the MongoDB driver are affected when a request-controlled value is used for a unique property or an onConflictFields property in upsert data without checking its type, e.g. em.upsert(User, { email: body.email, ... }) where body.email comes from parsed JSON.
Passing an operator object instead of a string lets the caller choose which existing documents the upsert updates. The update is not restricted by entity filters, so documents of other users or tenants can be overwritten with the rest of the submitted data. The entity returned from the call is loaded with the same filter and contains the data of a matched document.
Not affected:
- SQL drivers, where the object is bound as a single value,
- upserts keyed by the primary key, as the resulting query is rejected by MongoDB,
- applications that validate the types of the incoming data before calling
em.upsert().
Affected versions
| Line | Package | Affected versions |
|---|---|---|
| v7 | @mikro-orm/mongodb | <= 7.2.12 |
| v6 | @mikro-orm/mongodb | <= 6.6.19 |
Patches
Fixed in 7.2.13 and 6.6.20.
Workarounds
Validate that the values used for unique properties in upsert data are scalars of the expected type.