set-object-value 0.0.5 → 0.1.0

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.
@@ -3,28 +3,46 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports["default"] = void 0;
6
+ exports.unsafeSet = exports["default"] = void 0;
7
7
 
8
- var replaceValue = function replaceValue(obj, arrKey, val, isAppend) {
9
- var last = arrKey.pop();
10
- arrKey.forEach(function (k) {
11
- obj[k] = obj[k] || {};
12
- obj = obj[k];
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, "|"));
13
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;
14
24
 
15
- if (isAppend && (!obj[last] || !obj[last].push)) {
16
- if (!obj[last]) {
17
- obj[last] = [val];
25
+ linkObj[k] = (_linkObj$k = linkObj[k]) !== null && _linkObj$k !== void 0 ? _linkObj$k : Object.create(null);
26
+ linkObj = linkObj[k];
27
+ });
28
+
29
+ if (isAppend && (!linkObj[last] || !linkObj[last].push)) {
30
+ if (!linkObj[last]) {
31
+ linkObj[last] = [val];
18
32
  } else {
19
- obj[last] = [obj[last], val];
33
+ linkObj[last] = [linkObj[last], val];
20
34
  }
21
- } else if (isAppend && obj[last].push) {
22
- obj[last].push(val);
35
+ } else if (isAppend && linkObj[last].push) {
36
+ linkObj[last].push(val);
23
37
  } else {
24
- obj[last] = val;
38
+ linkObj[last] = val;
25
39
  }
26
40
  };
27
41
 
42
+ var unsafeSet = function unsafeSet(obj, arrPath, val, isAppend) {
43
+ return replaceValue(obj, arrPath, val, isAppend, true);
44
+ };
45
+
46
+ exports.unsafeSet = unsafeSet;
28
47
  var _default = replaceValue;
29
- exports["default"] = _default;
30
- module.exports = exports.default;
48
+ exports["default"] = _default;
@@ -0,0 +1,74 @@
1
+ import set, { unsafeSet } from "../index.mjs";
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
+ });
@@ -0,0 +1,40 @@
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
+ }
12
+
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];
20
+ });
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);
30
+ } else {
31
+ linkObj[last] = val;
32
+ }
33
+ };
34
+
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,10 +1,19 @@
1
1
  {
2
+ "version": "0.1.0",
2
3
  "name": "set-object-value",
3
- "version": "0.0.5",
4
+ "repository": {
5
+ "type": "git",
6
+ "url": "https://github.com/react-atomic/react-atomic-organism",
7
+ "directory": "packages/lib/set-object-value"
8
+ },
9
+ "homepage": "https://github.com/react-atomic/react-atomic-organism/tree/main/packages/lib/set-object-value",
4
10
  "description": "set object nested value",
5
- "repository": "react-atomic/react-atomic-organism",
6
11
  "main": "./build/cjs/src/index.js",
7
- "module": "./build/es/src/index.js",
12
+ "module": "./build/es/src/index.mjs",
13
+ "exports": {
14
+ "require": "./build/cjs/src/index.js",
15
+ "import": "./build/es/src/index.mjs"
16
+ },
8
17
  "keywords": [],
9
18
  "author": "Hill <hill@kimo.com>",
10
19
  "license": "MIT",
@@ -15,11 +24,14 @@
15
24
  "mocha": "*"
16
25
  },
17
26
  "scripts": {
18
- "build:cjs": "BABEL_ENV=build babel src -d build/cjs/src --root-mode upward",
19
- "build:es": "BABEL_ENV=es babel src -d build/es/src --root-mode upward",
20
- "build": "rm -rf build && npm run build:cjs && npm run build:es",
21
- "prepublishOnly": "npm run test && npm run build",
22
- "test": "npm run build && mocha 'build/cjs/**/__tests__/*.js'"
27
+ "clean": "find ./build -name '*.*' | xargs rm -rf",
28
+ "build:cjs": "BABEL_ENV=cjs babel src -d build/cjs/src --ignore /**/__tests__ --root-mode upward",
29
+ "build:es": "BABEL_ENV=es babel src -d build/es/src --out-file-extension .mjs --root-mode upward",
30
+ "build": "npm run clean && npm run build:cjs && npm run build:es",
31
+ "mochaFor": "mocha",
32
+ "mocha": "npm run mochaFor -- 'build/es/**/__tests__/*.mjs'",
33
+ "test": "npm run build && npm run mocha",
34
+ "prepublishOnly": "npm run test"
23
35
  },
24
36
  "files": [
25
37
  "build",
@@ -1,37 +0,0 @@
1
- 'use strict';
2
-
3
- var _interopRequireDefault = require("reshow-runtime/helpers/interopRequireDefault");
4
-
5
- var _index = _interopRequireDefault(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('Test append', function () {
21
- it('test simple append', function () {
22
- var obj = {};
23
- (0, _index["default"])(obj, ['a'], 'a1', true);
24
- (0, _index["default"])(obj, ['a'], 'a2', true);
25
- (0, _chai.expect)(obj).to.deep.equal({
26
- a: ['a1', 'a2']
27
- });
28
- });
29
- it('should keep origin value', function () {
30
- var obj = {};
31
- (0, _index["default"])(obj, ['a'], 'a3');
32
- (0, _index["default"])(obj, ['a'], 'a4', true);
33
- (0, _chai.expect)(obj).to.deep.equal({
34
- a: ['a3', 'a4']
35
- });
36
- });
37
- });
@@ -1,33 +0,0 @@
1
- 'use strict';
2
-
3
- import set from '../index.js';
4
- import { expect } from 'chai';
5
- describe('Test set', function () {
6
- it('check set result', function () {
7
- var obj = {};
8
- set(obj, ['a', 'b'], 'c');
9
- expect(obj).to.deep.equal({
10
- a: {
11
- b: 'c'
12
- }
13
- });
14
- });
15
- });
16
- describe('Test append', function () {
17
- it('test simple append', function () {
18
- var obj = {};
19
- set(obj, ['a'], 'a1', true);
20
- set(obj, ['a'], 'a2', true);
21
- expect(obj).to.deep.equal({
22
- a: ['a1', 'a2']
23
- });
24
- });
25
- it('should keep origin value', function () {
26
- var obj = {};
27
- set(obj, ['a'], 'a3');
28
- set(obj, ['a'], 'a4', true);
29
- expect(obj).to.deep.equal({
30
- a: ['a3', 'a4']
31
- });
32
- });
33
- });
@@ -1,21 +0,0 @@
1
- var replaceValue = function replaceValue(obj, arrKey, val, isAppend) {
2
- var last = arrKey.pop();
3
- arrKey.forEach(function (k) {
4
- obj[k] = obj[k] || {};
5
- obj = obj[k];
6
- });
7
-
8
- if (isAppend && (!obj[last] || !obj[last].push)) {
9
- if (!obj[last]) {
10
- obj[last] = [val];
11
- } else {
12
- obj[last] = [obj[last], val];
13
- }
14
- } else if (isAppend && obj[last].push) {
15
- obj[last].push(val);
16
- } else {
17
- obj[last] = val;
18
- }
19
- };
20
-
21
- export default replaceValue;