Skip to content

[csp provider] Add CSPProvider - #3553

Merged
atomiks merged 9 commits into
mui:masterfrom
atomiks:fix/select-scroll-area-nonce
Jan 14, 2026
Merged

atomiks merged 9 commits into
mui:masterfrom
atomiks:fix/select-scroll-area-nonce

Conversation

@atomiks

@atomiks atomiks commented Dec 15, 2025 •

Copy link
Copy Markdown
Contributor

This allows adding a nonce prop to every component to allow inline <script>/<style> elements with CSP using CSPProvider: 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:

  • React inline styles style={...} don't trigger style-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.
  • Passing nonce correctly fixes the issue. Since the <style> tag is hoisted, the first component to be created with a nonce means other ones don't need it, though in practice all of them should have the nonce still
  • <script> violations apply only to SSR apps

Alternatives

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.

@atomiks atomiks added component: select Changes related to the select component. component: tabs Changes related to the tabs component. component: slider Changes related to the slider component. type: new feature Expand the scope of the product to solve a new problem. component: scroll area Changes related to the scroll area component. labels Dec 15, 2025
@pkg-pr-new

pkg-pr-new Bot commented Dec 15, 2025 •

Copy link
Copy Markdown
  • vite-css-base-ui-example

    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/react@3553
    
    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/utils@3553
    

commit: f206b49

@mui-bot

mui-bot commented Dec 15, 2025 •

Copy link
Copy Markdown

Bundle size report

Bundle Parsed size Gzip size
@base-ui/react 🔺+465B(+0.11%) 🔺+146B(+0.11%)

Details of bundle changes


Check out the code infra dashboard for more information about this PR.

@atomiks
atomiks force-pushed the fix/select-scroll-area-nonce branch from 7812605 to d0e7e65 Compare December 15, 2025 22:47
@netlify

netlify Bot commented Dec 15, 2025

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 7812605
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/69408f886f55f60008be48a3
😎 Deploy Preview https://deploy-preview-3553--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify

netlify Bot commented Dec 15, 2025 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit f206b49
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/696769a298ef140008a584ba
😎 Deploy Preview https://deploy-preview-3553--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@atomiks
atomiks force-pushed the fix/select-scroll-area-nonce branch 3 times, most recently from e9513af to 3224fce Compare December 16, 2025 02:54
@atomiks
atomiks marked this pull request as ready for review December 16, 2025 04:05

@mnajdova mnajdova left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@Janpot

Janpot commented Dec 17, 2025

Copy link
Copy Markdown
Member

Few observations that pop in my mind:

  • It feels a bit weird to me to have this as a prop as I don't see a use case where you want to supply it on a case-by-case basis. It's really just all or nothing-at-all. A nonce context could solve that. It would also have the benefit that you can add style tags to components without it being a breaking change.
  • Looking at this, it feels super inviting to just supply a static string to shut up CSP warnings, completely defeating its purpose.

@atomiks

atomiks commented Dec 17, 2025

Copy link
Copy Markdown
Contributor Author

NonceProvider would indeed be much more ergonomic

@atomiks atomiks changed the title [scroll area][select][tabs][slider] Add nonce prop [nonce] Add NonceProvider Dec 17, 2025
@atomiks
atomiks force-pushed the fix/select-scroll-area-nonce branch 5 times, most recently from 2138fc3 to cba40ce Compare December 18, 2025 00:06
@Janpot

Janpot commented Dec 18, 2025

Copy link
Copy Markdown
Member

As an API for passing nonces, I think this looks good. (Though my suggestion for an implementation would be to just create an InlineStyle component that wraps the style element but is nonce aware.)

I was wondering, would it make sense to create a single provider for multiple global settings. e.g. <BaseUiprovider disableStyleElements > or <BaseUiprovider nonce={nonce} >

@github-actions github-actions Bot added PR: out-of-date The pull request has merge conflicts and can't be merged. and removed PR: out-of-date The pull request has merge conflicts and can't be merged. labels Dec 18, 2025
@atomiks

atomiks commented Dec 19, 2025

Copy link
Copy Markdown
Contributor Author

(Though my suggestion for an implementation would be to just create an InlineStyle component that wraps the style element but is nonce aware.)

