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

Content Security Policy nonce option is not working #2066

Closed
MglMX opened this issue Jun 18, 2020 · 8 comments
Closed

Content Security Policy nonce option is not working #2066

MglMX opened this issue Jun 18, 2020 · 8 comments
Labels
bug Something isn't working wontfix This will not be worked on

Comments

@MglMX
Copy link
Contributor

MglMX commented Jun 18, 2020

Describe the bug
The possibility to add a cspNonce was introduced in this MR #1438.

The problem is that material-table depends on the package react-beautiful-dnd version 11.0.31 which does not have the option to pass a cspNonce as a property.

To Reproduce
Steps to reproduce the behaviour:

  • Set up a CSP where the style src is not set to unsafe-inline
  • Use material-table

Expected behaviour
There should be no CSP errors but there is. This is because the react-beautiful-dnd library sets inline styles.

Screenshots
image

Desktop :

  • OS: Ubuntu
  • Browser Firefox
  • Version 77.0.1
@stale
Copy link

stale bot commented Sep 16, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. You can reopen it if it required.

@stale stale bot added the wontfix This will not be worked on label Sep 16, 2020
@stale stale bot closed this as completed Sep 23, 2020
@xiaochenzhang
Copy link

xiaochenzhang commented Jul 8, 2021

Hi there, I see there is a pull request to fix this bug and already merged. But I still see the same issue now in our application, we are using v1.69.0, tried on latest Chrome and Safari, the same problem. Can someone check this again please? @MglMX

@MglMX
Copy link
Contributor Author

MglMX commented Jul 10, 2021

I have checked and it is still working on version v1.69.0.

What CSP are you using for style-src? Can you share some code with the issue?

@xiaochenzhang
Copy link

xiaochenzhang commented Jul 12, 2021

@MglMX
Our application is currently served statically, so no server side code. Below is the CSP in our index.html, the nonce is currently static also, because we really need to pass our audit asap, dynamically add nonce is planned for later, so ignore the length etc. as well. It is just for testing.
<meta http-equiv="Content-Security-Policy" content="default-src 'self' https:;frame-src 'self';img-src 'self' data:;connect-src 'self' http: https:;style-src 'self' 'nonce-dX5eH2xW7yW0dC0b' https:">
<meta property="csp-nonce" content="dX5eH2xW7yW0dC0b" />

When I run our application, I see every other mui elements have the nonce added, including materiable-table, but the react-beautiful-dnd (rbd) does not have nonce, see the the style tag and console errors:
Screenshot 2021-07-12 at 08 56 41

Here is what inside the style tag:
Screenshot 2021-07-12 at 10 53 31

The problem we are facing is, the table and its header, cell data are all working, only the table icons are not showing. Below is how we create the icon:

{ 
    cellStyle: { padding: 0 },
     render: (row) => (
       <div className={classes.iconsContainer} style={displayDetailButtons(row)}>
           <IconButton id="PermitListOpenDetailButton" className={classes.eyeIcon} size="small" onClick={() => openPermitDetails(row)}>
              <FontAwesomeIcon icon={faEye} />
           </IconButton>
      </div>
    ),
},

However, even if I removed everything, only basic table data is there (see below code), I still see the CSP inline-style error. So this error is not caused by my code. Maybe this is caused by the "drag and drop column" feature?

<MaterialTable 
  columns={[
    {
      title: t('permitListPage.id'), field: 'id', defaultSort: 'desc', type: 'string',
    },
  ]}
  title={emptyTitle}  // const emptyTitle = '';
  icons={tableIcons}  // import tableIcons from '../TableIcons';
  data={permits}
/>

@MglMX
Copy link
Contributor Author

MglMX commented Jul 12, 2021

You need to specify the nonce to the MaterialTablecomponent. It will then be passed down to the style tag of react-beautiful-dnd.

<MaterialTable 
  columns={[
    {
      title: t('permitListPage.id'), field: 'id', defaultSort: 'desc', type: 'string',
    },
  ]}
  title={emptyTitle}  // const emptyTitle = '';
  icons={tableIcons}  // import tableIcons from '../TableIcons';
  data={permits}
  options={{cspNonce: "dX5eH2xW7yW0dC0b"}}
/>

The problem with the icons is not related with the CSP. You need to install @material-ui/icons and import them as it is described in the readme.

@xiaochenzhang
Copy link

xiaochenzhang commented Jul 12, 2021

Hi, thanks for the reply.

It works as you suggested, the nonce is added to the style tag, and the error is gone.

But what is the different between material-table and other material ui components? because for other mui components, the nonce is automatically added by React webpack (I forgot to mention we are using React v16.8.6). If now I have to manually add this nonce in all material-tables in my application, it will be a mess. And if we want to create new tables then we have to remember to add the nonce there as well. Also we are planning to dynamically inject the nonce in our code, then this solution seems not sustainable?

@MglMX
Copy link
Contributor Author

MglMX commented Jul 12, 2021

Material-UI uses JSS to generate the style tags. The nonce used by JSS is taken dynamically from the <meta property="csp-nonce" content="yourNonce" /> tag. Webpack does not add the nonce to the style tags. You can find an explanation here.

material-table uses react-beautiful-dnd, which does not use JSS but their own solution to create the style tags. That's why you need to add the nonce here one more time. There is a discussion here on why it is not automatically obtained from the meta tag like JSS does but it needs to be passed as a prop.

You could create a getCSP function that dynamically gets the nonce from the meta tag. Indeed, it needs to be added to every table you create.

options={{cspNonce: getCSP()}}

@xiaochenzhang
Copy link

ah, ok, thanks a lot @MglMX ! I thought the nonce is added by setting webpack_nonce so webpack is generating these tags... For now I will modify all the tables to include the nonce, hope it can be set dynamically in the table in the future. Thanks again for the help! Really appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants