Skip to content

Commit cc70685

Browse files
author
kai-agent-free
committed
fix: reject request IDs exceeding Number.MAX_SAFE_INTEGER
Adds a validation refinement to RequestIdSchema to reject numeric request IDs outside the safe integer range. Previously, a single request with an ID > MAX_SAFE_INTEGER (e.g. 9007199254740992) would cause the server to hang indefinitely with no error response, as JSON.parse silently loses precision on large integers. The fix validates that numeric IDs fall within Number.MIN_SAFE_INTEGER to Number.MAX_SAFE_INTEGER, causing the Zod parse to fail and return a proper JSON-RPC error response instead of silently hanging. Fixes #1765
1 parent e86b183 commit cc70685

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

packages/core/src/types/schemas.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,15 @@ export const ResultSchema = z.looseObject({
119119
/**
120120
* A uniquely identifying ID for a request in JSON-RPC.
121121
*/
122-
export const RequestIdSchema = z.union([z.string(), z.number().int()]);
122+
export const RequestIdSchema = z.union([
123+
z.string(),
124+
z
125+
.number()
126+
.int()
127+
.refine(n => n >= Number.MIN_SAFE_INTEGER && n <= Number.MAX_SAFE_INTEGER, {
128+
message: 'Request ID must be within Number.MAX_SAFE_INTEGER range'
129+
})
130+
]);
123131

124132
/**
125133
* A request that expects a response.

packages/core/test/types.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
RequestIdSchema,
23
CallToolResultSchema,
34
ClientCapabilitiesSchema,
45
CompleteRequestSchema,
@@ -984,3 +985,23 @@ describe('Types', () => {
984985
});
985986
});
986987
});
988+
989+
describe('RequestIdSchema', () => {
990+
test('should accept string IDs', () => {
991+
expect(RequestIdSchema.parse('abc-123')).toBe('abc-123');
992+
});
993+
994+
test('should accept safe integer IDs', () => {
995+
expect(RequestIdSchema.parse(1)).toBe(1);
996+
expect(RequestIdSchema.parse(Number.MAX_SAFE_INTEGER)).toBe(Number.MAX_SAFE_INTEGER);
997+
});
998+
999+
test('should reject IDs exceeding MAX_SAFE_INTEGER', () => {
1000+
expect(() => RequestIdSchema.parse(Number.MAX_SAFE_INTEGER + 1)).toThrow();
1001+
expect(() => RequestIdSchema.parse(9007199254740992)).toThrow();
1002+
});
1003+
1004+
test('should reject non-integer numeric IDs', () => {
1005+
expect(() => RequestIdSchema.parse(1.5)).toThrow();
1006+
});
1007+
});

0 commit comments

Comments
 (0)