Skip to content

Commit 15c1f4a

Browse files
Mossakaclaude
andcommitted
fix(test): use atomic wx flag to avoid TOCTOU race in credential setup
Replace existsSync+writeFileSync with writeFileSync({flag:'wx'}) to eliminate the file-system-race CodeQL alert in the test beforeAll hook. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent df5a3af commit 15c1f4a

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

tests/integration/credential-hiding.test.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -256,14 +256,20 @@ describe('Credential Hiding Security', () => {
256256
const homeDir = os.homedir();
257257
for (const p of untestedPaths) {
258258
const fullPath = `${homeDir}/${p.path}`;
259-
if (!fs.existsSync(fullPath)) {
260-
const dir = fullPath.substring(0, fullPath.lastIndexOf('/'));
261-
if (!fs.existsSync(dir)) {
262-
fs.mkdirSync(dir, { recursive: true });
263-
createdDirs.push(dir);
264-
}
265-
fs.writeFileSync(fullPath, 'DUMMY_SECRET_VALUE');
259+
const dir = fullPath.substring(0, fullPath.lastIndexOf('/'));
260+
fs.mkdirSync(dir, { recursive: true });
261+
if (!createdDirs.includes(dir)) {
262+
createdDirs.push(dir);
263+
}
264+
try {
265+
// Use 'wx' flag: atomic create-if-not-exists (avoids TOCTOU race)
266+
fs.writeFileSync(fullPath, 'DUMMY_SECRET_VALUE', { flag: 'wx' });
266267
createdFiles.push(fullPath);
268+
} catch (err: unknown) {
269+
// EEXIST means file already exists, which is fine
270+
if (err instanceof Error && 'code' in err && (err as NodeJS.ErrnoException).code !== 'EEXIST') {
271+
throw err;
272+
}
267273
}
268274
}
269275
});

0 commit comments

Comments
 (0)