1
0
Fork 0

core: fix possible filesystem race condition

Otherwise CodeQL complains when @actions/core is used in bundled scripts.
pull/1822/head
rindeal 2024-09-03 07:04:17 +02:00 committed by GitHub
parent 6c4e082c18
commit e817aa0d36
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 17 additions and 6 deletions

View File

@ -15,13 +15,24 @@ export function issueFileCommand(command: string, message: any): void {
`Unable to find environment variable for file command ${command}`
)
}
if (!fs.existsSync(filePath)) {
throw new Error(`Missing file at path: ${filePath}`)
}
fs.appendFileSync(filePath, `${toCommandValue(message)}${os.EOL}`, {
encoding: 'utf8'
})
// do not use appendFileSync() because of CodeQL js/file-system-race
let fd
try {
fd = fs.openSync(filePath, 'a')
} catch (err) {
if (err.code === 'ENOENT') {
throw new Error(`Missing file at path: ${filePath}`)
} else {
throw err
}
}
try {
fs.writeSync(fd, `${toCommandValue(message)}${os.EOL}`, null, 'utf8')
} finally {
fs.closeSync(fd)
}
}
export function prepareKeyValueMessage(key: string, value: any): string {