Skip to content

Commit

Permalink
fix(js): non-literal filename rule (#380)
Browse files Browse the repository at this point in the history
  • Loading branch information
elsapet authored Apr 29, 2024
1 parent 599c85a commit 6067afe
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 25 deletions.
55 changes: 30 additions & 25 deletions rules/javascript/lang/non_literal_fs_filename.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ imports:
- javascript_shared_import_library
patterns:
- pattern: |
$<FS>.$<METHOD>($<...>$<INPUT>$<...>)
$<FS>.$<METHOD>($<INPUT>$<...>)
filters:
- variable: FS
detection: javascript_lang_non_literal_fs_filename_fs_init
Expand All @@ -14,10 +14,6 @@ patterns:
- chmod
- chown
- close
- copyFile
- copyFile
- cp
- cp
- createReadStream
- createWriteStream
- exists
Expand All @@ -31,28 +27,22 @@ patterns:
- lchmod
- lchown
- lutimes
- link
- link
- lstat
- mkdir
- mkdtemp
- open
- openAsBlob
- opendir
- read
- read
- readdir
- readFile
- readlink
- readv
- realpath
- realpath
- rename
- rename
- rmdir
- rm
- stat
- symlink
- symlink
- statfs
- truncate
- unlink
- unwatchFile
Expand All @@ -67,10 +57,6 @@ patterns:
- chmodSync
- chownSync
- closeSync
- copyFileSync
- copyFileSync
- cpSync
- cpSync
- existsSync
- fchmodSync
- fchownSync
Expand All @@ -82,8 +68,6 @@ patterns:
- lchmodSync
- lchownSync
- lutimesSync
- linkSync
- linkSync
- lstatSync
- mkdirSync
- mkdtempSync
Expand All @@ -93,17 +77,11 @@ patterns:
- readFileSync
- readlinkSync
- readSync
- readSync
- readvSync
- realpathync
- realpathSync
- renameSync
- renameSync
- rmdirSync
- rmSync
- statSync
- symlinkSync
- symlinkSync
- truncateSync
- unlinkSync
- utimesSync
Expand All @@ -114,6 +92,33 @@ patterns:
variable: INPUT
detection: string_literal
scope: result
- pattern: |
$<FS>.$<METHOD>($<INPUT_1>, $<INPUT_2>$<...>)
filters:
- variable: FS
detection: javascript_lang_non_literal_fs_filename_fs_init
scope: cursor
- variable: METHOD
values:
- copyFile # copyFile(sourcePath, destPath)
- cp # cp(sourcePath, destPath)
- link # link(oldPath, newPath, ...)
- rename # rename(oldPath, newPath, ...)
- symlink # symlink(target, path)
- copyFileSync
- cpSync
- linkSync
- renameSync
- symlinkSync
- either:
- not:
variable: INPUT_1
detection: string_literal
scope: result
- not:
variable: INPUT_2
detection: string_literal
scope: result
auxiliary:
- id: javascript_lang_non_literal_fs_filename_fs_init
patterns:
Expand Down
19 changes: 19 additions & 0 deletions tests/javascript/lang/non_literal_fs_filename/testdata/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@ export function bad(options) {
}
}

export function bad2(options) {
// bearer:expected javascript_lang_non_literal_fs_filename
Fs.copyFileSync(options.filePath, 'some-new-filepath.txt', 'utf8');

// bearer:expected javascript_lang_non_literal_fs_filename
Fs.symlink('some-filepath.txt', options.filePath, 'utf8');

// bearer:expected javascript_lang_non_literal_fs_filename
Fs.rename(options.filePath, options.newFilePath);
}

export function ok() {
stdioTarget = Fs.createWriteStream('some-string-literal', 'utf8');
}

export function ok2(data, encoding) {
stdioTarget = Fs.createWriteStream('some-string-literal', data, encoding);
}

export function ok3(data) {
Fs.symlink('some-filepath.txt', 'some-other-filepath.txt', data.options);
}

0 comments on commit 6067afe

Please sign in to comment.