CSP Provider
Sets the Content Security Policy (CSP) values of the inline elements that Vase UI parts render.
Anatomy
Import the component and wrap it around your app:
<script setup lang="ts">
import { CSPProvider } from '@vase-ui/vue/csp-provider';
</script>
<template>
<CSPProvider nonce="...">
<!-- Your app or a group of components -->
</CSPProvider>
</template>Supplying a nonce
A strict CSP blocks an inline <style> or <script> element unless its nonce attribute matches the nonce of the CSP. If you enforce such a CSP, configure your server to:
- Make a random nonce for each request.
- Include the nonce in your CSP header, in
style-src-elemandscript-src. - Give the same nonce to
CSPProviderwhen it renders.
const nonce = crypto.randomUUID();
// Example CSP header
const csp = [
`default-src 'self'`,
`script-src 'self' 'nonce-${nonce}'`,
`style-src-elem 'self' 'nonce-${nonce}'`,
].join('; ');Then give the nonce to the root component as a prop, for example with createSSRApp(App, { nonce }):
<script setup lang="ts">
import { CSPProvider } from '@vase-ui/vue/csp-provider';
defineProps<{ nonce: string }>();
</script>
<template>
<CSPProvider :nonce="nonce">
<!-- ... -->
</CSPProvider>
</template>These parts render an inline element that gets the nonce:
ScrollArea.Rootrenders a<style>element that hides the native scrollbar ofScrollArea.Viewport.Select.PopupandSelect.Listuse the same<style>element while the popup aligns its item with the trigger. They also use it while the popup has a scroll arrow and the user does not use touch.Tabs.IndicatorwithrenderBeforeHydrationrenders a<script>element in the server HTML.
Disable inline style elements
You need no nonce for <style> elements if you disable them and use only external style sheets. Set disableStyleElements:
<script setup lang="ts">
import { CSPProvider } from '@vase-ui/vue/csp-provider';
</script>
<template>
<CSPProvider disable-style-elements>
<!-- ... -->
</CSPProvider>
</template>Then put the rules of the <style> element in your own style sheet:
.vase-ui-disable-scrollbar {
scrollbar-width: none;
}
.vase-ui-disable-scrollbar::-webkit-scrollbar {
display: none;
}disableStyleElements has no effect on <script> elements, and CSPProvider has no prop to disable them. A part that renders an inline script needs a nonce.
Inline style attributes
CSPProvider covers inline <style> and <script> elements. It does not cover inline style attributes, such as <div style="...">. The style-src-attr directive of CSP controls the inline style attributes in the HTML that the browser parses from a server render. It has no effect on the styles that client-side JavaScript sets.
In CSP, style-src applies to both <style> elements and style="" attributes. To control only <style> elements, use style-src-elem.
If your CSP blocks inline style attributes in addition to elements, use one of these options:
- Add
'unsafe-inline'to thestyle-src-attrdirective, or usestyle-src-elemand notstyle-src. A style attribute is a smaller security risk than a style element, but a high-security environment can refuse this option. - Render the affected components only on the client. The first HTML then has no inline styles.
- Remove an inline style of a part in its
styleattribute, and put the style in your CSS. An example is<ScrollArea.Viewport :style="{ overflow: undefined }">. Check each new version of Vase UI for new inline styles.
API reference
CSPProvider
Gives a nonce to the inline <style> and <script> elements of the Vase UI parts below it, and
can remove the <style> elements. The nearest provider gives both values, and takes none from a
provider around it.
Doesn't render its own HTML element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
disableStyleElements | boolean | undefined | false | The parts render no inline <style> element. Put the rules of each such element in your own
style sheet. |
nonce | string | undefined | The nonce attribute of the inline <style> and <script> elements of the parts. |
Slots
| Slot | Slot props | Description |
|---|---|---|
default | The content of the part. |
Differences from Base UI
Vase UI ports CSP Provider from Base UI 1.9.0.
CSP Provider has no difference of its own.
The handbook gives the differences that all components share: Styling, Animation, Composition, Customization, Forms and TypeScript.