Skip to content

Conversation

@YossiSaadi
Copy link
Contributor

No description provided.

// Add enhanced user identification with session tracking
if (config.enableSessionTracking) {
options.identify = async (request: any, extra?: any) => {
const sessionId = extra?.sessionId || 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 2 months ago

To fix the problem, you should replace the use of Math.random() in generateSessionId() with a cryptographically secure random generator. In Node.js, the recommended method is to use the crypto module's randomBytes() function. The crypto.randomBytes() function generates cryptographically secure random bytes, which can then be used to build unpredictable, secure session IDs.

Specifically, you should:

  • Import the Node.js crypto module.
  • Replace the Math.random().toString(36).substr(2, 9) portion with a base64- or hex-encoded value generated from crypto.randomBytes(), of suitable length (for more than 9 random characters, 12 random bytes encoded in base64 or hex is a good choice).
  • Retain the other session ID format (sess_${Date.now()}_...) if desired, but ensure that the random component is now secure.

Edit only within the definition of generateSessionId() and add the required import at the top of the file.


Suggested changeset 1
packages/mcp/src/mcpcat-config.ts

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/mcp/src/mcpcat-config.ts b/packages/mcp/src/mcpcat-config.ts
--- a/packages/mcp/src/mcpcat-config.ts
+++ b/packages/mcp/src/mcpcat-config.ts
@@ -1,5 +1,6 @@
 import { track } from "mcpcat";
 import { server } from "./server/index.js";
+import { randomBytes } from "crypto";
 
 export interface MCPcatConfig {
   projectId: string | null;
@@ -444,7 +445,9 @@
 
 // Helper function to generate session IDs
 function generateSessionId(): string {
-  return `sess_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
+  // Use cryptographically secure random bytes instead of Math.random
+  const randomPart = randomBytes(12).toString("base64url");
+  return `sess_${Date.now()}_${randomPart}`;
 }
 
 /**
EOF
@@ -1,5 +1,6 @@
import { track } from "mcpcat";
import { server } from "./server/index.js";
import { randomBytes } from "crypto";

export interface MCPcatConfig {
projectId: string | null;
@@ -444,7 +445,9 @@

// Helper function to generate session IDs
function generateSessionId(): string {
return `sess_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
// Use cryptographically secure random bytes instead of Math.random
const randomPart = randomBytes(12).toString("base64url");
return `sess_${Date.now()}_${randomPart}`;
}

/**
Copilot is powered by AI and may make mistakes. Always verify output.
inputSchema: ComponentNameParamsSchema.shape,
execute: async (input: z.infer<typeof ComponentNameParamsSchema>): Promise<any> => {
const { componentName } = input;
const sessionId = 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 2 months ago

To fix this problem, we need to generate session IDs using a cryptographically secure random value, instead of Math.random(). In Node.js, the crypto module provides the correct API for secure random bytes. The best approach is to patch the generateSessionId function in packages/mcp/src/mcpcat-config.ts to replace the non-secure .${Math.random().toString(36).substr(2, 9)} with a securely generated random string, e.g., using crypto.randomBytes(9).toString('base64url'), ensuring URL-safe encoding (or hex). The function should be updated to import crypto if not already present.
No other regions need change, as all sessionId consumers will now benefit from securely generated IDs.


Suggested changeset 1
packages/mcp/src/mcpcat-config.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/mcp/src/mcpcat-config.ts b/packages/mcp/src/mcpcat-config.ts
--- a/packages/mcp/src/mcpcat-config.ts
+++ b/packages/mcp/src/mcpcat-config.ts
@@ -1,5 +1,6 @@
 import { track } from "mcpcat";
 import { server } from "./server/index.js";
+import { randomBytes } from "crypto";
 
 export interface MCPcatConfig {
   projectId: string | null;
@@ -444,7 +445,9 @@
 
 // Helper function to generate session IDs
 function generateSessionId(): string {
-  return `sess_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
+  // Use cryptographically secure random bytes for the random part
+  const secureRandomStr = randomBytes(9).toString('base64url'); // 9 bytes ≈ 12 chars base64url
+  return `sess_${Date.now()}_${secureRandomStr}`;
 }
 
 /**
EOF
@@ -1,5 +1,6 @@
import { track } from "mcpcat";
import { server } from "./server/index.js";
import { randomBytes } from "crypto";

export interface MCPcatConfig {
projectId: string | null;
@@ -444,7 +445,9 @@

// Helper function to generate session IDs
function generateSessionId(): string {
return `sess_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
// Use cryptographically secure random bytes for the random part
const secureRandomStr = randomBytes(9).toString('base64url'); // 9 bytes ≈ 12 chars base64url
return `sess_${Date.now()}_${secureRandomStr}`;
}

/**
Copilot is powered by AI and may make mistakes. Always verify output.
inputSchema: SearchIconsParamsSchema.shape,
execute: async (input: z.infer<typeof SearchIconsParamsSchema>): Promise<any> => {
const { query, category, limit, includeUsageExamples = false } = input;
const sessionId = 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 2 months ago

To fix the problem, we need to make generateSessionId use cryptographically secure randomness. In Node.js projects, this is typically achieved using crypto.randomBytes. We will import Node's built-in crypto module and use crypto.randomBytes to generate a secure random value for the session ID, replacing the use of Math.random(). In concrete terms, in packages/mcp/src/mcpcat-config.ts:

  • Import the crypto module.
  • Update generateSessionId to use both the timestamp and a securely generated random value (e.g., 8 bytes, hex encoded).

All calls to generateSessionId elsewhere (such as in list-vibe-icons.ts) will automatically benefit from the improved randomness, and no changes are needed in their code.

Suggested changeset 1
packages/mcp/src/mcpcat-config.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/mcp/src/mcpcat-config.ts b/packages/mcp/src/mcpcat-config.ts
--- a/packages/mcp/src/mcpcat-config.ts
+++ b/packages/mcp/src/mcpcat-config.ts
@@ -1,5 +1,6 @@
 import { track } from "mcpcat";
 import { server } from "./server/index.js";
+import * as crypto from "crypto";
 
 export interface MCPcatConfig {
   projectId: string | null;
@@ -444,7 +445,9 @@
 
 // Helper function to generate session IDs
 function generateSessionId(): string {
-  return `sess_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
+  // Use cryptographically secure randomness for the session portion
+  const randomHex = crypto.randomBytes(8).toString("hex"); // 16 characters
+  return `sess_${Date.now()}_${randomHex}`;
 }
 
 /**
EOF
@@ -1,5 +1,6 @@
import { track } from "mcpcat";
import { server } from "./server/index.js";
import * as crypto from "crypto";

export interface MCPcatConfig {
projectId: string | null;
@@ -444,7 +445,9 @@

// Helper function to generate session IDs
function generateSessionId(): string {
return `sess_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
// Use cryptographically secure randomness for the session portion
const randomHex = crypto.randomBytes(8).toString("hex"); // 16 characters
return `sess_${Date.now()}_${randomHex}`;
}

/**
Copilot is powered by AI and may make mistakes. Always verify output.
"Get a list of all public @vibe/core & @vibe/core/next components names. Use this tool to get the names of components to use in the get-vibe-component-metadata tool.",
inputSchema: {},
execute: async () => {
const sessionId = 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 2 months ago

To fix the problem, replace the use of Math.random() in generateSessionId() with a cryptographically secure random string generator. In Node.js, the native crypto module provides a secure way to generate random values, e.g., via crypto.randomBytes. We'll import crypto and implement the secure generation within generateSessionId(), replacing the random part with a string generated from cryptographically secure random bytes, base36-encoded for compactness.
Required changes:

  • In packages/mcp/src/mcpcat-config.ts:
    • Import the native crypto module.
    • Change the implementation of generateSessionId() to use crypto.randomBytes instead of Math.random().
    • Review that no other lines in the snippet use insecure randomness.
  • No changes are required in list-vibe-public-components.ts—the function is simply called there.

Suggested changeset 1
packages/mcp/src/mcpcat-config.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/mcp/src/mcpcat-config.ts b/packages/mcp/src/mcpcat-config.ts
--- a/packages/mcp/src/mcpcat-config.ts
+++ b/packages/mcp/src/mcpcat-config.ts
@@ -1,5 +1,6 @@
 import { track } from "mcpcat";
 import { server } from "./server/index.js";
+import crypto from "crypto";
 
 export interface MCPcatConfig {
   projectId: string | null;
@@ -444,7 +445,9 @@
 
 // Helper function to generate session IDs
 function generateSessionId(): string {
-  return `sess_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
+  // Use 9 bytes to match previous entropy, and base36 for compactness
+  const randomPart = crypto.randomBytes(9).toString("base36");
+  return `sess_${Date.now()}_${randomPart}`;
 }
 
 /**
EOF
@@ -1,5 +1,6 @@
import { track } from "mcpcat";
import { server } from "./server/index.js";
import crypto from "crypto";

export interface MCPcatConfig {
projectId: string | null;
@@ -444,7 +445,9 @@

// Helper function to generate session IDs
function generateSessionId(): string {
return `sess_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
// Use 9 bytes to match previous entropy, and base36 for compactness
const randomPart = crypto.randomBytes(9).toString("base36");
return `sess_${Date.now()}_${randomPart}`;
}

/**
Copilot is powered by AI and may make mistakes. Always verify output.
@github-actions
Copy link
Contributor

github-actions bot commented Sep 4, 2025

A new prerelease version of this PR has been published! 🎉
To install this prerelease version, run the following command in your terminal with any one of the packages changed in this PR:

To update @vibe/core:

yarn add @vibe/[email protected]

Or with npm:

npm i @vibe/[email protected]

To update @vibe/mcp:

yarn add @vibe/[email protected]

Or with npm:

npm i @vibe/[email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants