Skip to content

Commit 3b5fd5c

Browse files
committed
fix: drop the check-then-use file access in the stub-tar test
CodeQL flagged js/file-system-race (high) on the test: existsSync + statSync + readFileSync on the same path is a time-of-check-to-time-of-use race. Read the file once and assert on the buffer instead (readFileSync throws if missing, length covers the size check, the first two bytes are the gzip magic). Same assertions, one access, no race. Note: AI-assisted (Claude Code). Manually verified: the 3 stub-tar tests pass, eslint and prettier clean, and no existsSync/statSync remain on the path so the CodeQL check-then-use pattern is gone.
1 parent b89cc61 commit 3b5fd5c

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

test/unit/stub-env-tar.test.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@ describe('createStubEnvTar', () => {
2424
// Act
2525
const returned = createStubEnvTar(out);
2626

27-
// Assert
27+
// Assert. Read once (readFileSync throws if it is missing) and assert on the
28+
// buffer, instead of existsSync + statSync + readFileSync which is a TOCTOU
29+
// race on the same path.
2830
expect(returned).toBe(out);
29-
expect(fs.existsSync(out)).toBe(true);
30-
expect(fs.statSync(out).size).toBeGreaterThan(0);
31-
const header = fs.readFileSync(out).subarray(0, 2);
32-
expect(header[0]).toBe(0x1f);
33-
expect(header[1]).toBe(0x8b);
31+
const content = fs.readFileSync(out);
32+
expect(content.length).toBeGreaterThan(0);
33+
expect(content[0]).toBe(0x1f);
34+
expect(content[1]).toBe(0x8b);
3435
});
3536

3637
it('packs the STUB_FOR_E2E marker file into the tarball', () => {

0 commit comments

Comments
 (0)