Skip to content

Commit 39dbd68

Browse files
committed
fix: potential manipulation of data passed
1 parent 87c06c3 commit 39dbd68

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/nested-list-view/nested-list-view.tsx

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ export interface IProps {
3333
keepOpenedState?: boolean;
3434
}
3535

36+
const DEFAULT_CHILDREN_NAME = 'items';
37+
3638
const defaultRootNode = {
3739
_internalId: 'root',
3840
items: [],
@@ -58,7 +60,11 @@ const NestedListView: React.FC<IProps> = React.memo(
5860
};
5961
}
6062

61-
const childrenName = getChildrenName ? getChildrenName(node) : 'items';
63+
const copyNode = { ...node };
64+
65+
const childrenName = getChildrenName
66+
? getChildrenName(node)
67+
: DEFAULT_CHILDREN_NAME;
6268
let children = node[childrenName];
6369

6470
if (children) {
@@ -67,19 +73,19 @@ const NestedListView: React.FC<IProps> = React.memo(
6773
(key: string) => children[key],
6874
);
6975
}
70-
node[childrenName] = children.map((_: INode, index: number) =>
76+
copyNode[childrenName] = children.map((_: INode, index: number) =>
7177
generateIds(children[index]),
7278
);
7379
}
7480

75-
if (node._internalId) {
81+
if (copyNode._internalId) {
7682
// @ts-ignore
77-
delete node._internalId;
83+
delete copyNode._internalId;
7884
}
7985

80-
node._internalId = hashObjectGenerator(node);
86+
copyNode._internalId = hashObjectGenerator(copyNode);
8187

82-
return node;
88+
return copyNode;
8389
},
8490
[getChildrenName],
8591
);
@@ -89,9 +95,7 @@ const NestedListView: React.FC<IProps> = React.memo(
8995
return {
9096
_internalId: 'root',
9197
items: props.data
92-
? props.data.map((_: INode, index: number) =>
93-
generateIds(props.data[index]),
94-
)
98+
? props.data.map((item: any) => generateIds(item))
9599
: [],
96100
name: 'root',
97101
opened: true,

0 commit comments

Comments
 (0)