Skip to content

fix(security): use crypto.randomBytes for secure ID generation - #177

Merged
will-lamerton merged 1 commit into
Nano-Collective:mainfrom
abhisek-1221:issue163
Dec 21, 2025
Merged

will-lamerton merged 1 commit into
Nano-Collective:mainfrom
abhisek-1221:issue163

Conversation

@abhisek-1221

Copy link
Copy Markdown
Contributor

Description

Fix #163
Replace insecure Math.random() usage with cryptographically secure crypto.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

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update

Testing

Automated Tests

  • New features include passing tests in .spec.ts/tsx files
  • All existing tests pass (pnpm test:all completes successfully)
  • Tests cover both success and error scenarios
Screenshot 2025-12-21 at 12 38 03 AM Screenshot 2025-12-21 at 12 40 10 AM

Manual Testing

  • Tested with Ollama
  • Tested with OpenRouter
  • Tested with OpenAI-compatible API
  • Tested MCP integration (if applicable)

Checklist

  • Code follows project style guidelines
  • Self-review completed
  • Documentation updated (if needed)
  • No breaking changes (or clearly documented)
  • Appropriate logging added using structured logging (see CONTRIBUTING.md)

Notes

ID Format Change:

  • Before: ${Date.now()}-${Math.random().toString(36).substring(7)} (base36, ~5-6 chars)
  • After: ${Date.now()}-${randomBytes(8).toString('hex')} (hex, 16 chars)

Tool ID fallbacks are generated only when providers do not supply IDs themselves.
This change does not affect or break any external API contracts.

Copilot AI review requested due to automatic review settings December 20, 2025 19:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) with randomBytes(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.

@abhisek-1221

Copy link
Copy Markdown
Contributor Author

The remaining Math.random() usages are confined to test files (.spec.ts) where they are used for:

  • Creating unique test directory names (not security-sensitive)
  • Introducing random delay timing in tests
  • Generating unique test query strings

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

@will-lamerton
will-lamerton merged commit 5194871 into Nano-Collective:main Dec 21, 2025
JimStenstrom added a commit to JimStenstrom/nanocoder that referenced this pull request Dec 23, 2025
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replace insecure Math.random() with crypto

4 participants