fix(security): use CSPRNG for CSP nonce generation#5966
Conversation
The CSP nonce was generated with `nanoid/non-secure`, which derives every character from `Math.random()`. A predictable nonce weakens nonce-based CSP as a defense against XSS. Switch to the crypto-backed `nanoid` entry point so the nonce stays unpredictable.
There was a problem hiding this comment.
Code Review
This pull request replaces the non-secure nanoid/non-secure import with the cryptographically secure nanoid in the security plugin's context. It also adds a test case to ensure that the generated CSP nonce is unpredictable even when Math.random is mocked. There are no review comments, and I have no feedback to provide.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ 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 |
Deploying egg-v3 with
|
| Latest commit: |
10d759f
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://e5e149dd.egg-v3.pages.dev |
| Branch Preview URL: | https://fix-security-csp-nonce-csprn.egg-v3.pages.dev |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## next #5966 +/- ##
==========================================
+ Coverage 85.32% 85.80% +0.48%
==========================================
Files 670 658 -12
Lines 19553 19266 -287
Branches 3864 3795 -69
==========================================
- Hits 16683 16531 -152
+ Misses 2479 2365 -114
+ Partials 391 370 -21 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Problem
The CSP nonce (
ctx.nonce) was generated withnanoid/non-secure, which derives every character fromMath.random().Math.random()is not cryptographically secure and its internal state can be recovered from observed outputs, making the nonce predictable. A predictable nonce weakens nonce-based CSP as a defense against XSS.Fix
Import
nanoidfrom its crypto-backed entry point instead ofnanoid/non-secure, so the nonce stays unpredictable. No API or output-shape change (still a 16-char URL-safe string).Test
Added a regression test that pins
Math.randomto a constant and asserts the nonce is still unpredictable across requests. It fails on the oldnon-secureimport (nonce collapses to a constant value) and passes with the crypto-backed RNG.plugins/securitysuite: 199 passed, 4 skipped.