Skip to content

Commit e90035a

Browse files
authored
Merge pull request github#5439 from erik-krogh/topPack
Approved by esbena
2 parents 0511e72 + 77ba7b4 commit e90035a

File tree

8 files changed

+97
-104
lines changed

8 files changed

+97
-104
lines changed

javascript/ql/src/Security/CWE-078/UnsafeShellCommandConstruction.ql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ import DataFlow::PathGraph
1818

1919
from Configuration cfg, DataFlow::PathNode source, DataFlow::PathNode sink, Sink sinkNode
2020
where cfg.hasFlowPath(source, sink) and sinkNode = sink.getNode()
21-
select sinkNode.getAlertLocation(), source, sink, "$@ based on library input is later used in $@.",
22-
sinkNode.getAlertLocation(), sinkNode.getSinkType(), sinkNode.getCommandExecution(),
23-
"shell command"
21+
select sinkNode.getAlertLocation(), source, sink, "$@ based on $@ is later used in $@.",
22+
sinkNode.getAlertLocation(), sinkNode.getSinkType(), source.getNode(), "library input",
23+
sinkNode.getCommandExecution(), "shell command"

javascript/ql/src/semmle/javascript/NodeModuleResolutionImpl.qll

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,11 @@ File resolveMainModule(PackageJSON pkg, int priority) {
9696
)
9797
)
9898
or
99-
result =
100-
tryExtensions(pkg.getFile().getParentContainer(), "index", priority - prioritiesPerCandidate())
99+
exists(Folder folder | folder = pkg.getFile().getParentContainer() |
100+
result =
101+
tryExtensions([folder, folder.getChildContainer(["src", "lib"])], "index",
102+
priority - prioritiesPerCandidate())
103+
)
101104
}
102105

103106
/**

javascript/ql/src/semmle/javascript/PackageExports.qll

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,12 @@ DataFlow::ParameterNode getALibraryInputParameter() {
1717
}
1818

1919
/**
20-
* Gets the number of occurrences of "/" in `path`.
21-
*/
22-
bindingset[path]
23-
private int countSlashes(string path) { result = count(path.splitAt("/")) - 1 }
24-
25-
/**
26-
* Gets the topmost named package.json that appears in the project.
27-
*
28-
* There can be multiple results if the there exists multiple package.json that are equally deeply nested in the folder structure.
29-
* Results are limited to package.json files that are at most nested 2 directories deep.
30-
*/
31-
PackageJSON getTopmostPackageJSON() {
32-
result =
33-
min(PackageJSON j |
34-
countSlashes(j.getFile().getRelativePath()) <= 3 and
35-
exists(j.getPackageName())
36-
|
37-
j order by countSlashes(j.getFile().getRelativePath())
38-
)
39-
}
40-
41-
/**
42-
* Gets a value exported by the main module from one of the topmost `package.json` files (see `getTopmostPackageJSON`).
20+
* Gets a value exported by the main module from a named `package.json` file.
4321
* The value is either directly the `module.exports` value, a nested property of `module.exports`, or a method on an exported class.
4422
*/
4523
private DataFlow::Node getAValueExportedByPackage() {
46-
result = getAnExportFromModule(getTopmostPackageJSON().getMainModule())
24+
result =
25+
getAnExportFromModule(any(PackageJSON pack | exists(pack.getPackageName())).getMainModule())
4726
or
4827
result = getAValueExportedByPackage().(DataFlow::PropWrite).getRhs()
4928
or

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -646,8 +646,6 @@ module NodeJSLib {
646646
}
647647
}
648648

649-
private import semmle.javascript.PackageExports as Exports
650-
651649
/**
652650
* A direct step from an named export to a property-read reading the exported value.
653651
*/

javascript/ql/test/library-tests/PackageExports/tests.expected

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
getTopmostPackageJSON
2-
| absent_main/package.json:1:1:4:1 | {\\n " ... t.js"\\n} |
3-
| lib1/package.json:1:1:4:1 | {\\n " ... n.js"\\n} |
41
getALibraryInputParameter
52
| lib1/foo.js:3:44:3:44 | d |
63
| lib1/main.js:6:17:6:17 | a |

javascript/ql/test/library-tests/PackageExports/tests.ql

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import javascript
22
import semmle.javascript.PackageExports as Exports
33

4-
query PackageJSON getTopmostPackageJSON() { result = Exports::getTopmostPackageJSON() }
5-
64
query DataFlow::Node getALibraryInputParameter() { result = Exports::getALibraryInputParameter() }
75

86
query DataFlow::Node getAnExportedValue(Module mod, string name) {

javascript/ql/test/query-tests/Security/CWE-078/UnsafeShellCommandConstruction.expected

Lines changed: 85 additions & 67 deletions
Large diffs are not rendered by default.

javascript/ql/test/query-tests/Security/CWE-078/lib/subLib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var cp = require("child_process")
22

33
module.exports = function (name) {
4-
cp.exec("rm -rf " + name); // OK - this file belongs in a sub-"module", and is not the primary exported module.
4+
cp.exec("rm -rf " + name); // NOT OK - functions exported as part of a submodule are also flagged.
55
};
66

77
module.exports.foo = function (name) {

0 commit comments

Comments
 (0)