You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

json_patch.c 9.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * Copyright (c) 2021 Alexandru Ardelean.
  3. * Copyright (c) 2023 Eric Hawicz
  4. *
  5. * This is free software; you can redistribute it and/or modify
  6. * it under the terms of the MIT license. See COPYING for details.
  7. */
  8. #include "config.h"
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <errno.h>
  12. #include "json_patch.h"
  13. #include "json_object_private.h"
  14. #include "json_pointer_private.h"
  15. #include <limits.h>
  16. #ifndef SIZE_T_MAX
  17. #if SIZEOF_SIZE_T == SIZEOF_INT
  18. #define SIZE_T_MAX UINT_MAX
  19. #elif SIZEOF_SIZE_T == SIZEOF_LONG
  20. #define SIZE_T_MAX ULONG_MAX
  21. #elif SIZEOF_SIZE_T == SIZEOF_LONG_LONG
  22. #define SIZE_T_MAX ULLONG_MAX
  23. #else
  24. #error Unable to determine size of size_t
  25. #endif
  26. #endif
  27. #define _set_err(_errval, _errmsg) do { \
  28. patch_error->errno_code = (_errval); \
  29. patch_error->errmsg = (_errmsg); \
  30. errno = 0; /* To avoid confusion */ \
  31. } while (0)
  32. #define _set_err_from_ptrget(_errval, _fieldname) do { \
  33. patch_error->errno_code = (_errval); \
  34. patch_error->errmsg = (_errval) == ENOENT ? \
  35. "Did not find element referenced by " _fieldname " field" : \
  36. "Invalid " _fieldname " field"; \
  37. errno = 0; /* To avoid confusion */ \
  38. } while(0)
  39. /**
  40. * JavaScript Object Notation (JSON) Patch
  41. * RFC 6902 - https://tools.ietf.org/html/rfc6902
  42. */
  43. static int json_patch_apply_test(struct json_object **res,
  44. struct json_object *patch_elem,
  45. const char *path, struct json_patch_error *patch_error)
  46. {
  47. struct json_object *value1, *value2;
  48. if (!json_object_object_get_ex(patch_elem, "value", &value1)) {
  49. _set_err(EINVAL, "Patch object does not contain a 'value' field");
  50. return -1;
  51. }
  52. if (json_pointer_get(*res, path, &value2))
  53. {
  54. _set_err_from_ptrget(errno, "path");
  55. return -1;
  56. }
  57. if (!json_object_equal(value1, value2)) {
  58. _set_err(ENOENT, "Value of element referenced by 'path' field did not match 'value' field");
  59. return -1;
  60. }
  61. return 0;
  62. }
  63. static int __json_patch_apply_remove(struct json_pointer_get_result *jpres)
  64. {
  65. if (json_object_is_type(jpres->parent, json_type_array)) {
  66. return json_object_array_del_idx(jpres->parent, jpres->index_in_parent, 1);
  67. } else if (jpres->parent && jpres->key_in_parent) {
  68. json_object_object_del(jpres->parent, jpres->key_in_parent);
  69. return 0;
  70. } else {
  71. // We're removing the root object
  72. (void)json_object_put(jpres->obj);
  73. jpres->obj = NULL;
  74. return 0;
  75. }
  76. }
  77. static int json_patch_apply_remove(struct json_object **res, const char *path, struct json_patch_error *patch_error)
  78. {
  79. struct json_pointer_get_result jpres;
  80. int rc;
  81. if (json_pointer_get_internal(*res, path, &jpres))
  82. {
  83. _set_err_from_ptrget(errno, "path");
  84. return -1;
  85. }
  86. rc = __json_patch_apply_remove(&jpres);
  87. if (rc < 0)
  88. _set_err(EINVAL, "Unable to remove path referenced by 'path' field");
  89. // This means we removed and freed the root object, i.e. *res
  90. if (jpres.parent == NULL)
  91. *res = NULL;
  92. return rc;
  93. }
  94. // callback for json_pointer_set_with_array_cb()
  95. static int json_object_array_insert_idx_cb(struct json_object *parent, size_t idx,
  96. struct json_object *value, void *priv)
  97. {
  98. int rc;
  99. int *add = priv;
  100. if (idx > json_object_array_length(parent))
  101. {
  102. // Note: will propagate back out through json_pointer_set_with_array_cb()
  103. errno = EINVAL;
  104. return -1;
  105. }
  106. if (*add)
  107. rc = json_object_array_insert_idx(parent, idx, value);
  108. else
  109. rc = json_object_array_put_idx(parent, idx, value);
  110. if (rc < 0)
  111. errno = EINVAL;
  112. return rc;
  113. }
  114. static int json_patch_apply_add_replace(struct json_object **res,
  115. struct json_object *patch_elem,
  116. const char *path, int add, struct json_patch_error *patch_error)
  117. {
  118. struct json_object *value;
  119. int rc;
  120. if (!json_object_object_get_ex(patch_elem, "value", &value)) {
  121. _set_err(EINVAL, "Patch object does not contain a 'value' field");
  122. return -1;
  123. }
  124. /* if this is a replace op, then we need to make sure it exists before replacing */
  125. if (!add && json_pointer_get(*res, path, NULL)) {
  126. _set_err_from_ptrget(errno, "path");
  127. return -1;
  128. }
  129. rc = json_pointer_set_with_array_cb(res, path, json_object_get(value),
  130. json_object_array_insert_idx_cb, &add);
  131. if (rc)
  132. {
  133. _set_err(errno, "Failed to set value at path referenced by 'path' field");
  134. json_object_put(value);
  135. }
  136. return rc;
  137. }
  138. // callback for json_pointer_set_with_array_cb()
  139. static int json_object_array_move_cb(struct json_object *parent, size_t idx,
  140. struct json_object *value, void *priv)
  141. {
  142. int rc;
  143. struct json_pointer_get_result *from = priv;
  144. size_t len = json_object_array_length(parent);
  145. /**
  146. * If it's the same array parent, it means that we removed
  147. * and element from it, so the length is temporarily reduced
  148. * by 1, which means that if we try to move an element to
  149. * the last position, we need to check the current length + 1
  150. */
  151. if (parent == from->parent)
  152. len++;
  153. if (idx > len)
  154. {
  155. // Note: will propagate back out through json_pointer_set_with_array_cb()
  156. errno = EINVAL;
  157. return -1;
  158. }
  159. rc = json_object_array_insert_idx(parent, idx, value);
  160. if (rc < 0)
  161. errno = EINVAL;
  162. return rc;
  163. }
  164. static int json_patch_apply_move_copy(struct json_object **res,
  165. struct json_object *patch_elem,
  166. const char *path, int move, struct json_patch_error *patch_error)
  167. {
  168. json_pointer_array_set_cb array_set_cb;
  169. struct json_pointer_get_result from;
  170. struct json_object *jfrom;
  171. const char *from_s;
  172. size_t from_s_len;
  173. int rc;
  174. if (!json_object_object_get_ex(patch_elem, "from", &jfrom)) {
  175. _set_err(EINVAL, "Patch does not contain a 'from' field");
  176. return -1;
  177. }
  178. from_s = json_object_get_string(jfrom);
  179. from_s_len = strlen(from_s);
  180. if (strncmp(from_s, path, from_s_len) == 0) {
  181. /**
  182. * If lengths match, it's a noop, if they don't,
  183. * then we're trying to move a parent under a child
  184. * which is not allowed as per RFC 6902 section 4.4
  185. * The "from" location MUST NOT be a proper prefix of the "path"
  186. * location; i.e., a location cannot be moved into one of its children.
  187. */
  188. if (from_s_len == strlen(path))
  189. return 0;
  190. _set_err(EINVAL, "Invalid attempt to move parent under a child");
  191. return -1;
  192. }
  193. rc = json_pointer_get_internal(*res, from_s, &from);
  194. if (rc)
  195. {
  196. _set_err_from_ptrget(errno, "from");
  197. return rc;
  198. }
  199. // Note: it's impossible for json_pointer to find the root obj, due
  200. // to the path check above, so from.parent is guaranteed non-NULL
  201. json_object_get(from.obj);
  202. if (!move) {
  203. array_set_cb = json_object_array_insert_idx_cb;
  204. } else {
  205. rc = __json_patch_apply_remove(&from);
  206. if (rc < 0) {
  207. json_object_put(from.obj);
  208. return rc;
  209. }
  210. array_set_cb = json_object_array_move_cb;
  211. }
  212. rc = json_pointer_set_with_array_cb(res, path, from.obj, array_set_cb, &from);
  213. if (rc)
  214. {
  215. _set_err(errno, "Failed to set value at path referenced by 'path' field");
  216. json_object_put(from.obj);
  217. }
  218. return rc;
  219. }
  220. int json_patch_apply(struct json_object *copy_from, struct json_object *patch,
  221. struct json_object **base, struct json_patch_error *patch_error)
  222. {
  223. size_t ii;
  224. int rc = 0;
  225. struct json_patch_error placeholder;
  226. if (!patch_error)
  227. patch_error = &placeholder;
  228. patch_error->patch_failure_idx = SIZE_T_MAX;
  229. patch_error->errno_code = 0;
  230. if (base == NULL||
  231. (*base == NULL && copy_from == NULL) ||
  232. (*base != NULL && copy_from != NULL))
  233. {
  234. _set_err(EFAULT, "Exactly one of *base or copy_from must be non-NULL");
  235. return -1;
  236. }
  237. if (!json_object_is_type(patch, json_type_array)) {
  238. _set_err(EFAULT, "Patch object is not of type json_type_array");
  239. return -1;
  240. }
  241. if (copy_from != NULL)
  242. {
  243. if (json_object_deep_copy(copy_from, base, NULL) < 0)
  244. {
  245. _set_err(ENOMEM, "Unable to copy copy_from using json_object_deep_copy()");
  246. return -1;
  247. }
  248. }
  249. /* Go through all operations ; apply them on res */
  250. for (ii = 0; ii < json_object_array_length(patch); ii++) {
  251. struct json_object *jop, *jpath;
  252. struct json_object *patch_elem = json_object_array_get_idx(patch, ii);
  253. const char *op, *path;
  254. patch_error->patch_failure_idx = ii;
  255. if (!json_object_object_get_ex(patch_elem, "op", &jop)) {
  256. _set_err(EINVAL, "Patch object does not contain 'op' field");
  257. return -1;
  258. }
  259. op = json_object_get_string(jop);
  260. if (!json_object_object_get_ex(patch_elem, "path", &jpath)) {
  261. _set_err(EINVAL, "Patch object does not contain 'path' field");
  262. return -1;
  263. }
  264. path = json_object_get_string(jpath); // Note: empty string is ok!
  265. if (!strcmp(op, "test"))
  266. rc = json_patch_apply_test(base, patch_elem, path, patch_error);
  267. else if (!strcmp(op, "remove"))
  268. rc = json_patch_apply_remove(base, path, patch_error);
  269. else if (!strcmp(op, "add"))
  270. rc = json_patch_apply_add_replace(base, patch_elem, path, 1, patch_error);
  271. else if (!strcmp(op, "replace"))
  272. rc = json_patch_apply_add_replace(base, patch_elem, path, 0, patch_error);
  273. else if (!strcmp(op, "move"))
  274. rc = json_patch_apply_move_copy(base, patch_elem, path, 1, patch_error);
  275. else if (!strcmp(op, "copy"))
  276. rc = json_patch_apply_move_copy(base, patch_elem, path, 0, patch_error);
  277. else {
  278. _set_err(EINVAL, "Patch object has invalid 'op' field");
  279. return -1;
  280. }
  281. if (rc < 0)
  282. break;
  283. }
  284. return rc;
  285. }