Skip to content

Commit d279992

Browse files
fix(infra): resolve file system race condition in env-sync
Replace existsSync + writeFileSync/appendFileSync with readFileSync catch ENOENT + writeFileSync to eliminate TOCTOU race. Fixes code-scanning #8, #9 Generated with oh-my-agent Co-Authored-By: First Fluke <our.first.fluke@gmail.com>
1 parent 90cae36 commit d279992

1 file changed

Lines changed: 7 additions & 14 deletions

File tree

apps/infra/freemium/scripts/env-sync.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -239,23 +239,16 @@ async function injectVercelEnvVars(
239239
function ensureEnvExampleKeys(filePath: string, keys: string[]): void {
240240
let content = "";
241241

242-
if (existsSync(filePath)) {
242+
try {
243243
content = readFileSync(filePath, "utf-8");
244-
} else {
245-
// Create empty file
246-
writeFileSync(filePath, "", "utf-8");
244+
} catch (err: unknown) {
245+
if ((err as NodeJS.ErrnoException).code !== "ENOENT") throw err;
247246
console.log(` [created] ${filePath}`);
248247
}
249248

250-
const lines = content.split("\n");
251-
const missing: string[] = [];
252-
253-
for (const key of keys) {
254-
const hasKey = lines.some((line) => line.startsWith(`${key}=`));
255-
if (!hasKey) {
256-
missing.push(key);
257-
}
258-
}
249+
const missing = keys.filter(
250+
(key) => !content.split("\n").some((line) => line.startsWith(`${key}=`)),
251+
);
259252

260253
if (missing.length === 0) {
261254
console.log(` [ok] ${filePath} — all keys present`);
@@ -267,7 +260,7 @@ function ensureEnvExampleKeys(filePath: string, keys: string[]): void {
267260
missing.map((k) => `${k}=`).join("\n") +
268261
"\n";
269262

270-
appendFileSync(filePath, toAppend, "utf-8");
263+
writeFileSync(filePath, content + toAppend, "utf-8");
271264

272265
for (const key of missing) {
273266
console.log(` [added] ${key} to ${filePath}`);

0 commit comments

Comments
 (0)