Repository navigation
[csp provider] Add CSPProvider - #3553
Conversation
commit: |
Bundle size report
Check out the code infra dashboard for more information about this PR. |
7812605 to
d0e7e65
Compare
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
e9513af to
3224fce
Compare
mnajdova
left a comment
There was a problem hiding this comment.
While this unblocks people to add nonce, I think we need to also add some documentation around CSP, e.g. on MUI we had this page: https://mui.com/material-ui/guides/content-security-policy/
We were discussing this with @Janpot some time ago, maybe even a provider where people add the nonce could be better solution, e.g. this is how Emotion solved it. We could have a CSP provider with nonce, add it wherever is needed (client side and server context).
|
Few observations that pop in my mind:
|
|
|
nonce propNonceProvider
2138fc3 to
cba40ce
Compare
|
As an API for passing nonces, I think this looks good. (Though my suggestion for an implementation would be to just create an I was wondering, would it make sense to create a single provider for multiple global settings. e.g. |
Since we have
|
|
|
||
| 1. Relax your CSP by adding `'unsafe-inline'` to the `style-src-attr` directive (or using only `style-src-elem` instead of `style-src`). Style attributes specifically pose a less severe security risk than style elements, but this approach may not be acceptable in high-security environments. | ||
| 2. Render the affected components only on the client, so that no inline styles are present in the initial HTML. | ||
| 3. Manually unset inline styles and specify them in your CSS instead. Any component can have its inline styles unset, such as `<ScrollArea.Viewport style={{ overflow: undefined }}>`. Note that you'll need to ensure you vet upgrades for any new inline styles added by Base UI components. |
There was a problem hiding this comment.
Per @Janpot's suggestion. The Provider could be extended (or named differently).
disableScriptElements isn't necessary because it's not a default and must be turned on manually.
<CspProvider nonce="..." disableStyleElements disableInlineStyles>
Disabling inline styles (that are present in the initial HTML) is probably going to take a bunch of work but is theoretically possible to solve style-src-attr
753c3cc to
d5d4c4f
Compare
I'd prefer not introducing it now and instead rely on multiple more granular providers. We can always introduce a helper wrapper that uses the NonceProvider, but let's not do this until there's a clear need for it. |
f904500 to
d6db06a
Compare
b188c52 to
4db148b
Compare
Greptile SummaryImplemented
This resolves the strict CSP incompatibility issues reported in #3550 and #2619, where inline styles caused violations. The implementation leverages React 19's style hoisting feature, ensuring inline style elements are automatically moved to the document head. Confidence Score: 5/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant App
participant CSPProvider
participant Component as Component<br/>(ScrollArea/Select/Slider/Tabs)
participant CSPContext
participant StyleElement as Style/Script Element
App->>CSPProvider: Wrap with nonce/disableStyleElements
CSPProvider->>CSPContext: Provide {nonce, disableStyleElements}
Component->>CSPContext: useCSPContext()
CSPContext-->>Component: {nonce, disableStyleElements}
alt disableStyleElements is false
Component->>StyleElement: Render with nonce attribute
StyleElement-->>Component: Element rendered with CSP compliance
else disableStyleElements is true
Component->>Component: Skip inline style/script rendering
end
Note over Component,StyleElement: React 19 hoists style elements<br/>to document head automatically
|
Greptile's behavior is changing!From now on, if a review finishes with no comments, we will not post an additional "statistics" comment to confirm that our review found nothing to comment on. However, you can confirm that we reviewed your changes in the status check section. This feature can be toggled off in your Code Review Settings by deselecting "Create a status check for each PR". |
michaldudak
left a comment
There was a problem hiding this comment.
Looks good to me overall. I tested it in a Next.js app with SSR, and it behaves as expected.
b6e48a3 to
f206b49
Compare
This allows adding a
nonceprop to every component to allow inline<script>/<style>elements with CSP usingCSPProvider: https://deploy-preview-3553--base-ui.netlify.app/react/utils/csp-provider.Affected components:
<ScrollArea.Root><Select.Popup><Tabs.Indicator><Slider.Thumb>Fixes #3550
Fixes #2619 (via
disableStyleElements)I verified with a custom app (client-only though).
From my findings:
style={...}don't triggerstyle-src-attr 'none'in a client-rendered app. The app needs to be server pre-rendered (style="..."present in the HTML while parsing) to trigger a violation. So popup components don't violate it, but other ones like Slider will.noncecorrectly fixes the issue. Since the<style>tag is hoisted, the first component to be created with anoncemeans other ones don't need it, though in practice all of them should have thenoncestill<script>violations apply only to SSR appsAlternatives
For inline
<style>tags to remove scrollbars, there could be an option to avoid rendering them entirely so the user has to specify in the CSS manually. The only reason<style>needs to exist is for scrollbar-width which is not yet Baseline Widely Available under our browser support guide as::-webkit-scrollbar{display:none}must exist for older browsers.