Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

Potential fix for code scanning alert no. 54: Insecure randomness

Diego Rodrigues de Sa e Souzae78ede45b630
diff --git a/open-sse/services/accountSelector.ts b/open-sse/services/accountSelector.ts
index a4eaef4e92..5d028e26e6 100644
--- a/open-sse/services/accountSelector.ts
+++ b/open-sse/services/accountSelector.ts
@@ -6,6 +6,7 @@
*/
import { getAccountHealth } from "./accountFallback.ts";
+import crypto from "crypto";
/**
* P2C selection: pick 2 random candidates, return the healthier one.
@@ -19,9 +20,9 @@ export function selectAccountP2C(accounts, model = null) {
if (!accounts || accounts.length === 0) return null;
if (accounts.length === 1) return accounts[0];
- // Pick 2 random distinct indices
- const i = Math.floor(Math.random() * accounts.length);
- let j = Math.floor(Math.random() * (accounts.length - 1));
+ // Pick 2 random distinct indices (cryptographically secure)
+ const i = crypto.randomInt(accounts.length);
+ let j = crypto.randomInt(accounts.length - 1);
if (j >= i) j++; // Ensure distinct
const a = accounts[i];
@@ -59,7 +60,7 @@ export function selectAccount(
case "random":
return {
- account: accounts[Math.floor(Math.random() * accounts.length)],
+ account: accounts[crypto.randomInt(accounts.length)],
state,
};