security.test.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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 } from '../src/utils';
  15. import CodeGraph from '../src/index';
  16. import { ToolHandler, tools } from '../src/mcp/tools';
  17. import { shouldIncludeFile, scanDirectory } from '../src/extraction';
  18. import { shouldIncludeFile as configShouldInclude } from '../src/config';
  19. import { CodeGraphConfig, DEFAULT_CONFIG } from '../src/types';
  20. import { DatabaseConnection, getDatabasePath } from '../src/db';
  21. import { QueryBuilder } from '../src/db/queries';
  22. function createTempDir(): string {
  23. return fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-security-test-'));
  24. }
  25. function cleanupTempDir(dir: string): void {
  26. if (fs.existsSync(dir)) {
  27. fs.rmSync(dir, { recursive: true, force: true });
  28. }
  29. }
  30. describe('FileLock', () => {
  31. let tempDir: string;
  32. let lockPath: string;
  33. beforeEach(() => {
  34. tempDir = createTempDir();
  35. lockPath = path.join(tempDir, 'test.lock');
  36. });
  37. afterEach(() => {
  38. cleanupTempDir(tempDir);
  39. });
  40. it('should acquire and release a lock', () => {
  41. const lock = new FileLock(lockPath);
  42. lock.acquire();
  43. expect(fs.existsSync(lockPath)).toBe(true);
  44. const content = fs.readFileSync(lockPath, 'utf-8').trim();
  45. expect(parseInt(content, 10)).toBe(process.pid);
  46. lock.release();
  47. expect(fs.existsSync(lockPath)).toBe(false);
  48. });
  49. it('should prevent double acquisition within same process', () => {
  50. const lock1 = new FileLock(lockPath);
  51. const lock2 = new FileLock(lockPath);
  52. lock1.acquire();
  53. // Second lock should fail because our PID is alive
  54. expect(() => lock2.acquire()).toThrow(/locked by another process/);
  55. lock1.release();
  56. });
  57. it('should detect and remove stale locks from dead processes', () => {
  58. // Write a lock file with a PID that doesn't exist
  59. // PID 99999999 is extremely unlikely to be a real process
  60. fs.writeFileSync(lockPath, '99999999');
  61. const lock = new FileLock(lockPath);
  62. // Should succeed because the PID is dead
  63. expect(() => lock.acquire()).not.toThrow();
  64. lock.release();
  65. });
  66. it('should execute function with withLock', () => {
  67. const lock = new FileLock(lockPath);
  68. const result = lock.withLock(() => {
  69. expect(fs.existsSync(lockPath)).toBe(true);
  70. return 42;
  71. });
  72. expect(result).toBe(42);
  73. expect(fs.existsSync(lockPath)).toBe(false);
  74. });
  75. it('should release lock even if function throws', () => {
  76. const lock = new FileLock(lockPath);
  77. expect(() => {
  78. lock.withLock(() => {
  79. throw new Error('test error');
  80. });
  81. }).toThrow('test error');
  82. expect(fs.existsSync(lockPath)).toBe(false);
  83. });
  84. it('should execute async function with withLockAsync', async () => {
  85. const lock = new FileLock(lockPath);
  86. const result = await lock.withLockAsync(async () => {
  87. expect(fs.existsSync(lockPath)).toBe(true);
  88. return 'async-result';
  89. });
  90. expect(result).toBe('async-result');
  91. expect(fs.existsSync(lockPath)).toBe(false);
  92. });
  93. it('should release lock even if async function throws', async () => {
  94. const lock = new FileLock(lockPath);
  95. await expect(
  96. lock.withLockAsync(async () => {
  97. throw new Error('async error');
  98. })
  99. ).rejects.toThrow('async error');
  100. expect(fs.existsSync(lockPath)).toBe(false);
  101. });
  102. it('release should be idempotent', () => {
  103. const lock = new FileLock(lockPath);
  104. lock.acquire();
  105. lock.release();
  106. // Second release should not throw
  107. expect(() => lock.release()).not.toThrow();
  108. });
  109. });
  110. describe('Path Traversal Prevention', () => {
  111. let testDir: string;
  112. let cg: CodeGraph;
  113. beforeEach(async () => {
  114. testDir = createTempDir();
  115. const srcDir = path.join(testDir, 'src');
  116. fs.mkdirSync(srcDir);
  117. fs.writeFileSync(
  118. path.join(srcDir, 'hello.ts'),
  119. `export function hello(): string { return "hi"; }\n`
  120. );
  121. cg = CodeGraph.initSync(testDir, {
  122. config: { include: ['**/*.ts'], exclude: [] },
  123. });
  124. await cg.indexAll();
  125. });
  126. afterEach(() => {
  127. if (cg) cg.close();
  128. cleanupTempDir(testDir);
  129. });
  130. it('should read code for valid nodes within project', async () => {
  131. const nodes = cg.getNodesByKind('function');
  132. const hello = nodes.find((n) => n.name === 'hello');
  133. expect(hello).toBeDefined();
  134. const code = await cg.getCode(hello!.id);
  135. expect(code).toContain('hello');
  136. });
  137. it('should return null for non-existent node', async () => {
  138. const code = await cg.getCode('does-not-exist');
  139. expect(code).toBeNull();
  140. });
  141. });
  142. describe('MCP Input Validation', () => {
  143. let testDir: string;
  144. let cg: CodeGraph;
  145. let handler: ToolHandler;
  146. beforeEach(async () => {
  147. testDir = createTempDir();
  148. const srcDir = path.join(testDir, 'src');
  149. fs.mkdirSync(srcDir);
  150. fs.writeFileSync(
  151. path.join(srcDir, 'example.ts'),
  152. `export function exampleFunc(): void {}\nexport class ExampleClass {}\n`
  153. );
  154. cg = CodeGraph.initSync(testDir, {
  155. config: { include: ['**/*.ts'], exclude: [] },
  156. });
  157. await cg.indexAll();
  158. handler = new ToolHandler(cg);
  159. });
  160. afterEach(() => {
  161. if (cg) cg.close();
  162. cleanupTempDir(testDir);
  163. });
  164. it('should reject non-string query in codegraph_search', async () => {
  165. const result = await handler.execute('codegraph_search', { query: null });
  166. expect(result.isError).toBe(true);
  167. expect(result.content[0].text).toContain('non-empty string');
  168. });
  169. it('should reject empty string query in codegraph_search', async () => {
  170. const result = await handler.execute('codegraph_search', { query: '' });
  171. expect(result.isError).toBe(true);
  172. expect(result.content[0].text).toContain('non-empty string');
  173. });
  174. it('should accept valid query in codegraph_search', async () => {
  175. const result = await handler.execute('codegraph_search', { query: 'example' });
  176. expect(result.isError).toBeFalsy();
  177. });
  178. it('should clamp limit to valid range in codegraph_search', async () => {
  179. // Extremely large limit should still work (clamped to 100)
  180. const result = await handler.execute('codegraph_search', { query: 'example', limit: 999999 });
  181. expect(result.isError).toBeFalsy();
  182. });
  183. it('should reject non-string symbol in codegraph_callers', async () => {
  184. const result = await handler.execute('codegraph_callers', { symbol: 123 });
  185. expect(result.isError).toBe(true);
  186. expect(result.content[0].text).toContain('non-empty string');
  187. });
  188. it('should reject non-string task in codegraph_context', async () => {
  189. const result = await handler.execute('codegraph_context', { task: undefined });
  190. expect(result.isError).toBe(true);
  191. expect(result.content[0].text).toContain('non-empty string');
  192. });
  193. it('should reject non-string symbol in codegraph_impact', async () => {
  194. const result = await handler.execute('codegraph_impact', { symbol: [] });
  195. expect(result.isError).toBe(true);
  196. });
  197. it('should reject non-string symbol in codegraph_node', async () => {
  198. const result = await handler.execute('codegraph_node', { symbol: false });
  199. expect(result.isError).toBe(true);
  200. });
  201. it('should reject non-string symbol in codegraph_callees', async () => {
  202. const result = await handler.execute('codegraph_callees', { symbol: {} });
  203. expect(result.isError).toBe(true);
  204. });
  205. it('should handle NaN limit gracefully', async () => {
  206. const result = await handler.execute('codegraph_search', { query: 'example', limit: 'abc' });
  207. expect(result.isError).toBeFalsy();
  208. });
  209. it('should handle negative limit gracefully', async () => {
  210. const result = await handler.execute('codegraph_search', { query: 'example', limit: -5 });
  211. expect(result.isError).toBeFalsy();
  212. });
  213. });
  214. describe('Atomic Writes', () => {
  215. let tempDir: string;
  216. beforeEach(() => {
  217. tempDir = createTempDir();
  218. });
  219. afterEach(() => {
  220. cleanupTempDir(tempDir);
  221. });
  222. it('should not leave temp files on success', () => {
  223. // We test this indirectly through the config-writer module
  224. // by checking that no .tmp files remain after writing
  225. const configDir = path.join(tempDir, '.claude');
  226. fs.mkdirSync(configDir, { recursive: true });
  227. const testFile = path.join(configDir, 'test.json');
  228. // Simulate what atomicWriteFileSync does
  229. const tmpPath = testFile + '.tmp.' + process.pid;
  230. fs.writeFileSync(tmpPath, '{"test": true}');
  231. fs.renameSync(tmpPath, testFile);
  232. expect(fs.existsSync(testFile)).toBe(true);
  233. expect(fs.existsSync(tmpPath)).toBe(false);
  234. const content = JSON.parse(fs.readFileSync(testFile, 'utf-8'));
  235. expect(content.test).toBe(true);
  236. });
  237. });
  238. describe('Glob Matching (picomatch)', () => {
  239. const makeConfig = (include: string[], exclude: string[]): CodeGraphConfig => ({
  240. ...DEFAULT_CONFIG,
  241. rootDir: '/test',
  242. include,
  243. exclude,
  244. });
  245. it('should match standard glob patterns in extraction', () => {
  246. const config = makeConfig(['**/*.ts'], ['node_modules/**']);
  247. expect(shouldIncludeFile('src/index.ts', config)).toBe(true);
  248. expect(shouldIncludeFile('src/deep/nested/file.ts', config)).toBe(true);
  249. expect(shouldIncludeFile('src/index.js', config)).toBe(false);
  250. expect(shouldIncludeFile('node_modules/lib/index.ts', config)).toBe(false);
  251. });
  252. it('should match standard glob patterns in config', () => {
  253. const config = makeConfig(['**/*.py'], ['__pycache__/**']);
  254. expect(configShouldInclude('src/main.py', config)).toBe(true);
  255. expect(configShouldInclude('src/main.ts', config)).toBe(false);
  256. expect(configShouldInclude('__pycache__/module.py', config)).toBe(false);
  257. });
  258. it('should handle complex glob patterns correctly', () => {
  259. const config = makeConfig(['src/**/*.{ts,tsx}', 'lib/**/*.js'], []);
  260. expect(shouldIncludeFile('src/component.ts', config)).toBe(true);
  261. expect(shouldIncludeFile('src/component.tsx', config)).toBe(true);
  262. expect(shouldIncludeFile('lib/util.js', config)).toBe(true);
  263. expect(shouldIncludeFile('src/component.css', config)).toBe(false);
  264. });
  265. it('should handle patterns that previously caused ReDoS', () => {
  266. // This pattern would cause catastrophic backtracking with hand-rolled regex
  267. const evilPattern = '**/**/**/**/**/**/**/**/**/**/**/**/**/**/a';
  268. const config = makeConfig([evilPattern], []);
  269. const start = Date.now();
  270. // This should return quickly, not hang
  271. shouldIncludeFile('x/x/x/x/x/x/x/x/x/x/x/x/x/x/b', config);
  272. const elapsed = Date.now() - start;
  273. // Should complete in under 100ms, not seconds
  274. expect(elapsed).toBeLessThan(100);
  275. });
  276. it('should handle dot files correctly', () => {
  277. const config = makeConfig(['**/*.ts'], []);
  278. expect(shouldIncludeFile('.hidden/index.ts', config)).toBe(true);
  279. });
  280. });
  281. describe('JSON.parse Error Boundaries in DB', () => {
  282. let tempDir: string;
  283. beforeEach(() => {
  284. tempDir = createTempDir();
  285. });
  286. afterEach(() => {
  287. cleanupTempDir(tempDir);
  288. });
  289. it('should not crash when node has malformed JSON in decorators column', () => {
  290. const dbPath = path.join(tempDir, 'test.db');
  291. const db = DatabaseConnection.initialize(dbPath);
  292. const queries = new QueryBuilder(db.getDb());
  293. // Insert a node with malformed JSON in the decorators column
  294. db.getDb().prepare(`
  295. 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)
  296. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  297. `).run(
  298. 'test-node-1', 'function', 'myFunc', 'myFunc', 'test.ts', 'typescript',
  299. 1, 5, 0, 0,
  300. '{not valid json!!!}', // malformed decorators
  301. 0, 0, 0, 0, Date.now()
  302. );
  303. // Should not throw - should return node with undefined decorators
  304. const node = queries.getNodeById('test-node-1');
  305. expect(node).not.toBeNull();
  306. expect(node!.name).toBe('myFunc');
  307. expect(node!.decorators).toBeUndefined();
  308. db.close();
  309. });
  310. it('should not crash when edge has malformed JSON in metadata column', () => {
  311. const dbPath = path.join(tempDir, 'test.db');
  312. const db = DatabaseConnection.initialize(dbPath);
  313. const queries = new QueryBuilder(db.getDb());
  314. // Insert two nodes first
  315. const insertNode = db.getDb().prepare(`
  316. 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)
  317. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  318. `);
  319. insertNode.run('node-a', 'function', 'funcA', 'funcA', 'a.ts', 'typescript', 1, 5, 0, 0, 0, 0, 0, 0, Date.now());
  320. insertNode.run('node-b', 'function', 'funcB', 'funcB', 'b.ts', 'typescript', 1, 5, 0, 0, 0, 0, 0, 0, Date.now());
  321. // Insert edge with malformed metadata
  322. db.getDb().prepare(`
  323. INSERT INTO edges (source, target, kind, metadata)
  324. VALUES (?, ?, ?, ?)
  325. `).run('node-a', 'node-b', 'calls', 'broken json {{{');
  326. // Should not throw - should return edge with undefined metadata
  327. const edges = queries.getOutgoingEdges('node-a');
  328. expect(edges.length).toBe(1);
  329. expect(edges[0].source).toBe('node-a');
  330. expect(edges[0].target).toBe('node-b');
  331. expect(edges[0].metadata).toBeUndefined();
  332. db.close();
  333. });
  334. it('should not crash when file record has malformed JSON in errors column', () => {
  335. const dbPath = path.join(tempDir, 'test.db');
  336. const db = DatabaseConnection.initialize(dbPath);
  337. const queries = new QueryBuilder(db.getDb());
  338. // Insert a file with malformed errors JSON
  339. db.getDb().prepare(`
  340. INSERT INTO files (path, content_hash, language, size, modified_at, indexed_at, node_count, errors)
  341. VALUES (?, ?, ?, ?, ?, ?, ?, ?)
  342. `).run('test.ts', 'abc123', 'typescript', 100, Date.now(), Date.now(), 5, 'not-an-array');
  343. // Should not throw - should return file with undefined errors
  344. const file = queries.getFileByPath('test.ts');
  345. expect(file).not.toBeNull();
  346. expect(file!.path).toBe('test.ts');
  347. expect(file!.errors).toBeUndefined();
  348. db.close();
  349. });
  350. });
  351. describe('Symlink Cycle Detection', () => {
  352. let tempDir: string;
  353. beforeEach(() => {
  354. tempDir = createTempDir();
  355. });
  356. afterEach(() => {
  357. cleanupTempDir(tempDir);
  358. });
  359. it('should handle symlink cycle without infinite loop', () => {
  360. // Create directory structure with a symlink cycle
  361. const srcDir = path.join(tempDir, 'src');
  362. fs.mkdirSync(srcDir);
  363. fs.writeFileSync(path.join(srcDir, 'index.ts'), 'export const x = 1;\n');
  364. // Create a symlink from src/loop -> tempDir (parent directory)
  365. try {
  366. fs.symlinkSync(tempDir, path.join(srcDir, 'loop'), 'dir');
  367. } catch {
  368. // Skip test if symlinks not supported (e.g., Windows without admin)
  369. return;
  370. }
  371. const config: CodeGraphConfig = {
  372. ...DEFAULT_CONFIG,
  373. rootDir: tempDir,
  374. include: ['**/*.ts'],
  375. exclude: [],
  376. };
  377. // This should complete without hanging
  378. const files = scanDirectory(tempDir, config);
  379. // Should find the real file but not loop infinitely
  380. expect(files).toContain('src/index.ts');
  381. // Should not find duplicates via the symlink path
  382. const indexFiles = files.filter(f => f.endsWith('index.ts'));
  383. expect(indexFiles.length).toBe(1);
  384. });
  385. it('should follow valid symlinks to directories', () => {
  386. // Create source directory with a file
  387. const realDir = path.join(tempDir, 'real');
  388. fs.mkdirSync(realDir);
  389. fs.writeFileSync(path.join(realDir, 'hello.ts'), 'export function hello() {}\n');
  390. // Create a symlink to realDir
  391. const srcDir = path.join(tempDir, 'src');
  392. fs.mkdirSync(srcDir);
  393. try {
  394. fs.symlinkSync(realDir, path.join(srcDir, 'linked'), 'dir');
  395. } catch {
  396. return;
  397. }
  398. const config: CodeGraphConfig = {
  399. ...DEFAULT_CONFIG,
  400. rootDir: tempDir,
  401. include: ['**/*.ts'],
  402. exclude: [],
  403. };
  404. const files = scanDirectory(tempDir, config);
  405. // Should find files from both the real dir and via the symlink
  406. // But deduplicate since they resolve to the same real path
  407. expect(files.some(f => f.includes('hello.ts'))).toBe(true);
  408. });
  409. it('should skip broken symlinks gracefully', () => {
  410. const srcDir = path.join(tempDir, 'src');
  411. fs.mkdirSync(srcDir);
  412. fs.writeFileSync(path.join(srcDir, 'valid.ts'), 'export const y = 2;\n');
  413. try {
  414. fs.symlinkSync('/nonexistent/path', path.join(srcDir, 'broken'), 'dir');
  415. } catch {
  416. return;
  417. }
  418. const config: CodeGraphConfig = {
  419. ...DEFAULT_CONFIG,
  420. rootDir: tempDir,
  421. include: ['**/*.ts'],
  422. exclude: [],
  423. };
  424. // Should not throw
  425. const files = scanDirectory(tempDir, config);
  426. expect(files).toContain('src/valid.ts');
  427. });
  428. });
  429. describe('Session marker symlink resistance', () => {
  430. // The marker write lives in src/mcp/tools.ts behind handleContext. We exercise
  431. // it end-to-end via ToolHandler.execute so the test exercises the same code
  432. // path Claude Code drives. The session id is per-test so other parallel test
  433. // runs can't collide with the marker file we plant a symlink at.
  434. const SESSION_ID = `cg-test-${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2)}`;
  435. const crypto = require('crypto') as typeof import('crypto');
  436. const hash = crypto.createHash('md5').update(SESSION_ID).digest('hex').slice(0, 16);
  437. const markerPath = path.join(os.tmpdir(), `codegraph-consulted-${hash}`);
  438. let projectDir: string;
  439. let victimDir: string;
  440. let victimFile: string;
  441. beforeEach(async () => {
  442. projectDir = createTempDir();
  443. victimDir = createTempDir();
  444. victimFile = path.join(victimDir, 'private.txt');
  445. fs.writeFileSync(victimFile, 'SECRET-DO-NOT-OVERWRITE\n');
  446. if (fs.existsSync(markerPath)) fs.unlinkSync(markerPath);
  447. // A real .codegraph/ has to exist for handleContext to get past the
  448. // "not initialized" guard — index a tiny fixture so the call reaches the
  449. // marker write step rather than short-circuiting on missing project state.
  450. fs.writeFileSync(path.join(projectDir, 'a.ts'), 'export const x = 1;\n');
  451. const cg = await CodeGraph.init(projectDir);
  452. await cg.indexAll();
  453. cg.close();
  454. });
  455. afterEach(() => {
  456. if (fs.existsSync(markerPath)) fs.unlinkSync(markerPath);
  457. cleanupTempDir(projectDir);
  458. cleanupTempDir(victimDir);
  459. });
  460. it('does not follow a pre-planted symlink at the marker path', async () => {
  461. // Skip on platforms where the user can't create symlinks (Windows without
  462. // dev mode + admin). The CWE-59 risk we're guarding against doesn't apply
  463. // when symlinks aren't creatable, so the skip is correct, not a gap.
  464. try {
  465. fs.symlinkSync(victimFile, markerPath);
  466. } catch {
  467. return;
  468. }
  469. const cg = await CodeGraph.open(projectDir);
  470. const handler = new ToolHandler(cg);
  471. process.env.CLAUDE_SESSION_ID = SESSION_ID;
  472. try {
  473. await handler.execute('codegraph_context', { task: 'find x' });
  474. } finally {
  475. delete process.env.CLAUDE_SESSION_ID;
  476. cg.close();
  477. }
  478. // The victim file's contents must be untouched — the old writeFileSync
  479. // path would have followed the symlink and written an ISO timestamp here.
  480. expect(fs.readFileSync(victimFile, 'utf8')).toBe('SECRET-DO-NOT-OVERWRITE\n');
  481. // And the marker path itself must still be the symlink we planted —
  482. // no fallback path that quietly unlinked + recreated it (which would
  483. // also work, but is a behavior we don't want to silently rely on).
  484. expect(fs.lstatSync(markerPath).isSymbolicLink()).toBe(true);
  485. });
  486. it('writes the marker file with 0o600 perms on a clean path', async () => {
  487. // No symlink planted — happy path. Verifies the new openSync(mode: 0o600)
  488. // call is what actually lands on disk (regression guard for the perm
  489. // tightening that came with the O_NOFOLLOW fix).
  490. const cg = await CodeGraph.open(projectDir);
  491. const handler = new ToolHandler(cg);
  492. process.env.CLAUDE_SESSION_ID = SESSION_ID;
  493. try {
  494. await handler.execute('codegraph_context', { task: 'find x' });
  495. } finally {
  496. delete process.env.CLAUDE_SESSION_ID;
  497. cg.close();
  498. }
  499. expect(fs.existsSync(markerPath)).toBe(true);
  500. // chmod's low 9 bits — strip the file-type bits for a clean compare.
  501. // Windows can't enforce 0o600 in the POSIX sense; skip the assertion
  502. // there since the underlying OS will normalize the mode anyway.
  503. if (process.platform !== 'win32') {
  504. const mode = fs.statSync(markerPath).mode & 0o777;
  505. expect(mode).toBe(0o600);
  506. }
  507. });
  508. });