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}` `Unable to find environment variable for file command ${command}`
) )
} }
if (!fs.existsSync(filePath)) {
// 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}`) throw new Error(`Missing file at path: ${filePath}`)
} else {
throw err
}
} }
fs.appendFileSync(filePath, `${toCommandValue(message)}${os.EOL}`, { try {
encoding: 'utf8' fs.writeSync(fd, `${toCommandValue(message)}${os.EOL}`, null, 'utf8')
}) } finally {
fs.closeSync(fd)
}
} }
export function prepareKeyValueMessage(key: string, value: any): string { export function prepareKeyValueMessage(key: string, value: any): string {