Skip to content

Telemetry config#290

Draft
dkrai04 wants to merge 6 commits intodevfrom
telemetryConfig
Draft

Telemetry config#290
dkrai04 wants to merge 6 commits intodevfrom
telemetryConfig

Conversation

@dkrai04
Copy link
Collaborator

@dkrai04 dkrai04 commented Aug 22, 2025

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

})

// Generate session ID once per provider instance
const [sessionId] = useState(() => generateSessionId())

Check failure

Code scanning / CodeQL

Insecure randomness High

This uses a cryptographically insecure random number generated at
Math.random()
in a security context.

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.crypto in 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

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/packages/blend/lib/telemetry/utils.ts b/packages/blend/lib/telemetry/utils.ts
--- a/packages/blend/lib/telemetry/utils.ts
+++ b/packages/blend/lib/telemetry/utils.ts
@@ -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}`;
 }
 
 /**
EOF
@@ -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.
@dkrai04 dkrai04 marked this pull request as draft August 29, 2025 03:55
@dkrai04 dkrai04 added the Reference PR This PR need not to be merged this is just a reference PR label Feb 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Reference PR This PR need not to be merged this is just a reference PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant