From e38c71c7cc8b9617555be99da4db9f9e9549178a Mon Sep 17 00:00:00 2001 From: Michael Wang Date: Fri, 20 Nov 2026 16:27:39 -0800 Subject: [PATCH] test(media-use): read the preferences file through one no-follow handle The test checked the file's type and then read it by path, which CodeQL reports as a check-then-use race. Open it once without following a link and check and read through that handle. --- packages/cli/src/media-use/lib/links.test.mjs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/cli/src/media-use/lib/links.test.mjs b/packages/cli/src/media-use/lib/links.test.mjs index 435dcd8d95..efc119e239 100644 --- a/packages/cli/src/media-use/lib/links.test.mjs +++ b/packages/cli/src/media-use/lib/links.test.mjs @@ -1,9 +1,13 @@ import { strict as assert } from "node:assert"; import { + closeSync, + constants, existsSync, + fstatSync, lstatSync, mkdirSync, mkdtempSync, + openSync, readdirSync, readFileSync, realpathSync, @@ -171,8 +175,14 @@ test("a project preference never writes through links at predictable temp names" recordPreference({ projectDir: project, key: "aspect", value: "16:9" }); - assert.ok(lstatSync(prefs).isFile()); - assert.equal(JSON.parse(readFileSync(prefs, "utf8")).preferences.aspect.value, "16:9"); + // Opened without following a link, so a link left at the name would fail the open itself. + const fd = openSync(prefs, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0)); + try { + assert.ok(fstatSync(fd).isFile()); + assert.equal(JSON.parse(readFileSync(fd, "utf8")).preferences.aspect.value, "16:9"); + } finally { + closeSync(fd); + } assert.equal(existsSync(join(outside, `planted-${process.pid}`)), false); outsideUnchanged(); }),