Skip to content

Comments

Staging#688

Merged
jakeaturner merged 27 commits intomasterfrom
staging
Feb 17, 2026
Merged

Staging#688
jakeaturner merged 27 commits intomasterfrom
staging

Conversation

@jakeaturner
Copy link
Collaborator

No description provided.

* Create a new session
*/
async createSession(userId?: string): Promise<string> {
const sessionId = `session_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;

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 6 days ago

In general, to fix insecure randomness you should replace Math.random() (or similarly weak PRNGs) wherever they are used to generate security-sensitive values (session IDs, tokens, passwords, etc.) with a cryptographically secure random number generator. In Node.js, the standard solution is crypto.randomBytes() from the built-in crypto module, which uses a CSPRNG. Convert those bytes to a URL-safe or hex string of sufficient length and use that as the random component of your identifier.

For this specific case, we should change createSession so that it no longer uses Math.random() and instead uses crypto.randomBytes to generate the random segment. We will keep the overall behavior and string format (a session_ prefix and timestamp) but change only the random suffix generation. That means:

  • Add an import of Node’s crypto module at the top of server/api/services/agent.ts.
  • In createSession, replace Math.random().toString(36).substr(2, 9) with something like crypto.randomBytes(16).toString('hex'), which yields 32 hex characters (128 bits of randomness).
  • Keep the rest of the method intact so that how sessionId is stored and used remains unchanged.

Concretely:

  • Modify server/api/services/agent.ts:
    • Add import crypto from 'crypto'; (or import * as crypto from 'crypto';) alongside existing imports.
    • Update line 51 to build sessionId using crypto.randomBytes(...) instead of Math.random().
Suggested changeset 1
server/api/services/agent.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/server/api/services/agent.ts b/server/api/services/agent.ts
--- a/server/api/services/agent.ts
+++ b/server/api/services/agent.ts
@@ -14,6 +14,7 @@
   buildSystemPrompt,
 } from './prompts.js';
 import axios from 'axios';
+import crypto from 'crypto';
 
 export interface AgentSource {
   number: number;
@@ -48,7 +49,8 @@
    * Create a new session
    */
   async createSession(userId?: string): Promise<string> {
-    const sessionId = `session_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
+    const randomSuffix = crypto.randomBytes(16).toString('hex');
+    const sessionId = `session_${Date.now()}_${randomSuffix}`;
 
     await AgentHistory.create({
       sessionId,
EOF
@@ -14,6 +14,7 @@
buildSystemPrompt,
} from './prompts.js';
import axios from 'axios';
import crypto from 'crypto';

export interface AgentSource {
number: number;
@@ -48,7 +49,8 @@
* Create a new session
*/
async createSession(userId?: string): Promise<string> {
const sessionId = `session_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
const randomSuffix = crypto.randomBytes(16).toString('hex');
const sessionId = `session_${Date.now()}_${randomSuffix}`;

await AgentHistory.create({
sessionId,
Copilot is powered by AI and may make mistakes. Always verify output.
AkhilTheBoss and others added 26 commits February 16, 2026 22:08
Co-authored-by: Jake Turner <52841588+jakeaturner@users.noreply.github.com>
@jakeaturner jakeaturner merged commit c7a84aa into master Feb 17, 2026
6 of 7 checks passed
@libretexts-bot
Copy link
Collaborator

🎉 This PR is included in version 2.116.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

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.

3 participants