Skip to content

Commit 46226d3

Browse files
committed
Fix temp file leak and inconsistent ghsaDetails format in coana-fix
- Clean up temp file when no modified files are detected, before executing 'continue ghsaLoop' to prevent temp file accumulation - Transform ghsaDetails in local mode to match GHSA-keyed map format used in PR mode for consistent output structure
1 parent 2d52351 commit 46226d3

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

src/commands/fix/coana-fix.mts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ async function discoverGhsaIds(
111111

112112
export async function coanaFix(
113113
fixConfig: FixConfig,
114-
): Promise<CResult<{ fixedAll: boolean; ghsaDetails: Record<string, unknown> }>> {
114+
): Promise<
115+
CResult<{ fixedAll: boolean; ghsaDetails: Record<string, unknown> }>
116+
> {
115117
const {
116118
all,
117119
applyFixes,
@@ -290,7 +292,10 @@ export async function coanaFix(
290292
}
291293

292294
// Read the temporary file to get the actual fixes result.
293-
const fixesResultJson = readJsonSync(tmpFile, { throws: false })
295+
const fixesResultJson = readJsonSync(tmpFile, { throws: false }) as
296+
| { fixes?: Record<string, unknown> }
297+
| null
298+
| undefined
294299

295300
// Copy to outputFile if provided.
296301
if (outputFile) {
@@ -301,11 +306,24 @@ export async function coanaFix(
301306
await fs.writeFile(outputFile, tmpContent, 'utf8')
302307
}
303308

309+
// Transform to GHSA-keyed map format for consistency with PR mode.
310+
const ghsaDetails: Record<string, unknown> = {
311+
__proto__: null,
312+
} as Record<string, unknown>
313+
if (fixesResultJson && typeof fixesResultJson === 'object') {
314+
const fixes = fixesResultJson.fixes
315+
if (fixes && typeof fixes === 'object') {
316+
for (const ghsaKey of Object.keys(fixes)) {
317+
ghsaDetails[ghsaKey] = fixesResultJson
318+
}
319+
}
320+
}
321+
304322
return {
305323
ok: true,
306324
data: {
307325
fixedAll: true,
308-
ghsaDetails: (fixesResultJson as Record<string, unknown>) ?? {},
326+
ghsaDetails,
309327
},
310328
}
311329
} finally {
@@ -462,6 +480,13 @@ export async function coanaFix(
462480

463481
if (!modifiedFiles.length) {
464482
debugFn('notice', `skip: no changes for ${ghsaId}`)
483+
// Clean up temp file before continuing.
484+
try {
485+
// eslint-disable-next-line no-await-in-loop
486+
await fs.unlink(tmpFile)
487+
} catch {
488+
// Ignore cleanup errors.
489+
}
465490
continue ghsaLoop
466491
}
467492

0 commit comments

Comments
 (0)