Skip to content

Chat Client SDK#860

Open
xingsy97 wants to merge 11 commits intomainfrom
s/chat-sdk
Open

Chat Client SDK#860
xingsy97 wants to merge 11 commits intomainfrom
s/chat-sdk

Conversation

@xingsy97
Copy link
Collaborator

No description provided.

@xingsy97 xingsy97 marked this pull request as draft December 12, 2025 04:09
"license": "ISC",
"dependencies": {
"@azure/logger": "^1.3.0",
"@azure/web-pubsub-client": "file:G:\\\\azure-sdk-for-js\\\\sdk\\\\web-pubsub\\\\web-pubsub-client",
Copy link
Collaborator Author

@xingsy97 xingsy97 Dec 30, 2025

Choose a reason for hiding this comment

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

use local package temporarily. Invoke feature haven't been released to public yet.

@xingsy97 xingsy97 force-pushed the s/chat-sdk branch 2 times, most recently from ae8bddc to 509411c Compare December 30, 2025 08:41
@xingsy97 xingsy97 marked this pull request as ready for review December 30, 2025 08:43

export async function createTestClient(userId?: string): Promise<ChatClient> {
if (!userId) {
userId = `uid-${randomInt()}`;

Check failure

Code scanning / CodeQL

Insecure randomness High test

This uses a cryptographically insecure random number generated at
Math.random()
in a security context.

Copilot Autofix

AI 5 days ago

In general, to fix this kind of issue you should replace uses of Math.random() in security-relevant contexts with a cryptographically secure random generator, such as Node’s crypto.randomInt or crypto.randomBytes, or the browser’s crypto.getRandomValues. This avoids predictable sequences that can be guessed or brute‑forced.

For this specific file (sdk/webpubsub-chat-sdk/tests/testUtils.ts), the simplest fix that preserves existing behavior is to change randomInt to use Node’s crypto.randomInt, which directly returns an integer in a specified range with uniform distribution, instead of Math.random(). We can import randomInt from Node’s built‑in crypto module under an alias (e.g. secureRandomInt) to avoid naming conflicts with the existing randomInt function. Then, inside our exported randomInt helper, we call secureRandomInt(0, 10000000) instead of computing Math.floor(Math.random() * 10000000). This preserves the approximate range and type while making the randomness cryptographically secure. No other functions (getUserIds, createTestClient, etc.) need to change, since they already call randomInt().

Concretely:

  • Add an import of randomInt from "crypto" at the top of testUtils.ts, aliased to avoid clashing with the existing function.
  • Update the implementation of export const randomInt to call the imported secure function.
  • Leave all call sites (getUserIds, createTestClient) unchanged.
Suggested changeset 1
sdk/webpubsub-chat-sdk/tests/testUtils.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/sdk/webpubsub-chat-sdk/tests/testUtils.ts b/sdk/webpubsub-chat-sdk/tests/testUtils.ts
--- a/sdk/webpubsub-chat-sdk/tests/testUtils.ts
+++ b/sdk/webpubsub-chat-sdk/tests/testUtils.ts
@@ -1,5 +1,6 @@
 import { WebPubSubClient } from "@azure/web-pubsub-client";
 import { ChatClient } from "../src/chatClient.js";
+import { randomInt as secureRandomInt } from "crypto";
 
 // Test configuration
 export const negotiateUrl = "http://localhost:3000/negotiate";
@@ -7,7 +8,7 @@
 export const LONG_TEST_TIMEOUT = 10 * 1000;
 
 // Helper functions
-export const randomInt = () => Math.floor(Math.random() * 10000000);
+export const randomInt = () => secureRandomInt(0, 10000000);
 
 export const getUserIds = (count: number): string[] => {
   const userIds: string[] = [];
EOF
@@ -1,5 +1,6 @@
import { WebPubSubClient } from "@azure/web-pubsub-client";
import { ChatClient } from "../src/chatClient.js";
import { randomInt as secureRandomInt } from "crypto";

// Test configuration
export const negotiateUrl = "http://localhost:3000/negotiate";
@@ -7,7 +8,7 @@
export const LONG_TEST_TIMEOUT = 10 * 1000;

// Helper functions
export const randomInt = () => Math.floor(Math.random() * 10000000);
export const randomInt = () => secureRandomInt(0, 10000000);

export const getUserIds = (count: number): string[] => {
const userIds: string[] = [];
Copilot is powered by AI and may make mistakes. Always verify output.
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.

1 participant