Draft
Conversation
a5b7e3c to
69790df
Compare
| }) | ||
|
|
||
| // Generate session ID once per provider instance | ||
| const [sessionId] = useState(() => generateSessionId()) |
Check failure
Code scanning / CodeQL
Insecure randomness High
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 7 months ago
To resolve this issue, the generateSessionId function must use a cryptographically secure random number generator instead of Math.random(). For browsers, this means using window.crypto.getRandomValues, and for Node.js, using the crypto module's randomBytes. The fix involves updating generateSessionId (in packages/blend/lib/telemetry/utils.ts) to generate its random part securely:
- Add an environment check (
window.cryptoin browsers,require('crypto')in Node). - Generate random values using these APIs and convert them to a base36 substring similar to the current implementation.
- Remove use of
Math.random().
No changes are needed in TelemetryContext.tsx since it imports and uses generateSessionId only.
Suggested changeset
1
packages/blend/lib/telemetry/utils.ts
Outside changed files
| @@ -20,9 +20,23 @@ | ||
| * @returns Unique session identifier | ||
| */ | ||
| export function generateSessionId(): string { | ||
| const timestamp = Date.now() | ||
| const randomPart = Math.random().toString(36).substring(2, 11) | ||
| return `${SESSION_ID_PREFIX}_${timestamp}_${randomPart}` | ||
| const timestamp = Date.now(); | ||
| let randomPart: string; | ||
|
|
||
| if (typeof window !== "undefined" && window.crypto && window.crypto.getRandomValues) { | ||
| // Browser: Use crypto.getRandomValues | ||
| const array = new Uint32Array(2); | ||
| window.crypto.getRandomValues(array); | ||
| // Convert random numbers to base36 and concat | ||
| randomPart = Array.from(array).map(n => n.toString(36)).join('').substring(0, 9); | ||
| } else { | ||
| // Node.js or other: Use crypto.randomBytes | ||
| // Dynamically require crypto (avoid static import) | ||
| const crypto = require('crypto'); | ||
| randomPart = crypto.randomBytes(7).toString('base64').replace(/[^a-z0-9]/gi, '').substring(0, 9); | ||
| } | ||
|
|
||
| return `${SESSION_ID_PREFIX}_${timestamp}_${randomPart}`; | ||
| } | ||
|
|
||
| /** |
Copilot is powered by AI and may make mistakes. Always verify output.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Added a telemetry configuration in form of hooks in each components and configured the backed for these telemetry to be stored and render in blend-monitor dashboard