Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: fix possible filesystem race condition #1822

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions packages/core/src/file-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down