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

[DataGrid] Fix eval blocked by CSP #9863

Merged
merged 7 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -25,6 +25,14 @@ import {
gridVisibleColumnFieldsSelector,
} from '../columns';

let hasEval: boolean;
try {
// eslint-disable-next-line no-eval
hasEval = eval('true');
} catch (_: unknown) {
hasEval = false;
}

type GridFilterItemApplier =
| {
v7: false;
Expand Down Expand Up @@ -240,21 +248,23 @@ export const buildAggregatedFilterItemsApplier = (
return null;
}

// Original logic:
// return (row, shouldApplyFilter) => {
// const resultPerItemId: GridFilterItemResult = {};
//
// for (let i = 0; i < appliers.length; i += 1) {
// const applier = appliers[i];
// if (!shouldApplyFilter || shouldApplyFilter(applier.item.field)) {
// resultPerItemId[applier.item.id!] = applier.v7
// ? applier.fn(row)
// : applier.fn(getRowId ? getRowId(row) : row.id);
// }
// }
//
// return resultPerItemId;
// };
if (!hasEval) {
// This is the original logic, which is used if `eval()` is not supported (aka prevented by CSP).
return (row, shouldApplyFilter) => {
const resultPerItemId: GridFilterItemResult = {};

for (let i = 0; i < appliers.length; i += 1) {
const applier = appliers[i];
if (!shouldApplyFilter || shouldApplyFilter(applier.item.field)) {
resultPerItemId[applier.item.id!] = applier.v7
? applier.fn(row)
: applier.fn(getRowId ? getRowId(row) : row.id);
}
}

return resultPerItemId;
};
}

// We generate a new function with `eval()` to avoid expensive patterns for JS engines
// such as a dynamic object assignment, e.g. `{ [dynamicKey]: value }`.
Expand Down
49 changes: 49 additions & 0 deletions test/e2e/fixtures/DataGrid/FilterEval.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* NOTE: This test requires that a CSP preventing `eval()` be present to be useful.
* See ../template.html for the CSP meta tag.
*/

import * as React from 'react';
import { DataGrid } from '@mui/x-data-grid';

const baselineProps = {
rows: [
{
id: 0,
brand: 'Nike',
year: 1990,
},
{
id: 1,
brand: 'Adidas',
year: 1995,
},
{
id: 2,
brand: 'Puma',
year: 1993,
},
{
id: 3,
brand: 'Gucci',
year: 1996,
},
],
columns: [
{ field: 'brand', width: 120 },
{ field: 'year', width: 120 },
],
};

export default function FilterEval() {
return (
<div style={{ width: 400, height: 200 }}>
<DataGrid
{...baselineProps}
filterModel={{
items: [{ field: 'brand', operator: 'contains', value: 'Nike' }],
}}
/>
</div>
);
}
1 change: 1 addition & 0 deletions test/e2e/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<title>Playwright end-to-end test</title>
<meta charset="utf-8" />
<meta name="viewport" content="minimum-scale=1, initial-scale=1, width=device-width" />
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; img-src *">
romgrk marked this conversation as resolved.
Show resolved Hide resolved
<style>
body {
background-color: white;
Expand Down