Since we have <script> tags as well, it'd need InlineScript too. So imo the useNonce hook seems fine to use everywhere.

I was wondering, would it make sense to create a single provider for multiple global settings. e.g. or

DirectionProvider and Tooltip.Provider already set the precedent for different providers, but this is possible. Although both of those don't necessarily have to be app-level configuration.


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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@atomiks
atomiks force-pushed the fix/select-scroll-area-nonce branch from 753c3cc to d5d4c4f Compare December 19, 2025 01:39
@github-actions github-actions Bot added PR: out-of-date The pull request has merge conflicts and can't be merged. and removed PR: out-of-date The pull request has merge conflicts and can't be merged. labels Dec 21, 2025
@michaldudak

Copy link
Copy Markdown
Member

I was wondering, would it make sense to create a single provider for multiple global settings.

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.

@atomiks
atomiks force-pushed the fix/select-scroll-area-nonce branch from f904500 to d6db06a Compare January 14, 2026 08:13
@atomiks atomiks changed the title [nonce] Add NonceProvider [nonce] Add CSPProvider Jan 14, 2026
@atomiks
atomiks force-pushed the fix/select-scroll-area-nonce branch from b188c52 to 4db148b Compare January 14, 2026 08:45
@atomiks

atomiks commented Jan 14, 2026

Copy link
Copy Markdown
Contributor Author

@greptileai

@atomiks atomiks changed the title [nonce] Add CSPProvider [csp provider] Add CSPProvider Jan 14, 2026
@greptile-apps

greptile-apps Bot commented Jan 14, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

Implemented CSPProvider to enable Content Security Policy compliance across Base UI components. The provider allows passing a nonce attribute to inline <style> and <script> tags, and supports disabling inline style elements entirely via disableStyleElements prop.

  • Added new CSPProvider component with context-based configuration
  • Refactored styleDisableScrollbar from static element to function accepting nonce
  • Integrated CSP context into 4 affected components: ScrollArea.Root, Select.Popup, Slider.Thumb, and Tabs.Indicator
  • Added comprehensive tests covering nonce application and style element disabling
  • Included detailed documentation explaining CSP usage patterns and alternatives

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

  • This PR is safe to merge with minimal risk
  • The implementation is well-architected with proper separation of concerns using React Context, includes comprehensive test coverage, and follows established patterns in the codebase. All changes are backwards compatible (CSP context returns sensible defaults when provider is not used). The refactoring is minimal and focused, touching only the necessary integration points.
  • No files require special attention

Important Files Changed

Filename Overview
packages/react/src/csp-provider/CSPProvider.tsx New CSPProvider component that provides nonce and disableStyleElements context values. Clean implementation using React Context.
packages/react/src/csp-provider/CSPContext.tsx Context definition and hook for CSP configuration. Provides sensible defaults when context is not available.
packages/react/src/utils/styles.tsx Refactored styleDisableScrollbar from static element to getElement function accepting nonce parameter.
packages/react/src/scroll-area/root/ScrollAreaRoot.tsx Integrated CSP context and conditionally renders style element with nonce based on disableStyleElements flag.
packages/react/src/select/popup/SelectPopup.tsx Integrated CSP context and conditionally renders style element with nonce based on disableStyleElements flag.
packages/react/src/slider/thumb/SliderThumb.tsx Integrated CSP nonce for prehydration script rendering. Script only rendered when renderBeforeHydration is enabled.
packages/react/src/tabs/indicator/TabsIndicator.tsx Integrated CSP nonce for prehydration script rendering. Script only rendered when renderBeforeHydration is enabled.

Sequence Diagram

sequenceDiagram
    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
Loading

@greptile-apps

greptile-apps Bot commented Jan 14, 2026

Copy link
Copy Markdown
Contributor

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".

@atomiks atomiks mentioned this pull request Jan 14, 2026
1 task done

@michaldudak michaldudak left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me overall. I tested it in a Next.js app with SSR, and it behaves as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: scroll area Changes related to the scroll area component. component: select Changes related to the select component. component: slider Changes related to the slider component. component: tabs Changes related to the tabs component. type: new feature Expand the scope of the product to solve a new problem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incompatibility with strict CSPs [scroll area] styleDisableScrollbar unexpectedly injected into NodeView rendering (Tiptap + React)

5 participants