fix(query-devtools): set window.__nonce__ for goober CSP support#10893
fix(query-devtools): set window.__nonce__ for goober CSP support#10893DevLikhith5 wants to merge 1 commit into
Conversation
goober 2.1.17+ reads window.__nonce__ to set the nonce on every style
element access. The setupStyleSheet function set the nonce via
setAttribute('nonce', value) on the pre-created <style id='_goober'>
element, but goober would immediately overwrite it with undefined since
window.__nonce__ was never set, breaking the styleNonce prop.
Fixes TanStack#10820
📝 WalkthroughWalkthroughThe PR fixes a CSP nonce bug in query devtools by setting ChangesStyle Nonce Propagation Fix
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/query-devtools/src/utils.tsx (1)
310-313: ⚡ Quick winVerified: Core fix correctly addresses the goober CSP issue.
The implementation properly sets
window.__nonce__before goober's style element access, preventing goober from overwriting withundefined. The detailed comment accurately explains the goober 2.1.17+ behavior.♻️ Optional: Improve type safety with Window interface augmentation
Instead of
(window as any), consider declaring the global type:// At the top of the file or in a types file declare global { interface Window { __nonce__?: string } }Then use:
- ;(window as any).__nonce__ = nonce + window.__nonce__ = nonceThis provides type safety while documenting the global contract.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/query-devtools/src/utils.tsx` around lines 310 - 313, Add a Window interface augmentation to provide a typed __nonce__ property and then use the typed global rather than casting to any: declare a global augmentation (e.g., declare global { interface Window { __nonce__?: string } }) in this module or a shared types file, and replace the casted assignment `(window as any).__nonce__ = nonce` with the typed assignment `window.__nonce__ = nonce` (referencing the existing nonce variable and window.__nonce__ usage).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@packages/query-devtools/src/utils.tsx`:
- Around line 310-313: Add a Window interface augmentation to provide a typed
__nonce__ property and then use the typed global rather than casting to any:
declare a global augmentation (e.g., declare global { interface Window {
__nonce__?: string } }) in this module or a shared types file, and replace the
casted assignment `(window as any).__nonce__ = nonce` with the typed assignment
`window.__nonce__ = nonce` (referencing the existing nonce variable and
window.__nonce__ usage).
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: e8965d00-3cda-487b-b31e-0787538ba179
📒 Files selected for processing (1)
packages/query-devtools/src/utils.tsx
Issue
The
styleNonceprop on<ReactQueryDevtools>no longer works under a strict CSPstyle-srcpolicy. Passing a nonce results in CSP violations because the injected<style id="_goober">element ends up with an empty nonce.Fixes #10820
Root cause
goober 2.1.17+ introduced automatic nonce handling: on every access to the shared
<style id="_goober">element, it setsel.nonce = window.__nonce__. ThesetupStyleSheetfunction set the nonce viasetAttribute("nonce", value)on the pre-created element, but goober would immediately overwrite it withundefinedsincewindow.__nonce__was never set.Fix
Set
window.__nonce__insidesetupStyleSheetbefore any goober code runs, so goober reads the correct nonce instead ofundefined.Manual testing
@tanstack/react-query/@tanstack/react-query-devtoolsat a version that includes goober 2.1.18+Content-Security-Policyheader withstyle-src nonce-<your-nonce><ReactQueryDevtools styleNonce="<your-nonce>" />#_gooberstyle elementSummary by CodeRabbit