Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Understanding the protoAction behavior #50

Open
mwilde345 opened this issue Mar 16, 2021 · 2 comments
Open

Understanding the protoAction behavior #50

mwilde345 opened this issue Mar 16, 2021 · 2 comments

Comments

@mwilde345
Copy link

mwilde345 commented Mar 16, 2021

Using deepStrictEqual, I assumed the equality check would pass when stringifying and parsing a simple object. But the Object prototype is still null after parsing with json-bigint, even with protoAction: 'preserve' set.

The check passes if I do assert.deepStrictEqual(Object.assign({}, parsed), a)

Is this intended? Is it simply because JSON.stringify doesn't preserve prototypes into __proto__ keys?

const JSONBig = require("json-bigint")({
  protoAction: "preserve",
});
var assert = require("assert");

const a = { result: "ok" };
const response = JSON.stringify(a);
const parsed = JSONBig.parse(response);

assert.deepStrictEqual(parsed, a);
/*
AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal:
+ actual - expected

+ [Object: null prototype] {
- {
    result: 'ok'
  }
*/

Object.setPrototypeOf(parsed, {});
assert.deepStrictEqual(parsed, a);
/*
AssertionError [ERR_ASSERTION]: Values have same structure but are not reference-equal:

{
  result: 'ok'
}
*/
@anusha5695
Copy link

@mwilde345, they seem to parse with base class as null instead of Object to prevent prototype pollution.

We had a use case where, after parsing we wanted all prototypes at nested levels in place and we ended up used lodash deepClone to re add the prototypes back at level

@haoadoreorange
Copy link
Contributor

@mwilde345, they seem to parse with base class as null instead of Object to prevent prototype pollution.

We had a use case where, after parsing we wanted all prototypes at nested levels in place and we ended up used lodash deepClone to re add the prototypes back at level

To avoid prototype pollution I think it's enough to create the object with null prototype, and once proto get set, we can safely call setPrototypeOf upon the object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants