Skip to content

Commit 35cc5ff

Browse files
author
Stephan Brandauer
authored
Merge pull request github#7715 from kaeluka/recognize-fs-extra-path-args
JS: add a predicate to recognize path arguments in calls to the fs-extra lib
2 parents 06776d1 + 02db472 commit 35cc5ff

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

javascript/ql/lib/semmle/javascript/frameworks/NodeJSLib.qll

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,11 +408,13 @@ module NodeJSLib {
408408

409409
/**
410410
* Holds if the `i`th parameter of method `methodName` of the Node.js
411-
* `fs` module might represent a file path.
411+
* `fs` module or the `fs-extra` module might represent a file path.
412412
*
413-
* We determine this by looking for an externs declaration for
413+
* For `fs`, we determine this by looking for an externs declaration for
414414
* `fs.methodName` where the `i`th parameter's name is `filename` or
415415
* `path` or a variation thereof.
416+
*
417+
* For `fs-extra`, we use a manually maintained list.
416418
*/
417419
private predicate fsFileParam(string methodName, int i) {
418420
exists(ExternalMemberDecl decl, Function f, JSDocParamTag p, string n |
@@ -423,6 +425,47 @@ module NodeJSLib {
423425
|
424426
n = "filename" or n.regexpMatch("(old|new|src|dst|)path")
425427
)
428+
or
429+
fsExtraExtensionFileParam(methodName, i)
430+
}
431+
432+
/**
433+
* Holds if `methodName` is a function defined in the `fs-extra` library
434+
* as an extension to node.js' `fs` module and parameter `i` of of the
435+
* method might represent a file path.
436+
*/
437+
private predicate fsExtraExtensionFileParam(string methodName, int i) {
438+
methodName = ["copy", "copySync", "copyFile"] and i = [0, 1]
439+
or
440+
methodName = ["move", "moveSync"] and i = [0, 1]
441+
or
442+
methodName = ["createFile", "createFileSync"] and i = 0
443+
or
444+
methodName = ["createSymLink", "createSymlinkSync"] and i = [0, 1]
445+
or
446+
methodName = ["ensureDir", "ensureDirSync"] and i = 0
447+
or
448+
methodName = ["mkdirs", "mkdirp", "mkdirsSync", "mkdirpSync"] and i = 0
449+
or
450+
methodName = ["outputFile", "outputFileSync"] and i = 0
451+
or
452+
methodName = ["readJson", "readJSON", "readJsonSync", "readJSONSync"] and i = 0
453+
or
454+
methodName = ["remove", "removeSync"] and i = 0
455+
or
456+
methodName =
457+
["outputJSON", "outputJson", "writeJSON", "writeJson", "writeJSONSync", "writeJsonSync"] and
458+
i = 0
459+
or
460+
methodName = ["ensureFile", "ensureFileSync"] and i = 0
461+
or
462+
methodName = ["ensureLink", "createLink", "ensureLinkSync", "createLinkSync"] and i = [0, 1]
463+
or
464+
methodName = ["ensureSymlink", "ensureSymlinkSync"] and i = [0, 1]
465+
or
466+
methodName = ["emptyDir", "emptyDirSync"] and i = 0
467+
or
468+
methodName = ["pathExists", "pathExistsSync"] and i = 0
426469
}
427470

428471
/**

0 commit comments

Comments
 (0)