Repository navigation
fix(security): use crypto.randomBytes for secure ID generation - #177
Conversation
There was a problem hiding this comment.
Pull request overview
This PR enhances security by replacing insecure Math.random() with cryptographically secure crypto.randomBytes() for ID generation across the codebase. This change addresses a security vulnerability where predictable IDs could potentially be exploited.
- Replaces
Math.random().toString(36).substring(7)withrandomBytes(8).toString('hex')for 64 bits of cryptographic randomness - Updates import statements to use modern
node:prefix for built-in modules (node:crypto,node:os) - Maintains backward compatibility by only affecting fallback ID generation when providers don't supply IDs
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| source/utils/logging/request-tracker.ts | Updates request ID generation to use secure random bytes instead of Math.random() |
| source/utils/logging/performance.ts | Updates performance measurement ID generation with secure randomness; also modernizes os import |
| source/usage/tracker.ts | Replaces insecure session ID generation with cryptographically secure implementation |
| source/ai-sdk-client.ts | Updates tool call ID fallback generation (4 occurrences) to use secure random bytes for providers that don't provide IDs |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
The remaining
I’m assuming these test-only usages are acceptable since they are not related to cryptographic or security concerns and are used solely to avoid conflicts between test runs. @will-lamerton |
Fixes regression introduced in PR Nano-Collective#190 (AISDKClient refactor) where Math.random() was reintroduced after PR Nano-Collective#177 had replaced it with crypto for secure ID generation.
Description
Fix #163
Replace insecure
Math.random()usage with cryptographically securecrypto.randomBytes()for all ID generation across the codebase. This improves security and unpredictability of session IDs, tool IDs, request IDs, and performance measurement IDs while maintaining backward compatibility with external APIs.Type of Change
Testing
Automated Tests
.spec.ts/tsxfilespnpm test:allcompletes successfully)Manual Testing
Checklist
Notes
ID Format Change:
${Date.now()}-${Math.random().toString(36).substring(7)}(base36, ~5-6 chars)${Date.now()}-${randomBytes(8).toString('hex')}(hex, 16 chars)