did-btcr2-js

ADR 155: JSONPatch Applies the Rules of RFC 6901 and RFC 6902 with Its Own Engine and Accepts Each Member Name

Context

The update paths apply the patch by the rules of RFC 6902

The specification has the same rule on the write path and on the read path:

Apply jsonPatch to sourceDidDocument to create targetDidDocument. An [INVALID_DID_UPDATE] error MUST be raised if jsonPatch is malformed or fails to apply. (Update, Construct BTCR2 Unsigned Update)

Raise an [INVALID_DID_UPDATE] error if update.patch is malformed or fails to apply. JSON Patch operations are evaluated in order; the first operation that fails, including a failed test operation, fails the whole patch. (Resolve, Apply update)

RFC 6901 and RFC 6902 define when a patch is malformed and when it fails to apply:

Rule Text
Pointer syntax RFC 6901 section 3 gives escaped = "~" ( "0" / "1" ) and says: “It is an error condition if a JSON Pointer value does not conform to this syntax (see Section 7).”
Object token RFC 6901 section 4: “If the currently referenced value is a JSON object, the new referenced value is the object member with the name identified by the reference token.”
Array token RFC 6901 section 4: the token must be “characters comprised of digits (see ABNF below; note that leading zeros are not allowed)” or “exactly the single character “-“”.
Index bound RFC 6902 section 4.1: “The specified index MUST NOT be greater than the number of elements in the array.”
Location exists RFC 6902 sections 4.2 and 4.3: “The target location MUST exist for the operation to be successful.” Sections 4.4 and 4.5: “The “from” location MUST exist for the operation to be successful.”
Move into a child RFC 6902 section 4.4: “The “from” location MUST NOT be a proper prefix of the “path” location; i.e., a location cannot be moved into one of its children.”
Copy is an add RFC 6902 section 4.5: “This operation is functionally identical to an “add” operation at the target location using the value specified in the “from” member.”
Object equality RFC 6902 section 4.6: “objects: are considered equal if they contain the same number of members, and if each member can be considered equal to a member in the other object, by comparing their keys (as strings) and their values (using this list of type-specific rules).”
Ignored members RFC 6902 section 4: “Members that are not explicitly defined for the operation in question MUST be ignored”
Failure RFC 6902 section 5: “If a normative requirement is violated by a JSON Patch document, or if an operation is not successful, evaluation of the JSON Patch document SHOULD terminate and application of the entire patch document SHALL NOT be deemed successful.”

The old engine did not apply these rules

JSONPatch.apply ran the operation checks of the common package and then called fast-json-patch 3.1.1. The last release of that library is from 2022-03-24. The table shows the results of the old engine and the results of the RFC rules:

Document Patch Old result RFC result
{"alsoKnownAs":["a"]} add /alsoKnownAs/00 inserts at index 0 fails: 00 is not an array index
{"a":["p","q"]} add /a/ (an empty token) inserts at index 0 fails: an empty token is not an array index
{"a":["p","q"]} add /a/4294967296 inserts at index 0 fails: the index is greater than the number of elements
{} add /x~2y adds the member x~2y fails: ~2 is not an escape
{"service":[{"id":"#s"}]} remove /service/0/toString applies, no change fails: the location does not exist
{"o":{"x":1}} replace /o/valueOf adds the member valueOf fails: the location does not exist
{"s":[{"x":1},{"y":2}]} move from /s/0 to /s/0/z applies fails: a move into a child
{"a":"A","c":[1,2,3]} copy from /a to /c/9 appends fails: the index is greater than the number of elements
{"c":[1,2,3]} move from /c/0 to /c/3 appends fails: after the remove, the array has 2 elements
{"a":1} move from '' to /c a document with a cycle fails: a move into a child
{"o":{"hasOwnProperty":"x"}} test /o with the value {"hasOwnProperty":"x"} fails with a TypeError applies
{"service":[{"id":"#s"}]} add /service/0/constructor fails applies
{"o":{}} add /o/__proto__ fails applies
{} add /a with the member "from":"/constructor" fails applies: from is an ignored member
{"a":1} remove '' gives null no rule (decision 4)

The causes:

The two update paths use JSONPatch.apply. Thus the library signed and resolved updates that a conformant resolver refuses. It also refused updates that a conformant resolver applies.

The strict option

ADR 112 added the strict option to JSONPatch.apply, with the default false. ADR 112 also says: “the default flips at the next major version of the common package”. Common 10.0.0 made this change. Its CHANGELOG says: “JSONPatch.apply is strict by default (ADR 112).” Both method paths passed strict: true. Only the scenario tool of the test vectors passed strict: false, and the tool uses the source document if the apply fails.

Decision

1. The common package applies JSON Patch with its own engine

JSONPatch.apply(sourceDocument, operations) applies the rules of RFC 6901 and RFC 6902 with an engine in packages/common/src/json-patch.ts. The common package no longer depends on fast-json-patch. The steps of apply:

  1. If operations is not an array, the apply fails. This check runs before the JSON copy, because undefined has no JSON form.
  2. The engine makes a JSON copy of the operations (ADR 139) and checks the copy with validateOperations.
  3. The engine makes a JSON copy of the source document.
  4. The operations run in order on the copy of the document. The first operation that fails stops the patch (RFC 6902 section 5).

Each failure is a MethodError of type JSON_PATCH_APPLY_ERROR (ADR 085). Operations or a document with no JSON form also give this error, for example a value with a cycle or a BigInt. Before, these values gave a TypeError. The message names the index, the op, and the path of the operation that fails. data holds index, op, and path, and also from for a move or a copy.

2. The checks of the engine

validateOperations(operations) checks each operation with no document, in this order:

  1. The operation is a JSON object.
  2. op is one of the six operation codes of RFC 6902.
  3. path is a JSON Pointer by the syntax of RFC 6901 section 3.
  4. add, replace, and test have an own value member that is not undefined.
  5. move and copy have a from that is a JSON Pointer.
  6. The from of a move is not a proper prefix of its path.

The engine ignores each other member, for example a from in an add (RFC 6902 section 4). Then apply checks each operation against the document:

3. Each member name is data

The engine refuses no member name. constructor, prototype, and __proto__ are member names, as each other name is. The engine reads a member only if it is an own member (Object.hasOwn), and it writes each member with Object.defineProperty. It works on JSON copies, which hold only plain objects, arrays, and primitive values. Thus no pointer gets to a prototype or a global object, and the guard of common 9.3.0 is removed. A __proto__ member is an own data member, as in DidDocument (ADR 144).

4. A patch can replace the whole document, but cannot remove it

An add or a replace at the pointer '' replaces the whole document with the value (RFC 6902 section 4.1). The result can be any JSON value, and apply keeps the return type Record<any, any>. The two method paths then refuse a result that is not a DID document.

A remove at '' fails. RFC 6902 gives no document after the root is removed. Before, apply gave null, and only DidMethodApi.applyPatch showed this null. The two method paths refused the patch before and after this change, because null is not a DID document.

5. The apply has no options

These items are removed:

The two method update paths, DidMethodApi.applyPatch, and the scenario tool of the test vectors call apply with no options.

6. The common tests run the JSON Patch test suite

The common package adds the JSON Patch test suite of the json-patch/json-patch-tests repository as a devDependency. The suite holds data only, under the Apache-2.0 license. The dependency names the commit 2a928f90 (2025-02-21), because the npm release 1.1.0 (2022) has only 91 enabled cases. The commit has 108 enabled cases.

json-patch-test-suite.spec.ts runs each enabled case. A case with error must fail with JSON_PATCH_APPLY_ERROR. A case with expected must give that document. The spec also checks that the suite has 108 enabled cases. The old engine failed 2 of the 108 cases.

7. Each judgment call conforms to the specification texts

Judgment call Text Result
Remove the guard for __proto__, constructor, and prototype. RFC 6901 section 4: “If the currently referenced value is a JSON object, the new referenced value is the object member with the name identified by the reference token.” Resolve, Apply update: “Raise an [INVALID_DID_UPDATE] error if update.patch is malformed or fails to apply.” Conforms. A patch that names such a member is not malformed, and it applies.
An inherited JavaScript member does not exist. RFC 6901 section 4 (the same text). RFC 6902 section 4.2: “The target location MUST exist for the operation to be successful.” Conforms. An inherited function is not a member of the JSON object.
A remove at '' fails. Update, Construct BTCR2 Unsigned Update: “Apply jsonPatch to sourceDidDocument to create targetDidDocument.” RFC 6902 section 4.2: “The “remove” operation removes the value at the target location.” Conforms. After the root is removed, no document exists, so the patch fails to apply. Both method paths refused the patch before this change too.
A move to its own location makes no change after the from check. RFC 6902 section 4.4: “This operation is functionally identical to a “remove” operation on the “from” location, followed immediately by an “add” operation at the target location with the value that was just removed.” RFC 8259 section 1: “An object is an unordered collection of zero or more name/value pairs” Conforms. A remove and an add at one location give the same value. Only the member order can be different, and the member order is not significant. JCS sorts the members, so the hash is the same.
A test compares with a list of pairs, not with recursion. RFC 6902 section 4.6 (the object rule above). Conforms. The result is the same, and a deep value cannot overflow the call stack.
A value must be an own member that is not undefined. RFC 6902 section 4.1: “The operation object MUST contain a “value” member whose content specifies the value to be added.” Conforms. An inherited member is not a member of the operation object, and undefined has no JSON form.
The error data has from only for move and copy. RFC 6902 section 4: “Members that are not explicitly defined for the operation in question MUST be ignored” Conforms. A from in another operation is an ignored member.
A location that does not exist gives path "<p>" does not exist. Only the last token of an add in an array gives "<t>" is not an array index (RFC 6901 section 4). RFC 6901 section 4 (the array token rule). RFC 6902 section 4.2 (the rule above). Conforms. The specification and the RFCs define no error text. Each case fails.
A thrown value that is not an Error gives a fixed text. A toJSON method of an operation value can throw such a value. The specification and the RFCs define no error text. Conforms. The text does not convert the thrown value to a string, so the text cannot throw. ADR 085 applies.

Alternatives

Consequences

Superseded.

Unchanged.

Implementation

References