mcp-input-limits.test.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * MCP tool input-size limits
  3. *
  4. * Regression coverage for the DoS vector: MCP clients can ship
  5. * unbounded payloads (`query`, `task`, `symbol`, `projectPath`,
  6. * `path`, `pattern`). Before the cap, a 100MB string would hit
  7. * the FTS5 layer and pin the server. These tests assert that the
  8. * tool layer rejects oversize inputs early.
  9. */
  10. import { describe, it, expect, beforeEach, afterEach } from 'vitest';
  11. import * as fs from 'fs';
  12. import * as path from 'path';
  13. import * as os from 'os';
  14. import CodeGraph from '../../src/index';
  15. import { ToolHandler } from '../../src/mcp/tools';
  16. describe('MCP input size limits', () => {
  17. let tempDir: string;
  18. let cg: CodeGraph;
  19. let handler: ToolHandler;
  20. beforeEach(async () => {
  21. tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-mcp-limits-'));
  22. fs.mkdirSync(path.join(tempDir, 'src'), { recursive: true });
  23. fs.writeFileSync(
  24. path.join(tempDir, 'src', 'a.ts'),
  25. `export function alpha(): number { return 1; }\n`
  26. );
  27. cg = await CodeGraph.init(tempDir, {
  28. config: { include: ['**/*.ts'], exclude: [] },
  29. });
  30. await cg.indexAll();
  31. handler = new ToolHandler(cg);
  32. });
  33. afterEach(() => {
  34. if (cg) cg.destroy();
  35. if (fs.existsSync(tempDir)) {
  36. fs.rmSync(tempDir, { recursive: true, force: true });
  37. }
  38. });
  39. it('accepts a normal-sized query', async () => {
  40. const result = await handler.execute('codegraph_search', { query: 'alpha' });
  41. expect(result.isError).toBeFalsy();
  42. });
  43. it('rejects an oversize query on codegraph_search', async () => {
  44. const huge = 'a'.repeat(20_000);
  45. const result = await handler.execute('codegraph_search', { query: huge });
  46. expect(result.isError).toBe(true);
  47. expect(result.content[0]!.text).toMatch(/maximum length/i);
  48. });
  49. it('rejects an oversize task on codegraph_context', async () => {
  50. const huge = 'b'.repeat(50_000);
  51. const result = await handler.execute('codegraph_context', { task: huge });
  52. expect(result.isError).toBe(true);
  53. expect(result.content[0]!.text).toMatch(/maximum length/i);
  54. });
  55. it('rejects an oversize symbol on codegraph_callers', async () => {
  56. const huge = 'c'.repeat(15_000);
  57. const result = await handler.execute('codegraph_callers', { symbol: huge });
  58. expect(result.isError).toBe(true);
  59. expect(result.content[0]!.text).toMatch(/maximum length/i);
  60. });
  61. it('rejects an oversize symbol on codegraph_impact', async () => {
  62. const huge = 'd'.repeat(11_000);
  63. const result = await handler.execute('codegraph_impact', { symbol: huge });
  64. expect(result.isError).toBe(true);
  65. expect(result.content[0]!.text).toMatch(/maximum length/i);
  66. });
  67. it('rejects an oversize projectPath', async () => {
  68. const hugePath = '/tmp/' + 'x'.repeat(5_000);
  69. const result = await handler.execute('codegraph_search', {
  70. query: 'alpha',
  71. projectPath: hugePath,
  72. });
  73. expect(result.isError).toBe(true);
  74. expect(result.content[0]!.text).toMatch(/projectPath/);
  75. });
  76. it('rejects an oversize path filter on codegraph_files', async () => {
  77. const hugePath = 'src/' + 'y'.repeat(5_000);
  78. const result = await handler.execute('codegraph_files', { path: hugePath });
  79. expect(result.isError).toBe(true);
  80. expect(result.content[0]!.text).toMatch(/path/);
  81. });
  82. it('rejects an oversize glob pattern on codegraph_files', async () => {
  83. const hugePattern = '*'.repeat(5_000);
  84. const result = await handler.execute('codegraph_files', { pattern: hugePattern });
  85. expect(result.isError).toBe(true);
  86. expect(result.content[0]!.text).toMatch(/pattern/);
  87. });
  88. it('rejects a non-string projectPath', async () => {
  89. const result = await handler.execute('codegraph_search', {
  90. query: 'alpha',
  91. projectPath: 12345 as unknown as string,
  92. });
  93. expect(result.isError).toBe(true);
  94. expect(result.content[0]!.text).toMatch(/projectPath/);
  95. });
  96. });