From 136104d6ffc08070f140fe44374f0bf91d0fb1d9 Mon Sep 17 00:00:00 2001 From: Alex Dunae Date: Fri, 10 Jul 2026 12:40:38 -0700 Subject: [PATCH 1/3] Add nonce prop for MUI datagrid components --- ...x-datagrid-csp-nonce_2026-07-10-00-00.json | 10 ++++ packages/modules/imodel-browser/README.md | 10 ++++ .../src/mui/containers/DataGridNonce.test.tsx | 50 +++++++++++++++++++ .../mui/containers/ITwinGrid/ITwinGridMUI.tsx | 6 +++ .../containers/ITwinGrid/ITwinTableMUI.tsx | 6 +++ .../containers/iModelGrid/IModelGridMUI.tsx | 6 +++ .../containers/iModelGrid/IModelTableMUI.tsx | 6 +++ 7 files changed, 94 insertions(+) create mode 100644 common/changes/@itwin/imodel-browser-react/alex-datagrid-csp-nonce_2026-07-10-00-00.json create mode 100644 packages/modules/imodel-browser/src/mui/containers/DataGridNonce.test.tsx diff --git a/common/changes/@itwin/imodel-browser-react/alex-datagrid-csp-nonce_2026-07-10-00-00.json b/common/changes/@itwin/imodel-browser-react/alex-datagrid-csp-nonce_2026-07-10-00-00.json new file mode 100644 index 00000000..60591e20 --- /dev/null +++ b/common/changes/@itwin/imodel-browser-react/alex-datagrid-csp-nonce_2026-07-10-00-00.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@itwin/imodel-browser-react", + "comment": "Add CSP nonce support to the MUI iTwin and iModel DataGrid table views.", + "type": "patch" + } + ], + "packageName": "@itwin/imodel-browser-react" +} diff --git a/packages/modules/imodel-browser/README.md b/packages/modules/imodel-browser/README.md index c3c7d15c..1b268586 100644 --- a/packages/modules/imodel-browser/README.md +++ b/packages/modules/imodel-browser/README.md @@ -8,6 +8,16 @@ Contains components that let the user browse the iModels of a context and select When making changes to the src, run `rushx start` in the dev folder to enable source watching and rebuild, so the storybook dev-server will have access to updated code on successful code compilation. +## Content Security Policy for MUI Components + +Some MUI components create runtime style elements. Applications with a nonce-based Content Security Policy can pass the current document nonce to `ITwinGridMUI` or `IModelGridMUI`: + +```tsx + +``` + +The nonce must match the value in the document's `style-src` or `style-src-elem` directive.. + ## Changelog See complete CHANGELOG [here](https://github.com/iTwin/admin-components-react/blob/main/packages/modules/imodel-browser/CHANGELOG.md) diff --git a/packages/modules/imodel-browser/src/mui/containers/DataGridNonce.test.tsx b/packages/modules/imodel-browser/src/mui/containers/DataGridNonce.test.tsx new file mode 100644 index 00000000..bda86812 --- /dev/null +++ b/packages/modules/imodel-browser/src/mui/containers/DataGridNonce.test.tsx @@ -0,0 +1,50 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Bentley Systems, Incorporated. All rights reserved. + * See LICENSE.md in the project root for license terms and full copyright notice. + *--------------------------------------------------------------------------------------------*/ +import "@testing-library/jest-dom"; + +import { render, screen } from "@testing-library/react"; +import React from "react"; + +import { IModelTableMUI } from "./iModelGrid/IModelTableMUI"; +import { ITwinTableMUI } from "./ITwinGrid/ITwinTableMUI"; + +jest.mock("@mui/x-data-grid", () => ({ + DataGrid: ({ nonce }: { nonce?: string }) => ( +
+ ), +})); + +const nonce = "test-csp-nonce"; + +describe("MUI table CSP nonce", () => { + it("forwards the nonce to the iTwin DataGrid", () => { + render( + + ); + + expect(screen.getByTestId("data-grid")).toHaveAttribute("nonce", nonce); + }); + + it("forwards the nonce to the iModel DataGrid", () => { + render( + + ); + + expect(screen.getByTestId("data-grid")).toHaveAttribute("nonce", nonce); + }); +}); diff --git a/packages/modules/imodel-browser/src/mui/containers/ITwinGrid/ITwinGridMUI.tsx b/packages/modules/imodel-browser/src/mui/containers/ITwinGrid/ITwinGridMUI.tsx index bce76405..1afc1bc4 100644 --- a/packages/modules/imodel-browser/src/mui/containers/ITwinGrid/ITwinGridMUI.tsx +++ b/packages/modules/imodel-browser/src/mui/containers/ITwinGrid/ITwinGridMUI.tsx @@ -75,6 +75,10 @@ export interface ITwinGridPropsMUI tileOverrides?: Partial; /** Overrides for table column definitions and visibility in cells viewMode */ tableOverrides?: ITwinTableOverridesMUI; + /** + * Nonce applied to `