Eclipse Ditto JSON merge patch deletions and empty-object values bypass WRITE authorization on Things
Description
The authorization check for JSON merge patch commands on Things (HTTP PATCH / Ditto Protocol merge) does not check permissions for fields that a merge patch deletes or sets to an empty object:
- Fields with a
nullvalue in the patch body (RFC 7396 deletion) are skipped entirely by the authorization check. - Ditto's regex-based key deletion extension,
{{ ~<regex>~ }}with anullvalue, is not recognized by the authorization check with the documented and recommended~delimiter (only with the/delimiter). - Fields with an empty object value (
{}) in the patch body are skipped by the authorization check as well, although applying the patch replaces an existing non-object value with the empty object, or creates the field.
An authenticated user with WRITE permission on any part of a Thing can therefore delete any other part of that Thing, including fields or subtrees the policy explicitly revokes WRITE on and fields the user was never granted WRITE on at all. For example, a subject only allowed to write thing:/attributes/a can delete all attributes, features and the definition of the Thing with a single request. Such a subject can also overwrite any field with an empty object, or create new fields as empty objects.
Setting fields to values other than null or {} without permission is still rejected.
Severity: High (CWE-863).
Details
A MergeThing command is authorized by ThingCommandEnforcement.enforceMergeThingCommand. If the caller has no unrestricted WRITE on the patched path, but WRITE on some part below it, it collects the "leaves" of the patch with calculateLeaves and requires unrestricted WRITE on every leaf.
1. null values produce no leaf (affects 2.0.0 and later)
calculateLeaves recurses into every value for which isObject() is true. In Ditto's JSON library the JSON null literal reports isObject() == true and asObject() returns an empty object (ImmutableJsonNull.java#L52-L64). A field with a null value therefore contributes no leaf at all. For a patch that only deletes fields the set of leaves is empty, and Enforcer.hasUnrestrictedPermissions(Set, ...) returns true for an empty set (allMatch). The command is authorized and the merge deletes the fields.
2. {{ ~<regex>~ }} is not recognized as regex deletion (affects 3.5.5 and later)
For a regex deletion key, calculateLeaves is meant to require WRITE on the enclosing object. The check isMergeWithNulledKeysByRegex only accepts the /…/ delimiter, while the merge patch implementation (JsonMergePatch.java#L207-L239) also accepts ~…~, the delimiter the documentation recommends. A {{ ~<regex>~ }} key is treated as an ordinary field with a null value (see 1.), and the merge then deletes all keys matching the regex. The same applies when the regex key is the last segment of the patched path (e.g. a merge on /attributes/{{ ~firmware~ }} with value null): enforcement checks the literal, non-existing resource, which inherits the caller's grant on /attributes.
3. Empty objects produce no leaf (affects 2.0.0 and later)
For the same reason as in 1., an empty object value ({}) contributes no leaf. Applying the patch (JsonMergePatch.java#L152-L205) replaces an existing non-object value (string, number, boolean, array) with the empty object, and creates a missing field as empty object. If the patch only consists of null and {} values, the set of leaves is empty and the command is authorized (see 1.). Otherwise, a single other field the caller may write is enough to get the command authorized.
The authorized patch is applied by MergeThingStrategy without a further permission check.
Proof of concept
Thing T with attributes firmware ("1.0") and other, and a feature f.
Policy A for subject u: WRITE granted on thing:/attributes, revoked on thing:/attributes/firmware.
PATCH /api/2/things/<T>/attributes
Content-Type: application/merge-patch+json
{ "firmware": null }The request is authorized and attributes/firmware is deleted. { "{{ ~firmware~ }}": null } and { "{{ ~.*~ }}": null } have the same effect (the latter deletes all attributes). { "firmware": {} } and { "firmware": {}, "other": 1 } overwrite firmware with {}.
Policy B for subject u: WRITE granted only on thing:/attributes/other.
PATCH /api/2/things/<T>
Content-Type: application/merge-patch+json
{ "attributes": null, "features": null, "definition": null }The request is authorized, and all attributes, all features and the definition of T are deleted.
| Policy | Merge | Result |
|---|---|---|
| A | /attributes ← { "firmware": "x" } | rejected (correct) |
| A | /attributes/firmware ← null | rejected (correct) |
| A | /attributes ← { "{{ /firmware/ }}": null } | rejected (correct) |
| A | /attributes ← { "firmware": null } | authorized, firmware deleted |
| A | /attributes ← { "{{ ~firmware~ }}": null } | authorized, firmware deleted |
| A | /attributes/{{ ~firmware~ }} ← null | authorized, firmware deleted |
| A | /attributes ← { "firmware": {} } | authorized, firmware set to {} |
| A | /attributes ← { "firmware": {}, "other": 1 } | authorized, firmware set to {} |
| B | / ← { "features": "x" } | rejected (correct) |
| B | / ← { "attributes": null, "features": null } | authorized, attributes and features deleted |
| B | / ← { "attributes": { "other": 1, "x": {} } } | authorized, attribute x created |
Impact
This is an incorrect authorization (CWE-863) for deletions and empty-object values in JSON merge patches. The regex variant is additionally caused by an interpretation conflict (CWE-436) between the merge patch implementation and the authorization check.
Impacted are Eclipse Ditto deployments where subjects have WRITE permission on only a part of a Thing, e.g.:
- "may write everything except these fields" policies (
WRITEgranted on a parent, revoked on children), - "may only write these fields" policies (
WRITEgranted on selected attributes or features only).
Such a subject can delete any attributes, features (including properties and desired properties) and the definition of the Thing, overwrite any of these fields with an empty object, or create new fields as empty objects. This may break other components or users relying on that data. The changes are persisted and published as regular ThingMerged events.
Not impacted are subjects without any WRITE permission on the Thing. Read access control is not affected.
Affected versions: 2.0.0 up to 3.8.14 and 3.9.0 up to 3.9.8.
Workarounds
There is no configuration-based workaround. For untrusted subjects with partial WRITE permission, operators can reject HTTP PATCH requests to the Things API in their reverse proxy. This does not cover merge commands sent via the WebSocket API or connections.
Credits
Thanks to Jiwoo Kim for reporting the regex deletion variant of this vulnerability. The more general null deletion and empty object variants were identified by the Eclipse Ditto maintainers while analysing the report and reviewing the fix.