1
0

security.test.ts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. /**
  2. * Security Tests
  3. *
  4. * Tests for P0/P1 security fixes:
  5. * - FileLock (cross-process locking)
  6. * - Path traversal prevention
  7. * - MCP input validation
  8. * - Atomic writes
  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 { FileLock, validateProjectPath, validatePathWithinRoot } from '../src/utils';
  15. import CodeGraph from '../src/index';
  16. import { ToolHandler, tools } from '../src/mcp/tools';
  17. import { scanDirectory, isSourceFile } from '../src/extraction';
  18. import { DatabaseConnection, getDatabasePath } from '../src/db';
  19. import { QueryBuilder } from '../src/db/queries';
  20. function createTempDir(): string {
  21. return fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-security-test-'));
  22. }
  23. function cleanupTempDir(dir: string): void {
  24. if (fs.existsSync(dir)) {
  25. fs.rmSync(dir, { recursive: true, force: true });
  26. }
  27. }
  28. describe('FileLock', () => {
  29. let tempDir: string;
  30. let lockPath: string;
  31. beforeEach(() => {
  32. tempDir = createTempDir();
  33. lockPath = path.join(tempDir, 'test.lock');
  34. });
  35. afterEach(() => {
  36. cleanupTempDir(tempDir);
  37. });
  38. it('should acquire and release a lock', () => {
  39. const lock = new FileLock(lockPath);
  40. lock.acquire();
  41. expect(fs.existsSync(lockPath)).toBe(true);
  42. const content = fs.readFileSync(lockPath, 'utf-8').trim();
  43. expect(parseInt(content, 10)).toBe(process.pid);
  44. lock.release();
  45. expect(fs.existsSync(lockPath)).toBe(false);
  46. });
  47. it('should prevent double acquisition within same process', () => {
  48. const lock1 = new FileLock(lockPath);
  49. const lock2 = new FileLock(lockPath);
  50. lock1.acquire();
  51. // Second lock should fail because our PID is alive
  52. expect(() => lock2.acquire()).toThrow(/locked by another process/);
  53. lock1.release();
  54. });
  55. it('should detect and remove stale locks from dead processes', () => {
  56. // Write a lock file with a PID that doesn't exist
  57. // PID 99999999 is extremely unlikely to be a real process
  58. fs.writeFileSync(lockPath, '99999999');
  59. const lock = new FileLock(lockPath);
  60. // Should succeed because the PID is dead
  61. expect(() => lock.acquire()).not.toThrow();
  62. lock.release();
  63. });
  64. it('should execute function with withLock', () => {
  65. const lock = new FileLock(lockPath);
  66. const result = lock.withLock(() => {
  67. expect(fs.existsSync(lockPath)).toBe(true);
  68. return 42;
  69. });
  70. expect(result).toBe(42);
  71. expect(fs.existsSync(lockPath)).toBe(false);
  72. });
  73. it('should release lock even if function throws', () => {
  74. const lock = new FileLock(lockPath);
  75. expect(() => {
  76. lock.withLock(() => {
  77. throw new Error('test error');
  78. });
  79. }).toThrow('test error');
  80. expect(fs.existsSync(lockPath)).toBe(false);
  81. });
  82. it('should execute async function with withLockAsync', async () => {
  83. const lock = new FileLock(lockPath);
  84. const result = await lock.withLockAsync(async () => {
  85. expect(fs.existsSync(lockPath)).toBe(true);
  86. return 'async-result';
  87. });
  88. expect(result).toBe('async-result');
  89. expect(fs.existsSync(lockPath)).toBe(false);
  90. });
  91. it('should release lock even if async function throws', async () => {
  92. const lock = new FileLock(lockPath);
  93. await expect(
  94. lock.withLockAsync(async () => {
  95. throw new Error('async error');
  96. })
  97. ).rejects.toThrow('async error');
  98. expect(fs.existsSync(lockPath)).toBe(false);
  99. });
  100. it('release should be idempotent', () => {
  101. const lock = new FileLock(lockPath);
  102. lock.acquire();
  103. lock.release();
  104. // Second release should not throw
  105. expect(() => lock.release()).not.toThrow();
  106. });
  107. });
  108. describe('Path Traversal Prevention', () => {
  109. let testDir: string;
  110. let cg: CodeGraph;
  111. beforeEach(async () => {
  112. testDir = createTempDir();
  113. const srcDir = path.join(testDir, 'src');
  114. fs.mkdirSync(srcDir);
  115. fs.writeFileSync(
  116. path.join(srcDir, 'hello.ts'),
  117. `export function hello(): string { return "hi"; }\n`
  118. );
  119. cg = CodeGraph.initSync(testDir, {
  120. config: { include: ['**/*.ts'], exclude: [] },
  121. });
  122. await cg.indexAll();
  123. });
  124. afterEach(() => {
  125. if (cg) cg.close();
  126. cleanupTempDir(testDir);
  127. });
  128. it('should read code for valid nodes within project', async () => {
  129. const nodes = cg.getNodesByKind('function');
  130. const hello = nodes.find((n) => n.name === 'hello');
  131. expect(hello).toBeDefined();
  132. const code = await cg.getCode(hello!.id);
  133. expect(code).toContain('hello');
  134. });
  135. it('should return null for non-existent node', async () => {
  136. const code = await cg.getCode('does-not-exist');
  137. expect(code).toBeNull();
  138. });
  139. });
  140. describe('Symlink escape prevention (#527)', () => {
  141. // An in-repo symlink whose logical path is inside the project root but whose
  142. // REAL target escapes the root must never be served. validatePathWithinRoot
  143. // is the chokepoint both content-serving read sinks go through (codegraph_node
  144. // includeCode + codegraph_explore source rendering), so it must resolve
  145. // symlinks, not just compare strings. realpathSync the roots so the test's own
  146. // expectations don't trip over /tmp -> /private/tmp on macOS.
  147. let root: string;
  148. let outside: string;
  149. beforeEach(() => {
  150. root = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), 'cg-root-')));
  151. outside = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), 'cg-outside-')));
  152. fs.mkdirSync(path.join(root, 'src'));
  153. fs.writeFileSync(path.join(root, 'src', 'in.ts'), 'export const x = 1;\n');
  154. fs.mkdirSync(path.join(outside, 'pkg'));
  155. fs.writeFileSync(path.join(outside, 'pkg', 'secret.txt'), 'TOP-SECRET\n');
  156. });
  157. afterEach(() => {
  158. fs.rmSync(root, { recursive: true, force: true });
  159. fs.rmSync(outside, { recursive: true, force: true });
  160. });
  161. // Symlink creation needs privileges on Windows; skip gracefully if it fails.
  162. const link = (linkPath: string, target: string): boolean => {
  163. try { fs.symlinkSync(target, linkPath); return true; } catch { return false; }
  164. };
  165. it('allows a real file inside the root (and realpaths consistently)', () => {
  166. expect(validatePathWithinRoot(root, 'src/in.ts')).not.toBeNull();
  167. });
  168. it('allows a not-yet-existing path inside the root (ENOENT — files about to be written)', () => {
  169. expect(validatePathWithinRoot(root, 'src/will-write.ts')).not.toBeNull();
  170. });
  171. it('rejects a lexical ../ traversal out of the root', () => {
  172. expect(validatePathWithinRoot(root, `../${path.basename(outside)}/pkg/secret.txt`)).toBeNull();
  173. });
  174. it('rejects an in-repo symlink to an out-of-root FILE', () => {
  175. if (!link(path.join(root, 'escape'), path.join(outside, 'pkg', 'secret.txt'))) return;
  176. expect(validatePathWithinRoot(root, 'escape')).toBeNull();
  177. });
  178. it('rejects a path that escapes through an in-repo symlink to an out-of-root DIR', () => {
  179. if (!link(path.join(root, 'escapedir'), path.join(outside, 'pkg'))) return;
  180. expect(validatePathWithinRoot(root, 'escapedir/secret.txt')).toBeNull();
  181. });
  182. it('still allows an in-repo symlink that stays WITHIN the root (no over-blocking)', () => {
  183. if (!link(path.join(root, 'src', 'inlink.ts'), path.join(root, 'src', 'in.ts'))) return;
  184. expect(validatePathWithinRoot(root, 'src/inlink.ts')).not.toBeNull();
  185. });
  186. // The INDEXING read path opts into following in-root symlinks the directory
  187. // walk already descended into — discovery and the reader must agree, or files
  188. // reached via an in-root symlink-to-outside fail to index (#935). The lexical
  189. // `../` guard is NOT waived, and content-serving sinks never pass the flag.
  190. it('allowSymlinkEscape follows an in-repo symlink to an out-of-root FILE (indexing read)', () => {
  191. if (!link(path.join(root, 'escape'), path.join(outside, 'pkg', 'secret.txt'))) return;
  192. expect(validatePathWithinRoot(root, 'escape', { allowSymlinkEscape: true })).not.toBeNull();
  193. });
  194. it('allowSymlinkEscape follows a path through an in-repo out-of-root DIR symlink (indexing read)', () => {
  195. if (!link(path.join(root, 'escapedir'), path.join(outside, 'pkg'))) return;
  196. expect(validatePathWithinRoot(root, 'escapedir/secret.txt', { allowSymlinkEscape: true })).not.toBeNull();
  197. });
  198. it('allowSymlinkEscape STILL rejects a lexical ../ traversal (guard not waived)', () => {
  199. expect(
  200. validatePathWithinRoot(root, `../${path.basename(outside)}/pkg/secret.txt`, { allowSymlinkEscape: true })
  201. ).toBeNull();
  202. });
  203. it('end-to-end: getCode never serves an out-of-root file reached via a dir symlink', async () => {
  204. fs.writeFileSync(path.join(outside, 'pkg', 'leak.ts'),
  205. 'export function leaked() { return "LEAKED-ZZZ-9"; }\n');
  206. if (!link(path.join(root, 'vendored'), path.join(outside, 'pkg'))) return;
  207. const cg = CodeGraph.initSync(root, { config: { include: ['**/*.ts'], exclude: [] } });
  208. try {
  209. await cg.indexAll();
  210. // Whether or not extraction followed the dir symlink, NO node may ever
  211. // yield the out-of-root content through getCode.
  212. for (const n of cg.getNodesByKind('function')) {
  213. const code = await cg.getCode(n.id);
  214. expect(code ?? '').not.toContain('LEAKED-ZZZ-9');
  215. }
  216. } finally {
  217. cg.close();
  218. }
  219. });
  220. it('end-to-end (#935): indexes source reached through an in-root dir symlink to outside', async () => {
  221. // The Dota custom-game layout symlinks `game/` and `content/` into an SDK
  222. // tree outside the repo. Before #935 the batch reader's strict symlink-escape
  223. // guard blocked every file under them, so nothing indexed — even though the
  224. // directory walk deliberately followed the symlink to enumerate them. The
  225. // reader now agrees with discovery: the file indexes.
  226. fs.writeFileSync(path.join(outside, 'pkg', 'vendored.ts'),
  227. 'export function vendoredHelper() { return "LEAKED-ZZZ-9"; }\n');
  228. if (!link(path.join(root, 'game'), path.join(outside, 'pkg'))) return;
  229. const cg = CodeGraph.initSync(root, { config: { include: ['**/*.ts'], exclude: [] } });
  230. try {
  231. await cg.indexAll();
  232. // The symlinked-in file is now part of the graph...
  233. const names = cg.getNodesByKind('function').map((n) => n.name);
  234. expect(names).toContain('vendoredHelper');
  235. // ...but its out-of-root contents are STILL never served (the #527 sink
  236. // stays strict — indexing relaxes only the read path, not getCode).
  237. for (const n of cg.getNodesByKind('function')) {
  238. expect((await cg.getCode(n.id)) ?? '').not.toContain('LEAKED-ZZZ-9');
  239. }
  240. } finally {
  241. cg.close();
  242. }
  243. });
  244. });
  245. describe('validateProjectPath — sensitive directory blocking', () => {
  246. // POSIX-only: on Windows '/etc' resolves to C:\etc (non-existent), not a
  247. // sensitive dir — the Windows case is covered by the win32-gated test below.
  248. it.runIf(process.platform !== 'win32')('blocks POSIX system directories (exact match)', () => {
  249. expect(validateProjectPath('/')).toMatch(/sensitive system directory/i);
  250. expect(validateProjectPath('/etc')).toMatch(/sensitive system directory/i);
  251. });
  252. it('allows a normal, existing directory', () => {
  253. const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-validate-'));
  254. try {
  255. expect(validateProjectPath(dir)).toBeNull();
  256. } finally {
  257. fs.rmSync(dir, { recursive: true, force: true });
  258. }
  259. });
  260. // SENSITIVE_PATHS stores the Windows entries lowercase and validateProjectPath
  261. // matches via resolved.toLowerCase(), so 'C:\\Windows' and 'c:\\windows' are
  262. // both blocked. path.resolve is platform-specific, so this only runs on Windows.
  263. it.runIf(process.platform === 'win32')(
  264. 'blocks Windows system directories regardless of case',
  265. () => {
  266. expect(validateProjectPath('C:\\Windows')).toMatch(/sensitive system directory/i);
  267. expect(validateProjectPath('c:\\windows')).toMatch(/sensitive system directory/i);
  268. expect(validateProjectPath('C:\\WINDOWS\\System32')).toMatch(/sensitive system directory/i);
  269. }
  270. );
  271. });
  272. describe('MCP Input Validation', () => {
  273. let testDir: string;
  274. let cg: CodeGraph;
  275. let handler: ToolHandler;
  276. beforeEach(async () => {
  277. testDir = createTempDir();
  278. const srcDir = path.join(testDir, 'src');
  279. fs.mkdirSync(srcDir);
  280. fs.writeFileSync(
  281. path.join(srcDir, 'example.ts'),
  282. `export function exampleFunc(): void {}\nexport class ExampleClass {}\n`
  283. );
  284. cg = CodeGraph.initSync(testDir, {
  285. config: { include: ['**/*.ts'], exclude: [] },
  286. });
  287. await cg.indexAll();
  288. handler = new ToolHandler(cg);
  289. });
  290. afterEach(() => {
  291. if (cg) cg.close();
  292. cleanupTempDir(testDir);
  293. });
  294. it('should reject non-string query in codegraph_search', async () => {
  295. const result = await handler.execute('codegraph_search', { query: null });
  296. expect(result.isError).toBe(true);
  297. expect(result.content[0].text).toContain('non-empty string');
  298. });
  299. it('should reject empty string query in codegraph_search', async () => {
  300. const result = await handler.execute('codegraph_search', { query: '' });
  301. expect(result.isError).toBe(true);
  302. expect(result.content[0].text).toContain('non-empty string');
  303. });
  304. it('should accept valid query in codegraph_search', async () => {
  305. const result = await handler.execute('codegraph_search', { query: 'example' });
  306. expect(result.isError).toBeFalsy();
  307. });
  308. it('should clamp limit to valid range in codegraph_search', async () => {
  309. // Extremely large limit should still work (clamped to 100)
  310. const result = await handler.execute('codegraph_search', { query: 'example', limit: 999999 });
  311. expect(result.isError).toBeFalsy();
  312. });
  313. it('should reject non-string symbol in codegraph_callers', async () => {
  314. const result = await handler.execute('codegraph_callers', { symbol: 123 });
  315. expect(result.isError).toBe(true);
  316. expect(result.content[0].text).toContain('non-empty string');
  317. });
  318. it('should reject non-string query in codegraph_explore', async () => {
  319. const result = await handler.execute('codegraph_explore', { query: undefined });
  320. expect(result.isError).toBe(true);
  321. expect(result.content[0].text).toContain('non-empty string');
  322. });
  323. it('should truncate oversized tool output', async () => {
  324. // Force a huge result set through codegraph_search; the response must be
  325. // truncated with the sentinel rather than flooding the agent's context.
  326. const many = Array.from({ length: 3000 }, (_, i) => ({
  327. node: {
  328. id: `n${i}`,
  329. name: `symbol_${i}_${'x'.repeat(40)}`,
  330. kind: 'function',
  331. filePath: `src/very/deep/path/file_${i}.ts`,
  332. startLine: 1,
  333. endLine: 2,
  334. language: 'typescript',
  335. },
  336. score: 1,
  337. }));
  338. const fakeCg = {
  339. searchNodes: () => many,
  340. };
  341. const fakeHandler = new ToolHandler(fakeCg as unknown as CodeGraph);
  342. const result = await fakeHandler.execute('codegraph_search', { query: 'x' });
  343. expect(result.isError).toBeFalsy();
  344. expect(result.content[0].text).toContain('... (output truncated)');
  345. });
  346. it('should reject non-string symbol in codegraph_impact', async () => {
  347. const result = await handler.execute('codegraph_impact', { symbol: [] });
  348. expect(result.isError).toBe(true);
  349. });
  350. it('should reject non-string symbol in codegraph_node', async () => {
  351. const result = await handler.execute('codegraph_node', { symbol: false });
  352. expect(result.isError).toBe(true);
  353. });
  354. it('should reject non-string symbol in codegraph_callees', async () => {
  355. const result = await handler.execute('codegraph_callees', { symbol: {} });
  356. expect(result.isError).toBe(true);
  357. });
  358. it('should handle NaN limit gracefully', async () => {
  359. const result = await handler.execute('codegraph_search', { query: 'example', limit: 'abc' });
  360. expect(result.isError).toBeFalsy();
  361. });
  362. it('should handle negative limit gracefully', async () => {
  363. const result = await handler.execute('codegraph_search', { query: 'example', limit: -5 });
  364. expect(result.isError).toBeFalsy();
  365. });
  366. // #230: getCodeGraph must reject a sensitive system directory passed as
  367. // projectPath before opening it. The error surfaces through execute()'s
  368. // catch as an isError result. /etc is sensitive on POSIX; C:\Windows on
  369. // Windows (path.resolve is platform-specific, so each case is gated).
  370. it.runIf(process.platform !== 'win32')(
  371. 'rejects a sensitive POSIX projectPath (/etc) via the MCP handler',
  372. async () => {
  373. const result = await handler.execute('codegraph_search', {
  374. query: 'example',
  375. projectPath: '/etc',
  376. });
  377. expect(result.isError).toBe(true);
  378. expect(result.content[0].text).toMatch(/sensitive system directory/i);
  379. }
  380. );
  381. it.runIf(process.platform === 'win32')(
  382. 'rejects a sensitive Windows projectPath (C:\\Windows) via the MCP handler',
  383. async () => {
  384. const result = await handler.execute('codegraph_search', {
  385. query: 'example',
  386. projectPath: 'C:\\Windows',
  387. });
  388. expect(result.isError).toBe(true);
  389. expect(result.content[0].text).toMatch(/sensitive system directory/i);
  390. }
  391. );
  392. });
  393. describe('Atomic Writes', () => {
  394. let tempDir: string;
  395. beforeEach(() => {
  396. tempDir = createTempDir();
  397. });
  398. afterEach(() => {
  399. cleanupTempDir(tempDir);
  400. });
  401. it('should not leave temp files on success', () => {
  402. // We test this indirectly through the config-writer module
  403. // by checking that no .tmp files remain after writing
  404. const configDir = path.join(tempDir, '.claude');
  405. fs.mkdirSync(configDir, { recursive: true });
  406. const testFile = path.join(configDir, 'test.json');
  407. // Simulate what atomicWriteFileSync does
  408. const tmpPath = testFile + '.tmp.' + process.pid;
  409. fs.writeFileSync(tmpPath, '{"test": true}');
  410. fs.renameSync(tmpPath, testFile);
  411. expect(fs.existsSync(testFile)).toBe(true);
  412. expect(fs.existsSync(tmpPath)).toBe(false);
  413. const content = JSON.parse(fs.readFileSync(testFile, 'utf-8'));
  414. expect(content.test).toBe(true);
  415. });
  416. });
  417. describe('Source file detection (isSourceFile)', () => {
  418. it('selects files by supported extension', () => {
  419. expect(isSourceFile('src/index.ts')).toBe(true);
  420. expect(isSourceFile('src/deep/nested/file.ts')).toBe(true);
  421. expect(isSourceFile('src/component.tsx')).toBe(true);
  422. expect(isSourceFile('lib/util.js')).toBe(true);
  423. expect(isSourceFile('src/main.py')).toBe(true);
  424. });
  425. it('rejects unsupported extensions and extensionless files', () => {
  426. expect(isSourceFile('src/component.css')).toBe(false);
  427. expect(isSourceFile('README.md')).toBe(false);
  428. expect(isSourceFile('Makefile')).toBe(false);
  429. expect(isSourceFile('.gitignore')).toBe(false);
  430. });
  431. it('matches regardless of leading dot directories', () => {
  432. expect(isSourceFile('.hidden/index.ts')).toBe(true);
  433. });
  434. });
  435. describe('JSON.parse Error Boundaries in DB', () => {
  436. let tempDir: string;
  437. beforeEach(() => {
  438. tempDir = createTempDir();
  439. });
  440. afterEach(() => {
  441. cleanupTempDir(tempDir);
  442. });
  443. it('should not crash when node has malformed JSON in decorators column', () => {
  444. const dbPath = path.join(tempDir, 'test.db');
  445. const db = DatabaseConnection.initialize(dbPath);
  446. const queries = new QueryBuilder(db.getDb());
  447. // Insert a node with malformed JSON in the decorators column
  448. db.getDb().prepare(`
  449. INSERT INTO nodes (id, kind, name, qualified_name, file_path, language, start_line, end_line, start_column, end_column, decorators, is_exported, is_async, is_static, is_abstract, updated_at)
  450. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  451. `).run(
  452. 'test-node-1', 'function', 'myFunc', 'myFunc', 'test.ts', 'typescript',
  453. 1, 5, 0, 0,
  454. '{not valid json!!!}', // malformed decorators
  455. 0, 0, 0, 0, Date.now()
  456. );
  457. // Should not throw - should return node with undefined decorators
  458. const node = queries.getNodeById('test-node-1');
  459. expect(node).not.toBeNull();
  460. expect(node!.name).toBe('myFunc');
  461. expect(node!.decorators).toBeUndefined();
  462. db.close();
  463. });
  464. it('should not crash when edge has malformed JSON in metadata column', () => {
  465. const dbPath = path.join(tempDir, 'test.db');
  466. const db = DatabaseConnection.initialize(dbPath);
  467. const queries = new QueryBuilder(db.getDb());
  468. // Insert two nodes first
  469. const insertNode = db.getDb().prepare(`
  470. INSERT INTO nodes (id, kind, name, qualified_name, file_path, language, start_line, end_line, start_column, end_column, is_exported, is_async, is_static, is_abstract, updated_at)
  471. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  472. `);
  473. insertNode.run('node-a', 'function', 'funcA', 'funcA', 'a.ts', 'typescript', 1, 5, 0, 0, 0, 0, 0, 0, Date.now());
  474. insertNode.run('node-b', 'function', 'funcB', 'funcB', 'b.ts', 'typescript', 1, 5, 0, 0, 0, 0, 0, 0, Date.now());
  475. // Insert edge with malformed metadata
  476. db.getDb().prepare(`
  477. INSERT INTO edges (source, target, kind, metadata)
  478. VALUES (?, ?, ?, ?)
  479. `).run('node-a', 'node-b', 'calls', 'broken json {{{');
  480. // Should not throw - should return edge with undefined metadata
  481. const edges = queries.getOutgoingEdges('node-a');
  482. expect(edges.length).toBe(1);
  483. expect(edges[0].source).toBe('node-a');
  484. expect(edges[0].target).toBe('node-b');
  485. expect(edges[0].metadata).toBeUndefined();
  486. db.close();
  487. });
  488. it('should not crash when file record has malformed JSON in errors column', () => {
  489. const dbPath = path.join(tempDir, 'test.db');
  490. const db = DatabaseConnection.initialize(dbPath);
  491. const queries = new QueryBuilder(db.getDb());
  492. // Insert a file with malformed errors JSON
  493. db.getDb().prepare(`
  494. INSERT INTO files (path, content_hash, language, size, modified_at, indexed_at, node_count, errors)
  495. VALUES (?, ?, ?, ?, ?, ?, ?, ?)
  496. `).run('test.ts', 'abc123', 'typescript', 100, Date.now(), Date.now(), 5, 'not-an-array');
  497. // Should not throw - should return file with undefined errors
  498. const file = queries.getFileByPath('test.ts');
  499. expect(file).not.toBeNull();
  500. expect(file!.path).toBe('test.ts');
  501. expect(file!.errors).toBeUndefined();
  502. db.close();
  503. });
  504. });
  505. describe('Symlink Cycle Detection', () => {
  506. let tempDir: string;
  507. beforeEach(() => {
  508. tempDir = createTempDir();
  509. });
  510. afterEach(() => {
  511. cleanupTempDir(tempDir);
  512. });
  513. it('should handle symlink cycle without infinite loop', () => {
  514. // Create directory structure with a symlink cycle
  515. const srcDir = path.join(tempDir, 'src');
  516. fs.mkdirSync(srcDir);
  517. fs.writeFileSync(path.join(srcDir, 'index.ts'), 'export const x = 1;\n');
  518. // Create a symlink from src/loop -> tempDir (parent directory)
  519. try {
  520. fs.symlinkSync(tempDir, path.join(srcDir, 'loop'), 'dir');
  521. } catch {
  522. // Skip test if symlinks not supported (e.g., Windows without admin)
  523. return;
  524. }
  525. // This should complete without hanging
  526. const files = scanDirectory(tempDir);
  527. // Should find the real file but not loop infinitely
  528. expect(files).toContain('src/index.ts');
  529. // Should not find duplicates via the symlink path
  530. const indexFiles = files.filter(f => f.endsWith('index.ts'));
  531. expect(indexFiles.length).toBe(1);
  532. });
  533. it('should follow valid symlinks to directories', () => {
  534. // Create source directory with a file
  535. const realDir = path.join(tempDir, 'real');
  536. fs.mkdirSync(realDir);
  537. fs.writeFileSync(path.join(realDir, 'hello.ts'), 'export function hello() {}\n');
  538. // Create a symlink to realDir
  539. const srcDir = path.join(tempDir, 'src');
  540. fs.mkdirSync(srcDir);
  541. try {
  542. fs.symlinkSync(realDir, path.join(srcDir, 'linked'), 'dir');
  543. } catch {
  544. return;
  545. }
  546. const files = scanDirectory(tempDir);
  547. // Should find files from both the real dir and via the symlink
  548. // But deduplicate since they resolve to the same real path
  549. expect(files.some(f => f.includes('hello.ts'))).toBe(true);
  550. });
  551. it('should skip broken symlinks gracefully', () => {
  552. const srcDir = path.join(tempDir, 'src');
  553. fs.mkdirSync(srcDir);
  554. fs.writeFileSync(path.join(srcDir, 'valid.ts'), 'export const y = 2;\n');
  555. try {
  556. fs.symlinkSync('/nonexistent/path', path.join(srcDir, 'broken'), 'dir');
  557. } catch {
  558. return;
  559. }
  560. // Should not throw
  561. const files = scanDirectory(tempDir);
  562. expect(files).toContain('src/valid.ts');
  563. });
  564. });