Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,35 @@ describe('<DataGridPremium /> - Row grouping', () => {
'',
]);
});

// Regression test for https://github.com/mui/mui-x/issues/22310
it('should not crash when grouping values match Object.prototype property names', () => {
render(
<Test
rows={[
{ id: 0, category1: 'constructor', category2: 'Cat 1' },
{ id: 1, category1: 'constructor', category2: 'Cat 2' },
{ id: 2, category1: '__proto__', category2: 'Cat 1' },
{ id: 3, category1: 'toString', category2: 'Cat 2' },
{ id: 4, category1: 'hasOwnProperty', category2: 'Cat 1' },
]}
initialState={{ rowGrouping: { model: ['category1'] } }}
defaultGroupingExpansionDepth={-1}
/>,
);

expect(getColumnValues(0)).to.deep.equal([
'constructor (2)',
'',
'',
'__proto__ (1)',
'',
'toString (1)',
'',
'hasOwnProperty (1)',
'',
]);
});
});

describe('colDef: groupingValueGetter & valueGetter', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ class DropOnLeafOperation extends BaseReorderOperation {
...targetNode,
type: 'group',
children: [sourceNode.id],
childrenFromPath: {},
childrenFromPath: Object.create(null),
groupingField: null,
isAutoGenerated: false,
childrenExpanded: true,
Expand Down
15 changes: 15 additions & 0 deletions packages/x-data-grid-pro/src/tests/treeData.DataGridPro.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,21 @@ describe('<DataGridPro /> - Tree data', () => {
'C',
]);
});

// Regression test for https://github.com/mui/mui-x/issues/22310
it('should not crash when path segments match Object.prototype property names', () => {
render(
<Test
rows={[
{ id: 0, name: 'constructor.leaf1' },
{ id: 1, name: 'constructor.leaf2' },
]}
getRowId={(row) => row.id}
defaultGroupingExpansionDepth={-1}
/>,
);
expect(getColumnValues(1)).to.deep.equal(['', 'constructor.leaf1', 'constructor.leaf2']);
});
});

describe('prop: defaultGroupingExpansionDepth', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export const insertDataRowInTree = ({
groupingKey: key,
groupingField: field,
children: [],
childrenFromPath: {},
childrenFromPath: Object.create(null),
childrenExpanded: false,
serverChildrenCount,
};
Expand Down Expand Up @@ -172,7 +172,7 @@ export const insertDataRowInTree = ({
groupingKey: key,
groupingField: field,
children: [],
childrenFromPath: {},
childrenFromPath: Object.create(null),
childrenExpanded: false,
};

Expand Down Expand Up @@ -207,7 +207,7 @@ export const insertDataRowInTree = ({
groupingKey: key,
groupingField: field,
children: [],
childrenFromPath: {},
childrenFromPath: Object.create(null),
childrenExpanded: false,
};
tree[existingNodeIdWithPartialPath] = updateGroupDefaultExpansion(
Expand Down
6 changes: 3 additions & 3 deletions packages/x-data-grid-pro/src/utils/tree/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ export const insertNodeInTree = (
}

if (groupingField == null) {
parentNode.childrenFromPath[groupingFieldName] = {
[groupingKeyName.toString()]: node.id,
};
const newGroupingField = Object.create(null);
newGroupingField[groupingKeyName.toString()] = node.id;
parentNode.childrenFromPath[groupingFieldName] = newGroupingField;
} else {
groupingField[groupingKeyName.toString()] = node.id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ export const buildRootGroup = (): GridGroupNode => ({
groupingKey: null,
isAutoGenerated: true,
children: [],
childrenFromPath: {},
// Prototype-less so user-supplied grouping values like `'constructor'`
// or `'__proto__'` cannot collide with `Object.prototype` properties.
childrenFromPath: Object.create(null),
childrenExpanded: true,
parent: null,
});
Expand Down
Loading