Insecure temporary file or directory Medium

Insecure ways of creating temporary files and directories can lead to race conditions and other security vulnerabilities such as privilege escalation. Race conditions can be exploited for denial of service attacks.

Detector ID
typescript/insecure-temp-file@v1.0
Category
Common Weakness Enumeration (CWE) external icon

Noncompliant example

1import fs from 'fs'
2function insecureTempFileNoncompliant() {
3  // Noncompliant: the global directory path is given for opening a file or creating a file which can be vulnerable to injection attacks.
4  var tmp_file : string = "/tmp/f"
5  fs.readFile(tmp_file, 'utf8', function (err: any, data: any) {
6    // ...
7  })
8}

Compliant example

1import fs from 'fs'
2import tmp from 'tmp'
3function insecureTempFileCompliant() {
4  // Compliant: tmp library to securely create or read temporary files.
5  var tmp_obj = tmp.fileSync()
6  fs.readFile(tmp_obj, 'utf8')
7}