set-object-value 0.0.3 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+
3
+ var _interopRequireWildcard = require("reshow-runtime/helpers/interopRequireWildcard");
4
+
5
+ var _index = _interopRequireWildcard(require("../index.js"));
6
+
7
+ var _chai = require("chai");
8
+
9
+ describe("Test set", function () {
10
+ it("check set result", function () {
11
+ var obj = {};
12
+ (0, _index["default"])(obj, ["a", "b"], "c");
13
+ (0, _chai.expect)(obj).to.deep.equal({
14
+ a: {
15
+ b: "c"
16
+ }
17
+ });
18
+ });
19
+ });
20
+ describe("CVE-2020-28281", function () {
21
+ it("should not overwrite prototype", function () {
22
+ var obj = {};
23
+ (0, _chai.expect)(obj.isAdmin).to.be.undefined;
24
+ (0, _chai.expect)(function () {
25
+ return (0, _index["default"])(obj, ["__proto__", "isAdmin"], true);
26
+ }).to["throw"]();
27
+ (0, _chai.expect)(function () {
28
+ return (0, _index["default"])(obj, ["constructor", "isAdmin"], true);
29
+ }).to["throw"]();
30
+ (0, _chai.expect)(function () {
31
+ return (0, _index["default"])(obj, ["prototype", "isAdmin"], true);
32
+ }).to["throw"]();
33
+ (0, _chai.expect)(obj.isAdmin).to.be.undefined;
34
+ (0, _chai.expect)({}.isAdmin).to.be.undefined;
35
+ (0, _index["default"])(obj, ["isAdmin"], true);
36
+ (0, _chai.expect)(obj.isAdmin).to.be["true"];
37
+ });
38
+ it("should force overwrite prototype with unsafe mode", function () {
39
+ var obj = {};
40
+ (0, _index.unsafeSet)(obj, ["__proto__", "fake"], true);
41
+ (0, _chai.expect)(obj.fake).to.be["true"];
42
+ });
43
+ it("safe way with unsafe set", function () {
44
+ var obj = Object.create(null);
45
+ (0, _index.unsafeSet)(obj, ["__proto__", "fake"], true);
46
+ (0, _chai.expect)(obj.fake).to.be.undefined;
47
+ (0, _chai.expect)(obj.__proto__.fake).to.be["true"];
48
+ var obj2 = Object.create(null);
49
+ (0, _chai.expect)({}.fake).to.be["true"]; // bad case
50
+
51
+ (0, _chai.expect)(obj2.fake).to.be.undefined;
52
+ });
53
+ });
54
+ describe("Test append", function () {
55
+ it("test simple append", function () {
56
+ var obj = {};
57
+ (0, _index["default"])(obj, ["a"], "a1", true);
58
+ (0, _index["default"])(obj, ["a"], "a2", true);
59
+ (0, _chai.expect)(obj).to.deep.equal({
60
+ a: ["a1", "a2"]
61
+ });
62
+ });
63
+ it("should keep origin value", function () {
64
+ var obj = {};
65
+ (0, _index["default"])(obj, ["a"], "a3");
66
+ (0, _index["default"])(obj, ["a"], "a4", true);
67
+ (0, _chai.expect)(obj).to.deep.equal({
68
+ a: ["a3", "a4"]
69
+ });
70
+ });
71
+ it("last is not array", function () {
72
+ var obj = {
73
+ a: {
74
+ b: "foo"
75
+ }
76
+ };
77
+ (0, _index["default"])(obj, ["a", "b"], "bar", true);
78
+ (0, _chai.expect)(obj.a.b).to.deep.equal(["foo", "bar"]);
79
+ });
80
+ });
@@ -1,22 +1,48 @@
1
- 'use strict';
1
+ "use strict";
2
2
 
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- var replaceValue = function replaceValue(obj, arrKey, val, isAppend) {
7
- var last = arrKey.pop();
8
- arrKey.forEach(function (k) {
9
- obj[k] = obj[k] || {};
10
- obj = obj[k];
6
+ exports.unsafeSet = exports["default"] = void 0;
7
+
8
+ var isPrototypePolluted = function isPrototypePolluted(arrPath) {
9
+ var joinPath = "|".concat(arrPath.join("|"), "|");
10
+ return ["__proto__", "constructor", "prototype"].some(function (key) {
11
+ return -1 !== joinPath.indexOf("|".concat(key, "|"));
12
+ });
13
+ };
14
+
15
+ var replaceValue = function replaceValue(obj, arrPath, val, isAppend, unSafe) {
16
+ if (!unSafe && isPrototypePolluted(arrPath)) {
17
+ throw "Contain un-safe key.";
18
+ }
19
+
20
+ var last = arrPath.pop();
21
+ var linkObj = obj;
22
+ arrPath.forEach(function (k) {
23
+ var _linkObj$k;
24
+
25
+ linkObj[k] = (_linkObj$k = linkObj[k]) !== null && _linkObj$k !== void 0 ? _linkObj$k : Object.create(null);
26
+ linkObj = linkObj[k];
11
27
  });
12
- if (isAppend && !obj[last]) {
13
- obj[last] = [val];
14
- } else if (isAppend && obj[last].push) {
15
- obj[last].push(val);
28
+
29
+ if (isAppend && (!linkObj[last] || !linkObj[last].push)) {
30
+ if (!linkObj[last]) {
31
+ linkObj[last] = [val];
32
+ } else {
33
+ linkObj[last] = [linkObj[last], val];
34
+ }
35
+ } else if (isAppend && linkObj[last].push) {
36
+ linkObj[last].push(val);
16
37
  } else {
17
- obj[last] = val;
38
+ linkObj[last] = val;
18
39
  }
19
40
  };
20
41
 
21
- exports.default = replaceValue;
22
- module.exports = exports['default'];
42
+ var unsafeSet = function unsafeSet(obj, arrPath, val, isAppend) {
43
+ return replaceValue(obj, arrPath, val, isAppend, true);
44
+ };
45
+
46
+ exports.unsafeSet = unsafeSet;
47
+ var _default = replaceValue;
48
+ exports["default"] = _default;
@@ -0,0 +1,74 @@
1
+ import set, { unsafeSet } from "../index.js";
2
+ import { expect } from "chai";
3
+ describe("Test set", function () {
4
+ it("check set result", function () {
5
+ var obj = {};
6
+ set(obj, ["a", "b"], "c");
7
+ expect(obj).to.deep.equal({
8
+ a: {
9
+ b: "c"
10
+ }
11
+ });
12
+ });
13
+ });
14
+ describe("CVE-2020-28281", function () {
15
+ it("should not overwrite prototype", function () {
16
+ var obj = {};
17
+ expect(obj.isAdmin).to.be.undefined;
18
+ expect(function () {
19
+ return set(obj, ["__proto__", "isAdmin"], true);
20
+ }).to["throw"]();
21
+ expect(function () {
22
+ return set(obj, ["constructor", "isAdmin"], true);
23
+ }).to["throw"]();
24
+ expect(function () {
25
+ return set(obj, ["prototype", "isAdmin"], true);
26
+ }).to["throw"]();
27
+ expect(obj.isAdmin).to.be.undefined;
28
+ expect({}.isAdmin).to.be.undefined;
29
+ set(obj, ["isAdmin"], true);
30
+ expect(obj.isAdmin).to.be["true"];
31
+ });
32
+ it("should force overwrite prototype with unsafe mode", function () {
33
+ var obj = {};
34
+ unsafeSet(obj, ["__proto__", "fake"], true);
35
+ expect(obj.fake).to.be["true"];
36
+ });
37
+ it("safe way with unsafe set", function () {
38
+ var obj = Object.create(null);
39
+ unsafeSet(obj, ["__proto__", "fake"], true);
40
+ expect(obj.fake).to.be.undefined;
41
+ expect(obj.__proto__.fake).to.be["true"];
42
+ var obj2 = Object.create(null);
43
+ expect({}.fake).to.be["true"]; // bad case
44
+
45
+ expect(obj2.fake).to.be.undefined;
46
+ });
47
+ });
48
+ describe("Test append", function () {
49
+ it("test simple append", function () {
50
+ var obj = {};
51
+ set(obj, ["a"], "a1", true);
52
+ set(obj, ["a"], "a2", true);
53
+ expect(obj).to.deep.equal({
54
+ a: ["a1", "a2"]
55
+ });
56
+ });
57
+ it("should keep origin value", function () {
58
+ var obj = {};
59
+ set(obj, ["a"], "a3");
60
+ set(obj, ["a"], "a4", true);
61
+ expect(obj).to.deep.equal({
62
+ a: ["a3", "a4"]
63
+ });
64
+ });
65
+ it("last is not array", function () {
66
+ var obj = {
67
+ a: {
68
+ b: "foo"
69
+ }
70
+ };
71
+ set(obj, ["a", "b"], "bar", true);
72
+ expect(obj.a.b).to.deep.equal(["foo", "bar"]);
73
+ });
74
+ });
@@ -1,18 +1,40 @@
1
- 'use strict';
1
+ var isPrototypePolluted = function isPrototypePolluted(arrPath) {
2
+ var joinPath = "|".concat(arrPath.join("|"), "|");
3
+ return ["__proto__", "constructor", "prototype"].some(function (key) {
4
+ return -1 !== joinPath.indexOf("|".concat(key, "|"));
5
+ });
6
+ };
7
+
8
+ var replaceValue = function replaceValue(obj, arrPath, val, isAppend, unSafe) {
9
+ if (!unSafe && isPrototypePolluted(arrPath)) {
10
+ throw "Contain un-safe key.";
11
+ }
2
12
 
3
- var replaceValue = function replaceValue(obj, arrKey, val, isAppend) {
4
- var last = arrKey.pop();
5
- arrKey.forEach(function (k) {
6
- obj[k] = obj[k] || {};
7
- obj = obj[k];
13
+ var last = arrPath.pop();
14
+ var linkObj = obj;
15
+ arrPath.forEach(function (k) {
16
+ var _linkObj$k;
17
+
18
+ linkObj[k] = (_linkObj$k = linkObj[k]) !== null && _linkObj$k !== void 0 ? _linkObj$k : Object.create(null);
19
+ linkObj = linkObj[k];
8
20
  });
9
- if (isAppend && !obj[last]) {
10
- obj[last] = [val];
11
- } else if (isAppend && obj[last].push) {
12
- obj[last].push(val);
21
+
22
+ if (isAppend && (!linkObj[last] || !linkObj[last].push)) {
23
+ if (!linkObj[last]) {
24
+ linkObj[last] = [val];
25
+ } else {
26
+ linkObj[last] = [linkObj[last], val];
27
+ }
28
+ } else if (isAppend && linkObj[last].push) {
29
+ linkObj[last].push(val);
13
30
  } else {
14
- obj[last] = val;
31
+ linkObj[last] = val;
15
32
  }
16
33
  };
17
34
 
18
- export default replaceValue;
35
+ var unsafeSet = function unsafeSet(obj, arrPath, val, isAppend) {
36
+ return replaceValue(obj, arrPath, val, isAppend, true);
37
+ };
38
+
39
+ export default replaceValue;
40
+ export { unsafeSet };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "set-object-value",
3
- "version": "0.0.3",
3
+ "version": "0.0.8",
4
4
  "description": "set object nested value",
5
5
  "repository": "react-atomic/react-atomic-organism",
6
6
  "main": "./build/cjs/src/index.js",
@@ -10,21 +10,19 @@
10
10
  "license": "MIT",
11
11
  "dependencies": {},
12
12
  "devDependencies": {
13
- "babel-cli": "*",
13
+ "@babel/cli": "^7.x",
14
14
  "chai": "*",
15
15
  "mocha": "*"
16
16
  },
17
17
  "scripts": {
18
- "build:cjs": "BABEL_ENV=build babel src -d build/cjs/src",
19
- "build:es": "BABEL_ENV=es babel src -d build/es/src",
20
- "build": "rm -rf build && npm run build:cjs && npm run build:es",
21
- "prepublishOnly": "npm run test && npm run build",
22
- "build:test": "BABEL_ENV=build babel tests -d build/tests",
23
- "test": "npm run build && npm run build:test && mocha 'build/tests/**/*.js'"
18
+ "clean": "find ./build -name '*.*' | xargs rm -rf",
19
+ "build": "npm run clean && npm run build:cjs && npm run build:es",
20
+ "build:cjs": "BABEL_ENV=cjs babel src -d build/cjs/src --root-mode upward",
21
+ "build:es": "BABEL_ENV=es babel src -d build/es/src --root-mode upward",
22
+ "mochaFor": "mocha",
23
+ "mocha": "npm run mochaFor -- 'build/cjs/**/__tests__/*.js'",
24
+ "test": "npm run build && npm run mocha",
25
+ "prepublishOnly": "npm run test"
24
26
  },
25
- "files": [
26
- "build",
27
- "package.json",
28
- "README.md"
29
- ]
27
+ "files": ["build", "package.json", "README.md"]
30
28
  }