Skip to content

Commit b7a07c2

Browse files
authored
feat: add nonce for devtools style tag (#3506)
1 parent 0c66727 commit b7a07c2

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/devtools/devtools.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ interface DevtoolsOptions {
5858
* Defaults to 'aside'.
5959
*/
6060
containerElement?: string | any
61+
/**
62+
* nonce for style element for CSP
63+
*/
64+
styleNonce?: string
6165
}
6266

6367
interface DevtoolsPanelOptions {
@@ -73,6 +77,10 @@ interface DevtoolsPanelOptions {
7377
* A boolean variable indicating whether the panel is open or closed
7478
*/
7579
isOpen?: boolean
80+
/**
81+
* nonce for style element for CSP
82+
*/
83+
styleNonce?: string
7684
/**
7785
* A function that toggles the open and close state of the panel
7886
*/
@@ -92,6 +100,7 @@ export function ReactQueryDevtools({
92100
toggleButtonProps = {},
93101
position = 'bottom-left',
94102
containerElement: Container = 'aside',
103+
styleNonce,
95104
}: DevtoolsOptions): React.ReactElement | null {
96105
const rootRef = React.useRef<HTMLDivElement>(null)
97106
const panelRef = React.useRef<HTMLDivElement>(null)
@@ -229,6 +238,7 @@ export function ReactQueryDevtools({
229238
<ThemeProvider theme={theme}>
230239
<ReactQueryDevtoolsPanel
231240
ref={panelRef as any}
241+
styleNonce={styleNonce}
232242
{...otherPanelProps}
233243
style={{
234244
position: 'fixed',
@@ -375,7 +385,13 @@ export const ReactQueryDevtoolsPanel = React.forwardRef<
375385
HTMLDivElement,
376386
DevtoolsPanelOptions
377387
>(function ReactQueryDevtoolsPanel(props, ref): React.ReactElement {
378-
const { isOpen = true, setIsOpen, handleDragStart, ...panelProps } = props
388+
const {
389+
isOpen = true,
390+
styleNonce,
391+
setIsOpen,
392+
handleDragStart,
393+
...panelProps
394+
} = props
379395

380396
const queryClient = useQueryClient()
381397
const queryCache = queryClient.getQueryCache()
@@ -467,6 +483,7 @@ export const ReactQueryDevtoolsPanel = React.forwardRef<
467483
{...panelProps}
468484
>
469485
<style
486+
nonce={styleNonce}
470487
dangerouslySetInnerHTML={{
471488
__html: `
472489
.ReactQueryDevtoolsPanel * {

src/devtools/tests/devtools.test.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,4 +391,21 @@ describe('ReactQueryDevtools', () => {
391391
expect(queries[1]?.textContent).toEqual(query2Hash)
392392
expect(queries[2]?.textContent).toEqual(query3Hash)
393393
})
394+
395+
it('style should have a nonce', async () => {
396+
const { queryClient } = createQueryClient()
397+
398+
function Page() {
399+
return <div></div>
400+
}
401+
402+
const { container } = renderWithClient(queryClient, <Page />, {
403+
styleNonce: 'test-nonce',
404+
initialIsOpen: false,
405+
})
406+
const styleTag = container.querySelector('style')
407+
expect(styleTag).toHaveAttribute('nonce', 'test-nonce')
408+
409+
await screen.findByRole('button', { name: /react query devtools/i })
410+
})
394411
})

0 commit comments

Comments
 (0)